ctl_v3_move_leader_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2017 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package e2e
  15. import (
  16. "context"
  17. "crypto/tls"
  18. "fmt"
  19. "os"
  20. "testing"
  21. "time"
  22. "go.etcd.io/etcd/clientv3"
  23. "go.etcd.io/etcd/pkg/testutil"
  24. "go.etcd.io/etcd/pkg/transport"
  25. "go.etcd.io/etcd/pkg/types"
  26. )
  27. func TestCtlV3MoveLeaderSecure(t *testing.T) {
  28. testCtlV3MoveLeader(t, configTLS)
  29. }
  30. func TestCtlV3MoveLeaderInsecure(t *testing.T) {
  31. testCtlV3MoveLeader(t, configNoTLS)
  32. }
  33. func testCtlV3MoveLeader(t *testing.T, cfg etcdProcessClusterConfig) {
  34. defer testutil.AfterTest(t)
  35. epc := setupEtcdctlTest(t, &cfg, true)
  36. defer func() {
  37. if errC := epc.Close(); errC != nil {
  38. t.Fatalf("error closing etcd processes (%v)", errC)
  39. }
  40. }()
  41. var tcfg *tls.Config
  42. if cfg.clientTLS == clientTLS {
  43. tinfo := transport.TLSInfo{
  44. CertFile: certPath,
  45. KeyFile: privateKeyPath,
  46. TrustedCAFile: caPath,
  47. }
  48. var err error
  49. tcfg, err = tinfo.ClientConfig()
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. }
  54. var leadIdx int
  55. var leaderID uint64
  56. var transferee uint64
  57. for i, ep := range epc.EndpointsV3() {
  58. cli, err := clientv3.New(clientv3.Config{
  59. Endpoints: []string{ep},
  60. DialTimeout: 3 * time.Second,
  61. TLS: tcfg,
  62. })
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  67. resp, err := cli.Status(ctx, ep)
  68. if err != nil {
  69. t.Fatalf("failed to get status from endpoint %s: %v", ep, err)
  70. }
  71. cancel()
  72. cli.Close()
  73. if resp.Header.GetMemberId() == resp.Leader {
  74. leadIdx = i
  75. leaderID = resp.Leader
  76. } else {
  77. transferee = resp.Header.GetMemberId()
  78. }
  79. }
  80. os.Setenv("ETCDCTL_API", "3")
  81. defer os.Unsetenv("ETCDCTL_API")
  82. cx := ctlCtx{
  83. t: t,
  84. cfg: configNoTLS,
  85. dialTimeout: 7 * time.Second,
  86. epc: epc,
  87. }
  88. tests := []struct {
  89. prefixes []string
  90. expect string
  91. }{
  92. { // request to non-leader
  93. cx.prefixArgs([]string{cx.epc.EndpointsV3()[(leadIdx+1)%3]}),
  94. "no leader endpoint given at ",
  95. },
  96. { // request to leader
  97. cx.prefixArgs([]string{cx.epc.EndpointsV3()[leadIdx]}),
  98. fmt.Sprintf("Leadership transferred from %s to %s", types.ID(leaderID), types.ID(transferee)),
  99. },
  100. }
  101. for i, tc := range tests {
  102. cmdArgs := append(tc.prefixes, "move-leader", types.ID(transferee).String())
  103. if err := spawnWithExpect(cmdArgs, tc.expect); err != nil {
  104. t.Fatalf("#%d: %v", i, err)
  105. }
  106. }
  107. }