simple_snapshot_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package test
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "testing"
  6. "time"
  7. "github.com/coreos/go-etcd/etcd"
  8. )
  9. // This test creates a single node and then set a value to it to trigger snapshot
  10. func TestSimpleSnapshot(t *testing.T) {
  11. procAttr := new(os.ProcAttr)
  12. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  13. args := []string{"etcd", "-n=node1", "-d=/tmp/node1", "-snapshot=true", "-snapshotCount=500"}
  14. process, err := os.StartProcess(EtcdBinPath, append(args, "-f"), procAttr)
  15. if err != nil {
  16. t.Fatal("start process failed:" + err.Error())
  17. }
  18. defer process.Kill()
  19. time.Sleep(time.Second)
  20. c := etcd.NewClient(nil)
  21. c.SyncCluster()
  22. // issue first 501 commands
  23. for i := 0; i < 501; i++ {
  24. result, err := c.Set("foo", "bar", 100)
  25. if err != nil || result.Key != "/foo" || result.Value != "bar" || result.TTL < 95 {
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. t.Fatalf("Set failed with %s %s %v", result.Key, result.Value, result.TTL)
  30. }
  31. }
  32. // wait for a snapshot interval
  33. time.Sleep(3 * time.Second)
  34. snapshots, err := ioutil.ReadDir("/tmp/node1/snapshot")
  35. if err != nil {
  36. t.Fatal("list snapshot failed:" + err.Error())
  37. }
  38. if len(snapshots) != 1 {
  39. t.Fatal("wrong number of snapshot :[1/", len(snapshots), "]")
  40. }
  41. if snapshots[0].Name() != "0_503.ss" {
  42. t.Fatal("wrong name of snapshot :[0_503.ss/", snapshots[0].Name(), "]")
  43. }
  44. // issue second 501 commands
  45. for i := 0; i < 501; i++ {
  46. result, err := c.Set("foo", "bar", 100)
  47. if err != nil || result.Key != "/foo" || result.Value != "bar" || result.TTL < 95 {
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. t.Fatalf("Set failed with %s %s %v", result.Key, result.Value, result.TTL)
  52. }
  53. }
  54. // wait for a snapshot interval
  55. time.Sleep(3 * time.Second)
  56. snapshots, err = ioutil.ReadDir("/tmp/node1/snapshot")
  57. if err != nil {
  58. t.Fatal("list snapshot failed:" + err.Error())
  59. }
  60. if len(snapshots) != 1 {
  61. t.Fatal("wrong number of snapshot :[1/", len(snapshots), "]")
  62. }
  63. if snapshots[0].Name() != "0_1004.ss" {
  64. t.Fatal("wrong name of snapshot :[0_1004.ss/", snapshots[0].Name(), "]")
  65. }
  66. }