ctl_v3_make_mirror_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2016 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 e2e
  15. import (
  16. "fmt"
  17. "testing"
  18. "time"
  19. )
  20. func TestCtlV3MakeMirror(t *testing.T) { testCtl(t, makeMirrorTest) }
  21. func TestCtlV3MakeMirrorModifyDestPrefix(t *testing.T) { testCtl(t, makeMirrorModifyDestPrefixTest) }
  22. func TestCtlV3MakeMirrorNoDestPrefix(t *testing.T) { testCtl(t, makeMirrorNoDestPrefixTest) }
  23. func makeMirrorTest(cx ctlCtx) {
  24. var (
  25. flags = []string{}
  26. kvs = []kv{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}}
  27. prefix = "key"
  28. )
  29. testMirrorCommand(cx, flags, kvs, kvs, prefix, prefix)
  30. }
  31. func makeMirrorModifyDestPrefixTest(cx ctlCtx) {
  32. var (
  33. flags = []string{"--prefix", "o_", "--dest-prefix", "d_"}
  34. kvs = []kv{{"o_key1", "val1"}, {"o_key2", "val2"}, {"o_key3", "val3"}}
  35. kvs2 = []kv{{"d_key1", "val1"}, {"d_key2", "val2"}, {"d_key3", "val3"}}
  36. srcprefix = "o_"
  37. destprefix = "d_"
  38. )
  39. testMirrorCommand(cx, flags, kvs, kvs2, srcprefix, destprefix)
  40. }
  41. func makeMirrorNoDestPrefixTest(cx ctlCtx) {
  42. var (
  43. flags = []string{"--prefix", "o_", "--no-dest-prefix"}
  44. kvs = []kv{{"o_key1", "val1"}, {"o_key2", "val2"}, {"o_key3", "val3"}}
  45. kvs2 = []kv{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}}
  46. srcprefix = "o_"
  47. destprefix = "key"
  48. )
  49. testMirrorCommand(cx, flags, kvs, kvs2, srcprefix, destprefix)
  50. }
  51. func testMirrorCommand(cx ctlCtx, flags []string, sourcekvs, destkvs []kv, srcprefix, destprefix string) {
  52. // set up another cluster to mirror with
  53. mirrorcfg := configAutoTLS
  54. mirrorcfg.clusterSize = 1
  55. mirrorcfg.basePort = 10000
  56. mirrorctx := ctlCtx{
  57. t: cx.t,
  58. cfg: mirrorcfg,
  59. dialTimeout: 7 * time.Second,
  60. }
  61. mirrorepc, err := newEtcdProcessCluster(&mirrorctx.cfg)
  62. if err != nil {
  63. cx.t.Fatalf("could not start etcd process cluster (%v)", err)
  64. }
  65. mirrorctx.epc = mirrorepc
  66. defer func() {
  67. if err = mirrorctx.epc.Close(); err != nil {
  68. cx.t.Fatalf("error closing etcd processes (%v)", err)
  69. }
  70. }()
  71. cmdArgs := append(cx.PrefixArgs(), "make-mirror")
  72. cmdArgs = append(cmdArgs, flags...)
  73. cmdArgs = append(cmdArgs, fmt.Sprintf("localhost:%d", mirrorcfg.basePort))
  74. proc, err := spawnCmd(cmdArgs)
  75. if err != nil {
  76. cx.t.Fatal(err)
  77. }
  78. defer func() {
  79. err = proc.Stop()
  80. if err != nil {
  81. cx.t.Fatal(err)
  82. }
  83. }()
  84. for i := range sourcekvs {
  85. if err = ctlV3Put(cx, sourcekvs[i].key, sourcekvs[i].val, ""); err != nil {
  86. cx.t.Fatal(err)
  87. }
  88. }
  89. if err = ctlV3Get(cx, []string{srcprefix, "--prefix"}, sourcekvs...); err != nil {
  90. cx.t.Fatal(err)
  91. }
  92. if err = ctlV3Watch(mirrorctx, []string{destprefix, "--rev", "1", "--prefix"}, destkvs...); err != nil {
  93. cx.t.Fatal(err)
  94. }
  95. }