k8s.io/kubernetes@v1.29.3/pkg/registry/rbac/helpers_test.go (about) 1 /* 2 Copyright 2017 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 rbac 18 19 import ( 20 "reflect" 21 "testing" 22 23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 24 "k8s.io/apimachinery/pkg/runtime" 25 "k8s.io/apimachinery/pkg/types" 26 kapi "k8s.io/kubernetes/pkg/apis/core" 27 kapihelper "k8s.io/kubernetes/pkg/apis/core/helper" 28 29 fuzz "github.com/google/gofuzz" 30 ) 31 32 func newPod() *kapi.Pod { 33 return &kapi.Pod{ 34 ObjectMeta: metav1.ObjectMeta{ 35 Annotations: map[string]string{}, 36 Name: "foo", 37 OwnerReferences: []metav1.OwnerReference{}, 38 }, 39 } 40 41 } 42 43 func TestIsOnlyMutatingGCFields(t *testing.T) { 44 tests := []struct { 45 name string 46 obj func() runtime.Object 47 old func() runtime.Object 48 expected bool 49 }{ 50 { 51 name: "same", 52 obj: func() runtime.Object { 53 return newPod() 54 }, 55 old: func() runtime.Object { 56 return newPod() 57 }, 58 expected: true, 59 }, 60 { 61 name: "different managedFields", 62 obj: func() runtime.Object { 63 return newPod() 64 }, 65 old: func() runtime.Object { 66 obj := newPod() 67 obj.ManagedFields = []metav1.ManagedFieldsEntry{ 68 { 69 Manager: "manager", 70 }, 71 } 72 return obj 73 }, 74 expected: true, 75 }, 76 { 77 name: "only annotations", 78 obj: func() runtime.Object { 79 obj := newPod() 80 obj.Annotations["foo"] = "bar" 81 return obj 82 }, 83 old: func() runtime.Object { 84 return newPod() 85 }, 86 expected: false, 87 }, 88 { 89 name: "only other", 90 obj: func() runtime.Object { 91 obj := newPod() 92 obj.Spec.RestartPolicy = kapi.RestartPolicyAlways 93 return obj 94 }, 95 old: func() runtime.Object { 96 return newPod() 97 }, 98 expected: false, 99 }, 100 { 101 name: "only ownerRef", 102 obj: func() runtime.Object { 103 obj := newPod() 104 obj.OwnerReferences = append(obj.OwnerReferences, metav1.OwnerReference{Name: "foo"}) 105 return obj 106 }, 107 old: func() runtime.Object { 108 return newPod() 109 }, 110 expected: true, 111 }, 112 { 113 name: "ownerRef and finalizer", 114 obj: func() runtime.Object { 115 obj := newPod() 116 obj.OwnerReferences = append(obj.OwnerReferences, metav1.OwnerReference{Name: "foo"}) 117 obj.Finalizers = []string{"final"} 118 return obj 119 }, 120 old: func() runtime.Object { 121 return newPod() 122 }, 123 expected: true, 124 }, 125 { 126 name: "and annotations", 127 obj: func() runtime.Object { 128 obj := newPod() 129 obj.OwnerReferences = append(obj.OwnerReferences, metav1.OwnerReference{Name: "foo"}) 130 obj.Annotations["foo"] = "bar" 131 return obj 132 }, 133 old: func() runtime.Object { 134 return newPod() 135 }, 136 expected: false, 137 }, 138 { 139 name: "and other", 140 obj: func() runtime.Object { 141 obj := newPod() 142 obj.OwnerReferences = append(obj.OwnerReferences, metav1.OwnerReference{Name: "foo"}) 143 obj.Spec.RestartPolicy = kapi.RestartPolicyAlways 144 return obj 145 }, 146 old: func() runtime.Object { 147 return newPod() 148 }, 149 expected: false, 150 }, 151 { 152 name: "and nil", 153 obj: func() runtime.Object { 154 obj := newPod() 155 obj.OwnerReferences = append(obj.OwnerReferences, metav1.OwnerReference{Name: "foo"}) 156 obj.Spec.RestartPolicy = kapi.RestartPolicyAlways 157 return obj 158 }, 159 old: func() runtime.Object { 160 return (*kapi.Pod)(nil) 161 }, 162 expected: false, 163 }, 164 } 165 166 for _, tc := range tests { 167 actual := IsOnlyMutatingGCFields(tc.obj(), tc.old(), kapihelper.Semantic) 168 if tc.expected != actual { 169 t.Errorf("%s: expected %v, got %v", tc.name, tc.expected, actual) 170 } 171 } 172 } 173 174 func TestNewMetadataFields(t *testing.T) { 175 f := fuzz.New().NilChance(0.0).NumElements(1, 1) 176 for i := 0; i < 100; i++ { 177 objMeta := metav1.ObjectMeta{} 178 f.Fuzz(&objMeta) 179 objMeta.Name = "" 180 objMeta.GenerateName = "" 181 objMeta.Namespace = "" 182 objMeta.SelfLink = "" 183 objMeta.UID = types.UID("") 184 objMeta.ResourceVersion = "" 185 objMeta.Generation = 0 186 objMeta.CreationTimestamp = metav1.Time{} 187 objMeta.DeletionTimestamp = nil 188 objMeta.DeletionGracePeriodSeconds = nil 189 objMeta.Labels = nil 190 objMeta.Annotations = nil 191 objMeta.OwnerReferences = nil 192 objMeta.Finalizers = nil 193 objMeta.ManagedFields = nil 194 195 if !reflect.DeepEqual(metav1.ObjectMeta{}, objMeta) { 196 t.Fatalf(`A new field was introduced in ObjectMeta, add the field to 197 IsOnlyMutatingGCFields if necessary, and update this test: 198 %#v`, objMeta) 199 } 200 } 201 }