diff --git a/pkg/crc/machine/delete.go b/pkg/crc/machine/delete.go index 2983e8231f..66f3651fb2 100644 --- a/pkg/crc/machine/delete.go +++ b/pkg/crc/machine/delete.go @@ -22,7 +22,7 @@ func (client *client) Delete() error { // In case usermode networking make sure all the port bind on host should be released if client.useVSock() { - if err := unexposePorts(); err != nil { + if err := vm.UnExposePorts(); err != nil { return err } } diff --git a/pkg/crc/machine/fakemachine/virtualmachine.go b/pkg/crc/machine/fakemachine/virtualmachine.go index d19b521c37..2b36d8895d 100644 --- a/pkg/crc/machine/fakemachine/virtualmachine.go +++ b/pkg/crc/machine/fakemachine/virtualmachine.go @@ -5,6 +5,7 @@ import ( "github.com/crc-org/crc/v2/pkg/crc/machine/bundle" "github.com/crc-org/crc/v2/pkg/crc/machine/state" + crcPreset "github.com/crc-org/crc/v2/pkg/crc/preset" "github.com/crc-org/crc/v2/pkg/crc/ssh" "github.com/crc-org/crc/v2/pkg/libmachine" libmachinehost "github.com/crc-org/crc/v2/pkg/libmachine/host" @@ -39,6 +40,7 @@ type FakeVirtualMachine struct { FailingStop bool FailingState bool FakeSSHClient *FakeSSHClient + PortsExposed bool } func (vm *FakeVirtualMachine) Close() error { @@ -102,6 +104,16 @@ func (vm *FakeVirtualMachine) Kill() error { panic("not implemented") } +func (vm *FakeVirtualMachine) ExposePorts(_ crcPreset.Preset, _, _ uint) error { + vm.PortsExposed = true + return nil +} + +func (vm *FakeVirtualMachine) UnExposePorts() error { + vm.PortsExposed = false + return nil +} + func NewFakeVirtualMachine(failingStop bool, failingState bool) *FakeVirtualMachine { return &FakeVirtualMachine{ FakeSSHClient: NewFakeSSHClient(), diff --git a/pkg/crc/machine/start.go b/pkg/crc/machine/start.go index e573784487..8d7f1b98ec 100644 --- a/pkg/crc/machine/start.go +++ b/pkg/crc/machine/start.go @@ -368,7 +368,7 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig) logging.Infof("Starting CRC VM for %s %s...", startConfig.Preset, vm.Bundle().GetVersion()) if client.useVSock() { - if err := exposePorts(startConfig.Preset, startConfig.IngressHTTPPort, startConfig.IngressHTTPSPort); err != nil { + if err := vm.ExposePorts(startConfig.Preset, startConfig.IngressHTTPPort, startConfig.IngressHTTPSPort); err != nil { return nil, err } } diff --git a/pkg/crc/machine/stop.go b/pkg/crc/machine/stop.go index f25a2e18f8..8d6fb80da1 100644 --- a/pkg/crc/machine/stop.go +++ b/pkg/crc/machine/stop.go @@ -45,7 +45,7 @@ func (client *client) Stop() (state.State, error) { } // In case usermode networking make sure all the port bind on host should be released if client.useVSock() { - return status, unexposePorts() + return status, vm.UnExposePorts() } return status, nil } diff --git a/pkg/crc/machine/stop_test.go b/pkg/crc/machine/stop_test.go index a4ae6a1907..9882c79be7 100644 --- a/pkg/crc/machine/stop_test.go +++ b/pkg/crc/machine/stop_test.go @@ -19,8 +19,8 @@ func TestStop_WhenVMRunning_ThenShouldStopVirtualMachine(t *testing.T) { "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) + fakeVirtualMachine := fakemachine.NewFakeVirtualMachine(false, false) + client := newClientWithVirtualMachine("fake-virtual-machine", false, crcConfigStorage, fakeVirtualMachine) // When clusterState, stopErr := client.Stop() @@ -28,9 +28,9 @@ func TestStop_WhenVMRunning_ThenShouldStopVirtualMachine(t *testing.T) { // 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) + assert.Equal(t, fakeVirtualMachine.IsStopped, true) + assert.Equal(t, fakeVirtualMachine.FakeSSHClient.IsSSHClientClosed, true) + assert.Equal(t, fakeVirtualMachine.PortsExposed, false) } func TestStop_WhenStopVmFailed_ThenErrorThrown(t *testing.T) { @@ -40,8 +40,8 @@ func TestStop_WhenStopVmFailed_ThenErrorThrown(t *testing.T) { "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) + fakeVirtualMachine := fakemachine.NewFakeVirtualMachine(true, false) + client := newClientWithVirtualMachine("fake-virtual-machine", false, crcConfigStorage, fakeVirtualMachine) // When _, stopErr := client.Stop() @@ -57,10 +57,10 @@ func TestStop_WhenVMAlreadyStopped_ThenThrowError(t *testing.T) { "network-mode") _, err := crcConfigStorage.Set(crcConfig.NetworkMode, "true") assert.NoError(t, err) - virtualMachine := fakemachine.NewFakeVirtualMachine(false, false) - err = virtualMachine.Stop() + fakeVirtualMachine := fakemachine.NewFakeVirtualMachine(false, false) + err = fakeVirtualMachine.Stop() assert.NoError(t, err) - client := newClientWithVirtualMachine("fake-virtual-machine", false, crcConfigStorage, virtualMachine) + client := newClientWithVirtualMachine("fake-virtual-machine", false, crcConfigStorage, fakeVirtualMachine) // When clusterState, stopErr := client.Stop() @@ -68,7 +68,7 @@ func TestStop_WhenVMAlreadyStopped_ThenThrowError(t *testing.T) { // Then assert.EqualError(t, stopErr, "Instance is already stopped") assert.Equal(t, clusterState, state.Error) - assert.Equal(t, virtualMachine.IsStopped, true) + assert.Equal(t, fakeVirtualMachine.IsStopped, true) } func TestClient_WhenStopInvokedWithNonExistentVM_ThenThrowError(t *testing.T) { diff --git a/pkg/crc/machine/virtualmachine.go b/pkg/crc/machine/virtualmachine.go index 03a6b0e60f..38941b055c 100644 --- a/pkg/crc/machine/virtualmachine.go +++ b/pkg/crc/machine/virtualmachine.go @@ -3,6 +3,8 @@ package machine import ( "fmt" + crcPreset "github.com/crc-org/crc/v2/pkg/crc/preset" + "github.com/crc-org/crc/v2/pkg/crc/constants" "github.com/crc-org/crc/v2/pkg/crc/machine/bundle" "github.com/crc-org/crc/v2/pkg/crc/machine/state" @@ -26,6 +28,8 @@ type VirtualMachine interface { Driver() drivers.Driver API() libmachine.API Host() *libmachinehost.Host + ExposePorts(preset crcPreset.Preset, ingressHTTPPort, ingressHTTPSPort uint) error + UnExposePorts() error } type virtualMachine struct { diff --git a/pkg/crc/machine/vsock.go b/pkg/crc/machine/vsock.go index 7d9c72613a..eb7e129bb0 100644 --- a/pkg/crc/machine/vsock.go +++ b/pkg/crc/machine/vsock.go @@ -16,7 +16,7 @@ import ( "github.com/pkg/errors" ) -func exposePorts(preset crcPreset.Preset, ingressHTTPPort, ingressHTTPSPort uint) error { +func (vm *virtualMachine) ExposePorts(preset crcPreset.Preset, ingressHTTPPort, ingressHTTPSPort uint) error { portsToExpose := vsockPorts(preset, ingressHTTPPort, ingressHTTPSPort) daemonClient := daemonclient.New() alreadyOpenedPorts, err := listOpenPorts(daemonClient) @@ -47,7 +47,7 @@ func isOpened(exposed []types.ExposeRequest, port types.ExposeRequest) bool { return false } -func unexposePorts() error { +func (vm *virtualMachine) UnExposePorts() error { var mErr crcErrors.MultiError daemonClient := daemonclient.New() alreadyOpenedPorts, err := listOpenPorts(daemonClient)