Skip to content

Commit

Permalink
vcsim: fix ReconfigVM validation when changing disk size
Browse files Browse the repository at this point in the history
CapacityInBytes and the deprecated CapacityInKB fields do not both need to be set.

Closes: #3423
  • Loading branch information
davinderkvnera authored and dougm committed May 7, 2024
1 parent ce7ef70 commit e6fe159
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
9 changes: 8 additions & 1 deletion govc/test/vm.bats
Original file line number Diff line number Diff line change
Expand Up @@ -686,8 +686,15 @@ load test_helper

run govc vm.disk.create -vm "$vm" -name "$vm/$name" -size 1M
assert_success
result=$(govc device.ls -vm "$vm" | grep -c disk-)
disk=$(govc device.ls -vm "$vm" disk-* | awk '{print $1}')
result=$(grep -c disk- <<<"$disk")
[ "$result" -eq 1 ]

run govc vm.disk.change -vm "$vm" -disk.name "$disk" -size 2M
assert_success

run govc vm.disk.change -vm "$vm" -disk.name "$disk" -size 1M
assert_failure # cannot shrink disk
}

@test "vm.disk.attach" {
Expand Down
6 changes: 5 additions & 1 deletion govc/vm/disk/change.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,13 @@ func (cmd *change) Run(ctx context.Context, f *flag.FlagSet) error {
}

if int64(cmd.bytes) != 0 {
editdisk.CapacityInKB = int64(cmd.bytes) / 1024
editdisk.CapacityInBytes = int64(cmd.bytes)
editdisk.CapacityInKB = int64(0) // zero deprecated field
}

if editdisk.StorageIOAllocation == nil {
editdisk.StorageIOAllocation = new(types.StorageIOAllocationInfo)
}
editdisk.StorageIOAllocation.Limit = cmd.limit

switch backing := editdisk.Backing.(type) {
Expand Down
14 changes: 10 additions & 4 deletions simulator/virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,10 @@ func getDiskSize(disk *types.VirtualDisk) int64 {

func changedDiskSize(oldDisk *types.VirtualDisk, newDiskSpec *types.VirtualDisk) (int64, bool) {
// capacity cannot be decreased
if newDiskSpec.CapacityInBytes < oldDisk.CapacityInBytes || newDiskSpec.CapacityInKB < oldDisk.CapacityInKB {
if newDiskSpec.CapacityInBytes > 0 && newDiskSpec.CapacityInBytes < oldDisk.CapacityInBytes {
return 0, false
}
if newDiskSpec.CapacityInKB > 0 && newDiskSpec.CapacityInKB < oldDisk.CapacityInKB {
return 0, false
}

Expand All @@ -1196,10 +1199,13 @@ func changedDiskSize(oldDisk *types.VirtualDisk, newDiskSpec *types.VirtualDisk)
return newDiskSpec.CapacityInBytes, true
}

// CapacityInBytes and CapacityInKB indicate different values
if newDiskSpec.CapacityInBytes != newDiskSpec.CapacityInKB*1024 {
return 0, false
// if both set, CapacityInBytes and CapacityInKB must be the same
if newDiskSpec.CapacityInBytes > 0 && newDiskSpec.CapacityInKB > 0 {
if newDiskSpec.CapacityInBytes != newDiskSpec.CapacityInKB*1024 {
return 0, false
}
}

return newDiskSpec.CapacityInBytes, true
}

Expand Down

0 comments on commit e6fe159

Please sign in to comment.