k8s.io/kubernetes@v1.29.3/pkg/kubeapiserver/options/admission_test.go (about) 1 /* 2 Copyright 2018 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 options 18 19 import ( 20 "reflect" 21 "testing" 22 23 "github.com/spf13/pflag" 24 "github.com/stretchr/testify/assert" 25 ) 26 27 func TestValidate(t *testing.T) { 28 // 1. Both `--admission-control` and `--enable-admission-plugins` are specified 29 options := NewAdmissionOptions() 30 options.PluginNames = []string{"ServiceAccount"} 31 options.GenericAdmission.EnablePlugins = []string{"NodeRestriction"} 32 if len(options.Validate()) == 0 { 33 t.Errorf("Expect error, but got none") 34 } 35 36 // 2. Both `--admission-control` and `--disable-admission-plugins` are specified 37 options = NewAdmissionOptions() 38 options.PluginNames = []string{"ServiceAccount"} 39 options.GenericAdmission.DisablePlugins = []string{"NodeRestriction"} 40 if len(options.Validate()) == 0 { 41 t.Errorf("Expect error, but got none") 42 } 43 44 // 3. PluginNames is not registered 45 options = NewAdmissionOptions() 46 options.PluginNames = []string{"pluginA"} 47 if len(options.Validate()) == 0 { 48 t.Errorf("Expect error, but got none") 49 } 50 51 // 4. PluginNames is not valid 52 options = NewAdmissionOptions() 53 options.PluginNames = []string{"ServiceAccount"} 54 if errs := options.Validate(); len(errs) > 0 { 55 t.Errorf("Unexpected err: %v", errs) 56 } 57 58 // nil pointer 59 options = nil 60 if errs := options.Validate(); errs != nil { 61 t.Errorf("expected no errors, error found %+v", errs) 62 } 63 } 64 65 func TestComputeEnabledAdmission(t *testing.T) { 66 tests := []struct { 67 name string 68 all []string 69 enabled []string 70 expectedDisabled []string 71 }{ 72 { 73 name: "matches", 74 all: []string{"one", "two"}, 75 enabled: []string{"one", "two"}, 76 expectedDisabled: []string{}, 77 }, 78 { 79 name: "choose one", 80 all: []string{"one", "two"}, 81 enabled: []string{"one"}, 82 expectedDisabled: []string{"two"}, 83 }, 84 } 85 86 for _, tc := range tests { 87 t.Run(tc.name, func(t *testing.T) { 88 actualEnabled, actualDisabled := computePluginNames(tc.enabled, tc.all) 89 if e, a := tc.enabled, actualEnabled; !reflect.DeepEqual(e, a) { 90 t.Errorf("expected %v, got %v", e, a) 91 } 92 if e, a := tc.expectedDisabled, actualDisabled; !reflect.DeepEqual(e, a) { 93 t.Errorf("expected %v, got %v", e, a) 94 } 95 }) 96 } 97 } 98 99 func TestAdmissionOptionsAddFlags(t *testing.T) { 100 var args = []string{ 101 "--enable-admission-plugins=foo,bar,baz", 102 "--admission-control-config-file=admission_control_config.yaml", 103 } 104 105 opts := NewAdmissionOptions() 106 pf := pflag.NewFlagSet("test-admission-opts", pflag.ContinueOnError) 107 opts.AddFlags(pf) 108 109 if err := pf.Parse(args); err != nil { 110 t.Fatal(err) 111 } 112 113 // using assert because cannot compare neither pointer nor function of underlying GenericAdmission 114 assert.Equal(t, opts.GenericAdmission.ConfigFile, "admission_control_config.yaml") 115 assert.Equal(t, opts.GenericAdmission.EnablePlugins, []string{"foo", "bar", "baz"}) 116 }