-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsnapshots_test.go
551 lines (488 loc) · 17.5 KB
/
snapshots_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
/*
Copyright (c) 2019 Dell Inc, or its subsidiaries.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package goisilon
import (
"fmt"
"os"
"testing"
apiv1 "github.com/dell/goisilon/api/v1"
)
func TestSnapshotsGet(_ *testing.T) {
snapshotPath := "test_snapshots_get_volume"
snapshotName1 := "test_snapshots_get_snapshot_0"
snapshotName2 := "test_snapshots_get_snapshot_1"
// create the test volume
_, err := client.CreateVolume(defaultCtx, snapshotPath)
if err != nil {
panic(err)
}
defer client.DeleteVolume(defaultCtx, snapshotPath)
// identify all snapshots on the cluster
snapshotMap := make(map[int64]string)
snapshots, err := client.GetSnapshots(defaultCtx)
if err != nil {
panic(err)
}
for _, snapshot := range snapshots {
snapshotMap[snapshot.ID] = snapshot.Name
}
initialSnapshotCount := len(snapshots)
// Add the test snapshots
testSnapshot1, err := client.CreateSnapshot(
defaultCtx, snapshotPath, snapshotName1)
if err != nil {
panic(err)
}
testSnapshot2, err := client.CreateSnapshot(
defaultCtx, snapshotPath, snapshotName2)
if err != nil {
panic(err)
}
// make sure we clean up when we're done
defer client.RemoveSnapshot(defaultCtx, testSnapshot1.ID, snapshotName1)
defer client.RemoveSnapshot(defaultCtx, testSnapshot2.ID, snapshotName2)
// get the updated snapshot list
snapshots, err = client.GetSnapshots(defaultCtx)
if err != nil {
panic(err)
}
// verify that the new snapshots are there as well as all the old snapshots.
if len(snapshots) != initialSnapshotCount+2 {
panic(fmt.Sprintf("Incorrect number of snapshots. Expected: %d Actual: %d\n", initialSnapshotCount+2, len(snapshots)))
}
// remove the original snapshots and add the new ones. in the end, we
// should only have the snapshots we just created and nothing more.
for _, snapshot := range snapshots {
if _, found := snapshotMap[snapshot.ID]; found == true {
// this snapshot existed prior to the test start
delete(snapshotMap, snapshot.ID)
} else {
// this snapshot is new
snapshotMap[snapshot.ID] = snapshot.Name
}
}
if len(snapshotMap) != 2 {
panic(fmt.Sprintf("Incorrect number of new snapshots. Expected: 2 Actual: %d\n", len(snapshotMap)))
}
if _, found := snapshotMap[testSnapshot1.ID]; found == false {
panic("testSnapshot1 was not in the snapshot list\n")
}
if _, found := snapshotMap[testSnapshot2.ID]; found == false {
panic("testSnapshot2 was not in the snapshot list\n")
}
}
func TestSnapshotsGetByPath(_ *testing.T) {
snapshotPath1 := "test_snapshots_get_by_path_volume_1"
snapshotPath2 := "test_snapshots_get_by_path_volume_2"
snapshotName1 := "test_snapshots_get_by_path_snapshot_1"
snapshotName2 := "test_snapshots_get_by_path_snapshot_2"
snapshotName3 := "test_snapshots_get_by_path_snapshot_3"
// create the two test volumes
_, err := client.CreateVolume(defaultCtx, snapshotPath1)
if err != nil {
panic(err)
}
defer client.DeleteVolume(defaultCtx, snapshotPath1)
_, err = client.CreateVolume(defaultCtx, snapshotPath2)
if err != nil {
panic(err)
}
defer client.DeleteVolume(defaultCtx, snapshotPath2)
// identify all snapshots on the cluster
snapshotMap := make(map[int64]string)
snapshots, err := client.GetSnapshotsByPath(defaultCtx, snapshotPath1)
if err != nil {
panic(err)
}
for _, snapshot := range snapshots {
snapshotMap[snapshot.ID] = snapshot.Name
}
initialSnapshotCount := len(snapshots)
// Add the test snapshots
testSnapshot1, err := client.CreateSnapshot(
defaultCtx, snapshotPath1, snapshotName1)
if err != nil {
panic(err)
}
testSnapshot2, err := client.CreateSnapshot(
defaultCtx, snapshotPath2, snapshotName2)
if err != nil {
panic(err)
}
testSnapshot3, err := client.CreateSnapshot(
defaultCtx, snapshotPath1, snapshotName3)
if err != nil {
panic(err)
}
// make sure we clean up when we're done
defer client.RemoveSnapshot(defaultCtx, testSnapshot1.ID, snapshotName1)
defer client.RemoveSnapshot(defaultCtx, testSnapshot2.ID, snapshotName2)
defer client.RemoveSnapshot(defaultCtx, testSnapshot3.ID, snapshotName3)
// get the updated snapshot list
snapshots, err = client.GetSnapshotsByPath(defaultCtx, snapshotPath1)
if err != nil {
panic(err)
}
// verify that the new snapshots in the given path are there as well
// as all the old snapshots in that path.
if len(snapshots) != initialSnapshotCount+2 {
panic(fmt.Sprintf("Incorrect number of snapshots for path (%s). Expected: %d Actual: %d\n", snapshotPath1, initialSnapshotCount+2, len(snapshots)))
}
// remove the original snapshots and add the new ones. in the end, we
// should only have the snapshots we just created and nothing more.
for _, snapshot := range snapshots {
if _, found := snapshotMap[snapshot.ID]; found == true {
// this snapshot existed prior to the test start
delete(snapshotMap, snapshot.ID)
} else {
// this snapshot is new
snapshotMap[snapshot.ID] = snapshot.Name
}
}
if len(snapshotMap) != 2 {
panic(fmt.Sprintf("Incorrect number of new snapshots. Expected: 2 Actual: %d\n", len(snapshotMap)))
}
if _, found := snapshotMap[testSnapshot1.ID]; found == false {
panic("testSnapshot1 was not in the snapshot list\n")
}
if _, found := snapshotMap[testSnapshot3.ID]; found == false {
panic("testSnapshot3 was not in the snapshot list\n")
}
}
func TestSnapshotCreate(_ *testing.T) {
snapshotPath := "test_snapshot_create_volume"
snapshotName := "test_snapshot_create_snapshot"
// create the test volume
_, err := client.CreateVolume(defaultCtx, snapshotPath)
if err != nil {
panic(err)
}
defer client.DeleteVolume(defaultCtx, snapshotPath)
// make sure the snapshot doesn't exist yet
snapshot, err := client.GetSnapshot(defaultCtx, -1, snapshotName)
if err == nil && snapshot != nil {
panic(fmt.Sprintf("Snapshot (%s) already exists.\n", snapshotName))
}
// Add the test snapshot
testSnapshot, err := client.CreateSnapshot(
defaultCtx, snapshotPath, snapshotName)
if err != nil {
panic(err)
}
// make sure we clean up when we're done
defer client.RemoveSnapshot(defaultCtx, testSnapshot.ID, snapshotName)
// get the updated snapshot list
snapshot, err = client.GetSnapshot(
defaultCtx, testSnapshot.ID, snapshotName)
if err != nil {
panic(err)
}
if snapshot == nil {
panic(fmt.Sprintf("Snapshot (%s) was not created.\n", snapshotName))
}
if snapshot.Name != snapshotName {
panic(fmt.Sprintf("Snapshot name not set properly. Expected: (%s) Actual: (%s)\n", snapshotName, snapshot.Name))
}
if snapshot.Path != client.API.VolumePath(snapshotPath) {
panic(fmt.Sprintf("Snapshot path not set properly. Expected: (%s) Actual: (%s)\n", snapshotPath, snapshot.Path))
}
}
func TestSnapshotRemove(_ *testing.T) {
snapshotPath := "test_snapshot_remove_volume"
snapshotName := "test_snapshot_remove_snapshot"
// create the test volume
_, err := client.CreateVolume(defaultCtx, snapshotPath)
if err != nil {
panic(err)
}
defer client.DeleteVolume(defaultCtx, snapshotPath)
// make sure the snapshot exists
client.CreateSnapshot(defaultCtx, snapshotPath, snapshotName)
snapshot, err := client.GetSnapshot(defaultCtx, -1, snapshotName)
if err != nil {
panic(err)
}
if snapshot == nil {
panic(fmt.Sprintf("Test not setup properly. No test snapshot (%s).", snapshotName))
}
// remove the snapshot
err = client.RemoveSnapshot(defaultCtx, snapshot.ID, snapshotName)
if err != nil {
panic(err)
}
// make sure the snapshot was removed
snapshot, err = client.GetSnapshot(defaultCtx, snapshot.ID, snapshotName)
if err != nil {
panic(err)
}
if snapshot != nil {
panic(fmt.Sprintf("Snapshot (%s) was not removed.\n%+v\n", snapshotName, snapshot))
}
}
func TestSnapshotCopy(_ *testing.T) {
accessZone := "System"
sourceSnapshotPath := "test_snapshot_copy_src_volume"
sourceSnapshotName := "test_snapshot_copy_src_snapshot"
destinationVolume := "test_snapshot_copy_dst_volume"
subdirectoryName := "test_snapshot_copy_sub_dir"
sourceSubDirectory := fmt.Sprintf("%s/%s", sourceSnapshotPath, subdirectoryName)
destinationSubDirectory := fmt.Sprintf("%s/%s", destinationVolume, subdirectoryName)
// create the test volume
_, err := client.CreateVolume(defaultCtx, sourceSnapshotPath)
if err != nil {
panic(err)
}
// defer client.DeleteVolume(snapshotPath)
// create a subdirectory in the test tvolume
_, err = client.CreateVolume(defaultCtx, sourceSubDirectory)
if err != nil {
panic(err)
}
// make sure the snapshot doesn't exist yet
snapshot, err := client.GetSnapshot(defaultCtx, -1, sourceSnapshotName)
if err == nil && snapshot != nil {
panic(fmt.Sprintf("Snapshot (%s) already exists.\n", sourceSnapshotName))
}
// Add the test snapshot
testSnapshot, err := client.CreateSnapshot(
defaultCtx, sourceSnapshotPath, sourceSnapshotName)
if err != nil {
panic(err)
}
// make sure we clean up when we're done
defer client.RemoveSnapshot(defaultCtx, testSnapshot.ID, sourceSnapshotName)
// remove the sub directory
err = client.DeleteVolume(defaultCtx, sourceSubDirectory)
if err != nil {
panic(err)
}
// copy the snapshot to the destination volume
_, err = client.CreateVolume(defaultCtx, destinationVolume)
if err != nil {
panic(err)
}
copiedVolume, err := client.CopySnapshot(
defaultCtx, testSnapshot.ID, testSnapshot.Name, accessZone, destinationVolume)
if err != nil {
panic(err)
}
defer client.DeleteVolume(defaultCtx, destinationVolume)
if copiedVolume.Name != destinationVolume {
panic(fmt.Sprintf("Copied volume has incorrect name. Expected: (%s) Acutal: (%s)", destinationVolume, copiedVolume.Name))
}
// make sure the destination volume was created
volume, err := client.GetVolume(defaultCtx, "", destinationVolume)
if err != nil || volume == nil {
panic(fmt.Sprintf("Destination volume: (%s) was not created.\n", destinationVolume))
}
// make sure the sub directory was also created
subDirectory, err := client.GetVolume(defaultCtx, "", destinationSubDirectory)
if err != nil {
panic(fmt.Sprintf("Destination sub directory: (%s) was not created.\n", subdirectoryName))
}
if subDirectory.Name != destinationSubDirectory {
panic(fmt.Sprintf("Sub directory has incorrect name. Expected: (%s) Acutal: (%s)", destinationSubDirectory, subDirectory.Name))
}
}
func TestSnapshotCopyWithIsiPath(_ *testing.T) {
sourceSnapshotPath := "test_snapshot_copy_src_volume"
sourceSnapshotName := "test_snapshot_copy_src_snapshot"
destinationVolume := "test_snapshot_copy_dst_volume"
subdirectoryName := "test_snapshot_copy_sub_dir"
defaultAccessZone := "System"
sourceSubDirectory := fmt.Sprintf("%s/%s", sourceSnapshotPath, subdirectoryName)
destinationSubDirectory := fmt.Sprintf("%s/%s", destinationVolume, subdirectoryName)
// create the test volume
_, err := client.CreateVolume(defaultCtx, sourceSnapshotPath)
if err != nil {
panic(err)
}
// defer client.DeleteVolume(snapshotPath)
// create a subdirectory in the test tvolume
_, err = client.CreateVolume(defaultCtx, sourceSubDirectory)
if err != nil {
panic(err)
}
// make sure the snapshot doesn't exist yet
snapshot, err := client.GetSnapshot(defaultCtx, -1, sourceSnapshotName)
if err == nil && snapshot != nil {
panic(fmt.Sprintf("Snapshot (%s) already exists.\n", sourceSnapshotName))
}
// Add the test snapshot
testSnapshot, err := client.CreateSnapshot(
defaultCtx, sourceSnapshotPath, sourceSnapshotName)
if err != nil {
panic(err)
}
// make sure we clean up when we're done
defer client.RemoveSnapshot(defaultCtx, testSnapshot.ID, sourceSnapshotName)
// remove the sub directory
err = client.DeleteVolume(defaultCtx, sourceSubDirectory)
if err != nil {
panic(err)
}
// copy the snapshot to the destination volume
_, err = client.CreateVolume(defaultCtx, destinationVolume)
if err != nil {
panic(err)
}
newIsiPath := os.Getenv("GOISILON_VOLUMEPATH")
copiedVolume, err := client.CopySnapshotWithIsiPath(
defaultCtx, newIsiPath, newIsiPath, testSnapshot.ID, testSnapshot.Name, destinationVolume, defaultAccessZone)
if err != nil {
panic(err)
}
defer client.DeleteVolume(defaultCtx, destinationVolume)
if copiedVolume.Name != destinationVolume {
panic(fmt.Sprintf("Copied volume has incorrect name. Expected: (%s) Acutal: (%s)", destinationVolume, copiedVolume.Name))
}
// make sure the destination volume was created
volume, err := client.GetVolume(defaultCtx, "", destinationVolume)
if err != nil || volume == nil {
panic(fmt.Sprintf("Destination volume: (%s) was not created.\n", destinationVolume))
}
// make sure the sub directory was also created
subDirectory, err := client.GetVolume(defaultCtx, "", destinationSubDirectory)
if err != nil {
panic(fmt.Sprintf("Destination sub directory: (%s) was not created.\n", subdirectoryName))
}
if subDirectory.Name != destinationSubDirectory {
panic(fmt.Sprintf("Sub directory has incorrect name. Expected: (%s) Acutal: (%s)", destinationSubDirectory, subDirectory.Name))
}
}
func TestSnapshotGetByIdentity(_ *testing.T) {
snapshotPath := "test_snapshots_get_volume"
snapshotName1 := "test_snapshots_get_snapshot_0"
snapshotName2 := "test_snapshots_get_snapshot_1"
// create the test volume
_, err := client.CreateVolume(defaultCtx, snapshotPath)
if err != nil {
panic(err)
}
defer client.DeleteVolume(defaultCtx, snapshotPath)
// Add the test snapshots
testSnapshot1, err := client.CreateSnapshot(
defaultCtx, snapshotPath, snapshotName1)
if err != nil {
panic(err)
}
testSnapshot2, err := client.CreateSnapshot(
defaultCtx, snapshotPath, snapshotName2)
if err != nil {
panic(err)
}
// make sure we clean up when we're done
defer client.RemoveSnapshot(defaultCtx, testSnapshot1.ID, snapshotName1)
defer client.RemoveSnapshot(defaultCtx, testSnapshot2.ID, snapshotName2)
snapshot1, err := client.GetIsiSnapshotByIdentity(defaultCtx, snapshotName1)
if err != nil {
panic("failed to get testSnapshot1\n")
}
if snapshot1.ID != testSnapshot1.ID {
panic(fmt.Sprintf("testSnapshot1: id %d is incorrect\n", snapshot1.ID))
}
snapshot2, err := client.GetIsiSnapshotByIdentity(defaultCtx, snapshotName2)
if err != nil {
panic("failed to get testSnapshot2\n")
}
if snapshot2.ID != testSnapshot2.ID {
panic(fmt.Sprintf("testSnapshot2: id %d is incorrect\n", snapshot2.ID))
}
}
func TestSnapshotIsExistent(_ *testing.T) {
snapshotPath := "test_snapshots_exist_volume"
snapshotName1 := "test_snapshots_exist_snapshot_0"
// create the test volume
_, err := client.CreateVolume(defaultCtx, snapshotPath)
if err != nil {
panic(err)
}
defer client.DeleteVolume(defaultCtx, snapshotPath)
// check if snapshotName1 exists
if client.IsSnapshotExistent(defaultCtx, snapshotName1) {
panic(fmt.Sprintf("found snapshot %s, expected not found\n", snapshotName1))
}
// Add the test snapshots
testSnapshot1, err := client.CreateSnapshot(
defaultCtx, snapshotPath, snapshotName1)
if err != nil {
panic(err)
}
// make sure we clean up when we're done
defer client.RemoveSnapshot(defaultCtx, testSnapshot1.ID, snapshotName1)
// check if snapshotName1 exists
if !client.IsSnapshotExistent(defaultCtx, snapshotName1) {
panic(fmt.Sprintf("not found snapshot %s, expected found\n", snapshotName1))
}
}
func TestSnapshotExportWithZone(_ *testing.T) {
snapshotPath := "test_snapshots_export_volume"
snapshotName1 := "test_snapshots_export_snapshot_0"
defaultAccessZone := "System"
// create the test volume
_, err := client.CreateVolume(defaultCtx, snapshotPath)
if err != nil {
panic(err)
}
defer client.DeleteVolume(defaultCtx, snapshotPath)
// Add the test snapshots
testSnapshot1, err := client.CreateSnapshot(
defaultCtx, snapshotPath, snapshotName1)
if err != nil {
panic(err)
}
// make sure we clean up when we're done
defer client.RemoveSnapshot(defaultCtx, testSnapshot1.ID, snapshotName1)
// export snapshot
id, err := client.ExportSnapshotWithZone(defaultCtx, snapshotName1, snapshotPath, defaultAccessZone, "")
if err != nil {
panic(fmt.Sprintf("failed to export snapshot, name %s path %s \n", snapshotName1, snapshotPath))
}
// unexport snapshot
defer client.UnexportByIDWithZone(defaultCtx, id, defaultAccessZone)
}
func TestGetRealVolumeSnapshotPathWithIsiPath(_ *testing.T) {
volName := "volFromSnap0"
newIsiPath := os.Getenv("GOISILON_VOLUMEPATH")
accessZone := "System"
name := "snapshottest"
fmt.Println(apiv1.GetRealVolumeSnapshotPathWithIsiPath(newIsiPath, volName, name, accessZone))
}
func TestSnapshotSizeGet(_ *testing.T) {
snapshotPath := "test_snapshots_get_volume"
snapshotName1 := "test_snapshots_get_snapshot_0"
accessZone := "System"
// create the test volume
_, err := client.CreateVolume(defaultCtx, snapshotPath)
if err != nil {
panic(err)
}
defer client.DeleteVolume(defaultCtx, snapshotPath)
// Add the test snapshots
testSnapshot1, err := client.CreateSnapshot(
defaultCtx, snapshotPath, snapshotName1)
if err != nil {
panic(err)
}
// make sure we clean up when we're done
defer client.RemoveSnapshot(defaultCtx, testSnapshot1.ID, snapshotName1)
newIsiPath := os.Getenv("GOISILON_VOLUMEPATH")
// get the updated snapshot list
totalSize, err := client.GetSnapshotFolderSize(defaultCtx, newIsiPath, snapshotName1, accessZone)
if err != nil {
panic(err)
}
if totalSize < 0 {
panic(fmt.Sprintf("Snapshot folder size %d is not correct.\n", totalSize))
}
}