-
Notifications
You must be signed in to change notification settings - Fork 2
/
buddyfs_test.go
379 lines (295 loc) · 9.74 KB
/
buddyfs_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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package gobuddyfs_test
import (
"encoding/binary"
"fmt"
"strconv"
"testing"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"github.com/buddyfs/gobuddyfs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"golang.org/x/net/context"
)
type MockKVStore struct {
mock.Mock
gobuddyfs.KVStore
}
func (m *MockKVStore) Get(key string, retry bool) ([]byte, error) {
args := m.Mock.Called(key, retry)
bytes, ok := args.Get(0).([]byte)
if !ok {
return nil, args.Error(1)
}
return bytes, args.Error(1)
}
func (m *MockKVStore) Set(key string, value []byte) error {
args := m.Mock.Called(key, value)
return args.Error(0)
}
// FIXME: Re-enable this test after fixing error passing semantics in Get()
/*
func TestRootGetNodeError(t *testing.T) {
mkv := new(MockKVStore)
bfs := gobuddyfs.NewBuddyFS(mkv)
mkv.On("Get", "ROOT").Return(nil, fmt.Errorf("Something bad")).Once()
node, err := bfs.Root()
assert.Error(t, err)
assert.Nil(t, node, "Error should return nil Root")
mkv.AssertExpectations(t)
}
*/
func TestRootCreateSuccess(t *testing.T) {
mkv := new(MockKVStore)
bfs := gobuddyfs.NewBuddyFS(mkv)
mkv.On("Get", "ROOT", true).Return(nil, nil).Once()
mkv.On("Set", mock.Anything, mock.Anything).Return(nil).Twice()
node, err := bfs.Root()
assert.NoError(t, err)
assert.NotNil(t, node, "Successfully created root should be non-nil")
attr := fuse.Attr{}
node.Attr(&attr)
assert.NotNil(t, attr)
mkv.AssertExpectations(t)
}
func TestRootCreateAndReadRoot(t *testing.T) {
mkv := new(MockKVStore)
bfs := gobuddyfs.NewBuddyFS(mkv)
mkv.On("Get", "ROOT", true).Return(nil, nil).Once()
mkv.On("Set", mock.Anything, mock.Anything).Return(nil).Twice()
node, err := bfs.Root()
assert.NoError(t, err)
assert.NotNil(t, node, "Successfully created root should be non-nil")
attr := fuse.Attr{}
node.Attr(&attr)
assert.NotNil(t, attr)
mkv.AssertExpectations(t)
node2, err2 := bfs.Root()
assert.NoError(t, err2)
assert.NotNil(t, node2)
assert.Equal(t, node2, node, "Cached node")
mkv.AssertExpectations(t)
}
func TestRootCreateWriteNodeFail(t *testing.T) {
mkv := new(MockKVStore)
bfs := gobuddyfs.NewBuddyFS(mkv)
mkv.On("Get", "ROOT", true).Return(nil, nil).Once()
mkv.On("Set", mock.Anything, mock.Anything).Return(fmt.Errorf("Writing root node failed")).Once()
node, err := bfs.Root()
assert.Error(t, err)
assert.Nil(t, node, "Error should return nil Root")
mkv.AssertExpectations(t)
}
func TestRootCreateWriteROOTKeyFail(t *testing.T) {
mkv := new(MockKVStore)
bfs := gobuddyfs.NewBuddyFS(mkv)
mkv.On("Get", "ROOT", true).Return(nil, nil).Once()
mkv.On("Set", mock.Anything, mock.Anything).Return(nil).Once()
mkv.On("Set", mock.Anything, mock.Anything).Return(fmt.Errorf("Writing ROOT key failed")).Once()
node, err := bfs.Root()
assert.Error(t, err)
assert.Nil(t, node, "Error should return nil Root")
mkv.AssertExpectations(t)
}
func TestRootReadCorruptedExistingRootKey(t *testing.T) {
mkv := new(MockKVStore)
bfs := gobuddyfs.NewBuddyFS(mkv)
buffer := []byte{255, 255, 255}
mkv.On("Get", "ROOT", true).Return(buffer, nil).Once()
node, err := bfs.Root()
assert.Error(t, err)
assert.Nil(t, node, "Error should return nil Root")
mkv.AssertExpectations(t)
}
func TestRootReadCorruptedExistingRootNode(t *testing.T) {
mkv := new(MockKVStore)
bfs := gobuddyfs.NewBuddyFS(mkv)
buffer := make([]byte, 80)
id := int64(1000)
binary.PutVarint(buffer, id)
mkv.On("Get", "ROOT", true).Return(buffer, nil).Once()
mkv.On("Get", "1000", true).Return(buffer, nil).Once()
node, err := bfs.Root()
assert.Error(t, err)
assert.Nil(t, node, "Error should return nil Root")
mkv.AssertExpectations(t)
}
func TestRootReadExistingRoot(t *testing.T) {
mkv := new(MockKVStore)
bfs := gobuddyfs.NewBuddyFS(mkv)
buffer := make([]byte, 80)
id := int64(2000)
binary.PutVarint(buffer, id)
jsonDir := "{\"name\": \"x\", \"Inode\": 1, \"Id\": 2000}"
mkv.On("Get", "ROOT", true).Return(buffer, nil).Once()
mkv.On("Get", "2000", true).Return([]byte(jsonDir), nil).Once()
node, err := bfs.Root()
assert.NoError(t, err)
assert.NotNil(t, node, "Successfully created root should be non-nil")
attr := fuse.Attr{}
node.Attr(&attr)
assert.NotNil(t, attr)
mkv.AssertExpectations(t)
}
func TestRootReReadExistingRoot(t *testing.T) {
mkv := new(MockKVStore)
bfs := gobuddyfs.NewBuddyFS(mkv)
buffer := make([]byte, 80)
id := int64(2000)
binary.PutVarint(buffer, id)
jsonDir := "{\"NextInode\": 2, \"name\": \"x\", \"Inode\": 1, \"Id\": 3000}"
mkv.On("Get", "ROOT", true).Return(buffer, nil).Once()
mkv.On("Get", "2000", true).Return([]byte(jsonDir), nil).Once()
node, err := bfs.Root()
assert.NoError(t, err)
assert.NotNil(t, node, "Successfully created root should be non-nil")
attr := fuse.Attr{}
node.Attr(&attr)
assert.NotNil(t, attr)
node2, err2 := bfs.Root()
assert.NoError(t, err2)
assert.NotNil(t, node2)
assert.Equal(t, node2, node, "Cached node")
mkv.AssertExpectations(t)
}
func TestMkdirWithDuplicate(t *testing.T) {
memkv := gobuddyfs.NewMemStore()
bfs := gobuddyfs.NewBuddyFS(memkv)
root, _ := bfs.Root()
node, err := root.(*gobuddyfs.FSMeta).Mkdir(context.TODO(), &fuse.MkdirRequest{Name: "foo"})
assert.NoError(t, err)
assert.NotNil(t, node, "Newly created directory node should not be nil")
// Create duplicate directory
node, err = root.(*gobuddyfs.FSMeta).Mkdir(context.TODO(), &fuse.MkdirRequest{Name: "foo"})
assert.Error(t, err, "Duplicate directory name")
assert.Nil(t, node)
}
func TestParallelMkdirWithDuplicate(t *testing.T) {
memkv := gobuddyfs.NewMemStore()
bfs := gobuddyfs.NewBuddyFS(memkv)
root, _ := bfs.Root()
const parallelism = 10
nodes := make([]fs.Node, parallelism)
errs := make([]error, parallelism)
dones := make([]chan bool, parallelism)
for i := 0; i < parallelism; i++ {
dones[i] = make(chan bool)
}
mkdir := func(node *fs.Node, err *error, done chan bool) {
*node, *err = root.(*gobuddyfs.FSMeta).Mkdir(context.TODO(), &fuse.MkdirRequest{Name: "foo"})
done <- true
}
for i := 0; i < parallelism; i++ {
go mkdir(&nodes[i], &errs[i], dones[i])
}
for i := 0; i < parallelism; i++ {
<-dones[i]
}
/*
* Constraints:
* - Exactly one of Node1 and Node2 will be non-nil and the other should be nil
* - For each pair of Node_i, Err_i, exactly one of them should be non-nil and the other should be nil
*/
nodesNonNil := 0
for i := 0; i < parallelism; i++ {
if nodes[i] != nil {
nodesNonNil++
t.Logf("Node[%d] succeeded", i)
}
}
assert.Equal(t, 1, nodesNonNil, "Exactly one of the mkdirs should have succeeded")
for i := 0; i < parallelism; i++ {
if nodes[i] == nil && errs[i] == nil {
t.Errorf("Both node and err are nil while exactly one should have been non-nil")
} else if nodes[i] != nil && errs[i] != nil {
t.Errorf("Both node and err are non-nil while exactly one should have been non-nil")
}
}
}
func TestCreateWithDuplicate(t *testing.T) {
memkv := gobuddyfs.NewMemStore()
bfs := gobuddyfs.NewBuddyFS(memkv)
root, _ := bfs.Root()
node, _, err := root.(*gobuddyfs.FSMeta).Create(context.TODO(), &fuse.CreateRequest{Name: "foo"}, nil)
assert.NoError(t, err)
assert.NotNil(t, node, "Newly created file node should not be nil")
// Create duplicate file
node, _, err = root.(*gobuddyfs.FSMeta).Create(context.TODO(), &fuse.CreateRequest{Name: "foo"}, nil)
assert.Error(t, err, "Duplicate file name")
assert.Nil(t, node)
}
func TestParallelCreateWithDuplicate(t *testing.T) {
memkv := gobuddyfs.NewMemStore()
bfs := gobuddyfs.NewBuddyFS(memkv)
root, _ := bfs.Root()
const parallelism = 10
nodes := make([]fs.Node, parallelism)
errs := make([]error, parallelism)
dones := make([]chan bool, parallelism)
for i := 0; i < parallelism; i++ {
dones[i] = make(chan bool)
}
mkdir := func(node *fs.Node, err *error, done chan bool) {
*node, _, *err = root.(*gobuddyfs.FSMeta).Create(context.TODO(), &fuse.CreateRequest{Name: "foo"}, nil)
done <- true
}
for i := 0; i < parallelism; i++ {
go mkdir(&nodes[i], &errs[i], dones[i])
}
for i := 0; i < parallelism; i++ {
<-dones[i]
}
/*
* Constraints:
* - Exactly one of Node1 and Node2 will be non-nil and the other should be nil
* - For each pair of Node_i, Err_i, exactly one of them should be non-nil and the other should be nil
*/
nodesNonNil := 0
for i := 0; i < parallelism; i++ {
if nodes[i] != nil {
nodesNonNil++
t.Logf("Node[%d] succeeded", i)
}
}
assert.Equal(t, 1, nodesNonNil, "Exactly one of the creates should have succeeded")
for i := 0; i < parallelism; i++ {
if nodes[i] == nil && errs[i] == nil {
t.Errorf("Both node and err are nil while exactly one should have been non-nil")
} else if nodes[i] != nil && errs[i] != nil {
t.Errorf("Both node and err are non-nil while exactly one should have been non-nil")
}
}
}
func TestParallelCreate(t *testing.T) {
memkv := gobuddyfs.NewMemStore()
bfs := gobuddyfs.NewBuddyFS(memkv)
root, _ := bfs.Root()
const parallelism = 100
nodes := make([]fs.Node, parallelism)
errs := make([]error, parallelism)
dones := make([]chan bool, parallelism)
for i := 0; i < parallelism; i++ {
dones[i] = make(chan bool)
}
mkdir := func(node *fs.Node, err *error, done chan bool, name string) {
*node, _, *err = root.(*gobuddyfs.FSMeta).Create(context.TODO(), &fuse.CreateRequest{Name: name}, nil)
done <- true
}
for i := 0; i < parallelism; i++ {
go mkdir(&nodes[i], &errs[i], dones[i], "name"+strconv.Itoa(i))
}
for i := 0; i < parallelism; i++ {
<-dones[i]
}
/*
* Constraints:
* - Exactly one of Node1 and Node2 will be non-nil and the other should be nil
* - For each pair of Node_i, Err_i, exactly one of them should be non-nil and the other should be nil
*/
for i := 0; i < parallelism; i++ {
if nodes[i] != nil {
assert.NoError(t, errs[i])
assert.NotNil(t, nodes[i], "Newly created file node should not be nil")
}
}
}