Skip to content

Commit

Permalink
test (machine) : add vsock interaction methods to VirtualMachine inte…
Browse files Browse the repository at this point in the history
…rface

Make VirtualMachine implement vsock methods so that vsock interaction
is also done via VirtualMachine interface. This would help in writing
tests, we can override the behavior of expose/unexpose in fake vm
implementation.

Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia committed Nov 22, 2024
1 parent 325b81b commit 7cc7672
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pkg/crc/machine/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/crc/machine/fakemachine/virtualmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -39,6 +40,7 @@ type FakeVirtualMachine struct {
FailingStop bool
FailingState bool
FakeSSHClient *FakeSSHClient
PortsExposed bool
}

func (vm *FakeVirtualMachine) Close() error {
Expand Down Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion pkg/crc/machine/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/crc/machine/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
22 changes: 11 additions & 11 deletions pkg/crc/machine/stop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ 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()

// 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) {
Expand All @@ -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()
Expand All @@ -57,18 +57,18 @@ 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()

// 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) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/crc/machine/virtualmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/crc/machine/vsock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7cc7672

Please sign in to comment.