ctl_v3_migrate_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. "os"
  18. "testing"
  19. "time"
  20. "golang.org/x/net/context"
  21. "github.com/coreos/etcd/clientv3"
  22. "github.com/coreos/etcd/pkg/testutil"
  23. )
  24. func TestCtlV3Migrate(t *testing.T) {
  25. defer testutil.AfterTest(t)
  26. epc := setupEtcdctlTest(t, &configNoTLS, false)
  27. defer func() {
  28. if errC := epc.Close(); errC != nil {
  29. t.Fatalf("error closing etcd processes (%v)", errC)
  30. }
  31. }()
  32. keys := make([]string, 3)
  33. vals := make([]string, 3)
  34. for i := range keys {
  35. keys[i] = fmt.Sprintf("foo_%d", i)
  36. vals[i] = fmt.Sprintf("bar_%d", i)
  37. }
  38. for i := range keys {
  39. if err := etcdctlSet(epc, keys[i], vals[i]); err != nil {
  40. t.Fatal(err)
  41. }
  42. }
  43. dataDir := epc.procs[0].cfg.dataDirPath
  44. if err := epc.StopAll(); err != nil {
  45. t.Fatalf("error closing etcd processes (%v)", err)
  46. }
  47. os.Setenv("ETCDCTL_API", "3")
  48. defer os.Unsetenv("ETCDCTL_API")
  49. cx := ctlCtx{
  50. t: t,
  51. cfg: configNoTLS,
  52. dialTimeout: 7 * time.Second,
  53. epc: epc,
  54. }
  55. if err := ctlV3Migrate(cx, dataDir, ""); err != nil {
  56. t.Fatal(err)
  57. }
  58. epc.procs[0].cfg.keepDataDir = true
  59. if err := epc.RestartAll(); err != nil {
  60. t.Fatal(err)
  61. }
  62. // to ensure revision increment is continuous from migrated v2 data
  63. if err := ctlV3Put(cx, "test", "value", ""); err != nil {
  64. t.Fatal(err)
  65. }
  66. cli, err := clientv3.New(clientv3.Config{
  67. Endpoints: epc.grpcEndpoints(),
  68. DialTimeout: 3 * time.Second,
  69. })
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. defer cli.Close()
  74. resp, err := cli.Get(context.TODO(), "test")
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. if len(resp.Kvs) != 1 {
  79. t.Fatalf("len(resp.Kvs) expected 1, got %+v", resp.Kvs)
  80. }
  81. if resp.Kvs[0].CreateRevision != 7 {
  82. t.Fatalf("resp.Kvs[0].CreateRevision expected 7, got %d", resp.Kvs[0].CreateRevision)
  83. }
  84. }
  85. func ctlV3Migrate(cx ctlCtx, dataDir, walDir string) error {
  86. cmdArgs := append(cx.PrefixArgs(), "migrate", "--data-dir", dataDir, "--wal-dir", walDir)
  87. return spawnWithExpects(cmdArgs, "finished transforming keys")
  88. }