k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/plugin/pkg/admission/disableservicelinks/admission_test.go (about) 1 /* 2 Copyright 2024 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 disableservicelinks 18 19 import ( 20 "context" 21 "testing" 22 23 "k8s.io/apiserver/pkg/admission" 24 admissiontesting "k8s.io/apiserver/pkg/admission/testing" 25 "k8s.io/kubernetes/pkg/apis/core" 26 "k8s.io/kubernetes/pkg/apis/core/helper" 27 "k8s.io/utils/ptr" 28 ) 29 30 func TestAdmit(t *testing.T) { 31 32 plugin := admissiontesting.WithReinvocationTesting(t, newDisableServiceLinks()) 33 34 tests := []struct { 35 description string 36 requestedPod core.Pod 37 expectedPod core.Pod 38 operation admission.Operation 39 }{ 40 { 41 description: "Create empty pod with default value of Spec.EnableServiceLinks", 42 requestedPod: core.Pod{ 43 Spec: core.PodSpec{}, 44 }, 45 expectedPod: core.Pod{ 46 Spec: core.PodSpec{EnableServiceLinks: ptr.To(false)}, 47 }, 48 operation: admission.Create, 49 }, 50 { 51 description: "Create empty pod with Spec.EnableServiceLinks set to true", 52 requestedPod: core.Pod{ 53 Spec: core.PodSpec{EnableServiceLinks: ptr.To(true)}, 54 }, 55 expectedPod: core.Pod{ 56 Spec: core.PodSpec{EnableServiceLinks: ptr.To(false)}, 57 }, 58 operation: admission.Create, 59 }, 60 { 61 description: "Update empty pod with Spec.EnableServiceLinks set to true", 62 requestedPod: core.Pod{ 63 Spec: core.PodSpec{EnableServiceLinks: ptr.To(true)}, 64 }, 65 expectedPod: core.Pod{ 66 Spec: core.PodSpec{EnableServiceLinks: ptr.To(true)}, 67 }, 68 operation: admission.Update, 69 }, 70 } 71 for i, test := range tests { 72 err := plugin.Admit(context.TODO(), admission.NewAttributesRecord(&test.requestedPod, nil, 73 core.Kind("Pod").WithVersion("version"), "foo", "name", 74 core.Resource("pods").WithVersion("version"), "", test.operation, 75 nil, false, nil), nil) 76 77 if err != nil { 78 t.Errorf("[%d: %s] unexpected error %v for pod %+v", i, test.description, err, test.requestedPod) 79 } 80 81 if !helper.Semantic.DeepEqual(test.expectedPod.Spec, test.requestedPod.Spec) { 82 t.Errorf("[%d: %s] expected %t got %t", i, test.description, *test.expectedPod.Spec.EnableServiceLinks, *test.requestedPod.Spec.EnableServiceLinks) 83 } 84 } 85 } 86 87 func TestHandles(t *testing.T) { 88 plugin := newDisableServiceLinks() 89 tests := map[admission.Operation]bool{ 90 admission.Create: true, 91 admission.Update: true, 92 admission.Delete: false, 93 admission.Connect: false, 94 } 95 for op, expected := range tests { 96 result := plugin.Handles(op) 97 if result != expected { 98 t.Errorf("Unexpected result for operation %s: %v\n", op, result) 99 } 100 } 101 }