forked from thecodeteam/goisilon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exports_test.go
565 lines (459 loc) · 14.3 KB
/
exports_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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
package goisilon
import (
"context"
"sort"
"testing"
"github.com/stretchr/testify/assert"
)
func TestExportsList(t *testing.T) {
volumeName1 := "test_get_exports1"
volumeName2 := "test_get_exports2"
volumeName3 := "test_get_exports3"
// Identify all exports currently on the cluster
exportMap := make(map[int]string)
exports, err := client.GetExports(context.Background())
assertNoError(t, err)
for _, export := range exports {
exportMap[export.ID] = (*export.Paths)[0]
}
initialExportCount := len(exports)
var (
vol Volume
exportID int
)
// Add the test exports
vol, err = client.CreateVolume(defaultCtx, volumeName1)
assertNoError(t, err)
assertNotNil(t, vol)
volumeName1 = vol.Name
volumePath1 := client.API.VolumePath(volumeName1)
t.Logf("created volume: %s", volumeName1)
vol, err = client.CreateVolume(defaultCtx, volumeName2)
assertNoError(t, err)
assertNotNil(t, vol)
volumeName2 = vol.Name
volumePath2 := client.API.VolumePath(volumeName2)
t.Logf("created volume: %s", volumeName2)
vol, err = client.CreateVolume(defaultCtx, volumeName3)
assertNoError(t, err)
assertNotNil(t, vol)
volumeName3 = vol.Name
volumePath3 := client.API.VolumePath(volumeName3)
t.Logf("created volume: %s", volumeName3)
exportID, err = client.Export(defaultCtx, volumeName1)
assertNoError(t, err)
t.Logf("created export: %d", exportID)
exportID, err = client.Export(defaultCtx, volumeName2)
assertNoError(t, err)
t.Logf("created export: %d", exportID)
exportID, err = client.Export(defaultCtx, volumeName3)
assertNoError(t, err)
t.Logf("created export: %d", exportID)
// make sure we clean up when we're done
defer client.Unexport(defaultCtx, volumeName1)
defer client.Unexport(defaultCtx, volumeName2)
defer client.Unexport(defaultCtx, volumeName3)
defer client.DeleteVolume(defaultCtx, volumeName1)
defer client.DeleteVolume(defaultCtx, volumeName2)
defer client.DeleteVolume(defaultCtx, volumeName3)
// Get the updated export list
exports, err = client.GetExports(defaultCtx)
assertNoError(t, err)
// Verify that the new exports are there as well as all the old exports.
if !assert.Equal(t, initialExportCount+3, len(exports)) {
t.FailNow()
}
// Remove the original exports and add the new ones. In the end, we should only have the
// exports we just created and nothing more.
for _, export := range exports {
if _, found := exportMap[export.ID]; found == true {
// this export was exported prior to the test start
delete(exportMap, export.ID)
} else {
// this export is new
exportMap[export.ID] = (*export.Paths)[0]
}
}
if !assert.Len(t, exportMap, 3) {
t.FailNow()
}
volumeBitmap := 0
for _, path := range exportMap {
if path == volumePath1 {
volumeBitmap += 1
} else if path == volumePath2 {
volumeBitmap += 2
} else if path == volumePath3 {
volumeBitmap += 4
}
}
assert.Equal(t, 7, volumeBitmap)
}
func TestExportCreate(t *testing.T) {
volumeName := "test_create_export"
volumePath := client.API.VolumePath(volumeName)
// setup the test
_, err := client.CreateVolume(defaultCtx, volumeName)
assertNoError(t, err)
// make sure we clean up when we're done
defer client.Unexport(defaultCtx, volumeName)
defer client.DeleteVolume(defaultCtx, volumeName)
// verify the volume isn't already exported
export, err := client.GetExportByName(defaultCtx, volumeName)
assertNoError(t, err)
assertNil(t, export)
// export the volume
_, err = client.Export(defaultCtx, volumeName)
assertNoError(t, err)
// verify the volume has been exported
export, err = client.GetExportByName(defaultCtx, volumeName)
assertNoError(t, err)
assertNotNil(t, export)
found := false
for _, path := range *export.Paths {
if path == volumePath {
found = true
break
}
}
assert.True(t, found)
}
func TestExportDelete(t *testing.T) {
volumeName := "test_unexport_volume"
// initialize the export
_, err := client.CreateVolume(defaultCtx, volumeName)
assertNoError(t, err)
_, err = client.Export(defaultCtx, volumeName)
assertNoError(t, err)
// make sure we clean up when we're done
defer client.DeleteVolume(defaultCtx, volumeName)
// verify the volume is exported
export, err := client.GetExportByName(defaultCtx, volumeName)
assertNoError(t, err)
assertNotNil(t, export)
// Unexport the volume
err = client.Unexport(defaultCtx, volumeName)
assertNoError(t, err)
// verify the volume is no longer exported
export, err = client.GetExportByName(defaultCtx, volumeName)
assertNoError(t, err)
assertNil(t, export)
}
func TestExportNonRootMapping(t *testing.T) {
testUserMapping(
t,
"test_export_non_root_mapping",
client.GetNonRootMappingByID,
client.EnableNonRootMappingByID,
client.DisableNonRootMappingByID)
}
func TestExportFailureMapping(t *testing.T) {
testUserMapping(
t,
"test_export_failure_mapping",
client.GetFailureMappingByID,
client.EnableFailureMappingByID,
client.DisableFailureMappingByID)
}
func TestExportRootMapping(t *testing.T) {
testUserMapping(
t,
"test_export_root_mapping",
client.GetRootMappingByID,
client.EnableRootMappingByID,
client.DisableRootMappingByID)
}
func testUserMapping(
t *testing.T,
volumeName string,
getMap func(ctx context.Context, id int) (UserMapping, error),
enaMap func(ctx context.Context, id int, user string) error,
disMap func(ctx context.Context, id int) error) {
var (
err error
exportID int
userMap UserMapping
)
// initialize the export
_, err = client.CreateVolume(defaultCtx, volumeName)
assertNoError(t, err)
exportID, err = client.Export(defaultCtx, volumeName)
assertNoError(t, err)
// make sure we clean up when we're done
defer client.UnexportByID(defaultCtx, exportID)
defer client.DeleteVolume(defaultCtx, volumeName)
// verify the existing mapping is mapped to nobody
userMap, err = getMap(defaultCtx, exportID)
assertNoError(t, err)
assertNotNil(t, userMap)
assertNotNil(t, userMap.User)
assertNotNil(t, userMap.User.ID)
assertNotNil(t, userMap.User.ID.ID)
assert.Equal(t, "nobody", userMap.User.ID.ID)
// update the user mapping to root
err = enaMap(defaultCtx, exportID, "root")
assertNoError(t, err)
// verify the user mapping is mapped to root
userMap, err = getMap(defaultCtx, exportID)
assertNoError(t, err)
assertNotNil(t, userMap)
assertNotNil(t, userMap.User)
assertNotNil(t, userMap.User.ID)
assertNotNil(t, userMap.User.ID.ID)
assert.Equal(t, "root", userMap.User.ID.ID)
// disable the user mapping
err = disMap(defaultCtx, exportID)
assertNoError(t, err)
// verify the user mapping is disabled
userMap, err = getMap(defaultCtx, exportID)
assertNoError(t, err)
assertNotNil(t, userMap.Enabled)
assert.False(t, *userMap.Enabled)
}
var (
getClients = func(ctx context.Context, e Export) []string {
return *e.Clients
}
getRootClients = func(ctx context.Context, e Export) []string {
return *e.RootClients
}
)
func TestExportClientsGet(t *testing.T) {
testExportClientsGet(
t,
"test_get_export_clients",
client.GetExportClientsByID,
client.SetExportClientsByID)
}
func TestExportClientsSet(t *testing.T) {
testExportClientsSet(
t,
"test_set_export_clients",
getClients,
client.SetExportClientsByID)
}
func TestExportClientsAdd(t *testing.T) {
testExportClientsAdd(
t,
"test_add_export_clients",
getClients,
client.SetExportClientsByID,
client.AddExportClientsByID)
}
func TestExportClientsClear(t *testing.T) {
testExportClientsClear(
t,
"test_clear_export_clients",
getClients,
client.SetExportClientsByID,
client.ClearExportClientsByID)
}
func TestExportRootClientsGet(t *testing.T) {
testExportClientsGet(
t,
"test_get_export_root_clients",
client.GetExportRootClientsByID,
client.SetExportRootClientsByID)
}
func TestExportRootClientsSet(t *testing.T) {
testExportClientsSet(
t,
"test_set_export_root_clients",
getRootClients,
client.SetExportRootClientsByID)
}
func TestExportRootClientsAdd(t *testing.T) {
testExportClientsAdd(
t,
"test_add_export_root_clients",
getRootClients,
client.SetExportRootClientsByID,
client.AddExportRootClientsByID)
}
func TestExportRootClientsClear(t *testing.T) {
testExportClientsClear(
t,
"test_clear_export_root_clients",
getRootClients,
client.SetExportRootClientsByID,
client.ClearExportRootClientsByID)
}
func testExportClientsGet(
t *testing.T,
volumeName string,
getClients func(ctx context.Context, id int) ([]string, error),
setClients func(ctx context.Context, id int, clients ...string) error) {
var (
err error
exportID int
clientList = []string{"1.2.3.4", "1.2.3.5"}
currentClients []string
)
// initialize the export
_, err = client.CreateVolume(defaultCtx, volumeName)
assertNoError(t, err)
exportID, err = client.Export(defaultCtx, volumeName)
assertNoError(t, err)
// make sure we clean up when we're done
defer client.UnexportByID(defaultCtx, exportID)
defer client.DeleteVolume(defaultCtx, volumeName)
// set the export client
err = setClients(defaultCtx, exportID, clientList...)
assertNoError(t, err)
// test getting the client list
currentClients, err = getClients(defaultCtx, exportID)
assertNoError(t, err)
// verify we received the correct clients
assert.Equal(t, len(clientList), len(currentClients))
sort.Strings(currentClients)
sort.Strings(clientList)
for i := range currentClients {
assert.Equal(t, currentClients[i], clientList[i])
}
}
func testExportClientsSet(
t *testing.T,
volumeName string,
getClients func(ctx context.Context, e Export) []string,
setClients func(ctx context.Context, id int, clients ...string) error) {
var (
err error
export Export
exportID int
currentClients []string
clientList = []string{"1.2.3.4", "1.2.3.5"}
)
sort.Strings(clientList)
// initialize the export
_, err = client.CreateVolume(defaultCtx, volumeName)
assertNoError(t, err)
exportID, err = client.Export(defaultCtx, volumeName)
assertNoError(t, err)
// make sure we clean up when we're done
defer client.UnexportByID(defaultCtx, exportID)
defer client.DeleteVolume(defaultCtx, volumeName)
// verify we aren't already exporting the volume to any of the clients
export, err = client.GetExportByID(defaultCtx, exportID)
assertNoError(t, err)
assertNotNil(t, export)
for _, currentClient := range getClients(defaultCtx, export) {
for _, newClient := range clientList {
assert.NotEqual(t, currentClient, newClient)
}
}
// test setting the export client
err = setClients(defaultCtx, exportID, clientList...)
assertNoError(t, err)
// verify the export client was set
export, err = client.GetExportByID(defaultCtx, exportID)
assertNoError(t, err)
assertNotNil(t, export)
currentClients = getClients(defaultCtx, export)
assert.Equal(t, len(clientList), len(currentClients))
sort.Strings(currentClients)
for i := range currentClients {
assert.Equal(t, currentClients[i], clientList[i])
}
}
func testExportClientsAdd(
t *testing.T,
volumeName string,
getClients func(ctx context.Context, e Export) []string,
setClients func(ctx context.Context, id int, clients ...string) error,
addClients func(ctx context.Context, id int, clients ...string) error) {
var (
err error
export Export
exportID int
currentClients []string
clientList = []string{"1.2.3.4", "1.2.3.5"}
addedClients = []string{"1.2.3.6", "1.2.3.7"}
allClients = append(clientList, addedClients...)
)
sort.Strings(clientList)
sort.Strings(allClients)
// initialize the export
_, err = client.CreateVolume(defaultCtx, volumeName)
assertNoError(t, err)
exportID, err = client.Export(defaultCtx, volumeName)
assertNoError(t, err)
// make sure we clean up when we're done
defer client.UnexportByID(defaultCtx, exportID)
defer client.DeleteVolume(defaultCtx, volumeName)
// verify we aren't already exporting the volume to any of the clients
export, err = client.GetExportByID(defaultCtx, exportID)
assertNoError(t, err)
assertNotNil(t, export)
for _, currentClient := range getClients(defaultCtx, export) {
for _, newClient := range clientList {
assert.NotEqual(t, currentClient, newClient)
}
}
// test setting the export client
err = setClients(defaultCtx, exportID, clientList...)
assertNoError(t, err)
export, err = client.GetExportByID(defaultCtx, exportID)
assertNoError(t, err)
assertNotNil(t, export)
currentClients = getClients(defaultCtx, export)
assert.Equal(t, len(clientList), len(currentClients))
sort.Strings(currentClients)
for i := range currentClients {
assert.Equal(t, currentClients[i], clientList[i])
}
// verify that added clients are added to the list
err = addClients(defaultCtx, exportID, addedClients...)
assertNoError(t, err)
export, err = client.GetExportByID(defaultCtx, exportID)
assertNoError(t, err)
assertNotNil(t, export)
currentClients = getClients(defaultCtx, export)
assert.Equal(t, len(allClients), len(currentClients))
sort.Strings(currentClients)
for i := range currentClients {
assert.Equal(t, currentClients[i], allClients[i])
}
}
func testExportClientsClear(
t *testing.T,
volumeName string,
getClients func(ctx context.Context, e Export) []string,
setClients func(ctx context.Context, id int, clients ...string) error,
nilClients func(ctx context.Context, id int) error) {
var (
err error
export Export
exportID int
currentClients []string
clientList = []string{"1.2.3.4", "1.2.3.5"}
)
sort.Strings(clientList)
// initialize the export
_, err = client.CreateVolume(defaultCtx, volumeName)
assertNoError(t, err)
exportID, err = client.Export(defaultCtx, volumeName)
assertNoError(t, err)
// make sure we clean up when we're done
defer client.UnexportByID(defaultCtx, exportID)
defer client.DeleteVolume(defaultCtx, volumeName)
// verify we are exporting the volume
err = setClients(defaultCtx, exportID, clientList...)
assertNoError(t, err)
export, err = client.GetExportByID(defaultCtx, exportID)
assertNoError(t, err)
assertNotNil(t, export)
currentClients = getClients(defaultCtx, export)
assert.Equal(t, len(clientList), len(currentClients))
sort.Strings(currentClients)
for i := range currentClients {
assert.Equal(t, currentClients[i], clientList[i])
}
// test clearing the export client
err = nilClients(defaultCtx, exportID)
assertNoError(t, err)
// verify the export client was cleared
export, err = client.GetExportByID(defaultCtx, exportID)
assertNoError(t, err)
assertNotNil(t, export)
assert.Len(t, getClients(defaultCtx, export), 0)
}