etcdctl: fix move-leader for multiple endpoints

Due to a duplicate call of clientConfigFromCmd, the move-leader command
would fail with "conflicting environment variable is shadowed by corresponding command-line flag".
Also in scenarios where no command-line flag was supplied.

Signed-off-by: Thomas Jungblut <tjungblu@redhat.com>
dependabot/go_modules/go.uber.org/atomic-1.10.0
Thomas Jungblut 2022-09-08 11:20:15 +02:00
parent 0e1c895108
commit 3fc16608f7
2 changed files with 32 additions and 16 deletions

View File

@ -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)

View File

@ -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()},
}
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"}},
}
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 TestCtlV3MoveLeaderInsecure(t *testing.T) {
testCtlV3MoveLeader(t, *e2e.NewConfigNoTLS())
}
func testCtlV3MoveLeader(t *testing.T, cfg e2e.EtcdProcessClusterConfig) {
e2e.BeforeTest(t)
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)