install_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package install
  14. import (
  15. "encoding/json"
  16. "reflect"
  17. "testing"
  18. internal "k8s.io/kubernetes/pkg/api"
  19. "k8s.io/kubernetes/pkg/api/unversioned"
  20. "k8s.io/kubernetes/pkg/apimachinery/registered"
  21. "k8s.io/kubernetes/pkg/runtime"
  22. )
  23. func TestResourceVersioner(t *testing.T) {
  24. pod := internal.Pod{ObjectMeta: internal.ObjectMeta{ResourceVersion: "10"}}
  25. version, err := accessor.ResourceVersion(&pod)
  26. if err != nil {
  27. t.Fatalf("unexpected error: %v", err)
  28. }
  29. if version != "10" {
  30. t.Errorf("unexpected version %v", version)
  31. }
  32. podList := internal.PodList{ListMeta: unversioned.ListMeta{ResourceVersion: "10"}}
  33. version, err = accessor.ResourceVersion(&podList)
  34. if err != nil {
  35. t.Fatalf("unexpected error: %v", err)
  36. }
  37. if version != "10" {
  38. t.Errorf("unexpected version %v", version)
  39. }
  40. }
  41. func TestCodec(t *testing.T) {
  42. pod := internal.Pod{}
  43. // We do want to use package registered rather than testapi here, because we
  44. // want to test if the package install and package registered work as expected.
  45. data, err := runtime.Encode(internal.Codecs.LegacyCodec(registered.GroupOrDie(internal.GroupName).GroupVersion), &pod)
  46. if err != nil {
  47. t.Fatalf("unexpected error: %v", err)
  48. }
  49. other := internal.Pod{}
  50. if err := json.Unmarshal(data, &other); err != nil {
  51. t.Fatalf("unexpected error: %v", err)
  52. }
  53. if other.APIVersion != registered.GroupOrDie(internal.GroupName).GroupVersion.Version || other.Kind != "Pod" {
  54. t.Errorf("unexpected unmarshalled object %#v", other)
  55. }
  56. }
  57. func TestInterfacesFor(t *testing.T) {
  58. if _, err := registered.GroupOrDie(internal.GroupName).InterfacesFor(internal.SchemeGroupVersion); err == nil {
  59. t.Fatalf("unexpected non-error: %v", err)
  60. }
  61. for i, version := range registered.GroupOrDie(internal.GroupName).GroupVersions {
  62. if vi, err := registered.GroupOrDie(internal.GroupName).InterfacesFor(version); err != nil || vi == nil {
  63. t.Fatalf("%d: unexpected result: %v", i, err)
  64. }
  65. }
  66. }
  67. func TestRESTMapper(t *testing.T) {
  68. gv := unversioned.GroupVersion{Group: "", Version: "v1"}
  69. rcGVK := gv.WithKind("ReplicationController")
  70. podTemplateGVK := gv.WithKind("PodTemplate")
  71. if gvk, err := registered.RESTMapper().KindFor(internal.SchemeGroupVersion.WithResource("replicationcontrollers")); err != nil || gvk != rcGVK {
  72. t.Errorf("unexpected version mapping: %v %v", gvk, err)
  73. }
  74. if m, err := registered.GroupOrDie(internal.GroupName).RESTMapper.RESTMapping(podTemplateGVK.GroupKind(), ""); err != nil || m.GroupVersionKind != podTemplateGVK || m.Resource != "podtemplates" {
  75. t.Errorf("unexpected version mapping: %#v %v", m, err)
  76. }
  77. for _, version := range registered.GroupOrDie(internal.GroupName).GroupVersions {
  78. mapping, err := registered.GroupOrDie(internal.GroupName).RESTMapper.RESTMapping(rcGVK.GroupKind(), version.Version)
  79. if err != nil {
  80. t.Errorf("unexpected error: %v", err)
  81. }
  82. if mapping.Resource != "replicationControllers" && mapping.Resource != "replicationcontrollers" {
  83. t.Errorf("incorrect resource name: %#v", mapping)
  84. }
  85. if mapping.GroupVersionKind.GroupVersion() != version {
  86. t.Errorf("incorrect version: %v", mapping)
  87. }
  88. interfaces, _ := registered.GroupOrDie(internal.GroupName).InterfacesFor(version)
  89. if mapping.ObjectConvertor != interfaces.ObjectConvertor {
  90. t.Errorf("unexpected: %#v, expected: %#v", mapping, interfaces)
  91. }
  92. rc := &internal.ReplicationController{ObjectMeta: internal.ObjectMeta{Name: "foo"}}
  93. name, err := mapping.MetadataAccessor.Name(rc)
  94. if err != nil {
  95. t.Errorf("unexpected error: %v", err)
  96. }
  97. if name != "foo" {
  98. t.Errorf("unable to retrieve object meta with: %v", mapping.MetadataAccessor)
  99. }
  100. }
  101. }
  102. func TestUnversioned(t *testing.T) {
  103. for _, obj := range []runtime.Object{
  104. &unversioned.Status{},
  105. &unversioned.ExportOptions{},
  106. } {
  107. if unversioned, ok := internal.Scheme.IsUnversioned(obj); !unversioned || !ok {
  108. t.Errorf("%v is expected to be unversioned", reflect.TypeOf(obj))
  109. }
  110. }
  111. }