simple_snapshot_test.go 3.4 KB

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