diff --git a/virtual_machine.go b/virtual_machine.go index ca50ba5..576eb43 100644 --- a/virtual_machine.go +++ b/virtual_machine.go @@ -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) { @@ -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) { @@ -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) { diff --git a/virtual_machine_test.go b/virtual_machine_test.go index 04d31cc..e0736c8 100644 --- a/virtual_machine_test.go +++ b/virtual_machine_test.go @@ -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())