github.com/macb/etcd@v0.3.1-0.20140227003422-a60481c6b1a0/tests/functional/single_node_recovery_test.go (about)

     1  package test
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/coreos/etcd/third_party/github.com/coreos/go-etcd/etcd"
     9  )
    10  
    11  // This test creates a single node and then set a value to it.
    12  // Then this test kills the node and restart it and tries to get the value again.
    13  func TestSingleNodeRecovery(t *testing.T) {
    14  	procAttr := new(os.ProcAttr)
    15  	procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
    16  	args := []string{"etcd", "-name=node1", "-data-dir=/tmp/node1"}
    17  
    18  	process, err := os.StartProcess(EtcdBinPath, append(args, "-f"), procAttr)
    19  	if err != nil {
    20  		t.Fatal("start process failed:" + err.Error())
    21  		return
    22  	}
    23  
    24  	time.Sleep(time.Second)
    25  
    26  	c := etcd.NewClient(nil)
    27  
    28  	c.SyncCluster()
    29  	// Test Set
    30  	result, err := c.Set("foo", "bar", 100)
    31  	node := result.Node
    32  
    33  	if err != nil || node.Key != "/foo" || node.Value != "bar" || node.TTL < 95 {
    34  		if err != nil {
    35  			t.Fatal(err)
    36  		}
    37  
    38  		t.Fatalf("Set 1 failed with %s %s %v", node.Key, node.Value, node.TTL)
    39  	}
    40  
    41  	time.Sleep(time.Second)
    42  
    43  	process.Kill()
    44  
    45  	process, err = os.StartProcess(EtcdBinPath, args, procAttr)
    46  	defer process.Kill()
    47  	if err != nil {
    48  		t.Fatal("start process failed:" + err.Error())
    49  		return
    50  	}
    51  
    52  	time.Sleep(time.Second)
    53  
    54  	result, err = c.Get("foo", false, false)
    55  	node = result.Node
    56  
    57  	if err != nil {
    58  		t.Fatal("get fail: " + err.Error())
    59  		return
    60  	}
    61  
    62  	if err != nil || node.Key != "/foo" || node.Value != "bar" || node.TTL > 99 {
    63  		if err != nil {
    64  			t.Fatal(err)
    65  		}
    66  		t.Fatalf("Recovery Get failed with %s %s %v", node.Key, node.Value, node.TTL)
    67  	}
    68  }