Skip to content

Commit

Permalink
test (machine) : add an interface for vsock interaction
Browse files Browse the repository at this point in the history
Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia committed Nov 5, 2024
1 parent 63c5eb3 commit 0e36ad5
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 12 deletions.
10 changes: 6 additions & 4 deletions pkg/crc/machine/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,28 @@ type client struct {
debug bool
config crcConfig.Storage
virtualMachine VirtualMachine
vSockClient AbstractVSock

diskDetails *memoize.Memoizer
ramDetails *memoize.Memoizer
}

func NewClient(name string, debug bool, config crcConfig.Storage) Client {
return newClientWithVirtualMachine(name, debug, config, nil)
return newClientWithVirtualMachineAndVSock(name, debug, config, nil, &VSock{})
}

// newClientWithVirtualMachine creates a Client instance with a overridden VirtualMachine implementation.
// newClientWithVirtualMachineAndVSock creates a Client instance with an overridden VirtualMachine and AbstractVSock 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 {
// that we can pass a fake VirtualMachine and AbstractVSock implementation.
func newClientWithVirtualMachineAndVSock(name string, debug bool, config crcConfig.Storage, vm VirtualMachine, vSock AbstractVSock) Client {
return &client{
name: name,
debug: debug,
config: config,
diskDetails: memoize.NewMemoizer(time.Minute, 5*time.Minute),
ramDetails: memoize.NewMemoizer(30*time.Second, 2*time.Minute),
virtualMachine: vm,
vSockClient: vSock,
}
}

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 := client.vSockClient.UnExposePorts(); err != nil {
return err
}
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/crc/machine/fakemachine/vsock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package fakemachine

import crcPreset "github.com/crc-org/crc/v2/pkg/crc/preset"

type FakeVSock struct {
PortsExposed bool
}

func (v *FakeVSock) ExposePorts(preset crcPreset.Preset, ingressHTTPPort, ingressHTTPSPort uint) error {

Check failure on line 9 in pkg/crc/machine/fakemachine/vsock.go

View workflow job for this annotation

GitHub Actions / build (windows-2022, 1.22)

unused-parameter: parameter 'preset' seems to be unused, consider removing or renaming it as _ (revive)

Check failure on line 9 in pkg/crc/machine/fakemachine/vsock.go

View workflow job for this annotation

GitHub Actions / build (macOS-13, 1.22)

unused-parameter: parameter 'preset' seems to be unused, consider removing or renaming it as _ (revive)

Check failure on line 9 in pkg/crc/machine/fakemachine/vsock.go

View workflow job for this annotation

GitHub Actions / build (macOS-12, 1.22)

unused-parameter: parameter 'preset' seems to be unused, consider removing or renaming it as _ (revive)

Check failure on line 9 in pkg/crc/machine/fakemachine/vsock.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 1.22)

unused-parameter: parameter 'preset' seems to be unused, consider removing or renaming it as _ (revive)

Check failure on line 9 in pkg/crc/machine/fakemachine/vsock.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04, 1.22)

unused-parameter: parameter 'preset' seems to be unused, consider removing or renaming it as _ (revive)
v.PortsExposed = true
return nil
}

func (v *FakeVSock) UnExposePorts() error {
v.PortsExposed = false
return nil
}

func NewFakeVSock() *FakeVSock {
return &FakeVSock{
PortsExposed: false,
}
}
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 := client.vSockClient.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, client.vSockClient.UnExposePorts()
}
return status, nil
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/crc/machine/stop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ func TestStop_WhenVMRunning_ThenShouldStopVirtualMachine(t *testing.T) {
_, err := crcConfigStorage.Set(crcConfig.NetworkMode, "true")
assert.NoError(t, err)
virtualMachine := fakemachine.NewFakeVirtualMachine(false, false)
client := newClientWithVirtualMachine("fake-virtual-machine", false, crcConfigStorage, virtualMachine)
fakeVSock := fakemachine.NewFakeVSock()
client := newClientWithVirtualMachineAndVSock("fake-virtual-machine", false, crcConfigStorage, virtualMachine, fakeVSock)

// When
clusterState, stopErr := client.Stop()
Expand All @@ -31,6 +32,7 @@ func TestStop_WhenVMRunning_ThenShouldStopVirtualMachine(t *testing.T) {
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, fakeVSock.PortsExposed, false)
}

func TestStop_WhenStopVmFailed_ThenErrorThrown(t *testing.T) {
Expand All @@ -41,7 +43,8 @@ func TestStop_WhenStopVmFailed_ThenErrorThrown(t *testing.T) {
_, err := crcConfigStorage.Set(crcConfig.NetworkMode, "true")
assert.NoError(t, err)
virtualMachine := fakemachine.NewFakeVirtualMachine(true, false)
client := newClientWithVirtualMachine("fake-virtual-machine", false, crcConfigStorage, virtualMachine)
fakeVSock := fakemachine.NewFakeVSock()
client := newClientWithVirtualMachineAndVSock("fake-virtual-machine", false, crcConfigStorage, virtualMachine, fakeVSock)

// When
_, stopErr := client.Stop()
Expand All @@ -60,7 +63,8 @@ func TestStop_WhenVMAlreadyStopped_ThenThrowError(t *testing.T) {
virtualMachine := fakemachine.NewFakeVirtualMachine(false, false)
err = virtualMachine.Stop()
assert.NoError(t, err)
client := newClientWithVirtualMachine("fake-virtual-machine", false, crcConfigStorage, virtualMachine)
fakeVSock := fakemachine.NewFakeVSock()
client := newClientWithVirtualMachineAndVSock("fake-virtual-machine", false, crcConfigStorage, virtualMachine, fakeVSock)

// When
clusterState, stopErr := client.Stop()
Expand Down
11 changes: 9 additions & 2 deletions pkg/crc/machine/vsock.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ import (
"github.com/pkg/errors"
)

func exposePorts(preset crcPreset.Preset, ingressHTTPPort, ingressHTTPSPort uint) error {
type AbstractVSock interface {
ExposePorts(preset crcPreset.Preset, ingressHTTPPort, ingressHTTPSPort uint) error
UnExposePorts() error
}

type VSock struct{}

func (v *VSock) 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 +54,7 @@ func isOpened(exposed []types.ExposeRequest, port types.ExposeRequest) bool {
return false
}

func unexposePorts() error {
func (v *VSock) UnExposePorts() error {
var mErr crcErrors.MultiError
daemonClient := daemonclient.New()
alreadyOpenedPorts, err := listOpenPorts(daemonClient)
Expand Down

0 comments on commit 0e36ad5

Please sign in to comment.