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

Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia committed Nov 6, 2024
1 parent d3ebc5a commit 4d2f859
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pkg/crc/machine/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewClient(name string, debug bool, config crcConfig.Storage) Client {
return newClientWithVirtualMachine(name, debug, config, nil)
}

// newClientWithVirtualMachine creates a Client instance with a overridden VirtualMachine implementation.
// newClientWithVirtualMachine creates a Client instance with an overridden VirtualMachine implementation.
// It would not create a new VirtualMachine object. This method is primarily created for usage in tests so
// that we can pass a fake VirtualMachine implementation.
func newClientWithVirtualMachine(name string, debug bool, config crcConfig.Storage, vm VirtualMachine) Client {
Expand Down
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 @@ -98,6 +100,16 @@ func (vm *FakeVirtualMachine) GetHost() *libmachinehost.Host {
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
23 changes: 12 additions & 11 deletions pkg/crc/machine/stop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ 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.LastExecutedCommand, "sudo -- sh -c 'crictl stop $(crictl ps -q)'")
assert.Equal(t, fakeVirtualMachine.FakeSSHClient.IsSSHClientClosed, true)
assert.Equal(t, fakeVirtualMachine.PortsExposed, false)
}

func TestStop_WhenStopVmFailed_ThenErrorThrown(t *testing.T) {
Expand All @@ -40,8 +41,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 +58,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 @@ -25,6 +27,8 @@ type VirtualMachine interface {
Driver() drivers.Driver
API() libmachine.API
GetHost() *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 4d2f859

Please sign in to comment.