v1_migration_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package test
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net/http"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "testing"
  10. "time"
  11. "github.com/coreos/etcd/tests"
  12. "github.com/coreos/etcd/third_party/github.com/stretchr/testify/assert"
  13. )
  14. // Ensure that we can start a v2 node from the log of a v1 node.
  15. func TestV1SoloMigration(t *testing.T) {
  16. path, _ := ioutil.TempDir("", "etcd-")
  17. os.MkdirAll(path, 0777)
  18. defer os.RemoveAll(path)
  19. nodepath := filepath.Join(path, "node0")
  20. fixturepath, _ := filepath.Abs("../fixtures/v1.solo/node0")
  21. fmt.Println("DATA_DIR =", nodepath)
  22. // Copy over fixture files.
  23. c := exec.Command("cp", "-rf", fixturepath, nodepath)
  24. if out, err := c.CombinedOutput(); err != nil {
  25. fmt.Println(">>>>>>\n", string(out), "<<<<<<")
  26. panic("Fixture initialization error:" + err.Error())
  27. }
  28. procAttr := new(os.ProcAttr)
  29. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  30. args := []string{"etcd", fmt.Sprintf("-data-dir=%s", nodepath)}
  31. args = append(args, "-addr", "127.0.0.1:4001")
  32. args = append(args, "-peer-addr", "127.0.0.1:7001")
  33. process, err := os.StartProcess(EtcdBinPath, args, procAttr)
  34. if err != nil {
  35. t.Fatal("start process failed:" + err.Error())
  36. return
  37. }
  38. defer process.Kill()
  39. time.Sleep(time.Second)
  40. // Ensure deleted message is removed.
  41. resp, err := tests.Get("http://localhost:4001/v2/keys/message")
  42. tests.ReadBody(resp)
  43. assert.Nil(t, err, "")
  44. assert.Equal(t, resp.StatusCode, 200, "")
  45. }
  46. // Ensure that we can start a v2 cluster from the logs of a v1 cluster.
  47. func TestV1ClusterMigration(t *testing.T) {
  48. path, _ := ioutil.TempDir("", "etcd-")
  49. os.RemoveAll(path)
  50. defer os.RemoveAll(path)
  51. nodes := []string{"node0", "node2"}
  52. for i, node := range nodes {
  53. nodepath := filepath.Join(path, node)
  54. fixturepath, _ := filepath.Abs(filepath.Join("../fixtures/v1.cluster/", node))
  55. fmt.Println("FIXPATH =", fixturepath)
  56. fmt.Println("NODEPATH =", nodepath)
  57. os.MkdirAll(filepath.Dir(nodepath), 0777)
  58. // Copy over fixture files.
  59. c := exec.Command("cp", "-rf", fixturepath, nodepath)
  60. if out, err := c.CombinedOutput(); err != nil {
  61. fmt.Println(">>>>>>\n", string(out), "<<<<<<")
  62. panic("Fixture initialization error:" + err.Error())
  63. }
  64. procAttr := new(os.ProcAttr)
  65. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  66. args := []string{"etcd", fmt.Sprintf("-data-dir=%s", nodepath)}
  67. args = append(args, "-addr", fmt.Sprintf("127.0.0.1:%d", 4001+i))
  68. args = append(args, "-peer-addr", fmt.Sprintf("127.0.0.1:%d", 7001+i))
  69. process, err := os.StartProcess(EtcdBinPath, args, procAttr)
  70. if err != nil {
  71. t.Fatal("start process failed:" + err.Error())
  72. return
  73. }
  74. defer process.Kill()
  75. time.Sleep(time.Second)
  76. }
  77. // Ensure deleted message is removed.
  78. resp, err := tests.Get("http://localhost:4001/v2/keys/message")
  79. body := tests.ReadBody(resp)
  80. assert.Nil(t, err, "")
  81. assert.Equal(t, resp.StatusCode, http.StatusNotFound)
  82. assert.Equal(t, string(body), `{"errorCode":100,"message":"Key not found","cause":"/message","index":11}`+"\n")
  83. // Ensure TTL'd message is removed.
  84. resp, err = tests.Get("http://localhost:4001/v2/keys/foo")
  85. body = tests.ReadBody(resp)
  86. assert.Nil(t, err, "")
  87. assert.Equal(t, resp.StatusCode, 200, "")
  88. assert.Equal(t, string(body), `{"action":"get","node":{"key":"/foo","value":"one","modifiedIndex":9,"createdIndex":9}}`)
  89. }