diff --git a/server/etcdmain/config_test.go b/server/etcdmain/config_test.go index b9058d33739..857053751bf 100644 --- a/server/etcdmain/config_test.go +++ b/server/etcdmain/config_test.go @@ -21,6 +21,7 @@ import ( "net/url" "os" "reflect" + "sort" "strings" "testing" "time" @@ -665,14 +666,14 @@ func TestConfigFileDeprecatedOptions(t *testing.T) { } testCases := []struct { - name string - configFileYAML configStruct - expectedWarnings map[string]string + name string + configFileYAML configStruct + expectedFlags map[string]struct{} }{ { - name: "no deprecated options", - configFileYAML: configStruct{}, - expectedWarnings: map[string]string{}, + name: "no deprecated options", + configFileYAML: configStruct{}, + expectedFlags: map[string]struct{}{}, }, { name: "deprecated experimental options", @@ -681,10 +682,10 @@ func TestConfigFileDeprecatedOptions(t *testing.T) { ExperimentalCompactHashCheckTime: 2 * time.Minute, ExperimentalWarningUnaryRequestDuration: time.Second, }, - expectedWarnings: map[string]string{ - "experimental-compact-hash-check-enabled": "--experimental-compact-hash-check-enabled is deprecated in 3.6 and will be decommissioned in 3.7. Use '--feature-gates=CompactHashCheck=true' instead.", - "experimental-compact-hash-check-time": "--experimental-compact-hash-check-time is deprecated in 3.6 and will be decommissioned in 3.7. Use '--compact-hash-check-time' instead.", - "experimental-warning-unary-request-duration": "--experimental-warning-unary-request-duration is deprecated, and will be decommissioned in v3.7. Use --warning-unary-request-duration instead.", + expectedFlags: map[string]struct{}{ + "experimental-compact-hash-check-enabled": {}, + "experimental-compact-hash-check-time": {}, + "experimental-warning-unary-request-duration": {}, }, }, { @@ -693,15 +694,16 @@ func TestConfigFileDeprecatedOptions(t *testing.T) { SnapshotCount: 10000, MaxSnapFiles: 5, }, - expectedWarnings: map[string]string{ - "snapshot-count": "--snapshot-count is deprecated in 3.6 and will be decommissioned in 3.7.", - "max-snapshots": "--max-snapshots is deprecated in 3.6 and will be decommissioned in 3.7.", + expectedFlags: map[string]struct{}{ + "snapshot-count": {}, + "max-snapshots": {}, }, }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { + // Create config file b, err := yaml.Marshal(&tc.configFileYAML) if err != nil { t.Fatal(err) @@ -710,32 +712,37 @@ func TestConfigFileDeprecatedOptions(t *testing.T) { tmpfile := mustCreateCfgFile(t, b) defer os.Remove(tmpfile.Name()) + // Parse config cfg := newConfig() err = cfg.parse([]string{fmt.Sprintf("--config-file=%s", tmpfile.Name())}) if err != nil { t.Fatal(err) } - // Check for unexpected warnings + // Check which flags were set and marked as deprecated + foundFlags := make(map[string]struct{}) for flagName := range cfg.ec.FlagsExplicitlySet { - if msg, ok := deprecatedFlags[flagName]; ok { - expectedMsg, shouldWarn := tc.expectedWarnings[flagName] - if !shouldWarn { - t.Errorf("unexpected deprecated flag warning for %q", flagName) - continue - } - if msg != expectedMsg { - t.Errorf("warning message mismatch for %q:\ngot: %q\nwant: %q", flagName, msg, expectedMsg) - } + if _, ok := deprecatedFlags[flagName]; ok { + foundFlags[flagName] = struct{}{} } } - // Check for missing warnings - for flagName, expectedMsg := range tc.expectedWarnings { - if msg, ok := deprecatedFlags[flagName]; !ok || msg != expectedMsg { - t.Errorf("missing or incorrect warning for %q:\ngot: %q\nwant: %q", flagName, msg, expectedMsg) - } + // Compare sets of flags + if !reflect.DeepEqual(foundFlags, tc.expectedFlags) { + t.Errorf("deprecated flags mismatch:\ngot: %v\nwant: %v", + mapToSortedSlice(foundFlags), + mapToSortedSlice(tc.expectedFlags)) } }) } } + +// Helper function to convert map keys to sorted slice for better error messages +func mapToSortedSlice(m map[string]struct{}) []string { + result := make([]string, 0, len(m)) + for k := range m { + result = append(result, k) + } + sort.Strings(result) + return result +}