Skip to content

Commit 23bdcbb

Browse files
committed
Verify that the discovery endpoint isn't empty
Signed-off-by: Benjamin Wang <[email protected]>
1 parent 856c3dd commit 23bdcbb

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

server/embed/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,12 @@ func (cfg *Config) Validate() error {
12571257
return errors.New("both --discovery-token and --discovery-endpoints must be set")
12581258
}
12591259

1260+
for _, ep := range cfg.DiscoveryCfg.Endpoints {
1261+
if strings.TrimSpace(ep) == "" {
1262+
return errors.New("--discovery-endpoints must not contain empty endpoints")
1263+
}
1264+
}
1265+
12601266
if cfg.TickMs == 0 {
12611267
return fmt.Errorf("--heartbeat-interval must be >0 (set to %dms)", cfg.TickMs)
12621268
}

server/embed/config_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ import (
3535
"go.etcd.io/etcd/client/pkg/v3/srv"
3636
"go.etcd.io/etcd/client/pkg/v3/transport"
3737
"go.etcd.io/etcd/client/pkg/v3/types"
38+
clientv3 "go.etcd.io/etcd/client/v3"
3839
"go.etcd.io/etcd/pkg/v3/featuregate"
40+
"go.etcd.io/etcd/server/v3/etcdserver/api/v3discovery"
3941
"go.etcd.io/etcd/server/v3/features"
4042
)
4143

@@ -1070,3 +1072,51 @@ func TestMatchNewConfigAddFlags(t *testing.T) {
10701072
t.Errorf("Diff: %s", diff)
10711073
}
10721074
}
1075+
1076+
func TestDiscoveryCfg(t *testing.T) {
1077+
testCases := []struct {
1078+
name string
1079+
discoveryCfg v3discovery.DiscoveryConfig
1080+
wantErr bool
1081+
}{
1082+
{
1083+
name: "Valid discovery config",
1084+
discoveryCfg: v3discovery.DiscoveryConfig{
1085+
ConfigSpec: clientv3.ConfigSpec{
1086+
Endpoints: []string{"http://10.0.0.100:2379", "http://10.0.0.101:2379"},
1087+
},
1088+
},
1089+
wantErr: false,
1090+
},
1091+
{
1092+
name: "Partial empty discovery endpoints",
1093+
discoveryCfg: v3discovery.DiscoveryConfig{
1094+
ConfigSpec: clientv3.ConfigSpec{
1095+
Endpoints: []string{"http://10.0.0.100:2379", ""},
1096+
},
1097+
},
1098+
wantErr: true,
1099+
},
1100+
{
1101+
name: "Empty discovery endpoint",
1102+
discoveryCfg: v3discovery.DiscoveryConfig{
1103+
ConfigSpec: clientv3.ConfigSpec{
1104+
Endpoints: []string{"", ""},
1105+
},
1106+
},
1107+
wantErr: true,
1108+
},
1109+
}
1110+
1111+
for _, tc := range testCases {
1112+
t.Run(tc.name, func(t *testing.T) {
1113+
cfg := NewConfig()
1114+
cfg.InitialCluster = ""
1115+
cfg.DiscoveryCfg = tc.discoveryCfg
1116+
cfg.DiscoveryCfg.Token = "foo"
1117+
err := cfg.Validate()
1118+
1119+
require.Equal(t, tc.wantErr, err != nil)
1120+
})
1121+
}
1122+
}

0 commit comments

Comments
 (0)