ctl_v3_member_test.go 3.7 KB

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