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 Apr 24, 2024
1 parent 2a2aef8 commit a9eed0f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 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
22 changes: 14 additions & 8 deletions simulator/virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1183,23 +1183,29 @@ func getDiskSize(disk *types.VirtualDisk) int64 {
}

func changedDiskSize(oldDisk *types.VirtualDisk, newDiskSpec *types.VirtualDisk) (int64, bool) {
// 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
}
}

// 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
}

// NOTE: capacity is ignored if specified value is same as before
// capacity is ignored if specified value is same as before
if newDiskSpec.CapacityInBytes == oldDisk.CapacityInBytes {
return newDiskSpec.CapacityInKB * 1024, true
return newDiskSpec.CapacityInBytes, true
}
if newDiskSpec.CapacityInKB == oldDisk.CapacityInKB {
return newDiskSpec.CapacityInBytes, true
return newDiskSpec.CapacityInKB * 1024, true
}

// CapacityInBytes and CapacityInKB indicate different values
if newDiskSpec.CapacityInBytes != newDiskSpec.CapacityInKB*1024 {
return 0, false
}
return newDiskSpec.CapacityInBytes, true
}

Expand Down

0 comments on commit a9eed0f

Please sign in to comment.