Skip to content

Commit

Permalink
test (machine) : Add additional tests in stop_test.go to use dummy Vi…
Browse files Browse the repository at this point in the history
…rtualMachine implementation

Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia committed Nov 6, 2024
1 parent f8b2fef commit d3ebc5a
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions pkg/crc/machine/stop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,71 @@ import (
"testing"

crcConfig "github.com/crc-org/crc/v2/pkg/crc/config"
"github.com/crc-org/crc/v2/pkg/crc/machine/fakemachine"
"github.com/crc-org/crc/v2/pkg/crc/machine/state"
crcOs "github.com/crc-org/crc/v2/pkg/os"
"github.com/stretchr/testify/assert"
)

func TestStop_WhenVMRunning_ThenShouldStopVirtualMachine(t *testing.T) {
// Given
crcConfigStorage := crcConfig.New(crcConfig.NewEmptyInMemoryStorage(), crcConfig.NewEmptyInMemorySecretStorage())
crcConfigStorage.AddSetting(crcConfig.NetworkMode, "user", crcConfig.ValidateBool, crcConfig.SuccessfullyApplied,
"network-mode")
_, err := crcConfigStorage.Set(crcConfig.NetworkMode, "true")
assert.NoError(t, err)
virtualMachine := fakemachine.NewFakeVirtualMachine(false, false)
client := newClientWithVirtualMachine("fake-virtual-machine", false, crcConfigStorage, virtualMachine)

// When
clusterState, stopErr := client.Stop()

// Then
assert.NoError(t, stopErr)
assert.Equal(t, clusterState, state.Stopped)
assert.Equal(t, virtualMachine.IsStopped, true)
assert.Equal(t, virtualMachine.FakeSSHClient.LastExecutedCommand, "sudo -- sh -c 'crictl stop $(crictl ps -q)'")
assert.Equal(t, virtualMachine.FakeSSHClient.IsSSHClientClosed, true)
}

func TestStop_WhenStopVmFailed_ThenErrorThrown(t *testing.T) {
// Given
crcConfigStorage := crcConfig.New(crcConfig.NewEmptyInMemoryStorage(), crcConfig.NewEmptyInMemorySecretStorage())
crcConfigStorage.AddSetting(crcConfig.NetworkMode, "user", crcConfig.ValidateBool, crcConfig.SuccessfullyApplied,
"network-mode")
_, err := crcConfigStorage.Set(crcConfig.NetworkMode, "true")
assert.NoError(t, err)
virtualMachine := fakemachine.NewFakeVirtualMachine(true, false)
client := newClientWithVirtualMachine("fake-virtual-machine", false, crcConfigStorage, virtualMachine)

// When
_, stopErr := client.Stop()

// Then
assert.ErrorContains(t, stopErr, "Cannot stop machine: stopping failed")
}

func TestStop_WhenVMAlreadyStopped_ThenThrowError(t *testing.T) {
// Given
crcConfigStorage := crcConfig.New(crcConfig.NewEmptyInMemoryStorage(), crcConfig.NewEmptyInMemorySecretStorage())
crcConfigStorage.AddSetting(crcConfig.NetworkMode, "user", crcConfig.ValidateBool, crcConfig.SuccessfullyApplied,
"network-mode")
_, err := crcConfigStorage.Set(crcConfig.NetworkMode, "true")
assert.NoError(t, err)
virtualMachine := fakemachine.NewFakeVirtualMachine(false, false)
err = virtualMachine.Stop()
assert.NoError(t, err)
client := newClientWithVirtualMachine("fake-virtual-machine", false, crcConfigStorage, virtualMachine)

// When
clusterState, stopErr := client.Stop()

// Then
assert.EqualError(t, stopErr, "Instance is already stopped")
assert.Equal(t, clusterState, state.Error)
assert.Equal(t, virtualMachine.IsStopped, true)
}

func TestClient_WhenStopInvokedWithNonExistentVM_ThenThrowError(t *testing.T) {
// Given
dir := t.TempDir()
Expand Down

0 comments on commit d3ebc5a

Please sign in to comment.