Skip to content

Commit

Permalink
fix for etcd-io#19066 Print warnings when deprecated options are conf…
Browse files Browse the repository at this point in the history
…igured in config file

Signed-off-by: mansoora <[email protected]>
  • Loading branch information
mansoor17syed committed Jan 10, 2025
1 parent 59dc520 commit 126c30a
Showing 1 changed file with 35 additions and 28 deletions.
63 changes: 35 additions & 28 deletions server/etcdmain/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net/url"
"os"
"reflect"
"sort"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -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",
Expand All @@ -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": {},
},
},
{
Expand All @@ -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)
Expand All @@ -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
}

0 comments on commit 126c30a

Please sign in to comment.