k8s.io/kubernetes@v1.29.3/pkg/api/testing/meta_test.go (about) 1 /* 2 Copyright 2014 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package testing 18 19 import ( 20 "reflect" 21 "testing" 22 23 "k8s.io/apimachinery/pkg/api/meta" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "k8s.io/apimachinery/pkg/runtime" 26 "k8s.io/apimachinery/pkg/runtime/schema" 27 "k8s.io/kubernetes/pkg/api/legacyscheme" 28 ) 29 30 var _ metav1.Object = &metav1.ObjectMeta{} 31 32 func TestAccessorImplementations(t *testing.T) { 33 for _, gv := range legacyscheme.Scheme.PrioritizedVersionsAllGroups() { 34 internalGV := schema.GroupVersion{Group: gv.Group, Version: runtime.APIVersionInternal} 35 for _, gv := range []schema.GroupVersion{gv, internalGV} { 36 for kind, knownType := range legacyscheme.Scheme.KnownTypes(gv) { 37 value := reflect.New(knownType) 38 obj := value.Interface() 39 if _, ok := obj.(runtime.Object); !ok { 40 t.Errorf("%v (%v) does not implement runtime.Object", gv.WithKind(kind), knownType) 41 } 42 lm, isLM := obj.(meta.ListMetaAccessor) 43 om, isOM := obj.(metav1.ObjectMetaAccessor) 44 switch { 45 case isLM && isOM: 46 t.Errorf("%v (%v) implements ListMetaAccessor and ObjectMetaAccessor", gv.WithKind(kind), knownType) 47 continue 48 case isLM: 49 m := lm.GetListMeta() 50 if m == nil { 51 t.Errorf("%v (%v) returns nil ListMeta", gv.WithKind(kind), knownType) 52 continue 53 } 54 m.SetResourceVersion("102030") 55 if m.GetResourceVersion() != "102030" { 56 t.Errorf("%v (%v) did not preserve resource version", gv.WithKind(kind), knownType) 57 continue 58 } 59 case isOM: 60 m := om.GetObjectMeta() 61 if m == nil { 62 t.Errorf("%v (%v) returns nil ObjectMeta", gv.WithKind(kind), knownType) 63 continue 64 } 65 m.SetResourceVersion("102030") 66 if m.GetResourceVersion() != "102030" { 67 t.Errorf("%v (%v) did not preserve resource version", gv.WithKind(kind), knownType) 68 continue 69 } 70 labels := map[string]string{"a": "b"} 71 m.SetLabels(labels) 72 if !reflect.DeepEqual(m.GetLabels(), labels) { 73 t.Errorf("%v (%v) did not preserve labels", gv.WithKind(kind), knownType) 74 continue 75 } 76 default: 77 if _, ok := obj.(metav1.ListMetaAccessor); ok { 78 continue 79 } 80 if _, ok := value.Elem().Type().FieldByName("ObjectMeta"); ok { 81 t.Errorf("%v (%v) has ObjectMeta but does not implement ObjectMetaAccessor", gv.WithKind(kind), knownType) 82 continue 83 } 84 if _, ok := value.Elem().Type().FieldByName("ListMeta"); ok { 85 t.Errorf("%v (%v) has ListMeta but does not implement ListMetaAccessor", gv.WithKind(kind), knownType) 86 continue 87 } 88 t.Logf("%v (%v) does not implement ListMetaAccessor or ObjectMetaAccessor", gv.WithKind(kind), knownType) 89 } 90 } 91 } 92 } 93 }