meta_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 api_test
  14. import (
  15. "reflect"
  16. "testing"
  17. "github.com/google/gofuzz"
  18. "k8s.io/kubernetes/pkg/api"
  19. "k8s.io/kubernetes/pkg/api/meta"
  20. "k8s.io/kubernetes/pkg/api/meta/metatypes"
  21. "k8s.io/kubernetes/pkg/api/testapi"
  22. "k8s.io/kubernetes/pkg/api/unversioned"
  23. "k8s.io/kubernetes/pkg/runtime"
  24. "k8s.io/kubernetes/pkg/util/uuid"
  25. )
  26. var _ meta.Object = &api.ObjectMeta{}
  27. // TestFillObjectMetaSystemFields validates that system populated fields are set on an object
  28. func TestFillObjectMetaSystemFields(t *testing.T) {
  29. ctx := api.NewDefaultContext()
  30. resource := api.ObjectMeta{}
  31. api.FillObjectMetaSystemFields(ctx, &resource)
  32. if resource.CreationTimestamp.Time.IsZero() {
  33. t.Errorf("resource.CreationTimestamp is zero")
  34. } else if len(resource.UID) == 0 {
  35. t.Errorf("resource.UID missing")
  36. }
  37. // verify we can inject a UID
  38. uid := uuid.NewUUID()
  39. ctx = api.WithUID(ctx, uid)
  40. resource = api.ObjectMeta{}
  41. api.FillObjectMetaSystemFields(ctx, &resource)
  42. if resource.UID != uid {
  43. t.Errorf("resource.UID expected: %v, actual: %v", uid, resource.UID)
  44. }
  45. }
  46. // TestHasObjectMetaSystemFieldValues validates that true is returned if and only if all fields are populated
  47. func TestHasObjectMetaSystemFieldValues(t *testing.T) {
  48. ctx := api.NewDefaultContext()
  49. resource := api.ObjectMeta{}
  50. if api.HasObjectMetaSystemFieldValues(&resource) {
  51. t.Errorf("the resource does not have all fields yet populated, but incorrectly reports it does")
  52. }
  53. api.FillObjectMetaSystemFields(ctx, &resource)
  54. if !api.HasObjectMetaSystemFieldValues(&resource) {
  55. t.Errorf("the resource does have all fields populated, but incorrectly reports it does not")
  56. }
  57. }
  58. func getObjectMetaAndOwnerReferences() (objectMeta api.ObjectMeta, metaOwnerReferences []metatypes.OwnerReference) {
  59. fuzz.New().NilChance(.5).NumElements(1, 5).Fuzz(&objectMeta)
  60. references := objectMeta.OwnerReferences
  61. metaOwnerReferences = make([]metatypes.OwnerReference, 0)
  62. for i := 0; i < len(references); i++ {
  63. metaOwnerReferences = append(metaOwnerReferences, metatypes.OwnerReference{
  64. Kind: references[i].Kind,
  65. Name: references[i].Name,
  66. UID: references[i].UID,
  67. APIVersion: references[i].APIVersion,
  68. Controller: references[i].Controller,
  69. })
  70. }
  71. if len(references) == 0 {
  72. objectMeta.OwnerReferences = make([]api.OwnerReference, 0)
  73. }
  74. return objectMeta, metaOwnerReferences
  75. }
  76. func testGetOwnerReferences(t *testing.T) {
  77. meta, expected := getObjectMetaAndOwnerReferences()
  78. refs := meta.GetOwnerReferences()
  79. if !reflect.DeepEqual(refs, expected) {
  80. t.Errorf("expect %v\n got %v", expected, refs)
  81. }
  82. }
  83. func testSetOwnerReferences(t *testing.T) {
  84. expected, newRefs := getObjectMetaAndOwnerReferences()
  85. objectMeta := &api.ObjectMeta{}
  86. objectMeta.SetOwnerReferences(newRefs)
  87. if !reflect.DeepEqual(expected.OwnerReferences, objectMeta.OwnerReferences) {
  88. t.Errorf("expect: %#v\n got: %#v", expected.OwnerReferences, objectMeta.OwnerReferences)
  89. }
  90. }
  91. func TestAccessOwnerReferences(t *testing.T) {
  92. fuzzIter := 5
  93. for i := 0; i < fuzzIter; i++ {
  94. testGetOwnerReferences(t)
  95. testSetOwnerReferences(t)
  96. }
  97. }
  98. func TestAccessorImplementations(t *testing.T) {
  99. for _, group := range testapi.Groups {
  100. for _, gv := range []unversioned.GroupVersion{*group.GroupVersion(), group.InternalGroupVersion()} {
  101. for kind, knownType := range api.Scheme.KnownTypes(gv) {
  102. value := reflect.New(knownType)
  103. obj := value.Interface()
  104. if _, ok := obj.(runtime.Object); !ok {
  105. t.Errorf("%v (%v) does not implement runtime.Object", gv.WithKind(kind), knownType)
  106. }
  107. lm, isLM := obj.(meta.ListMetaAccessor)
  108. om, isOM := obj.(meta.ObjectMetaAccessor)
  109. switch {
  110. case isLM && isOM:
  111. t.Errorf("%v (%v) implements ListMetaAccessor and ObjectMetaAccessor", gv.WithKind(kind), knownType)
  112. continue
  113. case isLM:
  114. m := lm.GetListMeta()
  115. if m == nil {
  116. t.Errorf("%v (%v) returns nil ListMeta", gv.WithKind(kind), knownType)
  117. continue
  118. }
  119. m.SetResourceVersion("102030")
  120. if m.GetResourceVersion() != "102030" {
  121. t.Errorf("%v (%v) did not preserve resource version", gv.WithKind(kind), knownType)
  122. continue
  123. }
  124. m.SetSelfLink("102030")
  125. if m.GetSelfLink() != "102030" {
  126. t.Errorf("%v (%v) did not preserve self link", gv.WithKind(kind), knownType)
  127. continue
  128. }
  129. case isOM:
  130. m := om.GetObjectMeta()
  131. if m == nil {
  132. t.Errorf("%v (%v) returns nil ObjectMeta", gv.WithKind(kind), knownType)
  133. continue
  134. }
  135. m.SetResourceVersion("102030")
  136. if m.GetResourceVersion() != "102030" {
  137. t.Errorf("%v (%v) did not preserve resource version", gv.WithKind(kind), knownType)
  138. continue
  139. }
  140. m.SetSelfLink("102030")
  141. if m.GetSelfLink() != "102030" {
  142. t.Errorf("%v (%v) did not preserve self link", gv.WithKind(kind), knownType)
  143. continue
  144. }
  145. labels := map[string]string{"a": "b"}
  146. m.SetLabels(labels)
  147. if !reflect.DeepEqual(m.GetLabels(), labels) {
  148. t.Errorf("%v (%v) did not preserve labels", gv.WithKind(kind), knownType)
  149. continue
  150. }
  151. default:
  152. if _, ok := obj.(unversioned.ListMetaAccessor); ok {
  153. continue
  154. }
  155. if _, ok := value.Elem().Type().FieldByName("ObjectMeta"); ok {
  156. t.Errorf("%v (%v) has ObjectMeta but does not implement ObjectMetaAccessor", gv.WithKind(kind), knownType)
  157. continue
  158. }
  159. if _, ok := value.Elem().Type().FieldByName("ListMeta"); ok {
  160. t.Errorf("%v (%v) has ListMeta but does not implement ListMetaAccessor", gv.WithKind(kind), knownType)
  161. continue
  162. }
  163. t.Logf("%v (%v) does not implement ListMetaAccessor or ObjectMetaAccessor", gv.WithKind(kind), knownType)
  164. }
  165. }
  166. }
  167. }
  168. }