-
Notifications
You must be signed in to change notification settings - Fork 2
/
wrapper_test.go
156 lines (145 loc) · 3.53 KB
/
wrapper_test.go
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package zk_wrapper
import (
"fmt"
"os"
"testing"
"time"
"github.com/meitu/go-zookeeper/zk"
)
var (
root = "/chroot"
conn *Conn
)
func init() {
var err error
conn, _, err = Connect([]string{"127.0.0.1:2181"}, 6*time.Second)
if err != nil {
fmt.Printf("Failed to setup test, err: %s", err)
os.Exit(1)
}
_, err = conn.Create(root, nil, 0, zk.WorldACL(zk.PermAll))
if err != nil && err != zk.ErrNodeExists {
fmt.Printf("Failed to create root path, err: %s", err)
os.Exit(1)
}
}
func TestAppendPrefix(t *testing.T) {
cases := []string{
"/a",
"/b/c",
}
mockConn := &Conn{
chroot: root,
}
for _, c := range cases {
expected := root + c
got := mockConn.AppendChroot(c)
if got != expected {
t.Errorf("%s was expected, but got %s", expected, got)
}
}
}
func TestTrimPrefix(t *testing.T) {
cases := []string{
"/a",
"/b/c",
}
mockConn := &Conn{
chroot: root,
}
for _, c := range cases {
input := mockConn.AppendChroot(c)
got := mockConn.TrimChroot(input)
if c != mockConn.TrimChroot(input) {
t.Errorf("%s was expected, but got %s", c, got)
}
}
}
func TestCreate(t *testing.T) {
path := "/create-test"
conn.Chroot(root)
zkPath, err := conn.Create(path, nil, 0, zk.WorldACL(zk.PermAll))
if zkPath != path && err != zk.ErrNodeExists {
t.Errorf("%s was expected, but got %s", path, zkPath)
}
conn.Chroot("")
exists, _, err := conn.Exists(root + path)
if !exists || err != nil {
t.Errorf("exists was expected, but got not exists")
}
}
func TestGetAndSet(t *testing.T) {
path := "/get-set-test"
data := []byte{1, 2, 3, 4}
conn.Chroot(root)
conn.Create(path, nil, 0, zk.WorldACL(zk.PermAll))
_, err := conn.Set(path, data, -1)
if err != nil {
t.Errorf("success to Set was expected, but got err %s", err)
}
v, _, err := conn.Get(path)
if err != nil {
t.Errorf("success to Get was expected, but got err %s", err)
}
if v == nil {
t.Errorf("data %v was expected, but got nil", data)
}
conn.Chroot("")
v1, _, err := conn.Get(root + path)
if err != nil {
t.Errorf("success to Get was expected, but got err %s", err)
}
if v1 == nil {
t.Errorf("%v was expected, but got nil", data)
}
}
func TestDelete(t *testing.T) {
path := "/delete-test"
conn.Create(root+path, nil, 0, zk.WorldACL(zk.PermAll))
conn.Chroot(root)
err := conn.Delete(path, -1)
if err != nil {
t.Errorf("ret nil was expected, but got err %s", err)
}
}
func TestChildren(t *testing.T) {
path := "/children-test"
conn.Chroot("")
_, err := conn.Create(root+path, nil, 0, zk.WorldACL(zk.PermAll))
fmt.Println(root+path, err)
subPaths := []string{"a", "b", "c"}
for _, sub := range subPaths {
_, err = conn.Create(root+path+"/"+sub, nil, 0, zk.WorldACL(zk.PermAll))
}
conn.Chroot(root)
children, _, _ := conn.Children(path)
if len(children) != len(subPaths) {
t.Errorf("children num %d was expected, but got %d", len(subPaths), len(children))
}
}
func TestRecursiveOperation(t *testing.T) {
mkdirCases := []string{
"/recursive-1/a/b/c",
"/recursivie-2",
"/a",
}
for _, c := range mkdirCases {
if err := conn.MkNodeRecursive(c, 0, nil); err != nil {
t.Errorf("mkdir error = nil was expected, but got %v", err)
}
}
data := "bar"
conn.MkNodeRecursive("/a", 0, []byte(data))
got, _, err := conn.Get("/a")
if err != nil {
t.Errorf("got error when get data, err: %v", err)
}
if string(got) != data {
t.Errorf("data %v was expected, but got %v", data, got)
}
for _, c := range mkdirCases {
if err := conn.DeleteRecursive(c, -1); err != nil {
t.Errorf("delete error = nil was expected, but got %v", err)
}
}
}