member_test.go 3.0 KB

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