...

Source file src/k8s.io/component-base/featuregate/testing/feature_gate.go

Documentation: k8s.io/component-base/featuregate/testing

     1  /*
     2  Copyright 2017 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  	"k8s.io/component-base/featuregate"
    26  )
    27  
    28  var (
    29  	overrideLock        sync.Mutex
    30  	featureFlagOverride map[featuregate.Feature]string
    31  )
    32  
    33  func init() {
    34  	featureFlagOverride = map[featuregate.Feature]string{}
    35  }
    36  
    37  // SetFeatureGateDuringTest sets the specified gate to the specified value for duration of the test.
    38  // Fails when it detects second call to the same flag or is unable to set or restore feature flag.
    39  // Returns empty cleanup function to maintain the old function signature that uses defer.
    40  // TODO: Remove defer from calls to SetFeatureGateDuringTest and update hack/verify-test-featuregates.sh when we can do large scale code change.
    41  //
    42  // WARNING: Can leak set variable when called in test calling t.Parallel(), however second attempt to set the same feature flag will cause fatal.
    43  //
    44  // Example use:
    45  //
    46  // defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.<FeatureName>, true)()
    47  func SetFeatureGateDuringTest(tb testing.TB, gate featuregate.FeatureGate, f featuregate.Feature, value bool) func() {
    48  	tb.Helper()
    49  	detectParallelOverrideCleanup := detectParallelOverride(tb, f)
    50  	originalValue := gate.Enabled(f)
    51  
    52  	// Specially handle AllAlpha and AllBeta
    53  	var cleanups []func()
    54  	if f == "AllAlpha" || f == "AllBeta" {
    55  		// Iterate over individual gates so their individual values get restored
    56  		for k, v := range gate.(featuregate.MutableFeatureGate).GetAll() {
    57  			if k == "AllAlpha" || k == "AllBeta" {
    58  				continue
    59  			}
    60  			if (f == "AllAlpha" && v.PreRelease == featuregate.Alpha) || (f == "AllBeta" && v.PreRelease == featuregate.Beta) {
    61  				cleanups = append(cleanups, SetFeatureGateDuringTest(tb, gate, k, value))
    62  			}
    63  		}
    64  	}
    65  
    66  	if err := gate.(featuregate.MutableFeatureGate).Set(fmt.Sprintf("%s=%v", f, value)); err != nil {
    67  		tb.Errorf("error setting %s=%v: %v", f, value, err)
    68  	}
    69  
    70  	tb.Cleanup(func() {
    71  		tb.Helper()
    72  		detectParallelOverrideCleanup()
    73  		if err := gate.(featuregate.MutableFeatureGate).Set(fmt.Sprintf("%s=%v", f, originalValue)); err != nil {
    74  			tb.Errorf("error restoring %s=%v: %v", f, originalValue, err)
    75  		}
    76  		for _, cleanup := range cleanups {
    77  			cleanup()
    78  		}
    79  	})
    80  	return func() {}
    81  }
    82  
    83  func detectParallelOverride(tb testing.TB, f featuregate.Feature) func() {
    84  	tb.Helper()
    85  	overrideLock.Lock()
    86  	defer overrideLock.Unlock()
    87  	beforeOverrideTestName := featureFlagOverride[f]
    88  	if beforeOverrideTestName != "" && !sameTestOrSubtest(tb, beforeOverrideTestName) {
    89  		tb.Fatalf("Detected parallel setting of a feature gate by both %q and %q", beforeOverrideTestName, tb.Name())
    90  	}
    91  	featureFlagOverride[f] = tb.Name()
    92  
    93  	return func() {
    94  		tb.Helper()
    95  		overrideLock.Lock()
    96  		defer overrideLock.Unlock()
    97  		if afterOverrideTestName := featureFlagOverride[f]; afterOverrideTestName != tb.Name() {
    98  			tb.Fatalf("Detected parallel setting of a feature gate between both %q and %q", afterOverrideTestName, tb.Name())
    99  		}
   100  		featureFlagOverride[f] = beforeOverrideTestName
   101  	}
   102  }
   103  
   104  func sameTestOrSubtest(tb testing.TB, testName string) bool {
   105  	// Assumes that "/" is not used in test names.
   106  	return tb.Name() == testName || strings.HasPrefix(tb.Name(), testName+"/")
   107  }
   108  

View as plain text