diff --git a/etcdctl/ctlv3/command/move_leader_command.go b/etcdctl/ctlv3/command/move_leader_command.go index 76b1736d541..7c69ffc6a43 100644 --- a/etcdctl/ctlv3/command/move_leader_command.go +++ b/etcdctl/ctlv3/command/move_leader_command.go @@ -43,9 +43,10 @@ func transferLeadershipCommandFunc(cmd *cobra.Command, args []string) { cobrautl.ExitWithError(cobrautl.ExitBadArgs, err) } - c := mustClientFromCmd(cmd) - eps := c.Endpoints() - c.Close() + cfg := clientConfigFromCmd(cmd) + cli := mustClient(cfg) + eps := cli.Endpoints() + cli.Close() ctx, cancel := commandCtx(cmd) @@ -53,7 +54,6 @@ func transferLeadershipCommandFunc(cmd *cobra.Command, args []string) { var leaderCli *clientv3.Client var leaderID uint64 for _, ep := range eps { - cfg := clientConfigFromCmd(cmd) cfg.Endpoints = []string{ep} cli := mustClient(cfg) resp, serr := cli.Status(ctx, ep) diff --git a/tests/e2e/ctl_v3_move_leader_test.go b/tests/e2e/ctl_v3_move_leader_test.go index d81cf8630c1..d9fa6f4f2f1 100644 --- a/tests/e2e/ctl_v3_move_leader_test.go +++ b/tests/e2e/ctl_v3_move_leader_test.go @@ -18,7 +18,6 @@ import ( "context" "crypto/tls" "fmt" - "os" "testing" "time" @@ -28,17 +27,31 @@ import ( "go.etcd.io/etcd/tests/v3/framework/e2e" ) -func TestCtlV3MoveLeaderSecure(t *testing.T) { - testCtlV3MoveLeader(t, *e2e.NewConfigTLS()) -} +func TestCtlV3MoveLeaderScenarios(t *testing.T) { + securityParent := map[string]struct { + cfg e2e.EtcdProcessClusterConfig + }{ + "Secure": {cfg: *e2e.NewConfigTLS()}, + "Insecure": {cfg: *e2e.NewConfigNoTLS()}, + } -func TestCtlV3MoveLeaderInsecure(t *testing.T) { - testCtlV3MoveLeader(t, *e2e.NewConfigNoTLS()) -} + tests := map[string]struct { + env map[string]string + }{ + "happy path": {env: map[string]string{}}, + "with env": {env: map[string]string{"ETCDCTL_ENDPOINTS": "something-else-is-set"}}, + } -func testCtlV3MoveLeader(t *testing.T, cfg e2e.EtcdProcessClusterConfig) { - e2e.BeforeTest(t) + for testName, tc := range securityParent { + for subTestName, tx := range tests { + t.Run(testName+" "+subTestName, func(t *testing.T) { + testCtlV3MoveLeader(t, tc.cfg, tx.env) + }) + } + } +} +func testCtlV3MoveLeader(t *testing.T, cfg e2e.EtcdProcessClusterConfig, envVars map[string]string) { epc := setupEtcdctlTest(t, &cfg, true) defer func() { if errC := epc.Close(); errC != nil { @@ -88,13 +101,12 @@ func testCtlV3MoveLeader(t *testing.T, cfg e2e.EtcdProcessClusterConfig) { } } - os.Setenv("ETCDCTL_API", "3") - defer os.Unsetenv("ETCDCTL_API") cx := ctlCtx{ t: t, cfg: *e2e.NewConfigNoTLS(), dialTimeout: 7 * time.Second, epc: epc, + envMap: envVars, } tests := []struct { @@ -109,6 +121,10 @@ func testCtlV3MoveLeader(t *testing.T, cfg e2e.EtcdProcessClusterConfig) { []string{cx.epc.EndpointsV3()[leadIdx]}, fmt.Sprintf("Leadership transferred from %s to %s", types.ID(leaderID), types.ID(transferee)), }, + { // request to all endpoints + cx.epc.EndpointsV3(), + fmt.Sprintf("Leadership transferred"), + }, } for i, tc := range tests { prefix := cx.prefixArgs(tc.eps)