v1_migration_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. args = append(args, "-name", "v1")
  34. process, err := os.StartProcess(EtcdBinPath, args, procAttr)
  35. if err != nil {
  36. t.Fatal("start process failed:" + err.Error())
  37. return
  38. }
  39. defer process.Kill()
  40. time.Sleep(time.Second)
  41. // Ensure deleted message is removed.
  42. resp, err := tests.Get("http://localhost:4001/v2/keys/message")
  43. tests.ReadBody(resp)
  44. assert.Nil(t, err, "")
  45. assert.Equal(t, resp.StatusCode, 200, "")
  46. }
  47. // Ensure that we can start a v2 cluster from the logs of a v1 cluster.
  48. func TestV1ClusterMigration(t *testing.T) {
  49. path, _ := ioutil.TempDir("", "etcd-")
  50. os.RemoveAll(path)
  51. defer os.RemoveAll(path)
  52. nodes := []string{"node0", "node2"}
  53. for i, node := range nodes {
  54. nodepath := filepath.Join(path, node)
  55. fixturepath, _ := filepath.Abs(filepath.Join("../fixtures/v1.cluster/", node))
  56. fmt.Println("FIXPATH =", fixturepath)
  57. fmt.Println("NODEPATH =", nodepath)
  58. os.MkdirAll(filepath.Dir(nodepath), 0777)
  59. // Copy over fixture files.
  60. c := exec.Command("cp", "-rf", fixturepath, nodepath)
  61. if out, err := c.CombinedOutput(); err != nil {
  62. fmt.Println(">>>>>>\n", string(out), "<<<<<<")
  63. panic("Fixture initialization error:" + err.Error())
  64. }
  65. procAttr := new(os.ProcAttr)
  66. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  67. args := []string{"etcd", fmt.Sprintf("-data-dir=%s", nodepath)}
  68. args = append(args, "-addr", fmt.Sprintf("127.0.0.1:%d", 4001+i))
  69. args = append(args, "-peer-addr", fmt.Sprintf("127.0.0.1:%d", 7001+i))
  70. args = append(args, "-name", node)
  71. process, err := os.StartProcess(EtcdBinPath, args, procAttr)
  72. if err != nil {
  73. t.Fatal("start process failed:" + err.Error())
  74. return
  75. }
  76. defer process.Kill()
  77. time.Sleep(time.Second)
  78. }
  79. // Ensure deleted message is removed.
  80. resp, err := tests.Get("http://localhost:4001/v2/keys/message")
  81. body := tests.ReadBody(resp)
  82. assert.Nil(t, err, "")
  83. assert.Equal(t, resp.StatusCode, http.StatusNotFound)
  84. assert.Equal(t, string(body), `{"errorCode":100,"message":"Key not found","cause":"/message","index":11}`+"\n")
  85. // Ensure TTL'd message is removed.
  86. resp, err = tests.Get("http://localhost:4001/v2/keys/foo")
  87. body = tests.ReadBody(resp)
  88. assert.Nil(t, err, "")
  89. assert.Equal(t, resp.StatusCode, 200, "")
  90. assert.Equal(t, string(body), `{"action":"get","node":{"key":"/foo","value":"one","modifiedIndex":9,"createdIndex":9}}`)
  91. }