ctl_v3_member_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. "github.com/coreos/etcd/etcdserver/etcdserverpb"
  22. )
  23. func TestCtlV3MemberList(t *testing.T) { testCtl(t, memberListTest) }
  24. func TestCtlV3MemberRemove(t *testing.T) { testCtl(t, memberRemoveTest, withQuorum()) }
  25. func TestCtlV3MemberAdd(t *testing.T) { testCtl(t, memberAddTest) }
  26. func TestCtlV3MemberUpdate(t *testing.T) { testCtl(t, memberUpdateTest) }
  27. func memberListTest(cx ctlCtx) {
  28. if err := ctlV3MemberList(cx); err != nil {
  29. cx.t.Fatalf("memberListTest ctlV3MemberList error (%v)", err)
  30. }
  31. }
  32. func ctlV3MemberList(cx ctlCtx) error {
  33. cmdArgs := append(cx.PrefixArgs(), "member", "list")
  34. lines := make([]string, cx.cfg.clusterSize)
  35. for i := range lines {
  36. lines[i] = "started"
  37. }
  38. return spawnWithExpects(cmdArgs, lines...)
  39. }
  40. func getMemberList(cx ctlCtx) (etcdserverpb.MemberListResponse, error) {
  41. cmdArgs := append(cx.PrefixArgs(), "--write-out", "json", "member", "list")
  42. proc, err := spawnCmd(cmdArgs)
  43. if err != nil {
  44. return etcdserverpb.MemberListResponse{}, err
  45. }
  46. var txt string
  47. txt, err = proc.Expect("members")
  48. if err != nil {
  49. return etcdserverpb.MemberListResponse{}, err
  50. }
  51. if err = proc.Close(); err != nil {
  52. return etcdserverpb.MemberListResponse{}, err
  53. }
  54. resp := etcdserverpb.MemberListResponse{}
  55. dec := json.NewDecoder(strings.NewReader(txt))
  56. if err := dec.Decode(&resp); err == io.EOF {
  57. return etcdserverpb.MemberListResponse{}, err
  58. }
  59. return resp, nil
  60. }
  61. func memberRemoveTest(cx ctlCtx) {
  62. n1 := cx.cfg.clusterSize
  63. if n1 < 2 {
  64. cx.t.Fatalf("%d-node is too small to test 'member remove'", n1)
  65. }
  66. resp, err := getMemberList(cx)
  67. if err != nil {
  68. cx.t.Fatal(err)
  69. }
  70. if n1 != len(resp.Members) {
  71. cx.t.Fatalf("expected %d, got %d", n1, len(resp.Members))
  72. }
  73. var (
  74. n2 = n1 - 1
  75. memIDToRemove = fmt.Sprintf("%x", resp.Header.MemberId)
  76. cluserID = fmt.Sprintf("%x", resp.Header.ClusterId)
  77. )
  78. if err = ctlV3MemberRemove(cx, memIDToRemove, cluserID); err != nil {
  79. cx.t.Fatal(err)
  80. }
  81. resp, err = getMemberList(cx)
  82. if err != nil {
  83. cx.t.Fatal(err)
  84. }
  85. if n2 != len(resp.Members) {
  86. cx.t.Fatalf("expected %d, got %d", n2, len(resp.Members))
  87. }
  88. }
  89. func ctlV3MemberRemove(cx ctlCtx, memberID, clusterID string) error {
  90. cmdArgs := append(cx.PrefixArgs(), "member", "remove", memberID)
  91. return spawnWithExpect(cmdArgs, fmt.Sprintf("%s removed from cluster %s", memberID, clusterID))
  92. }
  93. func memberAddTest(cx ctlCtx) {
  94. peerURL := fmt.Sprintf("http://localhost:%d", etcdProcessBasePort+11)
  95. cmdArgs := append(cx.PrefixArgs(), "member", "add", "newmember", fmt.Sprintf("--peer-urls=%s", peerURL))
  96. if err := spawnWithExpect(cmdArgs, " added to cluster "); err != nil {
  97. cx.t.Fatal(err)
  98. }
  99. mresp, err := getMemberList(cx)
  100. if err != nil {
  101. cx.t.Fatal(err)
  102. }
  103. if len(mresp.Members) != 2 {
  104. cx.t.Fatalf("expected 2, got %d", len(mresp.Members))
  105. }
  106. found := false
  107. for _, mem := range mresp.Members {
  108. for _, v := range mem.PeerURLs {
  109. if v == peerURL {
  110. found = true
  111. break
  112. }
  113. }
  114. }
  115. if !found {
  116. cx.t.Fatalf("expected %s in PeerURLs, got %+v", peerURL, mresp.Members)
  117. }
  118. }
  119. func memberUpdateTest(cx ctlCtx) {
  120. mr, err := getMemberList(cx)
  121. if err != nil {
  122. cx.t.Fatal(err)
  123. }
  124. peerURL := fmt.Sprintf("http://localhost:%d", etcdProcessBasePort+11)
  125. cmdArgs := append(cx.PrefixArgs(), "member", "update", fmt.Sprintf("%x", mr.Members[0].ID), fmt.Sprintf("--peer-urls=%s", peerURL))
  126. if err = spawnWithExpect(cmdArgs, " updated in cluster "); err != nil {
  127. cx.t.Fatal(err)
  128. }
  129. mresp, err := getMemberList(cx)
  130. if err != nil {
  131. cx.t.Fatal(err)
  132. }
  133. if len(mresp.Members) != 1 {
  134. cx.t.Fatalf("expected 1, got %d", len(mresp.Members))
  135. }
  136. if mresp.Members[0].PeerURLs[0] != peerURL {
  137. cx.t.Fatalf("expected %s in PeerURLs, got %+v", peerURL, mresp.Members)
  138. }
  139. }