simple_snapshot_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package test
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "strconv"
  6. "testing"
  7. "time"
  8. "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 TestSimpleSnapshot(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 <= 507 || index >= 510 {
  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 <= 1014 || index >= 1017 {
  69. t.Fatal("wrong name of snapshot :", snapshots[0].Name())
  70. }
  71. }