Skip to content

Commit

Permalink
Merge pull request #14445 from tjungblu/movelead_main
Browse files Browse the repository at this point in the history
etcdctl: fix move-leader for multiple endpoints
  • Loading branch information
serathius authored Sep 9, 2022
2 parents 1ee1eff + 3fc1660 commit 92d8d7f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
8 changes: 4 additions & 4 deletions etcdctl/ctlv3/command/move_leader_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ 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)

// find current leader
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)
Expand Down
38 changes: 27 additions & 11 deletions tests/e2e/ctl_v3_move_leader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"crypto/tls"
"fmt"
"os"
"testing"
"time"

Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit 92d8d7f

Please sign in to comment.