cluster_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2016 CoreOS, Inc.
  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/Godeps/_workspace/src/golang.org/x/net/context"
  19. "github.com/coreos/etcd/clientv3"
  20. "github.com/coreos/etcd/integration"
  21. "github.com/coreos/etcd/pkg/testutil"
  22. "github.com/coreos/etcd/pkg/types"
  23. )
  24. func TestMemberList(t *testing.T) {
  25. defer testutil.AfterTest(t)
  26. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  27. defer clus.Terminate(t)
  28. capi := clientv3.NewCluster(clus.RandClient())
  29. resp, err := capi.MemberList(context.Background())
  30. if err != nil {
  31. t.Fatalf("failed to list member %v", err)
  32. }
  33. if len(resp.Members) != 3 {
  34. t.Errorf("number of members = %d, want %d", len(resp.Members), 3)
  35. }
  36. }
  37. func TestMemberAdd(t *testing.T) {
  38. defer testutil.AfterTest(t)
  39. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  40. defer clus.Terminate(t)
  41. capi := clientv3.NewCluster(clus.RandClient())
  42. urls := []string{"http://127.0.0.1:1234"}
  43. resp, err := capi.MemberAdd(context.Background(), urls)
  44. if err != nil {
  45. t.Fatalf("failed to add member %v", err)
  46. }
  47. if !reflect.DeepEqual(resp.Member.PeerURLs, urls) {
  48. t.Errorf("urls = %v, want %v", urls, resp.Member.PeerURLs)
  49. }
  50. }
  51. func TestMemberRemove(t *testing.T) {
  52. defer testutil.AfterTest(t)
  53. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  54. defer clus.Terminate(t)
  55. capi := clientv3.NewCluster(clus.Client(1))
  56. resp, err := capi.MemberList(context.Background())
  57. if err != nil {
  58. t.Fatalf("failed to list member %v", err)
  59. }
  60. rmvID := resp.Members[0].ID
  61. // indexes in capi member list don't necessarily match cluster member list;
  62. // find member that is not the client to remove
  63. for _, m := range resp.Members {
  64. mURLs, _ := types.NewURLs(m.PeerURLs)
  65. if !reflect.DeepEqual(mURLs, clus.Members[1].ServerConfig.PeerURLs) {
  66. rmvID = m.ID
  67. break
  68. }
  69. }
  70. _, err = capi.MemberRemove(context.Background(), rmvID)
  71. if err != nil {
  72. t.Fatalf("failed to remove member %v", err)
  73. }
  74. resp, err = capi.MemberList(context.Background())
  75. if err != nil {
  76. t.Fatalf("failed to list member %v", err)
  77. }
  78. if len(resp.Members) != 2 {
  79. t.Errorf("number of members = %d, want %d", len(resp.Members), 2)
  80. }
  81. }
  82. func TestMemberUpdate(t *testing.T) {
  83. defer testutil.AfterTest(t)
  84. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  85. defer clus.Terminate(t)
  86. capi := clientv3.NewCluster(clus.RandClient())
  87. resp, err := capi.MemberList(context.Background())
  88. if err != nil {
  89. t.Fatalf("failed to list member %v", err)
  90. }
  91. urls := []string{"http://127.0.0.1:1234"}
  92. _, err = capi.MemberUpdate(context.Background(), resp.Members[0].ID, urls)
  93. if err != nil {
  94. t.Fatalf("failed to update member %v", err)
  95. }
  96. resp, err = capi.MemberList(context.Background())
  97. if err != nil {
  98. t.Fatalf("failed to list member %v", err)
  99. }
  100. if !reflect.DeepEqual(resp.Members[0].PeerURLs, urls) {
  101. t.Errorf("urls = %v, want %v", urls, resp.Members[0].PeerURLs)
  102. }
  103. }