-
Notifications
You must be signed in to change notification settings - Fork 165
/
meta_store_test.go
254 lines (212 loc) · 6.2 KB
/
meta_store_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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package main
import (
"fmt"
"os"
"testing"
"time"
)
var (
metaStoreTest *MetaStore
)
func TestGetMeta(t *testing.T) {
setupMeta()
defer teardownMeta()
meta, err := metaStoreTest.Get(&RequestVars{Oid: contentOid})
if err != nil {
t.Fatalf("Error retreiving meta: %s", err)
}
if meta.Oid != contentOid {
t.Errorf("expected to get content oid, got: %s", meta.Oid)
}
if meta.Size != contentSize {
t.Errorf("expected to get content size, got: %d", meta.Size)
}
}
func TestPutMeta(t *testing.T) {
setupMeta()
defer teardownMeta()
meta, err := metaStoreTest.Put(&RequestVars{Oid: nonExistingOid, Size: 42})
if err != nil {
t.Errorf("expected put to succeed, got : %s", err)
}
if meta.Existing {
t.Errorf("expected meta to not have existed")
}
meta, err = metaStoreTest.Get(&RequestVars{Oid: nonExistingOid})
if err != nil {
t.Errorf("expected to be able to retreive new put, got : %s", err)
}
if meta.Oid != nonExistingOid {
t.Errorf("expected oids to match, got: %s", meta.Oid)
}
if meta.Size != 42 {
t.Errorf("expected sizes to match, got: %d", meta.Size)
}
meta, err = metaStoreTest.Put(&RequestVars{Oid: nonExistingOid, Size: 42})
if err != nil {
t.Errorf("expected put to succeed, got : %s", err)
}
if !meta.Existing {
t.Errorf("expected meta to now exist")
}
}
func TestLocks(t *testing.T) {
setupMeta()
defer teardownMeta()
for i := 0; i < 5; i++ {
lock := NewTestLock(randomLockId(), fmt.Sprintf("path-%d", i), fmt.Sprintf("user-%d", i))
if err := metaStoreTest.AddLocks(testRepo, lock); err != nil {
t.Errorf("expected AddLocks to succeed, got : %s", err)
}
}
locks, err := metaStoreTest.Locks(testRepo)
if err != nil {
t.Errorf("expected Locks to succeed, got : %s", err)
}
if len(locks) != 5 {
t.Errorf("expected returned lock count to match, got: %d", len(locks))
}
}
func TestFilteredLocks(t *testing.T) {
setupMeta()
defer teardownMeta()
testLocks := make([]Lock, 0, 5)
for i := 0; i < 5; i++ {
lock := NewTestLock(randomLockId(), fmt.Sprintf("path-%d", i), fmt.Sprintf("user-%d", i))
testLocks = append(testLocks, lock)
}
if err := metaStoreTest.AddLocks(testRepo, testLocks...); err != nil {
t.Errorf("expected AddLocks to succeed, got : %s", err)
}
locks, next, err := metaStoreTest.FilteredLocks(testRepo, "", "", "3")
if err != nil {
t.Errorf("expected FilteredLocks to succeed, got : %s", err)
}
if len(locks) != 3 {
t.Errorf("expected locks count to match limit, got: %d", len(locks))
}
if next == "" {
t.Errorf("expected next to exist")
}
locks, next, err = metaStoreTest.FilteredLocks(testRepo, "", next, "2")
if err != nil {
t.Errorf("expected FilteredLocks to succeed, got : %s", err)
}
if len(locks) != 2 {
t.Errorf("expected locks count to match limit, got: %d", len(locks))
}
if next != "" {
t.Errorf("expected next to not exist, got: %s", next)
}
}
func TestAddLocks(t *testing.T) {
setupMeta()
defer teardownMeta()
lock := NewTestLock(lockId, lockPath, testUser)
if err := metaStoreTest.AddLocks(testRepo, lock); err != nil {
t.Errorf("expected AddLocks to succeed, got : %s", err)
}
locks, _, err := metaStoreTest.FilteredLocks(testRepo, lock.Path, "", "1")
if err != nil {
t.Errorf("expected FilteredLocks to succeed, got : %s", err)
}
if len(locks) != 1 {
t.Errorf("expected lock to be existed")
}
if locks[0].Id != lockId {
t.Errorf("expected lockId to match, got: %s", locks[0])
}
}
func TestDeleteLock(t *testing.T) {
setupMeta()
defer teardownMeta()
lock := NewTestLock(lockId, lockPath, testUser)
if err := metaStoreTest.AddLocks(testRepo, lock); err != nil {
t.Errorf("expected AddLocks to succeed, got : %s", err)
}
deleted, err := metaStoreTest.DeleteLock(testRepo, testUser, lock.Id, false)
if err != nil {
t.Errorf("expected DeleteLock to succeed, got : %s", err)
}
if deleted == nil || deleted.Id != lock.Id {
t.Errorf("expected deleted lock to be returned, got : %s", deleted)
}
}
func TestDeleteLockNotOwner(t *testing.T) {
setupMeta()
defer teardownMeta()
lock := NewTestLock(lockId, lockPath, testUser)
if err := metaStoreTest.AddLocks(testRepo, lock); err != nil {
t.Errorf("expected AddLocks to succeed, got : %s", err)
}
deleted, err := metaStoreTest.DeleteLock(testRepo, testUser1, lock.Id, false)
if err == nil || deleted != nil {
t.Errorf("expected DeleteLock to failed")
}
if err != errNotOwner {
t.Errorf("expected DeleteLock error match, got: %s", err)
}
}
func TestDeleteLockNotOwnerForce(t *testing.T) {
setupMeta()
defer teardownMeta()
lock := NewTestLock(lockId, lockPath, testUser)
if err := metaStoreTest.AddLocks(testRepo, lock); err != nil {
t.Errorf("expected AddLocks to succeed, got : %s", err)
}
deleted, err := metaStoreTest.DeleteLock(testRepo, testUser1, lock.Id, true)
if err != nil {
t.Errorf("expected DeleteLock(force) to succeed, got : %s", err)
}
if deleted == nil || deleted.Id != lock.Id {
t.Errorf("expected deleted lock to be returned, got : %s", deleted)
}
}
func TestDeleteLockNonExisting(t *testing.T) {
setupMeta()
defer teardownMeta()
lock := NewTestLock(lockId, lockPath, testUser)
if err := metaStoreTest.AddLocks(testRepo, lock); err != nil {
t.Errorf("expected AddLocks to succeed, got : %s", err)
}
deleted, err := metaStoreTest.DeleteLock(testRepo, testUser, nonExistingLockId, false)
if err != nil {
t.Errorf("expected DeleteLock to succeed, got : %s", err)
}
if deleted != nil {
t.Errorf("expected nil returned, got : %s", deleted)
}
}
func NewTestLock(id, path, user string) Lock {
return Lock{
Id: id,
Path: path,
Owner: User{
Name: user,
},
LockedAt: time.Now(),
}
}
func setupMeta() {
store, err := NewMetaStore("test-meta-store.db")
if err != nil {
fmt.Printf("error initializing test meta store: %s\n", err)
os.Exit(1)
}
metaStoreTest = store
if err := metaStoreTest.AddUser(testUser, testPass); err != nil {
teardownMeta()
fmt.Printf("error adding test user to meta store: %s\n", err)
os.Exit(1)
}
rv := &RequestVars{Oid: contentOid, Size: contentSize}
if _, err := metaStoreTest.Put(rv); err != nil {
teardownMeta()
fmt.Printf("error seeding test meta store: %s\n", err)
os.Exit(1)
}
}
func teardownMeta() {
metaStoreTest.Close()
os.RemoveAll("test-meta-store.db")
}