github.com/macb/etcd@v0.3.1-0.20140227003422-a60481c6b1a0/tests/functional/single_node_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 // Create a single node and try to set value 12 func TestSingleNode(t *testing.T) { 13 procAttr := new(os.ProcAttr) 14 procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr} 15 args := []string{"etcd", "-name=node1", "-f", "-data-dir=/tmp/node1"} 16 17 process, err := os.StartProcess(EtcdBinPath, args, procAttr) 18 if err != nil { 19 t.Fatal("start process failed:" + err.Error()) 20 return 21 } 22 defer process.Kill() 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("Set 1: ", 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 result, err = c.Set("foo", "bar", 100) 44 node = result.Node 45 46 if err != nil || node.Key != "/foo" || node.Value != "bar" || node.TTL != 100 { 47 if err != nil { 48 t.Fatal("Set 2: ", err) 49 } 50 t.Fatalf("Set 2 failed with %s %s %v", node.Key, node.Value, node.TTL) 51 } 52 53 // Add a test-and-set test 54 55 // First, we'll test we can change the value if we get it write 56 result, err = c.CompareAndSwap("foo", "foobar", 100, "bar", 0) 57 node = result.Node 58 59 if err != nil || node.Key != "/foo" || node.Value != "foobar" || node.TTL != 100 { 60 if err != nil { 61 t.Fatal(err) 62 } 63 t.Fatalf("Set 3 failed with %s %s %v", node.Key, node.Value, node.TTL) 64 } 65 66 // Next, we'll make sure we can't set it without the correct prior value 67 _, err = c.CompareAndSwap("foo", "foofoo", 100, "bar", 0) 68 69 if err == nil { 70 t.Fatalf("Set 4 expecting error when setting key with incorrect previous value") 71 } 72 }