k8s.io/client-go@v0.31.1/features/testing/features.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 testing 18 19 import ( 20 "fmt" 21 "strings" 22 "sync" 23 "testing" 24 25 clientfeatures "k8s.io/client-go/features" 26 ) 27 28 var ( 29 overriddenFeaturesLock sync.Mutex 30 overriddenFeatures map[clientfeatures.Feature]string 31 ) 32 33 func init() { 34 overriddenFeatures = map[clientfeatures.Feature]string{} 35 } 36 37 type featureGatesSetter interface { 38 clientfeatures.Gates 39 40 Set(clientfeatures.Feature, bool) error 41 } 42 43 // SetFeatureDuringTest sets the specified feature to the specified value for the duration of the test. 44 // 45 // Example use: 46 // 47 // clientfeaturestesting.SetFeatureDuringTest(t, clientfeatures.WatchListClient, true) 48 func SetFeatureDuringTest(tb testing.TB, feature clientfeatures.Feature, featureValue bool) { 49 if err := setFeatureDuringTestInternal(tb, feature, featureValue); err != nil { 50 tb.Fatal(err) 51 } 52 } 53 54 func setFeatureDuringTestInternal(tb testing.TB, feature clientfeatures.Feature, featureValue bool) error { 55 overriddenFeaturesLock.Lock() 56 defer overriddenFeaturesLock.Unlock() 57 58 currentFeatureGates := clientfeatures.FeatureGates() 59 featureGates, ok := currentFeatureGates.(featureGatesSetter) 60 if !ok { 61 panic(fmt.Errorf("clientfeatures.FeatureGates(): %T does not implement featureGatesSetter interface", currentFeatureGates)) 62 } 63 64 originalFeatureValue := featureGates.Enabled(feature) 65 if overridingTestName, ok := overriddenFeatures[feature]; ok { 66 if !sameTestOrSubtest(tb, overridingTestName) { 67 return fmt.Errorf("client-go feature %q is currently overridden by %q test and cannot be also modified by %q", feature, overridingTestName, tb.Name()) 68 } 69 } 70 71 if err := featureGates.Set(feature, featureValue); err != nil { 72 return err 73 } 74 overriddenFeatures[feature] = tb.Name() 75 76 tb.Cleanup(func() { 77 overriddenFeaturesLock.Lock() 78 defer overriddenFeaturesLock.Unlock() 79 delete(overriddenFeatures, feature) 80 if err := featureGates.Set(feature, originalFeatureValue); err != nil { 81 tb.Errorf("failed restoring client-go feature: %v to its original value: %v, err: %v", feature, originalFeatureValue, err) 82 } 83 }) 84 return nil 85 } 86 87 // copied from component-base/featuregate/testing 88 func sameTestOrSubtest(tb testing.TB, testName string) bool { 89 return tb.Name() == testName || strings.HasPrefix(tb.Name(), testName+"/") 90 }