|
| 1 | +// Package utils provides helper functions for e2e tests, including |
| 2 | +// feature gate detection and validation utilities. |
| 3 | +package utils |
| 4 | + |
| 5 | +import ( |
| 6 | + "strings" |
| 7 | + "sync" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + appsv1 "k8s.io/api/apps/v1" |
| 12 | + "k8s.io/component-base/featuregate" |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 14 | + |
| 15 | + catdfeatures "github.com/operator-framework/operator-controller/internal/catalogd/features" |
| 16 | + opconfeatures "github.com/operator-framework/operator-controller/internal/operator-controller/features" |
| 17 | +) |
| 18 | + |
| 19 | +var ( |
| 20 | + featureGateStatus map[string]bool |
| 21 | + featureGateStatusOnce sync.Once |
| 22 | +) |
| 23 | + |
| 24 | +const ( |
| 25 | + fgPrefix = "--feature-gates=" |
| 26 | +) |
| 27 | + |
| 28 | +// SkipIfFeatureGateDisabled skips the test if the specified feature gate is disabled. |
| 29 | +// It queries the OLM deployments to detect feature gate settings and falls back to |
| 30 | +// programmatic defaults if the feature gate is not explicitly configured. |
| 31 | +func SkipIfFeatureGateDisabled(t *testing.T, fg string) { |
| 32 | + if !isFeatureGateEnabled(t, fg) { |
| 33 | + t.Skipf("Feature-gate %q disabled", fg) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func isFeatureGateEnabled(t *testing.T, fg string) bool { |
| 38 | + gatherFeatureGates(t) |
| 39 | + enabled, ok := featureGateStatus[fg] |
| 40 | + if ok { |
| 41 | + return enabled |
| 42 | + } |
| 43 | + |
| 44 | + // Not found (i.e. not explicitly set), so we need to find the programmed default. |
| 45 | + // Because feature-gates are organized by catd/opcon, we need to check each individually. |
| 46 | + // To avoid a panic, we need to check if it's a known gate first. |
| 47 | + mfgs := []featuregate.MutableFeatureGate{ |
| 48 | + catdfeatures.CatalogdFeatureGate, |
| 49 | + opconfeatures.OperatorControllerFeatureGate, |
| 50 | + } |
| 51 | + f := featuregate.Feature(fg) |
| 52 | + for _, mfg := range mfgs { |
| 53 | + known := mfg.GetAll() |
| 54 | + if _, ok := known[f]; ok { |
| 55 | + e := mfg.Enabled(f) |
| 56 | + t.Logf("Feature-gate %q not found in arguments, defaulting to %v", fg, e) |
| 57 | + return e |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + t.Fatalf("Unknown feature-gate: %q", fg) |
| 62 | + return false // unreachable, but required for compilation |
| 63 | +} |
| 64 | + |
| 65 | +func processFeatureGate(t *testing.T, featureGateValue string) { |
| 66 | + fgvs := strings.Split(featureGateValue, ",") |
| 67 | + for _, fg := range fgvs { |
| 68 | + v := strings.Split(fg, "=") |
| 69 | + require.Len(t, v, 2, "invalid feature-gate format: %q (expected name=value)", fg) |
| 70 | + switch v[1] { |
| 71 | + case "true": |
| 72 | + featureGateStatus[v[0]] = true |
| 73 | + t.Logf("Feature-gate %q enabled", v[0]) |
| 74 | + case "false": |
| 75 | + featureGateStatus[v[0]] = false |
| 76 | + t.Logf("Feature-gate %q disabled", v[0]) |
| 77 | + default: |
| 78 | + t.Fatalf("invalid feature-gate value: %q (expected true or false)", fg) |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func gatherFeatureGatesFromDeployment(t *testing.T, dep *appsv1.Deployment) { |
| 84 | + for _, con := range dep.Spec.Template.Spec.Containers { |
| 85 | + for _, arg := range con.Args { |
| 86 | + if strings.HasPrefix(arg, fgPrefix) { |
| 87 | + processFeatureGate(t, strings.TrimPrefix(arg, fgPrefix)) |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func gatherFeatureGates(t *testing.T) { |
| 94 | + featureGateStatusOnce.Do(func() { |
| 95 | + featureGateStatus = make(map[string]bool) |
| 96 | + |
| 97 | + depList := &appsv1.DeploymentList{} |
| 98 | + err := c.List(t.Context(), depList, client.MatchingLabels{ |
| 99 | + "app.kubernetes.io/part-of": "olm", |
| 100 | + }) |
| 101 | + require.NoError(t, err) |
| 102 | + require.Len(t, depList.Items, 2) |
| 103 | + |
| 104 | + for _, d := range depList.Items { |
| 105 | + gatherFeatureGatesFromDeployment(t, &d) |
| 106 | + } |
| 107 | + }) |
| 108 | +} |
0 commit comments