ctl_v3_member_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2016 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. "encoding/json"
  17. "fmt"
  18. "io"
  19. "strings"
  20. "testing"
  21. "go.etcd.io/etcd/etcdserver/etcdserverpb"
  22. )
  23. func TestCtlV3MemberList(t *testing.T) { testCtl(t, memberListTest) }
  24. func TestCtlV3MemberListNoTLS(t *testing.T) { testCtl(t, memberListTest, withCfg(configNoTLS)) }
  25. func TestCtlV3MemberListClientTLS(t *testing.T) { testCtl(t, memberListTest, withCfg(configClientTLS)) }
  26. func TestCtlV3MemberListClientAutoTLS(t *testing.T) {
  27. testCtl(t, memberListTest, withCfg(configClientAutoTLS))
  28. }
  29. func TestCtlV3MemberListPeerTLS(t *testing.T) { testCtl(t, memberListTest, withCfg(configPeerTLS)) }
  30. func TestCtlV3MemberRemove(t *testing.T) {
  31. testCtl(t, memberRemoveTest, withQuorum(), withNoStrictReconfig())
  32. }
  33. func TestCtlV3MemberRemoveNoTLS(t *testing.T) {
  34. testCtl(t, memberRemoveTest, withQuorum(), withNoStrictReconfig(), withCfg(configNoTLS))
  35. }
  36. func TestCtlV3MemberRemoveClientTLS(t *testing.T) {
  37. testCtl(t, memberRemoveTest, withQuorum(), withNoStrictReconfig(), withCfg(configClientTLS))
  38. }
  39. func TestCtlV3MemberRemoveClientAutoTLS(t *testing.T) {
  40. testCtl(t, memberRemoveTest, withQuorum(), withNoStrictReconfig(), withCfg(
  41. // default clusterSize is 1
  42. etcdProcessClusterConfig{
  43. clusterSize: 3,
  44. isClientAutoTLS: true,
  45. clientTLS: clientTLS,
  46. initialToken: "new",
  47. }))
  48. }
  49. func TestCtlV3MemberRemovePeerTLS(t *testing.T) {
  50. testCtl(t, memberRemoveTest, withQuorum(), withNoStrictReconfig(), withCfg(configPeerTLS))
  51. }
  52. func TestCtlV3MemberAdd(t *testing.T) { testCtl(t, memberAddTest) }
  53. func TestCtlV3MemberAddNoTLS(t *testing.T) { testCtl(t, memberAddTest, withCfg(configNoTLS)) }
  54. func TestCtlV3MemberAddClientTLS(t *testing.T) { testCtl(t, memberAddTest, withCfg(configClientTLS)) }
  55. func TestCtlV3MemberAddClientAutoTLS(t *testing.T) {
  56. testCtl(t, memberAddTest, withCfg(configClientAutoTLS))
  57. }
  58. func TestCtlV3MemberAddPeerTLS(t *testing.T) { testCtl(t, memberAddTest, withCfg(configPeerTLS)) }
  59. func TestCtlV3MemberAddForLearner(t *testing.T) { testCtl(t, memberAddForLearnerTest) }
  60. func TestCtlV3MemberUpdate(t *testing.T) { testCtl(t, memberUpdateTest) }
  61. func TestCtlV3MemberUpdateNoTLS(t *testing.T) { testCtl(t, memberUpdateTest, withCfg(configNoTLS)) }
  62. func TestCtlV3MemberUpdateClientTLS(t *testing.T) {
  63. testCtl(t, memberUpdateTest, withCfg(configClientTLS))
  64. }
  65. func TestCtlV3MemberUpdateClientAutoTLS(t *testing.T) {
  66. testCtl(t, memberUpdateTest, withCfg(configClientAutoTLS))
  67. }
  68. func TestCtlV3MemberUpdatePeerTLS(t *testing.T) { testCtl(t, memberUpdateTest, withCfg(configPeerTLS)) }
  69. func memberListTest(cx ctlCtx) {
  70. if err := ctlV3MemberList(cx); err != nil {
  71. cx.t.Fatalf("memberListTest ctlV3MemberList error (%v)", err)
  72. }
  73. }
  74. func ctlV3MemberList(cx ctlCtx) error {
  75. cmdArgs := append(cx.PrefixArgs(), "member", "list")
  76. lines := make([]string, cx.cfg.clusterSize)
  77. for i := range lines {
  78. lines[i] = "started"
  79. }
  80. return spawnWithExpects(cmdArgs, lines...)
  81. }
  82. func getMemberList(cx ctlCtx) (etcdserverpb.MemberListResponse, error) {
  83. cmdArgs := append(cx.PrefixArgs(), "--write-out", "json", "member", "list")
  84. proc, err := spawnCmd(cmdArgs)
  85. if err != nil {
  86. return etcdserverpb.MemberListResponse{}, err
  87. }
  88. var txt string
  89. txt, err = proc.Expect("members")
  90. if err != nil {
  91. return etcdserverpb.MemberListResponse{}, err
  92. }
  93. if err = proc.Close(); err != nil {
  94. return etcdserverpb.MemberListResponse{}, err
  95. }
  96. resp := etcdserverpb.MemberListResponse{}
  97. dec := json.NewDecoder(strings.NewReader(txt))
  98. if err := dec.Decode(&resp); err == io.EOF {
  99. return etcdserverpb.MemberListResponse{}, err
  100. }
  101. return resp, nil
  102. }
  103. func memberRemoveTest(cx ctlCtx) {
  104. ep, memIDToRemove, clusterID := cx.memberToRemove()
  105. if err := ctlV3MemberRemove(cx, ep, memIDToRemove, clusterID); err != nil {
  106. cx.t.Fatal(err)
  107. }
  108. }
  109. func ctlV3MemberRemove(cx ctlCtx, ep, memberID, clusterID string) error {
  110. cmdArgs := append(cx.prefixArgs([]string{ep}), "member", "remove", memberID)
  111. return spawnWithExpect(cmdArgs, fmt.Sprintf("%s removed from cluster %s", memberID, clusterID))
  112. }
  113. func memberAddTest(cx ctlCtx) {
  114. if err := ctlV3MemberAdd(cx, fmt.Sprintf("http://localhost:%d", etcdProcessBasePort+11), false); err != nil {
  115. cx.t.Fatal(err)
  116. }
  117. }
  118. func memberAddForLearnerTest(cx ctlCtx) {
  119. if err := ctlV3MemberAdd(cx, fmt.Sprintf("http://localhost:%d", etcdProcessBasePort+11), true); err != nil {
  120. cx.t.Fatal(err)
  121. }
  122. }
  123. func ctlV3MemberAdd(cx ctlCtx, peerURL string, isLearner bool) error {
  124. cmdArgs := append(cx.PrefixArgs(), "member", "add", "newmember", fmt.Sprintf("--peer-urls=%s", peerURL))
  125. if isLearner {
  126. cmdArgs = append(cmdArgs, "--learner")
  127. }
  128. return spawnWithExpect(cmdArgs, " added to cluster ")
  129. }
  130. func memberUpdateTest(cx ctlCtx) {
  131. mr, err := getMemberList(cx)
  132. if err != nil {
  133. cx.t.Fatal(err)
  134. }
  135. peerURL := fmt.Sprintf("http://localhost:%d", etcdProcessBasePort+11)
  136. memberID := fmt.Sprintf("%x", mr.Members[0].ID)
  137. if err = ctlV3MemberUpdate(cx, memberID, peerURL); err != nil {
  138. cx.t.Fatal(err)
  139. }
  140. }
  141. func ctlV3MemberUpdate(cx ctlCtx, memberID, peerURL string) error {
  142. cmdArgs := append(cx.PrefixArgs(), "member", "update", memberID, fmt.Sprintf("--peer-urls=%s", peerURL))
  143. return spawnWithExpect(cmdArgs, " updated in cluster ")
  144. }