meta_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 testing
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/apimachinery/pkg/api/meta"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/runtime"
  20. "k8s.io/apimachinery/pkg/runtime/schema"
  21. "k8s.io/kubernetes/pkg/api/legacyscheme"
  22. )
  23. var _ metav1.Object = &metav1.ObjectMeta{}
  24. func TestAccessorImplementations(t *testing.T) {
  25. for _, gv := range legacyscheme.Scheme.PrioritizedVersionsAllGroups() {
  26. internalGV := schema.GroupVersion{Group: gv.Group, Version: runtime.APIVersionInternal}
  27. for _, gv := range []schema.GroupVersion{gv, internalGV} {
  28. for kind, knownType := range legacyscheme.Scheme.KnownTypes(gv) {
  29. value := reflect.New(knownType)
  30. obj := value.Interface()
  31. if _, ok := obj.(runtime.Object); !ok {
  32. t.Errorf("%v (%v) does not implement runtime.Object", gv.WithKind(kind), knownType)
  33. }
  34. lm, isLM := obj.(meta.ListMetaAccessor)
  35. om, isOM := obj.(metav1.ObjectMetaAccessor)
  36. switch {
  37. case isLM && isOM:
  38. t.Errorf("%v (%v) implements ListMetaAccessor and ObjectMetaAccessor", gv.WithKind(kind), knownType)
  39. continue
  40. case isLM:
  41. m := lm.GetListMeta()
  42. if m == nil {
  43. t.Errorf("%v (%v) returns nil ListMeta", gv.WithKind(kind), knownType)
  44. continue
  45. }
  46. m.SetResourceVersion("102030")
  47. if m.GetResourceVersion() != "102030" {
  48. t.Errorf("%v (%v) did not preserve resource version", gv.WithKind(kind), knownType)
  49. continue
  50. }
  51. m.SetSelfLink("102030")
  52. if m.GetSelfLink() != "102030" {
  53. t.Errorf("%v (%v) did not preserve self link", gv.WithKind(kind), knownType)
  54. continue
  55. }
  56. case isOM:
  57. m := om.GetObjectMeta()
  58. if m == nil {
  59. t.Errorf("%v (%v) returns nil ObjectMeta", gv.WithKind(kind), knownType)
  60. continue
  61. }
  62. m.SetResourceVersion("102030")
  63. if m.GetResourceVersion() != "102030" {
  64. t.Errorf("%v (%v) did not preserve resource version", gv.WithKind(kind), knownType)
  65. continue
  66. }
  67. m.SetSelfLink("102030")
  68. if m.GetSelfLink() != "102030" {
  69. t.Errorf("%v (%v) did not preserve self link", gv.WithKind(kind), knownType)
  70. continue
  71. }
  72. labels := map[string]string{"a": "b"}
  73. m.SetLabels(labels)
  74. if !reflect.DeepEqual(m.GetLabels(), labels) {
  75. t.Errorf("%v (%v) did not preserve labels", gv.WithKind(kind), knownType)
  76. continue
  77. }
  78. default:
  79. if _, ok := obj.(metav1.ListMetaAccessor); ok {
  80. continue
  81. }
  82. if _, ok := value.Elem().Type().FieldByName("ObjectMeta"); ok {
  83. t.Errorf("%v (%v) has ObjectMeta but does not implement ObjectMetaAccessor", gv.WithKind(kind), knownType)
  84. continue
  85. }
  86. if _, ok := value.Elem().Type().FieldByName("ListMeta"); ok {
  87. t.Errorf("%v (%v) has ListMeta but does not implement ListMetaAccessor", gv.WithKind(kind), knownType)
  88. continue
  89. }
  90. t.Logf("%v (%v) does not implement ListMetaAccessor or ObjectMetaAccessor", gv.WithKind(kind), knownType)
  91. }
  92. }
  93. }
  94. }
  95. }