Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vcsim: Fix disk capacity fields in ReconfigVM_Task #2890

Merged
merged 1 commit into from Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions simulator/virtual_machine.go
Expand Up @@ -1184,6 +1184,10 @@ func (vm *VirtualMachine) configureDevice(ctx *Context, devices object.VirtualDe
}
}
case *types.VirtualDisk:
// NOTE: either of capacityInBytes and capacityInKB may not be specified
x.CapacityInBytes = getDiskSize(x)
x.CapacityInKB = getDiskSize(x) / 1024

summary = fmt.Sprintf("%s KB", numberToString(x.CapacityInKB, ','))
switch b := d.Backing.(type) {
case types.BaseVirtualDeviceFileBackingInfo:
Expand Down
94 changes: 94 additions & 0 deletions simulator/virtual_machine_test.go
Expand Up @@ -439,6 +439,7 @@ func TestReconfigVmDevice(t *testing.T) {

// Need FileOperation=="" to add an existing disk, see object.VirtualMachine.configureDevice
disk.CapacityInKB = 0
disk.CapacityInBytes = 0
if err = vm.AddDevice(ctx, d); err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -1208,6 +1209,99 @@ func TestCreateVmWithDevices(t *testing.T) {
}
}

func TestReconfigureDevicesCapacity(t *testing.T) {
tests := []struct {
name string
capacityInBytes int64
capacityInKB int64
expectedCapacityInBytes int64
expectedCapacityInKB int64
}{
{
"specify capacityInBytes",
512 * 1024,
0,
512 * 1024,
512,
},
{
"specify capacityInKB",
0,
512,
512 * 1024,
512,
},
{
"specify both",
512 * 1024,
512,
512 * 1024,
512,
},
{
"capacityInbytes takes precedence if two fields represents different capacity",
512 * 1024,
1024,
512 * 1024,
512,
},
}

for _, test := range tests {
test := test // assign to local var since loop var is reused
t.Run(test.name, func(t *testing.T) {
m := ESX()

Test(func(ctx context.Context, c *vim25.Client) {
vmm := Map.Any("VirtualMachine").(*VirtualMachine)
vm := object.NewVirtualMachine(c, vmm.Reference())

ds := Map.Any("Datastore").(*Datastore)

devices, err := vm.Device(ctx)
if err != nil {
t.Fatal(err)
}

controller, err := devices.FindDiskController("")
if err != nil {
t.Fatal(err)
}

disk := devices.CreateDisk(controller, ds.Reference(), "")
disk.CapacityInBytes = test.capacityInBytes
disk.CapacityInKB = test.capacityInKB

err = vm.AddDevice(ctx, disk)
if err != nil {
t.Fatal(err)
}

newDevices, err := vm.Device(ctx)
if err != nil {
t.Fatal(err)
}
disks := newDevices.SelectByType((*types.VirtualDisk)(nil))
if len(disks) == 0 {
t.Fatalf("len(disks)=%d", len(disks))
}

newDisk := disks[len(disks)-1].(*types.VirtualDisk)

if newDisk.CapacityInBytes != test.expectedCapacityInBytes {
t.Errorf("CapacityInBytes expected %d, got %d",
test.expectedCapacityInBytes, newDisk.CapacityInBytes)
}
if newDisk.CapacityInKB != test.expectedCapacityInKB {
t.Errorf("CapacityInKB expected %d, got %d",
test.expectedCapacityInKB, newDisk.CapacityInKB)
}

}, m)
})
}
}

func TestShutdownGuest(t *testing.T) {
// use the default vm for testing
ctx := context.Background()
Expand Down