mirror of
https://github.com/kubernetes/client-go.git
synced 2025-09-06 01:20:40 +00:00
This PR add a feature gates mechanisim to client-go as described in https://docs.google.com/document/d/1g9BGCRw-7ucUxO6OtCWbb3lfzUGA_uU9178wLdXAIfs In particular: - Adds a default feature gate implementation based on environment variables. - Adds a set of methods for reading, overwriting the default implementation, and adding features to an external registry. Co-authored-by: deads2k <deads@redhat.com> Co-authored-by: Ben Luddy <bluddy@redhat.com> Kubernetes-commit: d74c57d4f592d20a992afb54b1ee64f56215210e
80 lines
2.2 KiB
Go
80 lines
2.2 KiB
Go
/*
|
|
Copyright 2024 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"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"k8s.io/client-go/features"
|
|
)
|
|
|
|
func TestDriveInitDefaultFeatureGates(t *testing.T) {
|
|
featureGates := features.FeatureGates()
|
|
assertFunctionPanicsWithMessage(t, func() { featureGates.Enabled("FakeFeatureGate") }, "features.FeatureGates().Enabled", fmt.Sprintf("feature %q is not registered in FeatureGate", "FakeFeatureGate"))
|
|
|
|
fakeFeatureGates := &alwaysEnabledFakeGates{}
|
|
require.True(t, fakeFeatureGates.Enabled("FakeFeatureGate"))
|
|
|
|
features.ReplaceFeatureGates(fakeFeatureGates)
|
|
featureGates = features.FeatureGates()
|
|
|
|
assertFeatureGatesType(t, featureGates)
|
|
require.True(t, featureGates.Enabled("FakeFeatureGate"))
|
|
}
|
|
|
|
type alwaysEnabledFakeGates struct{}
|
|
|
|
func (f *alwaysEnabledFakeGates) Enabled(features.Feature) bool {
|
|
return true
|
|
}
|
|
|
|
func assertFeatureGatesType(t *testing.T, fg features.Gates) {
|
|
_, ok := fg.(*alwaysEnabledFakeGates)
|
|
if !ok {
|
|
t.Fatalf("passed features.FeatureGates() is NOT of type *alwaysEnabledFakeGates, it is of type = %T", fg)
|
|
}
|
|
}
|
|
|
|
func assertFunctionPanicsWithMessage(t *testing.T, f func(), fName, errMessage string) {
|
|
didPanic, panicMessage := didFunctionPanic(f)
|
|
if !didPanic {
|
|
t.Fatalf("function %q did not panicked", fName)
|
|
}
|
|
|
|
panicError, ok := panicMessage.(error)
|
|
if !ok || !strings.Contains(panicError.Error(), errMessage) {
|
|
t.Fatalf("func %q should panic with error message:\t%#v\n\tPanic value:\t%#v\n", fName, errMessage, panicMessage)
|
|
}
|
|
}
|
|
|
|
func didFunctionPanic(f func()) (didPanic bool, panicMessage interface{}) {
|
|
didPanic = true
|
|
|
|
defer func() {
|
|
panicMessage = recover()
|
|
}()
|
|
|
|
f()
|
|
didPanic = false
|
|
|
|
return
|
|
}
|