Skip to content

Commit

Permalink
vcsim: check if VM host InMaintenanceMode
Browse files Browse the repository at this point in the history
If a VM's host is InMaintenanceMode, certain tasks should return InvalidState,
such as CustomizeVM and PowerOnVM.
  • Loading branch information
dougm committed Aug 30, 2021
1 parent bd8668d commit e67b1b1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
16 changes: 16 additions & 0 deletions govc/test/vm.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1093,4 +1093,20 @@ load test_helper

run govc object.collect -s vm/DC0_H0_VM0 guest.ipAddress
assert_success 10.0.0.45

run govc vm.power -off DC0_H0_VM0
assert_success

host=$(govc ls -L "$(govc object.collect -s vm/DC0_H0_VM0 runtime.host)")
run govc host.maintenance.enter "$host"
assert_success

run govc vm.customize -vm DC0_H0_VM0 -ip 10.0.0.45 -netmask 255.255.0.0 -type Linux
assert_failure # InvalidState

run govc host.maintenance.exit "$host"
assert_success

run govc vm.customize -vm DC0_H0_VM0 -ip 10.0.0.45 -netmask 255.255.0.0 -type Linux
assert_success
}
12 changes: 12 additions & 0 deletions simulator/virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ func (vm *VirtualMachine) event() types.VmEvent {
}
}

func (vm *VirtualMachine) hostInMM(ctx *Context) bool {
return ctx.Map.Get(*vm.Runtime.Host).(*HostSystem).Runtime.InMaintenanceMode
}

func (vm *VirtualMachine) apply(spec *types.VirtualMachineConfigSpec) {
if spec.Files == nil {
spec.Files = new(types.VirtualMachineFileInfo)
Expand Down Expand Up @@ -1429,6 +1433,10 @@ func (c *powerVMTask) Run(task *Task) (types.AnyType, types.BaseMethodFault) {
}
}

if c.VirtualMachine.hostInMM(c.ctx) {
return nil, new(types.InvalidState)
}

var boot types.AnyType
if c.state == types.VirtualMachinePowerStatePoweredOn {
boot = time.Now()
Expand Down Expand Up @@ -1949,6 +1957,10 @@ func (vm *VirtualMachine) customize(ctx *Context) {

func (vm *VirtualMachine) CustomizeVMTask(ctx *Context, req *types.CustomizeVM_Task) soap.HasFault {
task := CreateTask(vm, "customizeVm", func(t *Task) (types.AnyType, types.BaseMethodFault) {
if vm.hostInMM(ctx) {
return nil, new(types.InvalidState)
}

if vm.Runtime.PowerState == types.VirtualMachinePowerStatePoweredOn {
return nil, &types.InvalidPowerState{
RequestedState: types.VirtualMachinePowerStatePoweredOff,
Expand Down

0 comments on commit e67b1b1

Please sign in to comment.