Use single string to define a flag allowing for easier testing

Signed-off-by: Marek Siarkowicz <siarkowicz@google.com>
This commit is contained in:
Marek Siarkowicz 2023-10-12 11:37:36 +02:00
parent 6d9a0a40b0
commit 6f63f4b676
2 changed files with 92 additions and 159 deletions

View File

@ -451,8 +451,8 @@ func (cfg *EtcdProcessClusterConfig) EtcdAllServerProcessConfigs(tb testing.TB)
func (cfg *EtcdProcessClusterConfig) SetInitialOrDiscovery(serverCfg *EtcdServerProcessConfig, initialCluster []string, initialClusterState string) { func (cfg *EtcdProcessClusterConfig) SetInitialOrDiscovery(serverCfg *EtcdServerProcessConfig, initialCluster []string, initialClusterState string) {
if cfg.Discovery == "" && len(cfg.ServerConfig.DiscoveryCfg.Endpoints) == 0 { if cfg.Discovery == "" && len(cfg.ServerConfig.DiscoveryCfg.Endpoints) == 0 {
serverCfg.InitialCluster = strings.Join(initialCluster, ",") serverCfg.InitialCluster = strings.Join(initialCluster, ",")
serverCfg.Args = append(serverCfg.Args, "--initial-cluster", serverCfg.InitialCluster) serverCfg.Args = append(serverCfg.Args, "--initial-cluster="+serverCfg.InitialCluster)
serverCfg.Args = append(serverCfg.Args, "--initial-cluster-state", initialClusterState) serverCfg.Args = append(serverCfg.Args, "--initial-cluster-state="+initialClusterState)
} }
if len(cfg.ServerConfig.DiscoveryCfg.Endpoints) > 0 { if len(cfg.ServerConfig.DiscoveryCfg.Endpoints) > 0 {
@ -509,19 +509,19 @@ func (cfg *EtcdProcessClusterConfig) EtcdServerProcessConfig(tb testing.TB, i in
} }
args := []string{ args := []string{
"--name", name, "--name=" + name,
"--listen-client-urls", strings.Join(curls, ","), "--listen-client-urls=" + strings.Join(curls, ","),
"--advertise-client-urls", strings.Join(curls, ","), "--advertise-client-urls=" + strings.Join(curls, ","),
"--listen-peer-urls", peerListenUrl.String(), "--listen-peer-urls=" + peerListenUrl.String(),
"--initial-advertise-peer-urls", peerAdvertiseUrl.String(), "--initial-advertise-peer-urls=" + peerAdvertiseUrl.String(),
"--initial-cluster-token", cfg.ServerConfig.InitialClusterToken, "--initial-cluster-token=" + cfg.ServerConfig.InitialClusterToken,
"--data-dir", dataDirPath, "--data-dir", dataDirPath,
"--snapshot-count", fmt.Sprintf("%d", cfg.ServerConfig.SnapshotCount), "--snapshot-count=" + fmt.Sprintf("%d", cfg.ServerConfig.SnapshotCount),
} }
var clientHttpUrl string var clientHttpUrl string
if cfg.ClientHttpSeparate { if cfg.ClientHttpSeparate {
clientHttpUrl = clientURL(cfg.ClientScheme(), clientHttpPort, cfg.Client.ConnectionType) clientHttpUrl = clientURL(cfg.ClientScheme(), clientHttpPort, cfg.Client.ConnectionType)
args = append(args, "--listen-client-http-urls", clientHttpUrl) args = append(args, "--listen-client-http-urls="+clientHttpUrl)
} }
if cfg.ServerConfig.ForceNewCluster { if cfg.ServerConfig.ForceNewCluster {
@ -529,7 +529,7 @@ func (cfg *EtcdProcessClusterConfig) EtcdServerProcessConfig(tb testing.TB, i in
} }
if cfg.ServerConfig.QuotaBackendBytes > 0 { if cfg.ServerConfig.QuotaBackendBytes > 0 {
args = append(args, args = append(args,
"--quota-backend-bytes", fmt.Sprintf("%d", cfg.ServerConfig.QuotaBackendBytes), "--quota-backend-bytes="+fmt.Sprintf("%d", cfg.ServerConfig.QuotaBackendBytes),
) )
} }
if !cfg.ServerConfig.StrictReconfigCheck { if !cfg.ServerConfig.StrictReconfigCheck {
@ -547,58 +547,58 @@ func (cfg *EtcdProcessClusterConfig) EtcdServerProcessConfig(tb testing.TB, i in
Scheme: cfg.MetricsURLScheme, Scheme: cfg.MetricsURLScheme,
Host: fmt.Sprintf("localhost:%d", metricsPort), Host: fmt.Sprintf("localhost:%d", metricsPort),
}).String() }).String()
args = append(args, "--listen-metrics-urls", murl) args = append(args, "--listen-metrics-urls="+murl)
} }
args = append(args, cfg.TlsArgs()...) args = append(args, cfg.TlsArgs()...)
if cfg.ServerConfig.AuthToken != "" && cfg.ServerConfig.AuthToken != embed.DefaultAuthToken { if cfg.ServerConfig.AuthToken != "" && cfg.ServerConfig.AuthToken != embed.DefaultAuthToken {
args = append(args, "--auth-token", cfg.ServerConfig.AuthToken) args = append(args, "--auth-token="+cfg.ServerConfig.AuthToken)
} }
if cfg.ServerConfig.V2Deprecation != "" && cfg.ServerConfig.V2Deprecation != config2.V2_DEPR_DEFAULT { if cfg.ServerConfig.V2Deprecation != "" && cfg.ServerConfig.V2Deprecation != config2.V2_DEPR_DEFAULT {
args = append(args, "--v2-deprecation", string(cfg.ServerConfig.V2Deprecation)) args = append(args, "--v2-deprecation="+string(cfg.ServerConfig.V2Deprecation))
} }
if cfg.Discovery != "" { if cfg.Discovery != "" {
args = append(args, "--discovery", cfg.Discovery) args = append(args, "--discovery="+cfg.Discovery)
} }
if cfg.ServerConfig.LogLevel != "" && cfg.ServerConfig.LogLevel != logutil.DefaultLogLevel { if cfg.ServerConfig.LogLevel != "" && cfg.ServerConfig.LogLevel != logutil.DefaultLogLevel {
args = append(args, "--log-level", cfg.ServerConfig.LogLevel) args = append(args, "--log-level="+cfg.ServerConfig.LogLevel)
} }
if cfg.ServerConfig.MaxConcurrentStreams != 0 && cfg.ServerConfig.MaxConcurrentStreams != embed.DefaultMaxConcurrentStreams { if cfg.ServerConfig.MaxConcurrentStreams != 0 && cfg.ServerConfig.MaxConcurrentStreams != embed.DefaultMaxConcurrentStreams {
args = append(args, "--max-concurrent-streams", fmt.Sprintf("%d", cfg.ServerConfig.MaxConcurrentStreams)) args = append(args, "--max-concurrent-streams="+fmt.Sprintf("%d", cfg.ServerConfig.MaxConcurrentStreams))
} }
if cfg.ServerConfig.ExperimentalCorruptCheckTime != 0 { if cfg.ServerConfig.ExperimentalCorruptCheckTime != 0 {
args = append(args, "--experimental-corrupt-check-time", fmt.Sprintf("%s", cfg.ServerConfig.ExperimentalCorruptCheckTime)) args = append(args, "--experimental-corrupt-check-time="+fmt.Sprintf("%s", cfg.ServerConfig.ExperimentalCorruptCheckTime))
} }
if cfg.ServerConfig.ExperimentalCompactHashCheckEnabled { if cfg.ServerConfig.ExperimentalCompactHashCheckEnabled {
args = append(args, "--experimental-compact-hash-check-enabled") args = append(args, "--experimental-compact-hash-check-enabled")
} }
if cfg.ServerConfig.ExperimentalCompactHashCheckTime != 0 && cfg.ServerConfig.ExperimentalCompactHashCheckTime != embed.DefaultExperimentalCompactHashCheckTime { if cfg.ServerConfig.ExperimentalCompactHashCheckTime != 0 && cfg.ServerConfig.ExperimentalCompactHashCheckTime != embed.DefaultExperimentalCompactHashCheckTime {
args = append(args, "--experimental-compact-hash-check-time", cfg.ServerConfig.ExperimentalCompactHashCheckTime.String()) args = append(args, "--experimental-compact-hash-check-time="+cfg.ServerConfig.ExperimentalCompactHashCheckTime.String())
} }
if cfg.ServerConfig.ExperimentalCompactionBatchLimit != 0 { if cfg.ServerConfig.ExperimentalCompactionBatchLimit != 0 {
args = append(args, "--experimental-compaction-batch-limit", fmt.Sprintf("%d", cfg.ServerConfig.ExperimentalCompactionBatchLimit)) args = append(args, "--experimental-compaction-batch-limit="+fmt.Sprintf("%d", cfg.ServerConfig.ExperimentalCompactionBatchLimit))
} }
if cfg.ServerConfig.ExperimentalCompactionSleepInterval != 0 { if cfg.ServerConfig.ExperimentalCompactionSleepInterval != 0 {
args = append(args, "--experimental-compaction-sleep-interval", cfg.ServerConfig.ExperimentalCompactionSleepInterval.String()) args = append(args, "--experimental-compaction-sleep-interval="+cfg.ServerConfig.ExperimentalCompactionSleepInterval.String())
} }
if cfg.ServerConfig.WarningUnaryRequestDuration != 0 { if cfg.ServerConfig.WarningUnaryRequestDuration != 0 {
args = append(args, "--warning-unary-request-duration", cfg.ServerConfig.WarningUnaryRequestDuration.String()) args = append(args, "--warning-unary-request-duration="+cfg.ServerConfig.WarningUnaryRequestDuration.String())
} }
if cfg.ServerConfig.ExperimentalWarningUnaryRequestDuration != 0 { if cfg.ServerConfig.ExperimentalWarningUnaryRequestDuration != 0 {
args = append(args, "--experimental-warning-unary-request-duration", cfg.ServerConfig.ExperimentalWarningUnaryRequestDuration.String()) args = append(args, "--experimental-warning-unary-request-duration="+cfg.ServerConfig.ExperimentalWarningUnaryRequestDuration.String())
} }
if cfg.ServerConfig.ExperimentalWatchProgressNotifyInterval != 0 { if cfg.ServerConfig.ExperimentalWatchProgressNotifyInterval != 0 {
args = append(args, "--experimental-watch-progress-notify-interval", cfg.ServerConfig.ExperimentalWatchProgressNotifyInterval.String()) args = append(args, "--experimental-watch-progress-notify-interval="+cfg.ServerConfig.ExperimentalWatchProgressNotifyInterval.String())
} }
if cfg.ServerConfig.SnapshotCatchUpEntries != 0 && cfg.ServerConfig.SnapshotCatchUpEntries != etcdserver.DefaultSnapshotCatchUpEntries { if cfg.ServerConfig.SnapshotCatchUpEntries != 0 && cfg.ServerConfig.SnapshotCatchUpEntries != etcdserver.DefaultSnapshotCatchUpEntries {
if cfg.Version == CurrentVersion || (cfg.Version == MinorityLastVersion && i <= cfg.ClusterSize/2) || (cfg.Version == QuorumLastVersion && i > cfg.ClusterSize/2) { if cfg.Version == CurrentVersion || (cfg.Version == MinorityLastVersion && i <= cfg.ClusterSize/2) || (cfg.Version == QuorumLastVersion && i > cfg.ClusterSize/2) {
args = append(args, "--experimental-snapshot-catchup-entries", fmt.Sprintf("%d", cfg.ServerConfig.SnapshotCatchUpEntries)) args = append(args, "--experimental-snapshot-catchup-entries="+fmt.Sprintf("%d", cfg.ServerConfig.SnapshotCatchUpEntries))
} }
} }
envVars := map[string]string{} envVars := map[string]string{}

View File

@ -30,114 +30,72 @@ func TestEtcdServerProcessConfig(t *testing.T) {
name: "Default", name: "Default",
config: NewConfig(), config: NewConfig(),
expectArgs: []string{ expectArgs: []string{
"--name", "--name=TestEtcdServerProcessConfigDefault-test-0",
"TestEtcdServerProcessConfigDefault-test-0", "--listen-client-urls=http://localhost:0",
"--listen-client-urls", "--advertise-client-urls=http://localhost:0",
"http://localhost:0", "--listen-peer-urls=http://localhost:1",
"--advertise-client-urls", "--initial-advertise-peer-urls=http://localhost:1",
"http://localhost:0", "--initial-cluster-token=new",
"--listen-peer-urls", "--data-dir=/tmp/fake/member-0",
"http://localhost:1", "--snapshot-count=10000",
"--initial-advertise-peer-urls",
"http://localhost:1",
"--initial-cluster-token",
"new",
"--data-dir",
"/tmp/fake/member-0",
"--snapshot-count",
"10000",
}, },
}, },
{ {
name: "SnapshotCount", name: "SnapshotCount",
config: NewConfig(WithSnapshotCount(42)), config: NewConfig(WithSnapshotCount(42)),
expectArgs: []string{ expectArgs: []string{
"--name", "--name=TestEtcdServerProcessConfigSnapshotCount-test-0",
"TestEtcdServerProcessConfigSnapshotCount-test-0", "--listen-client-urls=http://localhost:0",
"--listen-client-urls", "--advertise-client-urls=http://localhost:0",
"http://localhost:0", "--listen-peer-urls=http://localhost:1",
"--advertise-client-urls", "--initial-advertise-peer-urls=http://localhost:1",
"http://localhost:0", "--initial-cluster-token=new",
"--listen-peer-urls", "--data-dir=/tmp/fake/member-0",
"http://localhost:1", "--snapshot-count=42",
"--initial-advertise-peer-urls",
"http://localhost:1",
"--initial-cluster-token",
"new",
"--data-dir",
"/tmp/fake/member-0",
"--snapshot-count",
"42",
}, },
}, },
{ {
name: "SnapshotCatchUpEntries", name: "SnapshotCatchUpEntries",
config: NewConfig(WithSnapshotCatchUpEntries(12)), config: NewConfig(WithSnapshotCatchUpEntries(12)),
expectArgs: []string{ expectArgs: []string{
"--name", "--name=TestEtcdServerProcessConfigSnapshotCatchUpEntries-test-0",
"TestEtcdServerProcessConfigSnapshotCatchUpEntries-test-0", "--listen-client-urls=http://localhost:0",
"--listen-client-urls", "--advertise-client-urls=http://localhost:0",
"http://localhost:0", "--listen-peer-urls=http://localhost:1",
"--advertise-client-urls", "--initial-advertise-peer-urls=http://localhost:1",
"http://localhost:0", "--initial-cluster-token=new",
"--listen-peer-urls", "--data-dir=/tmp/fake/member-0",
"http://localhost:1", "--snapshot-count=10000",
"--initial-advertise-peer-urls", "--experimental-snapshot-catchup-entries=12",
"http://localhost:1",
"--initial-cluster-token",
"new",
"--data-dir",
"/tmp/fake/member-0",
"--snapshot-count",
"10000",
"--experimental-snapshot-catchup-entries",
"12",
}, },
}, },
{ {
name: "QuotaBackendBytes", name: "QuotaBackendBytes",
config: NewConfig(WithQuotaBackendBytes(123)), config: NewConfig(WithQuotaBackendBytes(123)),
expectArgs: []string{ expectArgs: []string{
"--name", "--name=TestEtcdServerProcessConfigQuotaBackendBytes-test-0",
"TestEtcdServerProcessConfigQuotaBackendBytes-test-0", "--listen-client-urls=http://localhost:0",
"--listen-client-urls", "--advertise-client-urls=http://localhost:0",
"http://localhost:0", "--listen-peer-urls=http://localhost:1",
"--advertise-client-urls", "--initial-advertise-peer-urls=http://localhost:1",
"http://localhost:0", "--initial-cluster-token=new",
"--listen-peer-urls", "--data-dir=/tmp/fake/member-0",
"http://localhost:1", "--snapshot-count=10000",
"--initial-advertise-peer-urls", "--quota-backend-bytes=123",
"http://localhost:1",
"--initial-cluster-token",
"new",
"--data-dir",
"/tmp/fake/member-0",
"--snapshot-count",
"10000",
"--quota-backend-bytes",
"123",
}, },
}, },
{ {
name: "CorruptCheck", name: "CorruptCheck",
config: NewConfig(WithInitialCorruptCheck(true)), config: NewConfig(WithInitialCorruptCheck(true)),
expectArgs: []string{ expectArgs: []string{
"--name", "--name=TestEtcdServerProcessConfigCorruptCheck-test-0",
"TestEtcdServerProcessConfigCorruptCheck-test-0", "--listen-client-urls=http://localhost:0",
"--listen-client-urls", "--advertise-client-urls=http://localhost:0",
"http://localhost:0", "--listen-peer-urls=http://localhost:1",
"--advertise-client-urls", "--initial-advertise-peer-urls=http://localhost:1",
"http://localhost:0", "--initial-cluster-token=new",
"--listen-peer-urls", "--data-dir=/tmp/fake/member-0",
"http://localhost:1", "--snapshot-count=10000",
"--initial-advertise-peer-urls",
"http://localhost:1",
"--initial-cluster-token",
"new",
"--data-dir",
"/tmp/fake/member-0",
"--snapshot-count",
"10000",
"--experimental-initial-corrupt-check", "--experimental-initial-corrupt-check",
}, },
}, },
@ -145,22 +103,14 @@ func TestEtcdServerProcessConfig(t *testing.T) {
name: "StrictReconfigCheck", name: "StrictReconfigCheck",
config: NewConfig(WithStrictReconfigCheck(false)), config: NewConfig(WithStrictReconfigCheck(false)),
expectArgs: []string{ expectArgs: []string{
"--name", "--name=TestEtcdServerProcessConfigStrictReconfigCheck-test-0",
"TestEtcdServerProcessConfigStrictReconfigCheck-test-0", "--listen-client-urls=http://localhost:0",
"--listen-client-urls", "--advertise-client-urls=http://localhost:0",
"http://localhost:0", "--listen-peer-urls=http://localhost:1",
"--advertise-client-urls", "--initial-advertise-peer-urls=http://localhost:1",
"http://localhost:0", "--initial-cluster-token=new",
"--listen-peer-urls", "--data-dir=/tmp/fake/member-0",
"http://localhost:1", "--snapshot-count=10000",
"--initial-advertise-peer-urls",
"http://localhost:1",
"--initial-cluster-token",
"new",
"--data-dir",
"/tmp/fake/member-0",
"--snapshot-count",
"10000",
"--strict-reconfig-check=false", "--strict-reconfig-check=false",
}, },
}, },
@ -168,46 +118,29 @@ func TestEtcdServerProcessConfig(t *testing.T) {
name: "CatchUpEntries", name: "CatchUpEntries",
config: NewConfig(WithSnapshotCatchUpEntries(100)), config: NewConfig(WithSnapshotCatchUpEntries(100)),
expectArgs: []string{ expectArgs: []string{
"--name", "--name=TestEtcdServerProcessConfigCatchUpEntries-test-0",
"TestEtcdServerProcessConfigCatchUpEntries-test-0", "--listen-client-urls=http://localhost:0",
"--listen-client-urls", "--advertise-client-urls=http://localhost:0",
"http://localhost:0", "--listen-peer-urls=http://localhost:1",
"--advertise-client-urls", "--initial-advertise-peer-urls=http://localhost:1",
"http://localhost:0", "--initial-cluster-token=new",
"--listen-peer-urls", "--data-dir=/tmp/fake/member-0",
"http://localhost:1", "--snapshot-count=10000",
"--initial-advertise-peer-urls", "--experimental-snapshot-catchup-entries=100",
"http://localhost:1",
"--initial-cluster-token",
"new",
"--data-dir",
"/tmp/fake/member-0",
"--snapshot-count",
"10000",
"--experimental-snapshot-catchup-entries",
"100",
}, },
}, },
{ {
name: "CatchUpEntriesLastVersion", name: "CatchUpEntriesLastVersion",
config: NewConfig(WithSnapshotCatchUpEntries(100), WithVersion(LastVersion)), config: NewConfig(WithSnapshotCatchUpEntries(100), WithVersion(LastVersion)),
expectArgs: []string{ expectArgs: []string{
"--name", "--name=TestEtcdServerProcessConfigCatchUpEntriesLastVersion-test-0",
"TestEtcdServerProcessConfigCatchUpEntriesLastVersion-test-0", "--listen-client-urls=http://localhost:0",
"--listen-client-urls", "--advertise-client-urls=http://localhost:0",
"http://localhost:0", "--listen-peer-urls=http://localhost:1",
"--advertise-client-urls", "--initial-advertise-peer-urls=http://localhost:1",
"http://localhost:0", "--initial-cluster-token=new",
"--listen-peer-urls", "--data-dir=/tmp/fake/member-0",
"http://localhost:1", "--snapshot-count=10000",
"--initial-advertise-peer-urls",
"http://localhost:1",
"--initial-cluster-token",
"new",
"--data-dir",
"/tmp/fake/member-0",
"--snapshot-count",
"10000",
}, },
}, },
} }