Skip to content

Commit 5d0caaa

Browse files
committed
feat: Add alpha feature verification to feature gates
Implement a new function, verifyAlphaFeatures, to ensure that alpha features cannot be enabled by default. Update the verifyOrUpdateFeatureList function to call this new verification. Add corresponding unit tests to validate the behavior of alpha feature handling.
1 parent 473ec01 commit 5d0caaa

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

test/compatibility_lifecycle/cmd/feature_gates.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ func verifyOrUpdateFeatureList(rootPath, featureListFile string, currentVersion
134134
}
135135
featureList = append(featureList, features...)
136136

137+
if err := verifyAlphaFeatures(featureList); err != nil {
138+
return err
139+
}
140+
137141
sort.Slice(featureList, func(i, j int) bool {
138142
return strings.ToLower(featureList[i].Name) < strings.ToLower(featureList[j].Name)
139143
})
@@ -175,7 +179,7 @@ func verifyOrUpdateFeatureList(rootPath, featureListFile string, currentVersion
175179
}
176180

177181
func dedupeFeatureList(featureList []featureInfo) ([]featureInfo, error) {
178-
if featureList == nil || len(featureList) < 1 {
182+
if len(featureList) < 1 {
179183
return featureList, nil
180184
}
181185
last := featureList[0]
@@ -262,6 +266,17 @@ func verifyFeatureRemoval(featureList []featureInfo, baseFeatureList []featureIn
262266
return nil
263267
}
264268

269+
func verifyAlphaFeatures(featureList []featureInfo) error {
270+
for _, f := range featureList {
271+
for _, spec := range f.VersionedSpecs {
272+
if spec.PreRelease == "Alpha" && spec.Default {
273+
return fmt.Errorf("alpha feature %s cannot be enabled by default", f.Name)
274+
}
275+
}
276+
}
277+
return nil
278+
}
279+
265280
func searchPathForFeatures(path string) ([]featureInfo, error) {
266281
allFeatures := []featureInfo{}
267282
// Create a FileSet to work with

test/compatibility_lifecycle/cmd/feature_gates_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,3 +894,53 @@ func TestVerifyFeatureRemoval(t *testing.T) {
894894
})
895895
}
896896
}
897+
898+
func TestVerifyAlphaFeatures(t *testing.T) {
899+
tests := []struct {
900+
name string
901+
featureList []featureInfo
902+
expectErr bool
903+
expectedErrMsg string
904+
}{
905+
{
906+
name: "no alpha features",
907+
featureList: []featureInfo{
908+
{Name: "FeatureB", VersionedSpecs: []featureSpec{{Version: "1.0", PreRelease: "Beta"}}},
909+
{Name: "FeatureC", VersionedSpecs: []featureSpec{{Version: "1.0", PreRelease: "GA", LockToDefault: true}}},
910+
},
911+
},
912+
{
913+
name: "alpha feature disabled",
914+
featureList: []featureInfo{
915+
{Name: "FeatureA", VersionedSpecs: []featureSpec{{Version: "1.0", PreRelease: "Alpha", Default: false}}},
916+
{Name: "FeatureB", VersionedSpecs: []featureSpec{{Version: "1.0", PreRelease: "Beta"}}},
917+
},
918+
},
919+
{
920+
name: "alpha feature enabled",
921+
featureList: []featureInfo{
922+
{Name: "FeatureA", VersionedSpecs: []featureSpec{{Version: "1.0", PreRelease: "Alpha", Default: true}}},
923+
{Name: "FeatureB", VersionedSpecs: []featureSpec{{Version: "1.0", PreRelease: "Beta"}}},
924+
},
925+
expectErr: true,
926+
expectedErrMsg: "alpha feature FeatureA cannot be enabled by default",
927+
},
928+
}
929+
for _, tc := range tests {
930+
t.Run(tc.name, func(t *testing.T) {
931+
err := verifyAlphaFeatures(tc.featureList)
932+
if tc.expectErr {
933+
if err == nil {
934+
t.Fatalf("expected error, got nil")
935+
}
936+
if !strings.Contains(err.Error(), tc.expectedErrMsg) {
937+
t.Fatalf("expected error message to contain %q, got %q", tc.expectedErrMsg, err.Error())
938+
}
939+
return
940+
}
941+
if err != nil {
942+
t.Fatalf("unexpected error: %v", err)
943+
}
944+
})
945+
}
946+
}

0 commit comments

Comments
 (0)