|
| 1 | +package features |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "k8s.io/component-base/featuregate" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +// expectedDefaultFeatureState is a map of features with known states |
| 14 | +// |
| 15 | +// Always update this test map when adding a new FeatureName to the api, |
| 16 | +// a test is added below to enforce this. |
| 17 | +var expectedDefaultFeatureState = map[bool][]featuregate.Feature{ |
| 18 | + // features ENABLED by default, |
| 19 | + // list of features which are expected to be enabled at runtime. |
| 20 | + true: {}, |
| 21 | + |
| 22 | + // features DISABLED by default, |
| 23 | + // list of features which are expected to be disabled at runtime. |
| 24 | + false: { |
| 25 | + featuregate.Feature("IstioCSR"), |
| 26 | + }, |
| 27 | +} |
| 28 | + |
| 29 | +func TestFeatureGates(t *testing.T) { |
| 30 | + t.Run("runtime features to test should be identical with operator features", func(t *testing.T) { |
| 31 | + testFeatureNames := make([]featuregate.Feature, 0) |
| 32 | + for _, featureNames := range expectedDefaultFeatureState { |
| 33 | + testFeatureNames = append(testFeatureNames, featureNames...) |
| 34 | + } |
| 35 | + |
| 36 | + knownOperatorFeatures := make([]featuregate.Feature, 0) |
| 37 | + feats := mutableFeatureGate.GetAll() |
| 38 | + for feat := range feats { |
| 39 | + // skip "AllBeta", "AllAlpha": our operator does not use those |
| 40 | + if feat == "AllBeta" || feat == "AllAlpha" { |
| 41 | + continue |
| 42 | + } |
| 43 | + knownOperatorFeatures = append(knownOperatorFeatures, feat) |
| 44 | + } |
| 45 | + |
| 46 | + assert.Equal(t, knownOperatorFeatures, testFeatureNames, |
| 47 | + `the list of features known to the operator differ from what is being tested here, |
| 48 | + it could be that there was a new Feature added to the api which wasn't added to the tests. |
| 49 | + Please verify "api/operator/v1alpha1" and "pkg/operator/features" have identical features.`) |
| 50 | + }) |
| 51 | + |
| 52 | + t.Run("all runtime features should honor expected default feature state", func(t *testing.T) { |
| 53 | + for expectedDefaultState, features := range expectedDefaultFeatureState { |
| 54 | + for _, featureName := range features { |
| 55 | + assert.Equal(t, expectedDefaultState, DefaultFeatureGate.Enabled(featureName), |
| 56 | + "violated by feature=%s", featureName) |
| 57 | + } |
| 58 | + } |
| 59 | + }) |
| 60 | + |
| 61 | + t.Run("all TechPreview features should be disabled by default", func(t *testing.T) { |
| 62 | + feats := mutableFeatureGate.GetAll() |
| 63 | + for feat, spec := range feats { |
| 64 | + // skip "AllBeta", "AllAlpha": our operator does not use those |
| 65 | + if feat == "AllBeta" || feat == "AllAlpha" { |
| 66 | + continue |
| 67 | + } |
| 68 | + |
| 69 | + assert.Equal(t, spec.PreRelease == "TechPreview", !spec.Default, |
| 70 | + "prerelease TechPreview %q feature should default to disabled", |
| 71 | + feat) |
| 72 | + } |
| 73 | + }) |
| 74 | + |
| 75 | + t.Run("runtime features should allow enabling all feature gates that are disabled by default", func(t *testing.T) { |
| 76 | + // enable all disabled features |
| 77 | + defaultDisabledFeatures := expectedDefaultFeatureState[false] |
| 78 | + |
| 79 | + featVals := make([]string, len(defaultDisabledFeatures)) |
| 80 | + for i := range featVals { |
| 81 | + featVals[i] = fmt.Sprintf("%s=true", defaultDisabledFeatures[i]) |
| 82 | + } |
| 83 | + |
| 84 | + err := mutableFeatureGate.Set(strings.Join(featVals, ",")) |
| 85 | + assert.NoError(t, err) |
| 86 | + |
| 87 | + // check if all those features were enabled successfully |
| 88 | + for _, featureName := range defaultDisabledFeatures { |
| 89 | + assert.Equal(t, true, DefaultFeatureGate.Enabled(featureName), |
| 90 | + "violated by feature=%s", featureName) |
| 91 | + } |
| 92 | + }) |
| 93 | +} |
0 commit comments