-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcreate_test.go
99 lines (75 loc) · 3.75 KB
/
create_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
package curator
import (
"sync"
"testing"
"github.com/samuel/go-zookeeper/zk"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type CreateBuilderTestSuite struct {
mockContainerTestSuite
}
func TestCreateBuilder(t *testing.T) {
suite.Run(t, new(CreateBuilderTestSuite))
}
func (s *CreateBuilderTestSuite) TestCreate() {
s.With(func(builder *CuratorFrameworkBuilder, client CuratorFramework, conn *mockConn, acls []zk.ACL) {
conn.On("Create", "/node", builder.DefaultData, int32(EPHEMERAL), acls).Return("/node", nil).Once()
path, err := client.Create().WithMode(EPHEMERAL).WithACL(acls...).ForPath("/node")
assert.Equal(s.T(), "/node", path)
assert.NoError(s.T(), err)
})
}
func (s *CreateBuilderTestSuite) TestNamespace() {
s.WithNamespace("parent", func(builder *CuratorFrameworkBuilder, client CuratorFramework, conn *mockConn, acls []zk.ACL) {
conn.On("Exists", "/parent").Return(false, nil, nil).Once()
conn.On("Create", "/parent", []byte{}, int32(PERSISTENT), OPEN_ACL_UNSAFE).Return("/parent", nil).Once()
conn.On("Create", "/parent/child", builder.DefaultData, int32(EPHEMERAL), acls).Return("/parent/child", nil).Once()
path, err := client.Create().WithMode(EPHEMERAL).WithACL(acls...).ForPath("/child")
assert.Equal(s.T(), "/child", path)
assert.NoError(s.T(), err)
})
}
func (s *CreateBuilderTestSuite) TestBackground() {
s.WithNamespace("parent", func(client CuratorFramework, conn *mockConn, wg *sync.WaitGroup, data []byte, acls []zk.ACL) {
ctxt := "context"
conn.On("Exists", "/parent").Return(true, nil, nil).Once()
conn.On("Create", "/parent/child", data, int32(PERSISTENT), acls).Return("", zk.ErrAPIError).Once()
path, err := client.Create().WithACL(acls...).InBackgroundWithCallbackAndContext(
func(client CuratorFramework, event CuratorEvent) error {
defer wg.Done()
assert.Equal(s.T(), CREATE, event.Type())
assert.Equal(s.T(), "/child", event.Path())
assert.Equal(s.T(), data, event.Data())
assert.Equal(s.T(), acls, event.ACLs())
assert.EqualError(s.T(), event.Err(), zk.ErrAPIError.Error())
assert.Equal(s.T(), "child", event.Name())
assert.Equal(s.T(), ctxt, event.Context())
return nil
}, ctxt).ForPathWithData("/child", data)
assert.Equal(s.T(), "/child", path)
assert.NoError(s.T(), err)
})
}
func (s *CreateBuilderTestSuite) TestCompression() {
s.With(func(client CuratorFramework, conn *mockConn, compress *mockCompressionProvider, data []byte, acls []zk.ACL) {
compressedData := []byte("compressedData")
compress.On("Compress", "/node", data).Return(compressedData, nil).Once()
conn.On("Create", "/node", compressedData, int32(PERSISTENT), acls).Return("/node", nil).Once()
path, err := client.Create().Compressed().WithACL(acls...).ForPathWithData("/node", data)
assert.Equal(s.T(), "/node", path)
assert.NoError(s.T(), err)
})
}
func (s *CreateBuilderTestSuite) TestCreateParents() {
s.With(func(builder *CuratorFrameworkBuilder, client CuratorFramework, conn *mockConn, data []byte, aclProvider *mockACLProvider, acls []zk.ACL) {
aclProvider.On("GetAclForPath", "/parent/child").Return(READ_ACL_UNSAFE).Once()
conn.On("Create", "/parent/child", data, int32(PERSISTENT), READ_ACL_UNSAFE).Return("", zk.ErrNoNode).Once()
conn.On("Exists", "/parent").Return(false, nil, nil).Once()
aclProvider.On("GetAclForPath", "/parent").Return(CREATOR_ALL_ACL).Once()
conn.On("Create", "/parent", []byte{}, int32(PERSISTENT), CREATOR_ALL_ACL).Return("", zk.ErrAPIError).Once()
path, err := client.Create().CreatingParentsIfNeeded().ForPathWithData("/parent/child", data)
assert.Equal(s.T(), "", path)
assert.Equal(s.T(), err, zk.ErrAPIError)
})
}