-
Notifications
You must be signed in to change notification settings - Fork 38.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add functions to set client-go featuregates directly for testing #124780
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
staging/src/k8s.io/client-go/features/testing/feature_gate.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
Copyright 2017 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package testing | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"sync" | ||
"testing" | ||
|
||
"k8s.io/client-go/features" | ||
) | ||
|
||
// nearly direct copy from component-base with package shifting | ||
|
||
var ( | ||
overrideLock sync.Mutex | ||
featureFlagOverride map[features.Feature]string | ||
) | ||
|
||
func init() { | ||
featureFlagOverride = map[features.Feature]string{} | ||
} | ||
|
||
// SetFeatureGateDuringTest sets the specified gate to the specified value for duration of the test. | ||
// Fails when it detects second call to the same flag or is unable to set or restore feature flag. | ||
// | ||
// WARNING: Can leak set variable when called in test calling t.Parallel(), however second attempt to set the same feature flag will cause fatal. | ||
// | ||
// Example use: | ||
// | ||
// featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.<FeatureName>, true) | ||
func SetFeatureGateDuringTest(tb testing.TB, gate features.Gates, f features.Feature, value bool) { | ||
tb.Helper() | ||
detectParallelOverrideCleanup := detectParallelOverride(tb, f) | ||
originalValue := gate.Enabled(f) | ||
|
||
if err := gate.(features.MutableGates).Set(fmt.Sprintf("%s=%v", f, value)); err != nil { | ||
tb.Errorf("error setting %s=%v: %v", f, value, err) | ||
} | ||
|
||
tb.Cleanup(func() { | ||
tb.Helper() | ||
detectParallelOverrideCleanup() | ||
if err := gate.(features.MutableGates).Set(fmt.Sprintf("%s=%v", f, originalValue)); err != nil { | ||
tb.Errorf("error restoring %s=%v: %v", f, originalValue, err) | ||
} | ||
}) | ||
} | ||
|
||
func detectParallelOverride(tb testing.TB, f features.Feature) func() { | ||
tb.Helper() | ||
overrideLock.Lock() | ||
defer overrideLock.Unlock() | ||
beforeOverrideTestName := featureFlagOverride[f] | ||
if beforeOverrideTestName != "" && !sameTestOrSubtest(tb, beforeOverrideTestName) { | ||
tb.Fatalf("Detected parallel setting of a feature gate by both %q and %q", beforeOverrideTestName, tb.Name()) | ||
} | ||
featureFlagOverride[f] = tb.Name() | ||
|
||
return func() { | ||
tb.Helper() | ||
overrideLock.Lock() | ||
defer overrideLock.Unlock() | ||
if afterOverrideTestName := featureFlagOverride[f]; afterOverrideTestName != tb.Name() { | ||
tb.Fatalf("Detected parallel setting of a feature gate between both %q and %q", afterOverrideTestName, tb.Name()) | ||
} | ||
featureFlagOverride[f] = beforeOverrideTestName | ||
} | ||
} | ||
|
||
func sameTestOrSubtest(tb testing.TB, testName string) bool { | ||
// Assumes that "/" is not used in test names. | ||
return tb.Name() == testName || strings.HasPrefix(tb.Name(), testName+"/") | ||
} |
149 changes: 149 additions & 0 deletions
149
staging/src/k8s.io/client-go/features/testing/feature_gate_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package testing | ||
|
||
import ( | ||
gotest "testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"k8s.io/client-go/features" | ||
) | ||
|
||
func expect(t *gotest.T, gate features.Gates, expect map[features.Feature]bool) { | ||
t.Helper() | ||
for k, v := range expect { | ||
if gate.Enabled(k) != v { | ||
t.Errorf("Expected %v=%v, got %v", k, v, gate.Enabled(k)) | ||
} | ||
} | ||
} | ||
|
||
func TestSetFeatureGateInTest(t *gotest.T) { | ||
gate := features.NewEnvVarFeatureGates(map[features.Feature]features.FeatureSpec{ | ||
"feature": {PreRelease: features.Alpha, Default: false}, | ||
}) | ||
|
||
assert.False(t, gate.Enabled("feature")) | ||
SetFeatureGateDuringTest(t, gate, "feature", true) | ||
SetFeatureGateDuringTest(t, gate, "feature", true) | ||
|
||
assert.True(t, gate.Enabled("feature")) | ||
t.Run("Subtest", func(t *gotest.T) { | ||
assert.True(t, gate.Enabled("feature")) | ||
}) | ||
|
||
t.Run("ParallelSubtest", func(t *gotest.T) { | ||
assert.True(t, gate.Enabled("feature")) | ||
// Calling t.Parallel in subtest will resume the main test body | ||
t.Parallel() | ||
assert.True(t, gate.Enabled("feature")) | ||
}) | ||
assert.True(t, gate.Enabled("feature")) | ||
|
||
t.Run("OverwriteInSubtest", func(t *gotest.T) { | ||
SetFeatureGateDuringTest(t, gate, "feature", false) | ||
assert.False(t, gate.Enabled("feature")) | ||
}) | ||
assert.True(t, gate.Enabled("feature")) | ||
} | ||
|
||
func TestDetectLeakToMainTest(t *gotest.T) { | ||
t.Cleanup(func() { | ||
featureFlagOverride = map[features.Feature]string{} | ||
}) | ||
gate := features.NewEnvVarFeatureGates(map[features.Feature]features.FeatureSpec{ | ||
"feature": {PreRelease: features.Alpha, Default: false}, | ||
}) | ||
|
||
// Subtest setting feature gate and calling parallel will leak it out | ||
t.Run("LeakingSubtest", func(t *gotest.T) { | ||
fakeT := &ignoreFatalT{T: t} | ||
SetFeatureGateDuringTest(fakeT, gate, "feature", true) | ||
// Calling t.Parallel in subtest will resume the main test body | ||
t.Parallel() | ||
// Leaked false from main test | ||
assert.False(t, gate.Enabled("feature")) | ||
}) | ||
// Leaked true from subtest | ||
assert.True(t, gate.Enabled("feature")) | ||
fakeT := &ignoreFatalT{T: t} | ||
SetFeatureGateDuringTest(fakeT, gate, "feature", false) | ||
assert.True(t, fakeT.fatalRecorded) | ||
} | ||
|
||
func TestDetectLeakToOtherSubtest(t *gotest.T) { | ||
t.Cleanup(func() { | ||
featureFlagOverride = map[features.Feature]string{} | ||
}) | ||
gate := features.NewEnvVarFeatureGates(map[features.Feature]features.FeatureSpec{ | ||
"feature": {PreRelease: features.Alpha, Default: false}, | ||
}) | ||
|
||
subtestName := "Subtest" | ||
// Subtest setting feature gate and calling parallel will leak it out | ||
t.Run(subtestName, func(t *gotest.T) { | ||
fakeT := &ignoreFatalT{T: t} | ||
SetFeatureGateDuringTest(fakeT, gate, "feature", true) | ||
t.Parallel() | ||
}) | ||
// Add suffix to name to prevent tests with the same prefix. | ||
t.Run(subtestName+"Suffix", func(t *gotest.T) { | ||
// Leaked true | ||
assert.True(t, gate.Enabled("feature")) | ||
|
||
fakeT := &ignoreFatalT{T: t} | ||
SetFeatureGateDuringTest(fakeT, gate, "feature", false) | ||
assert.True(t, fakeT.fatalRecorded) | ||
}) | ||
} | ||
|
||
func TestCannotDetectLeakFromSubtest(t *gotest.T) { | ||
t.Cleanup(func() { | ||
featureFlagOverride = map[features.Feature]string{} | ||
}) | ||
gate := features.NewEnvVarFeatureGates(map[features.Feature]features.FeatureSpec{ | ||
"feature": {PreRelease: features.Alpha, Default: false}, | ||
}) | ||
|
||
SetFeatureGateDuringTest(t, gate, "feature", false) | ||
// Subtest setting feature gate and calling parallel will leak it out | ||
t.Run("Subtest", func(t *gotest.T) { | ||
SetFeatureGateDuringTest(t, gate, "feature", true) | ||
t.Parallel() | ||
}) | ||
// Leaked true | ||
assert.True(t, gate.Enabled("feature")) | ||
} | ||
|
||
type ignoreFatalT struct { | ||
*gotest.T | ||
fatalRecorded bool | ||
} | ||
|
||
func (f *ignoreFatalT) Fatal(args ...any) { | ||
f.T.Helper() | ||
f.fatalRecorded = true | ||
newArgs := []any{"[IGNORED]"} | ||
newArgs = append(newArgs, args...) | ||
f.T.Log(newArgs...) | ||
} | ||
|
||
func (f *ignoreFatalT) Fatalf(format string, args ...any) { | ||
f.T.Helper() | ||
f.fatalRecorded = true | ||
f.T.Logf("[IGNORED] "+format, args...) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the "standard" function from component-base minus the allalpha and allbeta.