Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

etcdctl: fix move-leader for multiple endpoints #14445

Merged
merged 1 commit into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was spouting a warning, so I thought I'd clean this up. Let me know if I should leave it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ETCDCTL_API is 3 by default, why the case need this? It should be legacy "issue". So it's Ok to remove it.

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