k8s.io/apiserver@v0.31.1/pkg/server/options/api_enablement_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 "strings" 21 "testing" 22 23 utilerrors "k8s.io/apimachinery/pkg/util/errors" 24 cliflag "k8s.io/component-base/cli/flag" 25 ) 26 27 type fakeGroupRegistry struct{} 28 29 func (f fakeGroupRegistry) IsGroupRegistered(group string) bool { 30 return group == "apiregistration.k8s.io" 31 } 32 33 func TestAPIEnablementOptionsValidate(t *testing.T) { 34 testCases := []struct { 35 name string 36 runtimeConfig cliflag.ConfigurationMap 37 expectErr string 38 }{ 39 { 40 name: "test when options is nil", 41 }, 42 { 43 name: "test when invalid key with only api/all=false", 44 runtimeConfig: cliflag.ConfigurationMap{"api/all": "false"}, 45 expectErr: "invalid key with only api/all=false", 46 }, 47 { 48 name: "test when ConfigurationMap key is invalid", 49 runtimeConfig: cliflag.ConfigurationMap{"apiall": "false"}, 50 expectErr: "runtime-config invalid key", 51 }, 52 { 53 name: "test when unknown api groups", 54 runtimeConfig: cliflag.ConfigurationMap{"api/v1": "true"}, 55 expectErr: "unknown api groups", 56 }, 57 { 58 name: "test when valid api groups", 59 runtimeConfig: cliflag.ConfigurationMap{"apiregistration.k8s.io/v1beta1": "true"}, 60 }, 61 } 62 testGroupRegistry := fakeGroupRegistry{} 63 64 for _, testcase := range testCases { 65 t.Run(testcase.name, func(t *testing.T) { 66 testOptions := &APIEnablementOptions{ 67 RuntimeConfig: testcase.runtimeConfig, 68 } 69 errs := testOptions.Validate(testGroupRegistry) 70 if len(testcase.expectErr) != 0 && !strings.Contains(utilerrors.NewAggregate(errs).Error(), testcase.expectErr) { 71 t.Errorf("got err: %v, expected err: %s", errs, testcase.expectErr) 72 } 73 74 if len(testcase.expectErr) == 0 && len(errs) != 0 { 75 t.Errorf("got err: %s, expected err nil", errs) 76 } 77 }) 78 } 79 }