This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
forked from flier/curator.go
-
Notifications
You must be signed in to change notification settings - Fork 3
/
paths_test.go
171 lines (114 loc) · 5.3 KB
/
paths_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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package curator
import (
"testing"
"github.com/samuel/go-zookeeper/zk"
"github.com/stretchr/testify/assert"
)
func TestGetNodeFromPath(t *testing.T) {
assert.Equal(t, "child", GetNodeFromPath("child"))
assert.Equal(t, "", GetNodeFromPath("/child/"))
assert.Equal(t, "child", GetNodeFromPath("/child"))
assert.Equal(t, "child", GetNodeFromPath("/parent/child"))
}
func TestSplitPath(t *testing.T) {
p, err := SplitPath("test")
assert.NoError(t, err)
assert.Equal(t, p.Path, "test")
assert.Equal(t, p.Node, "")
p, err = SplitPath("/test/hello")
assert.NoError(t, err)
assert.Equal(t, p.Path, "/test")
assert.Equal(t, p.Node, "hello")
p, err = SplitPath("/hello")
assert.NoError(t, err)
assert.Equal(t, p.Path, "/")
assert.Equal(t, p.Node, "hello")
}
func TestJoinPath(t *testing.T) {
assert.Equal(t, JoinPath("parent", "child"), "/parent/child")
assert.Equal(t, JoinPath("parent/", "child"), "/parent/child")
assert.Equal(t, JoinPath("/parent/", "child"), "/parent/child")
assert.Equal(t, JoinPath("", "child"), "/child")
assert.Equal(t, JoinPath("parent", "", "child"), "/parent/child")
assert.Equal(t, JoinPath("parent", "child/"), "/parent/child")
assert.Equal(t, JoinPath("parent", "/child"), "/parent/child")
assert.Equal(t, JoinPath("parent", "child1", "child2"), "/parent/child1/child2")
}
func TestValidatePath(t *testing.T) {
assert.EqualError(t, ValidatePath(""), "Path cannot be null")
assert.EqualError(t, ValidatePath("test"), "Path must start with / character")
assert.EqualError(t, ValidatePath("/test/"), "Path must not end with / character")
assert.EqualError(t, ValidatePath("/\x00"), "null character not allowed @ 1")
assert.EqualError(t, ValidatePath("//test"), "empty node name specified @ 1")
assert.EqualError(t, ValidatePath("/.."), "relative paths not allowed @ 2")
assert.EqualError(t, ValidatePath("/../test"), "relative paths not allowed @ 2")
assert.EqualError(t, ValidatePath("/."), "relative paths not allowed @ 1")
assert.EqualError(t, ValidatePath("/./test"), "relative paths not allowed @ 1")
assert.EqualError(t, ValidatePath("/\u0010"), "invalid charater @ 1")
assert.EqualError(t, ValidatePath("/\u007f"), "invalid charater @ 1")
assert.EqualError(t, ValidatePath("/\uf805"), "invalid charater @ 1")
assert.EqualError(t, ValidatePath("/\ufff0"), "invalid charater @ 1")
}
func TestMakeDirs(t *testing.T) {
// skip exists `parent` and create `child`
conn := &mockConn{}
conn.On("Exists", "/parent").Return(true, nil, nil).Once()
conn.On("Exists", "/parent/child").Return(false, nil, nil).Once()
conn.On("Create", "/parent/child", []byte{}, int32(PERSISTENT), OPEN_ACL_UNSAFE).Return("", nil).Once()
assert.NoError(t, MakeDirs(conn, "/parent/child/node", false, nil))
conn.AssertExpectations(t)
// fail to create `parent`
conn = &mockConn{}
conn.On("Exists", "/parent").Return(true, nil, zk.ErrAPIError).Once()
assert.EqualError(t, MakeDirs(conn, "/parent/child/node", false, nil), zk.ErrAPIError.Error())
conn.AssertExpectations(t)
// create `child` which exists
conn = &mockConn{}
conn.On("Exists", "/parent").Return(true, nil, nil).Once()
conn.On("Exists", "/parent/child").Return(false, nil, nil).Once()
conn.On("Create", "/parent/child", []byte{}, int32(PERSISTENT), OPEN_ACL_UNSAFE).Return("", zk.ErrNodeExists).Once()
assert.NoError(t, MakeDirs(conn, "/parent/child/node", false, nil))
conn.AssertExpectations(t)
// create `child` with default ACLs
conn = &mockConn{}
acls := &mockACLProvider{}
conn.On("Exists", "/parent").Return(true, nil, nil).Once()
conn.On("Exists", "/parent/child").Return(false, nil, nil).Once()
acls.On("GetAclForPath", "/parent/child").Return([]zk.ACL{}).Once()
acls.On("GetDefaultAcl").Return(zk.AuthACL(zk.PermAdmin)).Once()
conn.On("Create", "/parent/child", []byte{}, int32(PERSISTENT), zk.AuthACL(zk.PermAdmin)).Return("", nil).Once()
assert.NoError(t, MakeDirs(conn, "/parent/child/node", false, acls))
conn.AssertExpectations(t)
acls.AssertExpectations(t)
}
func TestDeleteChildren(t *testing.T) {
// Delete children
conn := &mockConn{}
conn.On("Children", "/parent").Return([]string{"child1", "child2"}, nil, nil).Once()
conn.On("Children", "/parent/child1").Return(nil, nil, nil).Once()
conn.On("Children", "/parent/child2").Return(nil, nil, nil).Once()
conn.On("Delete", "/parent/child1", AnyVersion).Return(nil).Once()
conn.On("Delete", "/parent/child2", AnyVersion).Return(zk.ErrNoNode).Once()
assert.NoError(t, DeleteChildren(conn, "/parent", false))
conn.AssertExpectations(t)
// Children failed
conn = &mockConn{}
conn.On("Children", "/parent").Return(nil, nil, zk.ErrNoNode).Once()
assert.Equal(t, DeleteChildren(conn, "/parent", false), zk.ErrNoNode)
conn.AssertExpectations(t)
}
func TestEnsurePath(t *testing.T) {
helper := &mockEnsurePathHelper{log: t.Logf}
ensure := NewEnsurePathWithAclAndHelper("/parent/child", nil, helper)
assert.NotNil(t, ensure)
assert.True(t, ensure.makeLastNode)
ensure2 := ensure.ExcludingLast()
assert.NotNil(t, ensure2)
client := &mockCuratorZookeeperClient{log: t.Logf}
helper.On("Ensure", client, "/parent/child", true).Return(nil).Once()
assert.NoError(t, ensure.Ensure(client))
helper.On("Ensure", client, "/parent/child", false).Return(nil).Once()
assert.NoError(t, ensure2.Ensure(client))
helper.AssertExpectations(t)
client.AssertExpectations(t)
}