k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/plugin/pkg/admission/network/denyserviceexternalips/admission_test.go (about) 1 /* 2 Copyright 2020 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 denyserviceexternalips 18 19 import ( 20 "context" 21 "testing" 22 23 corev1 "k8s.io/api/core/v1" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "k8s.io/apimachinery/pkg/runtime" 26 "k8s.io/apiserver/pkg/admission" 27 "k8s.io/kubernetes/pkg/apis/core" 28 ) 29 30 func makeSvc(externalIPs ...string) *core.Service { 31 svc := &core.Service{} 32 svc.Namespace = "test-ns" 33 svc.Name = "test-svc" 34 svc.Spec.ExternalIPs = externalIPs 35 return svc 36 } 37 38 func TestAdmission(t *testing.T) { 39 testCases := []struct { 40 name string 41 newSvc *core.Service 42 oldSvc *core.Service 43 fail bool 44 }{{ 45 name: "create: without externalIPs", 46 newSvc: makeSvc(), 47 }, { 48 name: "create: with externalIPs", 49 newSvc: makeSvc("1.1.1.1"), 50 fail: true, 51 }, { 52 name: "update: same externalIPs", 53 newSvc: makeSvc("1.1.1.1", "2.2.2.2"), 54 oldSvc: makeSvc("1.1.1.1", "2.2.2.2"), 55 }, { 56 name: "update: reorder externalIPs", 57 newSvc: makeSvc("1.1.1.1", "2.2.2.2"), 58 oldSvc: makeSvc("2.2.2.2", "1.1.1.1"), 59 }, { 60 name: "update: change externalIPs", 61 newSvc: makeSvc("1.1.1.1", "2.2.2.2"), 62 oldSvc: makeSvc("1.1.1.1", "3.3.3.3"), 63 fail: true, 64 }, { 65 name: "update: add externalIPs", 66 newSvc: makeSvc("1.1.1.1", "2.2.2.2"), 67 oldSvc: makeSvc("1.1.1.1"), 68 fail: true, 69 }, { 70 name: "update: erase externalIPs", 71 newSvc: makeSvc(), 72 oldSvc: makeSvc("1.1.1.1", "2.2.2.2"), 73 }, { 74 name: "update: reduce externalIPs from back", 75 newSvc: makeSvc("1.1.1.1"), 76 oldSvc: makeSvc("1.1.1.1", "2.2.2.2"), 77 }, { 78 name: "update: reduce externalIPs from front", 79 newSvc: makeSvc("2.2.2.2"), 80 oldSvc: makeSvc("1.1.1.1", "2.2.2.2"), 81 }} 82 83 for _, tc := range testCases { 84 t.Run(tc.name, func(t *testing.T) { 85 ctrl := newPlugin() 86 87 var op admission.Operation 88 var opt runtime.Object 89 if tc.oldSvc == nil { 90 op = admission.Create 91 opt = &metav1.CreateOptions{} 92 } else { 93 op = admission.Update 94 opt = &metav1.UpdateOptions{} 95 } 96 97 attrs := admission.NewAttributesRecord( 98 tc.newSvc, // new object 99 tc.oldSvc, // old object 100 core.Kind("Service").WithVersion("version"), 101 tc.newSvc.Namespace, 102 tc.newSvc.Name, 103 corev1.Resource("services").WithVersion("version"), 104 "", // subresource 105 op, 106 opt, 107 false, // dryRun 108 nil, // userInfo 109 ) 110 111 err := ctrl.Validate(context.TODO(), attrs, nil) 112 if err != nil && !tc.fail { 113 t.Errorf("Unexpected failure: %v", err) 114 } 115 if err == nil && tc.fail { 116 t.Errorf("Unexpected success") 117 } 118 }) 119 } 120 }