github.com/jpetazzo/etcd@v0.2.1-0.20140113055439-97f1363afac5/tests/functional/simple_snapshot_test.go (about)

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