@@ -28,14 +28,15 @@ import (
28
28
func TestGetVirtualDiskInfoByUUID (t * testing.T ) {
29
29
30
30
type testCase struct {
31
- name string
32
- ctx context.Context
33
- client * vim25.Client
34
- mo mo.VirtualMachine
35
- fetchProperties bool
36
- diskUUID string
37
- diskInfo vmdk.VirtualDiskInfo
38
- err string
31
+ name string
32
+ ctx context.Context
33
+ client * vim25.Client
34
+ mo mo.VirtualMachine
35
+ fetchProperties bool
36
+ diskUUID string
37
+ diskInfo vmdk.VirtualDiskInfo
38
+ excludeSnapshots bool
39
+ err string
39
40
}
40
41
41
42
t .Run ("w cached properties" , func (t * testing.T ) {
@@ -386,18 +387,146 @@ func TestGetVirtualDiskInfoByUUID(t *testing.T) {
386
387
UniqueSize : (5 * 1024 * 1024 ) + 100 + 100 + 200 + 300 ,
387
388
},
388
389
},
390
+ {
391
+ name : "one disk w/o chain entries, it should return 0 for size and unique size" ,
392
+ mo : mo.VirtualMachine {
393
+ Config : & types.VirtualMachineConfigInfo {
394
+ Hardware : types.VirtualHardware {
395
+ Device : []types.BaseVirtualDevice {
396
+ getDisk (& types.VirtualDiskFlatVer2BackingInfo {
397
+ VirtualDeviceFileBackingInfo : types.VirtualDeviceFileBackingInfo {
398
+ FileName : fileName ,
399
+ },
400
+ Uuid : diskUUID ,
401
+ }),
402
+ },
403
+ },
404
+ },
405
+ LayoutEx : & types.VirtualMachineFileLayoutEx {
406
+ Disk : []types.VirtualMachineFileLayoutExDiskLayout {
407
+ {
408
+ Key : deviceKey ,
409
+ Chain : []types.VirtualMachineFileLayoutExDiskUnit {},
410
+ },
411
+ },
412
+ File : []types.VirtualMachineFileLayoutExFileInfo {
413
+ {
414
+ Key : 1 ,
415
+ Size : 100 ,
416
+ UniqueSize : 100 ,
417
+ },
418
+ },
419
+ },
420
+ },
421
+ diskUUID : diskUUID ,
422
+ diskInfo : vmdk.VirtualDiskInfo {
423
+ CapacityInBytes : tenGiBInBytes ,
424
+ DeviceKey : deviceKey ,
425
+ FileName : fileName ,
426
+ Size : 0 ,
427
+ UniqueSize : 0 ,
428
+ },
429
+ },
430
+ {
431
+ name : "one disk w multiple chain entries and includeSnapshots is false, it should exclude the snapshot delta disks, only the size offile keys (8, 9) should be included" ,
432
+ mo : mo.VirtualMachine {
433
+ Config : & types.VirtualMachineConfigInfo {
434
+ Hardware : types.VirtualHardware {
435
+ Device : []types.BaseVirtualDevice {
436
+ getDisk (& types.VirtualDiskFlatVer2BackingInfo {
437
+ VirtualDeviceFileBackingInfo : types.VirtualDeviceFileBackingInfo {
438
+ FileName : fileName ,
439
+ },
440
+ Uuid : diskUUID ,
441
+ }),
442
+ },
443
+ },
444
+ },
445
+ LayoutEx : & types.VirtualMachineFileLayoutEx {
446
+ Disk : []types.VirtualMachineFileLayoutExDiskLayout {
447
+ {
448
+ Key : deviceKey ,
449
+ Chain : []types.VirtualMachineFileLayoutExDiskUnit {
450
+ {
451
+ FileKey : []int32 {
452
+ 4 ,
453
+ 5 ,
454
+ },
455
+ },
456
+ {
457
+ FileKey : []int32 {
458
+ 6 ,
459
+ 7 ,
460
+ },
461
+ },
462
+ {
463
+ FileKey : []int32 {
464
+ 8 ,
465
+ 9 ,
466
+ },
467
+ },
468
+ },
469
+ },
470
+ },
471
+ File : []types.VirtualMachineFileLayoutExFileInfo {
472
+ {
473
+ Key : 4 ,
474
+ Size : 1 * 1024 * 1024 * 1024 , // 1 GiB
475
+ UniqueSize : 5 * 1024 * 1024 , // 500 MiB
476
+ },
477
+ {
478
+ Key : 5 ,
479
+ Size : 950 ,
480
+ UniqueSize : 100 ,
481
+ },
482
+ {
483
+ Key : 6 ,
484
+ Size : 500 ,
485
+ UniqueSize : 100 ,
486
+ },
487
+ {
488
+ Key : 7 ,
489
+ Size : 500 ,
490
+ UniqueSize : 200 ,
491
+ },
492
+ {
493
+ Key : 8 ,
494
+ Size : 1000 ,
495
+ UniqueSize : 300 ,
496
+ },
497
+ {
498
+ Key : 9 ,
499
+ Size : 1000 ,
500
+ UniqueSize : 300 ,
501
+ },
502
+ },
503
+ },
504
+ },
505
+ excludeSnapshots : true ,
506
+ diskUUID : diskUUID ,
507
+ diskInfo : vmdk.VirtualDiskInfo {
508
+ CapacityInBytes : tenGiBInBytes ,
509
+ DeviceKey : deviceKey ,
510
+ FileName : fileName ,
511
+ Size : 1000 + 1000 ,
512
+ UniqueSize : 300 + 300 ,
513
+ },
514
+ },
389
515
}
390
516
391
517
for i := range testCases {
392
518
tc := testCases [i ]
393
519
t .Run (tc .name , func (t * testing.T ) {
394
520
var ctx context.Context
395
521
dii , err := vmdk .GetVirtualDiskInfoByUUID (
396
- ctx , nil , tc .mo , false , tc .diskUUID )
522
+ ctx , nil , tc .mo , false , tc .excludeSnapshots , tc . diskUUID )
397
523
398
524
if tc .err != "" {
399
525
assert .EqualError (t , err , tc .err )
526
+ } else {
527
+ assert .NoError (t , err )
400
528
}
529
+
401
530
assert .Equal (t , tc .diskInfo , dii )
402
531
})
403
532
}
@@ -416,6 +545,9 @@ func TestGetVirtualDiskInfoByUUID(t *testing.T) {
416
545
}
417
546
finder .SetDatacenter (datacenter )
418
547
vmList , err := finder .VirtualMachineList (ctx , "*" )
548
+ if err != nil {
549
+ t .Fatalf ("failed to get vm list: %s" , err )
550
+ }
419
551
if len (vmList ) == 0 {
420
552
t .Fatal ("vmList == 0" )
421
553
}
@@ -574,10 +706,13 @@ func TestGetVirtualDiskInfoByUUID(t *testing.T) {
574
706
tc .client ,
575
707
tc .mo ,
576
708
tc .fetchProperties ,
709
+ tc .excludeSnapshots ,
577
710
tc .diskUUID )
578
711
579
712
if tc .err != "" {
580
713
assert .EqualError (t , err , tc .err )
714
+ } else {
715
+ assert .NoError (t , err )
581
716
}
582
717
assert .Equal (t , tc .diskInfo , dii )
583
718
})
0 commit comments