-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathetcd_test.go
More file actions
110 lines (89 loc) · 2.42 KB
/
etcd_test.go
File metadata and controls
110 lines (89 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package etcd
import (
"io/ioutil"
"os"
"os/exec"
"reflect"
"testing"
"time"
)
func init() {
_, err := exec.Command("etcd", "--version").Output()
if err != nil {
log.Fatal(err)
}
}
var etcdServer *exec.Cmd
func startEtcd() {
tmpDir, err := ioutil.TempDir(os.TempDir(), "etcd-test")
if err != nil {
log.Fatal("creating temp dir:", err)
}
log.Debugf("temp dir: %v", tmpDir)
etcdServer = exec.Command("etcd", "-data-dir="+tmpDir, "-name=default")
etcdServer.Start()
time.Sleep(1 * time.Second)
}
func stopEtcd() {
etcdServer.Process.Kill()
}
func TestGetSetEtcd(t *testing.T) {
startEtcd()
defer stopEtcd()
etcdClient := NewClient([]string{"http://localhost:4001"})
SetDefault(etcdClient, "/path", "value")
value := Get(etcdClient, "/path")
if value != "value" {
t.Fatalf("Expected '%v' but returned '%v'", "value", value)
}
Set(etcdClient, "/path", "", 0)
value = Get(etcdClient, "/path")
if value != "" {
t.Fatalf("Expected '%v' but returned '%v'", "", value)
}
Set(etcdClient, "/path", "value", uint64((1 * time.Second).Seconds()))
time.Sleep(2 * time.Second)
value = Get(etcdClient, "/path")
if value != "" {
t.Fatalf("Expected '%v' but returned '%v'", "", value)
}
}
func TestMkdirEtcd(t *testing.T) {
startEtcd()
defer stopEtcd()
etcdClient := NewClient([]string{"http://localhost:4001"})
Mkdir(etcdClient, "/directory")
values := GetList(etcdClient, "/directory")
if len(values) != 2 {
t.Fatalf("Expected '%v' but returned '%v'", 0, len(values))
}
Set(etcdClient, "/directory/item_1", "value", 0)
Set(etcdClient, "/directory/item_2", "value", 0)
values = GetList(etcdClient, "/directory")
if len(values) != 2 {
t.Fatalf("Expected '%v' but returned '%v'", 2, len(values))
}
lsResult := []string{"item_1", "item_2"}
if !reflect.DeepEqual(values, lsResult) {
t.Fatalf("Expected '%v' but returned '%v'", lsResult, values)
}
}
func TestWaitForKeysEtcd(t *testing.T) {
startEtcd()
defer stopEtcd()
etcdClient := NewClient([]string{"http://localhost:4001"})
Set(etcdClient, "/key", "value", 0)
start := time.Now()
err := WaitForKeys(etcdClient, []string{"/key"}, (10 * time.Second))
if err != nil {
t.Fatalf("%v", err)
}
end := time.Since(start)
if end.Seconds() > (2 * time.Second).Seconds() {
t.Fatalf("Expected '%vs' but returned '%vs'", 2, end.Seconds())
}
err = WaitForKeys(etcdClient, []string{"/key2"}, (2 * time.Second))
if err == nil {
t.Fatalf("Expected an error")
}
}