member_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2015 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. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "reflect"
  20. "testing"
  21. "github.com/coreos/etcd/client"
  22. "github.com/coreos/etcd/pkg/testutil"
  23. "golang.org/x/net/context"
  24. )
  25. func TestPauseMember(t *testing.T) {
  26. defer testutil.AfterTest(t)
  27. c := NewCluster(t, 5)
  28. c.Launch(t)
  29. defer c.Terminate(t)
  30. for i := 0; i < 5; i++ {
  31. c.Members[i].Pause()
  32. membs := append([]*member{}, c.Members[:i]...)
  33. membs = append(membs, c.Members[i+1:]...)
  34. c.waitLeader(t, membs)
  35. clusterMustProgress(t, membs)
  36. c.Members[i].Resume()
  37. }
  38. c.waitLeader(t, c.Members)
  39. clusterMustProgress(t, c.Members)
  40. }
  41. func TestRestartMember(t *testing.T) {
  42. defer testutil.AfterTest(t)
  43. c := NewCluster(t, 3)
  44. c.Launch(t)
  45. defer c.Terminate(t)
  46. for i := 0; i < 3; i++ {
  47. c.Members[i].Stop(t)
  48. membs := append([]*member{}, c.Members[:i]...)
  49. membs = append(membs, c.Members[i+1:]...)
  50. c.waitLeader(t, membs)
  51. clusterMustProgress(t, membs)
  52. err := c.Members[i].Restart(t)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. }
  57. clusterMustProgress(t, c.Members)
  58. }
  59. func TestLaunchDuplicateMemberShouldFail(t *testing.T) {
  60. size := 3
  61. c := NewCluster(t, size)
  62. m := c.Members[0].Clone(t)
  63. var err error
  64. m.DataDir, err = ioutil.TempDir(os.TempDir(), "etcd")
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. c.Launch(t)
  69. defer c.Terminate(t)
  70. if err := m.Launch(); err == nil {
  71. t.Errorf("unexpect successful launch")
  72. }
  73. }
  74. func TestSnapshotAndRestartMember(t *testing.T) {
  75. defer testutil.AfterTest(t)
  76. m := mustNewMember(t, memberConfig{name: "snapAndRestartTest"})
  77. m.SnapCount = 100
  78. m.Launch()
  79. defer m.Terminate(t)
  80. m.WaitOK(t)
  81. resps := make([]*client.Response, 120)
  82. var err error
  83. for i := 0; i < 120; i++ {
  84. cc := MustNewHTTPClient(t, []string{m.URL()}, nil)
  85. kapi := client.NewKeysAPI(cc)
  86. ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
  87. key := fmt.Sprintf("foo%d", i)
  88. resps[i], err = kapi.Create(ctx, "/"+key, "bar")
  89. if err != nil {
  90. t.Fatalf("#%d: create on %s error: %v", i, m.URL(), err)
  91. }
  92. cancel()
  93. }
  94. m.Stop(t)
  95. m.Restart(t)
  96. m.WaitOK(t)
  97. for i := 0; i < 120; i++ {
  98. cc := MustNewHTTPClient(t, []string{m.URL()}, nil)
  99. kapi := client.NewKeysAPI(cc)
  100. ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
  101. key := fmt.Sprintf("foo%d", i)
  102. resp, err := kapi.Get(ctx, "/"+key, nil)
  103. if err != nil {
  104. t.Fatalf("#%d: get on %s error: %v", i, m.URL(), err)
  105. }
  106. cancel()
  107. if !reflect.DeepEqual(resp.Node, resps[i].Node) {
  108. t.Errorf("#%d: node = %v, want %v", i, resp.Node, resps[i].Node)
  109. }
  110. }
  111. }