simple_snapshot_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // +build ignore
  2. package test
  3. import (
  4. "io/ioutil"
  5. "os"
  6. "strconv"
  7. "testing"
  8. "time"
  9. "github.com/coreos/etcd/third_party/github.com/coreos/go-etcd/etcd"
  10. )
  11. // This test creates a single node and then set a value to it to trigger snapshot
  12. func TestSnapshot(t *testing.T) {
  13. procAttr := new(os.ProcAttr)
  14. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  15. args := []string{"etcd", "-name=node1", "-data-dir=/tmp/node1", "-snapshot=true", "-snapshot-count=500"}
  16. process, err := os.StartProcess(EtcdBinPath, append(args, "-f"), procAttr)
  17. if err != nil {
  18. t.Fatal("start process failed:" + err.Error())
  19. }
  20. defer process.Kill()
  21. time.Sleep(time.Second)
  22. c := etcd.NewClient(nil)
  23. c.SyncCluster()
  24. // issue first 501 commands
  25. for i := 0; i < 501; i++ {
  26. result, err := c.Set("foo", "bar", 100)
  27. node := result.Node
  28. if err != nil || node.Key != "/foo" || node.Value != "bar" || node.TTL < 95 {
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. t.Fatalf("Set failed with %s %s %v", node.Key, node.Value, node.TTL)
  33. }
  34. }
  35. // wait for a snapshot interval
  36. time.Sleep(3 * time.Second)
  37. snapshots, err := ioutil.ReadDir("/tmp/node1/snapshot")
  38. if err != nil {
  39. t.Fatal("list snapshot failed:" + err.Error())
  40. }
  41. if len(snapshots) != 1 {
  42. t.Fatal("wrong number of snapshot :[1/", len(snapshots), "]")
  43. }
  44. index, _ := strconv.Atoi(snapshots[0].Name()[2:5])
  45. if index < 503 || index > 516 {
  46. t.Fatal("wrong name of snapshot :", snapshots[0].Name())
  47. }
  48. // issue second 501 commands
  49. for i := 0; i < 501; i++ {
  50. result, err := c.Set("foo", "bar", 100)
  51. node := result.Node
  52. if err != nil || node.Key != "/foo" || node.Value != "bar" || node.TTL < 95 {
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. t.Fatalf("Set failed with %s %s %v", node.Key, node.Value, node.TTL)
  57. }
  58. }
  59. // wait for a snapshot interval
  60. time.Sleep(3 * time.Second)
  61. snapshots, err = ioutil.ReadDir("/tmp/node1/snapshot")
  62. if err != nil {
  63. t.Fatal("list snapshot failed:" + err.Error())
  64. }
  65. if len(snapshots) != 1 {
  66. t.Fatal("wrong number of snapshot :[1/", len(snapshots), "]")
  67. }
  68. index, _ = strconv.Atoi(snapshots[0].Name()[2:6])
  69. if index < 1010 || index > 1029 {
  70. t.Fatal("wrong name of snapshot :", snapshots[0].Name())
  71. }
  72. }
  73. // TestSnapshotRestart tests etcd restarts with snapshot file
  74. func TestSnapshotRestart(t *testing.T) {
  75. procAttr := new(os.ProcAttr)
  76. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  77. args := []string{"etcd", "-name=node1", "-data-dir=/tmp/node1", "-snapshot=true", "-snapshot-count=500"}
  78. process, err := os.StartProcess(EtcdBinPath, append(args, "-f"), procAttr)
  79. if err != nil {
  80. t.Fatal("start process failed:" + err.Error())
  81. }
  82. time.Sleep(time.Second)
  83. c := etcd.NewClient(nil)
  84. c.SyncCluster()
  85. // issue first 501 commands
  86. for i := 0; i < 501; i++ {
  87. result, err := c.Set("foo", "bar", 100)
  88. node := result.Node
  89. if err != nil || node.Key != "/foo" || node.Value != "bar" || node.TTL < 95 {
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. t.Fatalf("Set failed with %s %s %v", node.Key, node.Value, node.TTL)
  94. }
  95. }
  96. // wait for a snapshot interval
  97. time.Sleep(3 * time.Second)
  98. _, err = ioutil.ReadDir("/tmp/node1/snapshot")
  99. if err != nil {
  100. t.Fatal("list snapshot failed:" + err.Error())
  101. }
  102. process.Kill()
  103. process, err = os.StartProcess(EtcdBinPath, args, procAttr)
  104. if err != nil {
  105. t.Fatal("start process failed:" + err.Error())
  106. }
  107. defer process.Kill()
  108. time.Sleep(1 * time.Second)
  109. _, err = c.Set("foo", "bar", 100)
  110. if err != nil {
  111. t.Fatal(err)
  112. }
  113. }