From 325b81b93a5d624aca6e19495eae739f77f8258b Mon Sep 17 00:00:00 2001 From: Rohan Kumar Date: Wed, 6 Nov 2024 12:29:35 +0530 Subject: [PATCH] test (machine) : Add additional tests in stop_test.go to use dummy VirtualMachine implementation Add additional tests in `stop_test.go` to verify that client.Stop() updates the state of virtual machine and unexposes exposed ports as expected. Use the fake vm implementation added previously to test vm state modification behavior on stop. Signed-off-by: Rohan Kumar --- pkg/crc/machine/stop_test.go | 60 ++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/pkg/crc/machine/stop_test.go b/pkg/crc/machine/stop_test.go index 3cee1d4207..a4ae6a1907 100644 --- a/pkg/crc/machine/stop_test.go +++ b/pkg/crc/machine/stop_test.go @@ -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()