simple_snapshot_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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, "-force-config"), 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. if err != nil || result.Key != "/foo" || result.Value != "bar" || result.TTL < 95 {
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. t.Fatalf("Set failed with %s %s %v", result.Key, result.Value, result.TTL)
  31. }
  32. }
  33. // wait for a snapshot interval
  34. time.Sleep(3 * time.Second)
  35. snapshots, err := ioutil.ReadDir("/tmp/node1/snapshot")
  36. if err != nil {
  37. t.Fatal("list snapshot failed:" + err.Error())
  38. }
  39. if len(snapshots) != 1 {
  40. t.Fatal("wrong number of snapshot :[1/", len(snapshots), "]")
  41. }
  42. index, _ := strconv.Atoi(snapshots[0].Name()[2:5])
  43. if index < 507 || index > 510 {
  44. t.Fatal("wrong name of snapshot :", snapshots[0].Name())
  45. }
  46. // issue second 501 commands
  47. for i := 0; i < 501; i++ {
  48. result, err := c.Set("foo", "bar", 100)
  49. if err != nil || result.Key != "/foo" || result.Value != "bar" || result.TTL < 95 {
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. t.Fatalf("Set failed with %s %s %v", result.Key, result.Value, result.TTL)
  54. }
  55. }
  56. // wait for a snapshot interval
  57. time.Sleep(3 * time.Second)
  58. snapshots, err = ioutil.ReadDir("/tmp/node1/snapshot")
  59. if err != nil {
  60. t.Fatal("list snapshot failed:" + err.Error())
  61. }
  62. if len(snapshots) != 1 {
  63. t.Fatal("wrong number of snapshot :[1/", len(snapshots), "]")
  64. }
  65. index, _ = strconv.Atoi(snapshots[0].Name()[2:6])
  66. if index < 1015 || index > 1018 {
  67. t.Fatal("wrong name of snapshot :", snapshots[0].Name())
  68. }
  69. }