Skip to content

Commit ada0fc6

Browse files
committed
test (machine) : add vsock interaction methods to VirtualMachine interface
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]>
1 parent b389f4a commit ada0fc6

File tree

7 files changed

+33
-16
lines changed

7 files changed

+33
-16
lines changed

pkg/crc/machine/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (client *client) Delete() error {
2222

2323
// In case usermode networking make sure all the port bind on host should be released
2424
if client.useVSock() {
25-
if err := unexposePorts(); err != nil {
25+
if err := vm.UnExposePorts(); err != nil {
2626
return err
2727
}
2828
}

pkg/crc/machine/fakemachine/virtualmachine.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/crc-org/crc/v2/pkg/crc/machine/bundle"
77
"github.com/crc-org/crc/v2/pkg/crc/machine/state"
8+
crcPreset "github.com/crc-org/crc/v2/pkg/crc/preset"
89
"github.com/crc-org/crc/v2/pkg/crc/ssh"
910
"github.com/crc-org/crc/v2/pkg/libmachine"
1011
libmachinehost "github.com/crc-org/crc/v2/pkg/libmachine/host"
@@ -39,6 +40,7 @@ type FakeVirtualMachine struct {
3940
FailingStop bool
4041
FailingState bool
4142
FakeSSHClient *FakeSSHClient
43+
PortsExposed bool
4244
}
4345

4446
func (vm *FakeVirtualMachine) Close() error {
@@ -98,6 +100,16 @@ func (vm *FakeVirtualMachine) GetHost() *libmachinehost.Host {
98100
panic("not implemented")
99101
}
100102

103+
func (vm *FakeVirtualMachine) ExposePorts(_ crcPreset.Preset, _, _ uint) error {
104+
vm.PortsExposed = true
105+
return nil
106+
}
107+
108+
func (vm *FakeVirtualMachine) UnExposePorts() error {
109+
vm.PortsExposed = false
110+
return nil
111+
}
112+
101113
func NewFakeVirtualMachine(failingStop bool, failingState bool) *FakeVirtualMachine {
102114
return &FakeVirtualMachine{
103115
FakeSSHClient: NewFakeSSHClient(),

pkg/crc/machine/start.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
368368
logging.Infof("Starting CRC VM for %s %s...", startConfig.Preset, vm.Bundle().GetVersion())
369369

370370
if client.useVSock() {
371-
if err := exposePorts(startConfig.Preset, startConfig.IngressHTTPPort, startConfig.IngressHTTPSPort); err != nil {
371+
if err := vm.ExposePorts(startConfig.Preset, startConfig.IngressHTTPPort, startConfig.IngressHTTPSPort); err != nil {
372372
return nil, err
373373
}
374374
}

pkg/crc/machine/stop.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (client *client) Stop() (state.State, error) {
4545
}
4646
// In case usermode networking make sure all the port bind on host should be released
4747
if client.useVSock() {
48-
return status, unexposePorts()
48+
return status, vm.UnExposePorts()
4949
}
5050
return status, nil
5151
}

pkg/crc/machine/stop_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,19 @@ func TestStop_WhenVMRunning_ThenShouldStopVirtualMachine(t *testing.T) {
1919
"network-mode")
2020
_, err := crcConfigStorage.Set(crcConfig.NetworkMode, "true")
2121
assert.NoError(t, err)
22-
virtualMachine := fakemachine.NewFakeVirtualMachine(false, false)
23-
client := newClientWithVirtualMachine("fake-virtual-machine", false, crcConfigStorage, virtualMachine)
22+
fakeVirtualMachine := fakemachine.NewFakeVirtualMachine(false, false)
23+
client := newClientWithVirtualMachine("fake-virtual-machine", false, crcConfigStorage, fakeVirtualMachine)
2424

2525
// When
2626
clusterState, stopErr := client.Stop()
2727

2828
// Then
2929
assert.NoError(t, stopErr)
3030
assert.Equal(t, clusterState, state.Stopped)
31-
assert.Equal(t, virtualMachine.IsStopped, true)
32-
assert.Equal(t, virtualMachine.FakeSSHClient.LastExecutedCommand, "sudo -- sh -c 'crictl stop $(crictl ps -q)'")
33-
assert.Equal(t, virtualMachine.FakeSSHClient.IsSSHClientClosed, true)
31+
assert.Equal(t, fakeVirtualMachine.IsStopped, true)
32+
assert.Equal(t, fakeVirtualMachine.FakeSSHClient.LastExecutedCommand, "sudo -- sh -c 'crictl stop $(crictl ps -q)'")
33+
assert.Equal(t, fakeVirtualMachine.FakeSSHClient.IsSSHClientClosed, true)
34+
assert.Equal(t, fakeVirtualMachine.PortsExposed, false)
3435
}
3536

3637
func TestStop_WhenStopVmFailed_ThenErrorThrown(t *testing.T) {
@@ -40,8 +41,8 @@ func TestStop_WhenStopVmFailed_ThenErrorThrown(t *testing.T) {
4041
"network-mode")
4142
_, err := crcConfigStorage.Set(crcConfig.NetworkMode, "true")
4243
assert.NoError(t, err)
43-
virtualMachine := fakemachine.NewFakeVirtualMachine(true, false)
44-
client := newClientWithVirtualMachine("fake-virtual-machine", false, crcConfigStorage, virtualMachine)
44+
fakeVirtualMachine := fakemachine.NewFakeVirtualMachine(true, false)
45+
client := newClientWithVirtualMachine("fake-virtual-machine", false, crcConfigStorage, fakeVirtualMachine)
4546

4647
// When
4748
_, stopErr := client.Stop()
@@ -57,18 +58,18 @@ func TestStop_WhenVMAlreadyStopped_ThenThrowError(t *testing.T) {
5758
"network-mode")
5859
_, err := crcConfigStorage.Set(crcConfig.NetworkMode, "true")
5960
assert.NoError(t, err)
60-
virtualMachine := fakemachine.NewFakeVirtualMachine(false, false)
61-
err = virtualMachine.Stop()
61+
fakeVirtualMachine := fakemachine.NewFakeVirtualMachine(false, false)
62+
err = fakeVirtualMachine.Stop()
6263
assert.NoError(t, err)
63-
client := newClientWithVirtualMachine("fake-virtual-machine", false, crcConfigStorage, virtualMachine)
64+
client := newClientWithVirtualMachine("fake-virtual-machine", false, crcConfigStorage, fakeVirtualMachine)
6465

6566
// When
6667
clusterState, stopErr := client.Stop()
6768

6869
// Then
6970
assert.EqualError(t, stopErr, "Instance is already stopped")
7071
assert.Equal(t, clusterState, state.Error)
71-
assert.Equal(t, virtualMachine.IsStopped, true)
72+
assert.Equal(t, fakeVirtualMachine.IsStopped, true)
7273
}
7374

7475
func TestClient_WhenStopInvokedWithNonExistentVM_ThenThrowError(t *testing.T) {

pkg/crc/machine/virtualmachine.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package machine
33
import (
44
"fmt"
55

6+
crcPreset "github.com/crc-org/crc/v2/pkg/crc/preset"
7+
68
"github.com/crc-org/crc/v2/pkg/crc/constants"
79
"github.com/crc-org/crc/v2/pkg/crc/machine/bundle"
810
"github.com/crc-org/crc/v2/pkg/crc/machine/state"
@@ -26,6 +28,8 @@ type VirtualMachine interface {
2628
Driver() drivers.Driver
2729
API() libmachine.API
2830
Host() *libmachinehost.Host
31+
ExposePorts(preset crcPreset.Preset, ingressHTTPPort, ingressHTTPSPort uint) error
32+
UnExposePorts() error
2933
}
3034

3135
type virtualMachine struct {

pkg/crc/machine/vsock.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"github.com/pkg/errors"
1717
)
1818

19-
func exposePorts(preset crcPreset.Preset, ingressHTTPPort, ingressHTTPSPort uint) error {
19+
func (vm *virtualMachine) ExposePorts(preset crcPreset.Preset, ingressHTTPPort, ingressHTTPSPort uint) error {
2020
portsToExpose := vsockPorts(preset, ingressHTTPPort, ingressHTTPSPort)
2121
daemonClient := daemonclient.New()
2222
alreadyOpenedPorts, err := listOpenPorts(daemonClient)
@@ -47,7 +47,7 @@ func isOpened(exposed []types.ExposeRequest, port types.ExposeRequest) bool {
4747
return false
4848
}
4949

50-
func unexposePorts() error {
50+
func (vm *virtualMachine) UnExposePorts() error {
5151
var mErr crcErrors.MultiError
5252
daemonClient := daemonclient.New()
5353
alreadyOpenedPorts, err := listOpenPorts(daemonClient)

0 commit comments

Comments
 (0)