Skip to content

Commit

Permalink
Fix VM state functions (#170)
Browse files Browse the repository at this point in the history
* Fix VirtualMachine.IsPaused

The condition in the IsPaused function checks whether the VM is running, not whether it's in a pause state.

* Fix VirtualMachine state functions
  • Loading branch information
rybnico authored Oct 8, 2024
1 parent 8be9455 commit d64b696
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
6 changes: 3 additions & 3 deletions virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (v *VirtualMachine) VNCWebSocket(vnc *VNC) (chan []byte, chan []byte, chan
}

func (v *VirtualMachine) IsRunning() bool {
return v.Status == StatusVirtualMachineRunning
return v.Status == StatusVirtualMachineRunning && (v.QMPStatus == "" || v.QMPStatus == StatusVirtualMachineRunning)
}

func (v *VirtualMachine) Start(ctx context.Context) (task *Task, err error) {
Expand All @@ -256,7 +256,7 @@ func (v *VirtualMachine) Start(ctx context.Context) (task *Task, err error) {
}

func (v *VirtualMachine) IsStopped() bool {
return v.Status == StatusVirtualMachineStopped
return v.Status == StatusVirtualMachineStopped && (v.Lock != "suspended")
}

func (v *VirtualMachine) Reset(ctx context.Context) (task *Task, err error) {
Expand Down Expand Up @@ -287,7 +287,7 @@ func (v *VirtualMachine) Stop(ctx context.Context) (task *Task, err error) {
}

func (v *VirtualMachine) IsPaused() bool {
return v.Status == StatusVirtualMachinePaused
return v.Status == StatusVirtualMachineRunning && v.QMPStatus == StatusVirtualMachinePaused
}

func (v *VirtualMachine) Pause(ctx context.Context) (task *Task, err error) {
Expand Down
39 changes: 35 additions & 4 deletions virtual_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,31 +80,62 @@ func TestVirtualMachineState(t *testing.T) {
mocks.On(mockConfig)
defer mocks.Off()
runningVM := VirtualMachine{
Status: "running",
Status: "running",
QMPStatus: "running",
}
assert.False(t, runningVM.IsStopped())
assert.False(t, runningVM.IsPaused())
assert.False(t, runningVM.IsHibernated())
assert.True(t, runningVM.IsRunning())
stoppedVM := VirtualMachine{
Status: "stopped",
Status: "stopped",
QMPStatus: "stopped",
}
assert.True(t, stoppedVM.IsStopped())
assert.False(t, stoppedVM.IsPaused())
assert.False(t, stoppedVM.IsHibernated())
assert.False(t, stoppedVM.IsRunning())
pausedVM := VirtualMachine{
Status: "paused",
Status: "running",
QMPStatus: "paused",
}
assert.False(t, pausedVM.IsStopped())
assert.True(t, pausedVM.IsPaused())
assert.False(t, pausedVM.IsHibernated())
assert.False(t, pausedVM.IsRunning())
hibernatedVM := VirtualMachine{
Status: "stopped",
QMPStatus: "stopped",
Lock: "suspended",
}
assert.False(t, hibernatedVM.IsStopped())
assert.False(t, hibernatedVM.IsPaused())
assert.True(t, hibernatedVM.IsHibernated())
assert.False(t, hibernatedVM.IsRunning())
}

func TestVirtualMachineStateWithoutQMPStatus(t *testing.T) {
mocks.On(mockConfig)
defer mocks.Off()
runningVM := VirtualMachine{
Status: "running",
}
assert.False(t, runningVM.IsStopped())
assert.False(t, runningVM.IsPaused())
assert.False(t, runningVM.IsHibernated())
assert.True(t, runningVM.IsRunning())
stoppedVM := VirtualMachine{
Status: "stopped",
}
assert.True(t, stoppedVM.IsStopped())
assert.False(t, stoppedVM.IsPaused())
assert.False(t, stoppedVM.IsHibernated())
assert.False(t, stoppedVM.IsRunning())
hibernatedVM := VirtualMachine{
Status: "stopped",
Lock: "suspended",
}
assert.True(t, hibernatedVM.IsStopped())
assert.False(t, hibernatedVM.IsStopped())
assert.False(t, hibernatedVM.IsPaused())
assert.True(t, hibernatedVM.IsHibernated())
assert.False(t, hibernatedVM.IsRunning())
Expand Down

0 comments on commit d64b696

Please sign in to comment.