member_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. "context"
  17. "fmt"
  18. "io/ioutil"
  19. "os"
  20. "reflect"
  21. "testing"
  22. "go.etcd.io/etcd/client"
  23. "go.etcd.io/etcd/pkg/testutil"
  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. c.waitLeader(t, c.Members)
  58. clusterMustProgress(t, c.Members)
  59. }
  60. func TestLaunchDuplicateMemberShouldFail(t *testing.T) {
  61. size := 3
  62. c := NewCluster(t, size)
  63. m := c.Members[0].Clone(t)
  64. var err error
  65. m.DataDir, err = ioutil.TempDir(os.TempDir(), "etcd")
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. c.Launch(t)
  70. defer c.Terminate(t)
  71. if err := m.Launch(); err == nil {
  72. t.Errorf("unexpect successful launch")
  73. }
  74. }
  75. func TestSnapshotAndRestartMember(t *testing.T) {
  76. defer testutil.AfterTest(t)
  77. m := mustNewMember(t, memberConfig{name: "snapAndRestartTest"})
  78. m.SnapshotCount = 100
  79. m.Launch()
  80. defer m.Terminate(t)
  81. m.WaitOK(t)
  82. resps := make([]*client.Response, 120)
  83. var err error
  84. for i := 0; i < 120; i++ {
  85. cc := MustNewHTTPClient(t, []string{m.URL()}, nil)
  86. kapi := client.NewKeysAPI(cc)
  87. ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
  88. key := fmt.Sprintf("foo%d", i)
  89. resps[i], err = kapi.Create(ctx, "/"+key, "bar")
  90. if err != nil {
  91. t.Fatalf("#%d: create on %s error: %v", i, m.URL(), err)
  92. }
  93. cancel()
  94. }
  95. m.Stop(t)
  96. m.Restart(t)
  97. m.WaitOK(t)
  98. for i := 0; i < 120; i++ {
  99. cc := MustNewHTTPClient(t, []string{m.URL()}, nil)
  100. kapi := client.NewKeysAPI(cc)
  101. ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
  102. key := fmt.Sprintf("foo%d", i)
  103. resp, err := kapi.Get(ctx, "/"+key, nil)
  104. if err != nil {
  105. t.Fatalf("#%d: get on %s error: %v", i, m.URL(), err)
  106. }
  107. cancel()
  108. if !reflect.DeepEqual(resp.Node, resps[i].Node) {
  109. t.Errorf("#%d: node = %v, want %v", i, resp.Node, resps[i].Node)
  110. }
  111. }
  112. }