cluster_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 integration
  15. import (
  16. "reflect"
  17. "testing"
  18. "github.com/coreos/etcd/integration"
  19. "github.com/coreos/etcd/pkg/testutil"
  20. "github.com/coreos/etcd/pkg/types"
  21. "golang.org/x/net/context"
  22. )
  23. func TestMemberList(t *testing.T) {
  24. defer testutil.AfterTest(t)
  25. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  26. defer clus.Terminate(t)
  27. capi := clus.RandClient()
  28. resp, err := capi.MemberList(context.Background())
  29. if err != nil {
  30. t.Fatalf("failed to list member %v", err)
  31. }
  32. if len(resp.Members) != 3 {
  33. t.Errorf("number of members = %d, want %d", len(resp.Members), 3)
  34. }
  35. }
  36. func TestMemberAdd(t *testing.T) {
  37. defer testutil.AfterTest(t)
  38. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  39. defer clus.Terminate(t)
  40. capi := clus.RandClient()
  41. urls := []string{"http://127.0.0.1:1234"}
  42. resp, err := capi.MemberAdd(context.Background(), urls)
  43. if err != nil {
  44. t.Fatalf("failed to add member %v", err)
  45. }
  46. if !reflect.DeepEqual(resp.Member.PeerURLs, urls) {
  47. t.Errorf("urls = %v, want %v", urls, resp.Member.PeerURLs)
  48. }
  49. }
  50. func TestMemberRemove(t *testing.T) {
  51. defer testutil.AfterTest(t)
  52. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  53. defer clus.Terminate(t)
  54. capi := clus.Client(1)
  55. resp, err := capi.MemberList(context.Background())
  56. if err != nil {
  57. t.Fatalf("failed to list member %v", err)
  58. }
  59. rmvID := resp.Members[0].ID
  60. // indexes in capi member list don't necessarily match cluster member list;
  61. // find member that is not the client to remove
  62. for _, m := range resp.Members {
  63. mURLs, _ := types.NewURLs(m.PeerURLs)
  64. if !reflect.DeepEqual(mURLs, clus.Members[1].ServerConfig.PeerURLs) {
  65. rmvID = m.ID
  66. break
  67. }
  68. }
  69. _, err = capi.MemberRemove(context.Background(), rmvID)
  70. if err != nil {
  71. t.Fatalf("failed to remove member %v", err)
  72. }
  73. resp, err = capi.MemberList(context.Background())
  74. if err != nil {
  75. t.Fatalf("failed to list member %v", err)
  76. }
  77. if len(resp.Members) != 2 {
  78. t.Errorf("number of members = %d, want %d", len(resp.Members), 2)
  79. }
  80. }
  81. func TestMemberUpdate(t *testing.T) {
  82. defer testutil.AfterTest(t)
  83. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  84. defer clus.Terminate(t)
  85. capi := clus.RandClient()
  86. resp, err := capi.MemberList(context.Background())
  87. if err != nil {
  88. t.Fatalf("failed to list member %v", err)
  89. }
  90. urls := []string{"http://127.0.0.1:1234"}
  91. _, err = capi.MemberUpdate(context.Background(), resp.Members[0].ID, urls)
  92. if err != nil {
  93. t.Fatalf("failed to update member %v", err)
  94. }
  95. resp, err = capi.MemberList(context.Background())
  96. if err != nil {
  97. t.Fatalf("failed to list member %v", err)
  98. }
  99. if !reflect.DeepEqual(resp.Members[0].PeerURLs, urls) {
  100. t.Errorf("urls = %v, want %v", urls, resp.Members[0].PeerURLs)
  101. }
  102. }
  103. func TestMemberAddUpdateWrongURLs(t *testing.T) {
  104. defer testutil.AfterTest(t)
  105. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  106. defer clus.Terminate(t)
  107. capi := clus.RandClient()
  108. tt := [][]string{
  109. // missing protocol scheme
  110. {"://127.0.0.1:2379"},
  111. // unsupported scheme
  112. {"mailto://127.0.0.1:2379"},
  113. // not conform to host:port
  114. {"http://127.0.0.1"},
  115. // contain a path
  116. {"http://127.0.0.1:2379/path"},
  117. // first path segment in URL cannot contain colon
  118. {"127.0.0.1:1234"},
  119. // URL scheme must be http, https, unix, or unixs
  120. {"localhost:1234"},
  121. }
  122. for i := range tt {
  123. _, err := capi.MemberAdd(context.Background(), tt[i])
  124. if err == nil {
  125. t.Errorf("#%d: MemberAdd err = nil, but error", i)
  126. }
  127. _, err = capi.MemberUpdate(context.Background(), 0, tt[i])
  128. if err == nil {
  129. t.Errorf("#%d: MemberUpdate err = nil, but error", i)
  130. }
  131. }
  132. }