From 8859e2e03b7bef7e2c54bfa50584e3e7978b7451 Mon Sep 17 00:00:00 2001 From: sophia Date: Wed, 15 Jun 2022 14:03:20 -0500 Subject: [PATCH] Add test communicator plugin to guest detection test --- internal/core/machine_test.go | 35 +++++++++++++++++++++++++--------- internal/core/testing_basis.go | 13 +++++++++++++ 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/internal/core/machine_test.go b/internal/core/machine_test.go index 165a75c64..548022ae8 100644 --- a/internal/core/machine_test.go +++ b/internal/core/machine_test.go @@ -138,6 +138,14 @@ func TestMachineGetExistentBox(t *testing.T) { } func TestMachineConfigedGuest(t *testing.T) { + commMock := BuildTestCommunicatorPlugin("ssh") + commMock.On("Ready", mock.AnythingOfType("*core.Machine")).Return(true, nil) + commPlugin := plugin.TestPlugin(t, + commMock, + plugin.WithPluginName("ssh"), + plugin.WithPluginTypes(component.CommunicatorType), + ) + type test struct { config *component.ConfigData errors bool @@ -157,6 +165,7 @@ func TestMachineConfigedGuest(t *testing.T) { plugin.WithPluginName("myguest"), plugin.WithPluginTypes(component.GuestType), ), + commPlugin, ) for _, tc := range tests { @@ -178,6 +187,14 @@ func TestMachineConfigedGuest(t *testing.T) { } func TestMachineNoConfigGuest(t *testing.T) { + commMock := BuildTestCommunicatorPlugin("ssh") + commMock.On("Ready", mock.AnythingOfType("*core.Machine")).Return(true, nil) + commPlugin := plugin.TestPlugin(t, + commMock, + plugin.WithPluginName("ssh"), + plugin.WithPluginTypes(component.CommunicatorType), + ) + guestMock := BuildTestGuestPlugin("myguest", "") guestMock.On("Detect", mock.AnythingOfType("*core.Machine")).Return(true, nil) guestMock.On("Parent").Return("", nil) @@ -211,12 +228,12 @@ func TestMachineNoConfigGuest(t *testing.T) { } tests := []test{ - {plugins: []*plugin.Plugin{detectingPlugin}, errors: false, expectedPluginName: "myguest"}, - {plugins: []*plugin.Plugin{detectingChildPlugin}, errors: true, expectedPluginName: "myguest-child"}, - {plugins: []*plugin.Plugin{detectingChildPlugin, detectingPlugin}, errors: false, expectedPluginName: "myguest-child"}, - {plugins: []*plugin.Plugin{detectingPlugin, nonDetectingPlugin}, errors: false, expectedPluginName: "myguest"}, - {plugins: []*plugin.Plugin{nonDetectingPlugin}, errors: true}, - {plugins: []*plugin.Plugin{}, errors: true}, + {plugins: []*plugin.Plugin{commPlugin, detectingPlugin}, errors: false, expectedPluginName: "myguest"}, + {plugins: []*plugin.Plugin{commPlugin, detectingChildPlugin}, errors: true, expectedPluginName: "myguest-child"}, + {plugins: []*plugin.Plugin{commPlugin, detectingChildPlugin, detectingPlugin}, errors: false, expectedPluginName: "myguest-child"}, + {plugins: []*plugin.Plugin{commPlugin, detectingPlugin, nonDetectingPlugin}, errors: false, expectedPluginName: "myguest"}, + {plugins: []*plugin.Plugin{commPlugin, nonDetectingPlugin}, errors: true}, + {plugins: []*plugin.Plugin{commPlugin}, errors: true}, } for _, tc := range tests { @@ -230,13 +247,13 @@ func TestMachineNoConfigGuest(t *testing.T) { require.Nil(t, guest) require.Nil(t, tm.cache.Get("guest")) } else { + require.NoError(t, err) + require.NotNil(t, guest) + require.NotNil(t, tm.cache.Get("guest")) n, _ := guest.PluginName() if n != tc.expectedPluginName { t.Error("Found unexpected plugin, ", n) } - require.NoError(t, err) - require.NotNil(t, guest) - require.NotNil(t, tm.cache.Get("guest")) } } } diff --git a/internal/core/testing_basis.go b/internal/core/testing_basis.go index 080d9821f..e7593e54b 100644 --- a/internal/core/testing_basis.go +++ b/internal/core/testing_basis.go @@ -28,6 +28,11 @@ func (p *PluginWithParent) SetParentComponent(in interface{}) { p.parentPlugin = in } +type TestCommunicatorPlugin struct { + plugin.TestPluginWithFakeBroker + coremocks.Communicator +} + type TestGuestPlugin struct { PluginWithParent plugin.TestPluginWithFakeBroker @@ -46,6 +51,14 @@ type TestSyncedFolderPlugin struct { coremocks.SyncedFolder } +func BuildTestCommunicatorPlugin(name string) *TestCommunicatorPlugin { + c := &TestCommunicatorPlugin{} + c.On("Seed", mock.AnythingOfType("*core.Seeds")).Return(nil) + c.On("Seeds").Return(core.NewSeeds(), nil) + c.On("PluginName").Return(name, nil) + return c +} + func BuildTestGuestPlugin(name string, parent string) *TestGuestPlugin { p := &TestGuestPlugin{} p.On("SetPluginName", mock.AnythingOfType("string")).Return(nil)