From 873dfcf9e0cd9a6dd8f092228eadffdfe574e3a8 Mon Sep 17 00:00:00 2001 From: Nicolai Rybnikar Date: Sat, 5 Oct 2024 21:31:16 +0200 Subject: [PATCH] Fix VirtualMachine.IsPaused The condition in the IsPaused function checks whether the VM is running, not whether it's in a pause state. --- virtual_machine.go | 2 +- virtual_machine_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/virtual_machine.go b/virtual_machine.go index fdb8514..ca50ba5 100644 --- a/virtual_machine.go +++ b/virtual_machine.go @@ -287,7 +287,7 @@ func (v *VirtualMachine) Stop(ctx context.Context) (task *Task, err error) { } func (v *VirtualMachine) IsPaused() bool { - return v.Status == StatusVirtualMachineRunning + return v.Status == 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 93cc9bb..04d31cc 100644 --- a/virtual_machine_test.go +++ b/virtual_machine_test.go @@ -75,3 +75,37 @@ func TestVirtualMachineCloneWithoutNewID(t *testing.T) { assert.Nil(t, err) assert.Equal(t, 100, newID) } + +func TestVirtualMachineState(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()) + pausedVM := VirtualMachine{ + Status: "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", + Lock: "suspended", + } + assert.True(t, hibernatedVM.IsStopped()) + assert.False(t, hibernatedVM.IsPaused()) + assert.True(t, hibernatedVM.IsHibernated()) + assert.False(t, hibernatedVM.IsRunning()) +}