member_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2018 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 snapshot
  15. import (
  16. "context"
  17. "fmt"
  18. "os"
  19. "path/filepath"
  20. "testing"
  21. "time"
  22. "go.etcd.io/etcd/clientv3"
  23. "go.etcd.io/etcd/embed"
  24. "go.etcd.io/etcd/etcdserver"
  25. "go.etcd.io/etcd/pkg/testutil"
  26. )
  27. // TestSnapshotV3RestoreMultiMemberAdd ensures that multiple members
  28. // can boot into the same cluster after being restored from a same
  29. // snapshot file, and also be able to add another member to the cluster.
  30. func TestSnapshotV3RestoreMultiMemberAdd(t *testing.T) {
  31. kvs := []kv{{"foo1", "bar1"}, {"foo2", "bar2"}, {"foo3", "bar3"}}
  32. dbPath := createSnapshotFile(t, kvs)
  33. clusterN := 3
  34. cURLs, pURLs, srvs := restoreCluster(t, clusterN, dbPath)
  35. defer func() {
  36. for i := 0; i < clusterN; i++ {
  37. os.RemoveAll(srvs[i].Config().Dir)
  38. srvs[i].Close()
  39. }
  40. }()
  41. // wait for health interval + leader election
  42. time.Sleep(etcdserver.HealthInterval + 2*time.Second)
  43. cli, err := clientv3.New(clientv3.Config{Endpoints: []string{cURLs[0].String()}})
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. defer cli.Close()
  48. urls := newEmbedURLs(2)
  49. newCURLs, newPURLs := urls[:1], urls[1:]
  50. if _, err = cli.MemberAdd(context.Background(), []string{newPURLs[0].String()}); err != nil {
  51. t.Fatal(err)
  52. }
  53. // wait for membership reconfiguration apply
  54. time.Sleep(testutil.ApplyTimeout)
  55. cfg := embed.NewConfig()
  56. cfg.Logger = "zap"
  57. cfg.LogOutputs = []string{"/dev/null"}
  58. cfg.Debug = false
  59. cfg.Name = "3"
  60. cfg.InitialClusterToken = testClusterTkn
  61. cfg.ClusterState = "existing"
  62. cfg.LCUrls, cfg.ACUrls = newCURLs, newCURLs
  63. cfg.LPUrls, cfg.APUrls = newPURLs, newPURLs
  64. cfg.InitialCluster = ""
  65. for i := 0; i < clusterN; i++ {
  66. cfg.InitialCluster += fmt.Sprintf(",%d=%s", i, pURLs[i].String())
  67. }
  68. cfg.InitialCluster = cfg.InitialCluster[1:]
  69. cfg.InitialCluster += fmt.Sprintf(",%s=%s", cfg.Name, newPURLs[0].String())
  70. cfg.Dir = filepath.Join(os.TempDir(), fmt.Sprint(time.Now().Nanosecond()))
  71. srv, err := embed.StartEtcd(cfg)
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. defer func() {
  76. os.RemoveAll(cfg.Dir)
  77. srv.Close()
  78. }()
  79. select {
  80. case <-srv.Server.ReadyNotify():
  81. case <-time.After(10 * time.Second):
  82. t.Fatalf("failed to start the newly added etcd member")
  83. }
  84. cli2, err := clientv3.New(clientv3.Config{Endpoints: []string{newCURLs[0].String()}})
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. defer cli2.Close()
  89. ctx, cancel := context.WithTimeout(context.Background(), testutil.RequestTimeout)
  90. mresp, err := cli2.MemberList(ctx)
  91. cancel()
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. if len(mresp.Members) != 4 {
  96. t.Fatalf("expected 4 members, got %+v", mresp)
  97. }
  98. // make sure restored cluster has kept all data on recovery
  99. var gresp *clientv3.GetResponse
  100. ctx, cancel = context.WithTimeout(context.Background(), testutil.RequestTimeout)
  101. gresp, err = cli2.Get(ctx, "foo", clientv3.WithPrefix())
  102. cancel()
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. for i := range gresp.Kvs {
  107. if string(gresp.Kvs[i].Key) != kvs[i].k {
  108. t.Fatalf("#%d: key expected %s, got %s", i, kvs[i].k, string(gresp.Kvs[i].Key))
  109. }
  110. if string(gresp.Kvs[i].Value) != kvs[i].v {
  111. t.Fatalf("#%d: value expected %s, got %s", i, kvs[i].v, string(gresp.Kvs[i].Value))
  112. }
  113. }
  114. }