ctl_v3_make_mirror_test.go 3.3 KB

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