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.
This commit is contained in:
yongruilin 2025-03-18 00:04:16 +00:00
parent 473ec01548
commit 5d0caaa1a6
2 changed files with 66 additions and 1 deletions

View File

@ -134,6 +134,10 @@ func verifyOrUpdateFeatureList(rootPath, featureListFile string, currentVersion
}
featureList = append(featureList, features...)
if err := verifyAlphaFeatures(featureList); err != nil {
return err
}
sort.Slice(featureList, func(i, j int) bool {
return strings.ToLower(featureList[i].Name) < strings.ToLower(featureList[j].Name)
})
@ -175,7 +179,7 @@ func verifyOrUpdateFeatureList(rootPath, featureListFile string, currentVersion
}
func dedupeFeatureList(featureList []featureInfo) ([]featureInfo, error) {
if featureList == nil || len(featureList) < 1 {
if len(featureList) < 1 {
return featureList, nil
}
last := featureList[0]
@ -262,6 +266,17 @@ func verifyFeatureRemoval(featureList []featureInfo, baseFeatureList []featureIn
return nil
}
func verifyAlphaFeatures(featureList []featureInfo) error {
for _, f := range featureList {
for _, spec := range f.VersionedSpecs {
if spec.PreRelease == "Alpha" && spec.Default {
return fmt.Errorf("alpha feature %s cannot be enabled by default", f.Name)
}
}
}
return nil
}
func searchPathForFeatures(path string) ([]featureInfo, error) {
allFeatures := []featureInfo{}
// Create a FileSet to work with

View File

@ -894,3 +894,53 @@ func TestVerifyFeatureRemoval(t *testing.T) {
})
}
}
func TestVerifyAlphaFeatures(t *testing.T) {
tests := []struct {
name string
featureList []featureInfo
expectErr bool
expectedErrMsg string
}{
{
name: "no alpha features",
featureList: []featureInfo{
{Name: "FeatureB", VersionedSpecs: []featureSpec{{Version: "1.0", PreRelease: "Beta"}}},
{Name: "FeatureC", VersionedSpecs: []featureSpec{{Version: "1.0", PreRelease: "GA", LockToDefault: true}}},
},
},
{
name: "alpha feature disabled",
featureList: []featureInfo{
{Name: "FeatureA", VersionedSpecs: []featureSpec{{Version: "1.0", PreRelease: "Alpha", Default: false}}},
{Name: "FeatureB", VersionedSpecs: []featureSpec{{Version: "1.0", PreRelease: "Beta"}}},
},
},
{
name: "alpha feature enabled",
featureList: []featureInfo{
{Name: "FeatureA", VersionedSpecs: []featureSpec{{Version: "1.0", PreRelease: "Alpha", Default: true}}},
{Name: "FeatureB", VersionedSpecs: []featureSpec{{Version: "1.0", PreRelease: "Beta"}}},
},
expectErr: true,
expectedErrMsg: "alpha feature FeatureA cannot be enabled by default",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
err := verifyAlphaFeatures(tc.featureList)
if tc.expectErr {
if err == nil {
t.Fatalf("expected error, got nil")
}
if !strings.Contains(err.Error(), tc.expectedErrMsg) {
t.Fatalf("expected error message to contain %q, got %q", tc.expectedErrMsg, err.Error())
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
})
}
}