From c712afad8faa5399cb827bfc0e00321b6de08b50 Mon Sep 17 00:00:00 2001 From: sophia Date: Mon, 2 May 2022 12:33:18 -0500 Subject: [PATCH 1/8] Update testing plugins --- internal/plugin/testing_plugin.go | 45 ++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/internal/plugin/testing_plugin.go b/internal/plugin/testing_plugin.go index 4575f3799..f2ee1391f 100644 --- a/internal/plugin/testing_plugin.go +++ b/internal/plugin/testing_plugin.go @@ -2,13 +2,42 @@ package plugin import ( "github.com/hashicorp/go-hclog" + "github.com/hashicorp/go-plugin" + "github.com/hashicorp/vagrant-plugin-sdk/component" + "github.com/hashicorp/vagrant-plugin-sdk/internal-shared/cleanup" "github.com/mitchellh/go-testing-interface" ) -func TestMinimalPlugin(t testing.T) *Plugin { +type TestPluginWithFakeBroker struct { + client interface{} +} + +func (p *TestPluginWithFakeBroker) GRPCBroker() *plugin.GRPCBroker { + return &plugin.GRPCBroker{} +} + +type MockClientProtocol struct { + plg interface{} +} + +func (m *MockClientProtocol) Dispense(s string) (interface{}, error) { + return m.plg, nil +} + +func (m *MockClientProtocol) Ping() error { + return nil +} + +func (m *MockClientProtocol) Close() error { + return nil +} + +func TestMinimalPlugin(t testing.T, client interface{}) *Plugin { plugin := &Plugin{ Location: "test", + Client: client.(plugin.ClientProtocol), logger: hclog.New(&hclog.LoggerOptions{}), + cleaner: cleanup.New(), } return plugin } @@ -16,8 +45,11 @@ func TestMinimalPlugin(t testing.T) *Plugin { // TestPlugin returns a fully in-memory and side-effect free Plugin that // can be used for testing. Additional options can be given to provide your own // factories, configuration, etc. -func TestPlugin(t testing.T, opts ...PluginProperty) (plugin *Plugin) { - plugin = TestMinimalPlugin(t) +func TestPlugin(t testing.T, plg interface{}, opts ...PluginProperty) (plugin *Plugin) { + mockClient := &MockClientProtocol{ + plg: &TestPluginWithFakeBroker{client: plg}, + } + plugin = TestMinimalPlugin(t, mockClient) for _, opt := range opts { if err := opt(plugin); err != nil { t.Error(err) @@ -34,3 +66,10 @@ func WithPluginName(name string) PluginProperty { return } } + +func WithPluginTypes(types ...component.Type) PluginProperty { + return func(p *Plugin) (err error) { + p.Types = types + return + } +} From b381487dd2a80611149c0aae26f5f4d52918ac91 Mon Sep 17 00:00:00 2001 From: sophia Date: Mon, 2 May 2022 14:52:41 -0500 Subject: [PATCH 2/8] Use testing plugins to populate testing plugin manager --- internal/core/basis_test.go | 113 ++++++++++++------------------ internal/plugin/testing_plugin.go | 3 +- 2 files changed, 46 insertions(+), 70 deletions(-) diff --git a/internal/core/basis_test.go b/internal/core/basis_test.go index e0f262c93..df0e3c8cf 100644 --- a/internal/core/basis_test.go +++ b/internal/core/basis_test.go @@ -4,28 +4,69 @@ import ( "testing" "github.com/hashicorp/vagrant-plugin-sdk/component" + componentmocks "github.com/hashicorp/vagrant-plugin-sdk/component/mocks" coremocks "github.com/hashicorp/vagrant-plugin-sdk/core/mocks" "github.com/hashicorp/vagrant/internal/plugin" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" ) +type TestGuestPlugin struct { + plugin.TestPluginWithFakeBroker + coremocks.Guest +} + +type TestHostPlugin struct { + plugin.TestPluginWithFakeBroker + componentmocks.Host +} + +type TestSyncedFolderPlugin struct { + plugin.TestPluginWithFakeBroker + componentmocks.SyncedFolder +} + +func BuildTestGuestPlugin() *TestGuestPlugin { + p := &TestGuestPlugin{} + p.On("SetPluginName", mock.AnythingOfType("string")).Return(nil) + p.On("Seed", mock.AnythingOfType("*core.Seeds")).Return(nil) + return p +} + +func BuildTestHostPlugin() *TestHostPlugin { + p := &TestHostPlugin{} + p.On("SetPluginName", mock.AnythingOfType("string")).Return(nil) + p.On("Seed", mock.AnythingOfType("*core.Seeds")).Return(nil) + return p +} + +func BuildTestSyncedFolderPlugin() *TestSyncedFolderPlugin { + p := &TestSyncedFolderPlugin{} + p.On("SetPluginName", mock.AnythingOfType("string")).Return(nil) + p.On("Seed", mock.AnythingOfType("*core.Seeds")).Return(nil) + return p +} + func TestBasisPlugins(t *testing.T) { myguest := plugin.TestPlugin(t, + BuildTestGuestPlugin(), plugin.WithPluginName("myguest"), - plugin.WithPluginMinimalComponents(component.GuestType, &coremocks.Guest{}), + plugin.WithPluginTypes(component.GuestType), ) myguesttwo := plugin.TestPlugin(t, + BuildTestGuestPlugin(), plugin.WithPluginName("myguesttwo"), - plugin.WithPluginMinimalComponents(component.GuestType, &coremocks.Guest{}), + plugin.WithPluginTypes(component.GuestType), ) myhost := plugin.TestPlugin(t, + BuildTestHostPlugin(), plugin.WithPluginName("myhost"), - plugin.WithPluginMinimalComponents(component.HostType, &coremocks.Host{}), + plugin.WithPluginTypes(component.HostType), ) mysf := plugin.TestPlugin(t, + BuildTestSyncedFolderPlugin(), plugin.WithPluginName("mysf"), - plugin.WithPluginMinimalComponents(component.SyncedFolderType, &coremocks.SyncedFolder{}), + plugin.WithPluginTypes(component.SyncedFolderType), ) type test struct { @@ -84,67 +125,3 @@ func TestBasisPlugins(t *testing.T) { // } // } // } - -func TestBasisNoConfigHost(t *testing.T) { - hostMock := seededHostMock("myhost") - hostMock.On("Detect", mock.AnythingOfType("*core.StateBag")).Return(true, nil) - detectPluginInstance := plugin.TestPluginInstance(t, - plugin.WithPluginInstanceName("myhost"), - plugin.WithPluginInstanceType(component.HostType), - plugin.WithPluginInstanceComponent(hostMock)) - detectingPlugin := plugin.TestPlugin(t, - plugin.WithPluginName("myhost"), - plugin.WithPluginInstance(detectPluginInstance)) - - notHostMock := seededHostMock("mynondetectinghost") - notHostMock.On("Detect", mock.AnythingOfType("*core.StateBag")).Return(false, nil) - nonDetectingPlugin := plugin.TestPlugin(t, - plugin.WithPluginName("mynondetectinghost"), - plugin.WithPluginMinimalComponents(component.HostType, notHostMock)) - - hostChildMock := seededHostMock("myhost-child") - hostChildMock.On("Detect", mock.AnythingOfType("*core.StateBag")).Return(true, nil) - detectChildPluginInstance := plugin.TestPluginInstance(t, - plugin.WithPluginInstanceName("myhost-child"), - plugin.WithPluginInstanceType(component.HostType), - plugin.WithPluginInstanceComponent(hostChildMock), - plugin.WithPluginInstanceParent(detectPluginInstance)) - detectingChildPlugin := plugin.TestPlugin(t, - plugin.WithPluginName("myhost-child"), - plugin.WithPluginInstance(detectChildPluginInstance), - ) - - type test struct { - plugins []*plugin.Plugin - errors bool - expectedPluginName string - } - - tests := []test{ - {plugins: []*plugin.Plugin{detectingPlugin}, errors: false, expectedPluginName: "myhost"}, - {plugins: []*plugin.Plugin{detectingChildPlugin}, errors: false, expectedPluginName: "myhost-child"}, - {plugins: []*plugin.Plugin{detectingChildPlugin, detectingPlugin}, errors: false, expectedPluginName: "myhost-child"}, - {plugins: []*plugin.Plugin{detectingPlugin, nonDetectingPlugin}, errors: false, expectedPluginName: "myhost"}, - {plugins: []*plugin.Plugin{nonDetectingPlugin}, errors: true}, - {plugins: []*plugin.Plugin{}, errors: true}, - } - - for _, tc := range tests { - pluginManager := plugin.TestManager(t, tc.plugins...) - b := TestBasis(t, - WithPluginManager(pluginManager), - ) - host, err := b.Host() - if tc.errors { - require.Error(t, err) - require.Nil(t, host) - } else { - n, _ := host.PluginName() - if n != tc.expectedPluginName { - t.Error("Found unexpected plugin, ", n) - } - require.NoError(t, err) - require.NotNil(t, host) - } - } -} diff --git a/internal/plugin/testing_plugin.go b/internal/plugin/testing_plugin.go index f2ee1391f..ec4285fcf 100644 --- a/internal/plugin/testing_plugin.go +++ b/internal/plugin/testing_plugin.go @@ -9,7 +9,6 @@ import ( ) type TestPluginWithFakeBroker struct { - client interface{} } func (p *TestPluginWithFakeBroker) GRPCBroker() *plugin.GRPCBroker { @@ -47,7 +46,7 @@ func TestMinimalPlugin(t testing.T, client interface{}) *Plugin { // factories, configuration, etc. func TestPlugin(t testing.T, plg interface{}, opts ...PluginProperty) (plugin *Plugin) { mockClient := &MockClientProtocol{ - plg: &TestPluginWithFakeBroker{client: plg}, + plg: plg, } plugin = TestMinimalPlugin(t, mockClient) for _, opt := range opts { From 852a87f9d0a3ec51525c83f562380a8d0b681318 Mon Sep 17 00:00:00 2001 From: sophia Date: Mon, 2 May 2022 14:56:27 -0500 Subject: [PATCH 3/8] Add mock behaviour for getting seeds --- internal/core/basis_test.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/internal/core/basis_test.go b/internal/core/basis_test.go index df0e3c8cf..4fb4382ce 100644 --- a/internal/core/basis_test.go +++ b/internal/core/basis_test.go @@ -5,6 +5,7 @@ import ( "github.com/hashicorp/vagrant-plugin-sdk/component" componentmocks "github.com/hashicorp/vagrant-plugin-sdk/component/mocks" + "github.com/hashicorp/vagrant-plugin-sdk/core" coremocks "github.com/hashicorp/vagrant-plugin-sdk/core/mocks" "github.com/hashicorp/vagrant/internal/plugin" "github.com/stretchr/testify/mock" @@ -30,6 +31,8 @@ func BuildTestGuestPlugin() *TestGuestPlugin { p := &TestGuestPlugin{} p.On("SetPluginName", mock.AnythingOfType("string")).Return(nil) p.On("Seed", mock.AnythingOfType("*core.Seeds")).Return(nil) + p.On("Seeds").Return(core.NewSeeds(), nil) + return p } @@ -37,6 +40,8 @@ func BuildTestHostPlugin() *TestHostPlugin { p := &TestHostPlugin{} p.On("SetPluginName", mock.AnythingOfType("string")).Return(nil) p.On("Seed", mock.AnythingOfType("*core.Seeds")).Return(nil) + p.On("Seeds").Return(core.NewSeeds(), nil) + return p } @@ -44,6 +49,8 @@ func BuildTestSyncedFolderPlugin() *TestSyncedFolderPlugin { p := &TestSyncedFolderPlugin{} p.On("SetPluginName", mock.AnythingOfType("string")).Return(nil) p.On("Seed", mock.AnythingOfType("*core.Seeds")).Return(nil) + p.On("Seeds").Return(core.NewSeeds(), nil) + return p } From 8df560d6595db5d8401276dbf8014b9c40cbbb02 Mon Sep 17 00:00:00 2001 From: sophia Date: Mon, 2 May 2022 15:31:41 -0500 Subject: [PATCH 4/8] Set mock guest/host plugin name --- internal/core/basis_test.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/internal/core/basis_test.go b/internal/core/basis_test.go index 4fb4382ce..f2434d524 100644 --- a/internal/core/basis_test.go +++ b/internal/core/basis_test.go @@ -27,21 +27,21 @@ type TestSyncedFolderPlugin struct { componentmocks.SyncedFolder } -func BuildTestGuestPlugin() *TestGuestPlugin { +func BuildTestGuestPlugin(name string) *TestGuestPlugin { p := &TestGuestPlugin{} p.On("SetPluginName", mock.AnythingOfType("string")).Return(nil) p.On("Seed", mock.AnythingOfType("*core.Seeds")).Return(nil) p.On("Seeds").Return(core.NewSeeds(), nil) - + p.On("PluginName").Return(name, nil) return p } -func BuildTestHostPlugin() *TestHostPlugin { +func BuildTestHostPlugin(name string) *TestHostPlugin { p := &TestHostPlugin{} p.On("SetPluginName", mock.AnythingOfType("string")).Return(nil) p.On("Seed", mock.AnythingOfType("*core.Seeds")).Return(nil) p.On("Seeds").Return(core.NewSeeds(), nil) - + p.On("PluginName").Return(name, nil) return p } @@ -50,23 +50,22 @@ func BuildTestSyncedFolderPlugin() *TestSyncedFolderPlugin { p.On("SetPluginName", mock.AnythingOfType("string")).Return(nil) p.On("Seed", mock.AnythingOfType("*core.Seeds")).Return(nil) p.On("Seeds").Return(core.NewSeeds(), nil) - return p } func TestBasisPlugins(t *testing.T) { myguest := plugin.TestPlugin(t, - BuildTestGuestPlugin(), + BuildTestGuestPlugin("myguest"), plugin.WithPluginName("myguest"), plugin.WithPluginTypes(component.GuestType), ) myguesttwo := plugin.TestPlugin(t, - BuildTestGuestPlugin(), + BuildTestGuestPlugin("myguesttwo"), plugin.WithPluginName("myguesttwo"), plugin.WithPluginTypes(component.GuestType), ) myhost := plugin.TestPlugin(t, - BuildTestHostPlugin(), + BuildTestHostPlugin("myhost"), plugin.WithPluginName("myhost"), plugin.WithPluginTypes(component.HostType), ) From 33ecb0715694b3994035e715020c73aaeb3d07fe Mon Sep 17 00:00:00 2001 From: sophia Date: Mon, 2 May 2022 15:32:10 -0500 Subject: [PATCH 5/8] Refactor mocks to be wrapped by testing plugin objects --- internal/core/basis_test.go | 45 ---------------------------- internal/core/testing_basis.go | 55 +++++++++++++++++++++++----------- 2 files changed, 37 insertions(+), 63 deletions(-) diff --git a/internal/core/basis_test.go b/internal/core/basis_test.go index f2434d524..56350665d 100644 --- a/internal/core/basis_test.go +++ b/internal/core/basis_test.go @@ -4,55 +4,10 @@ import ( "testing" "github.com/hashicorp/vagrant-plugin-sdk/component" - componentmocks "github.com/hashicorp/vagrant-plugin-sdk/component/mocks" - "github.com/hashicorp/vagrant-plugin-sdk/core" - coremocks "github.com/hashicorp/vagrant-plugin-sdk/core/mocks" "github.com/hashicorp/vagrant/internal/plugin" - "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" ) -type TestGuestPlugin struct { - plugin.TestPluginWithFakeBroker - coremocks.Guest -} - -type TestHostPlugin struct { - plugin.TestPluginWithFakeBroker - componentmocks.Host -} - -type TestSyncedFolderPlugin struct { - plugin.TestPluginWithFakeBroker - componentmocks.SyncedFolder -} - -func BuildTestGuestPlugin(name string) *TestGuestPlugin { - p := &TestGuestPlugin{} - p.On("SetPluginName", mock.AnythingOfType("string")).Return(nil) - p.On("Seed", mock.AnythingOfType("*core.Seeds")).Return(nil) - p.On("Seeds").Return(core.NewSeeds(), nil) - p.On("PluginName").Return(name, nil) - return p -} - -func BuildTestHostPlugin(name string) *TestHostPlugin { - p := &TestHostPlugin{} - p.On("SetPluginName", mock.AnythingOfType("string")).Return(nil) - p.On("Seed", mock.AnythingOfType("*core.Seeds")).Return(nil) - p.On("Seeds").Return(core.NewSeeds(), nil) - p.On("PluginName").Return(name, nil) - return p -} - -func BuildTestSyncedFolderPlugin() *TestSyncedFolderPlugin { - p := &TestSyncedFolderPlugin{} - p.On("SetPluginName", mock.AnythingOfType("string")).Return(nil) - p.On("Seed", mock.AnythingOfType("*core.Seeds")).Return(nil) - p.On("Seeds").Return(core.NewSeeds(), nil) - return p -} - func TestBasisPlugins(t *testing.T) { myguest := plugin.TestPlugin(t, BuildTestGuestPlugin("myguest"), diff --git a/internal/core/testing_basis.go b/internal/core/testing_basis.go index dcc36fa6b..e2de22c32 100644 --- a/internal/core/testing_basis.go +++ b/internal/core/testing_basis.go @@ -5,37 +5,56 @@ import ( "io/ioutil" "os" - sdkcore "github.com/hashicorp/vagrant-plugin-sdk/core" + "github.com/hashicorp/vagrant-plugin-sdk/core" coremocks "github.com/hashicorp/vagrant-plugin-sdk/core/mocks" "github.com/hashicorp/vagrant-plugin-sdk/datadir" "github.com/hashicorp/vagrant-plugin-sdk/proto/vagrant_plugin_sdk" + "github.com/hashicorp/vagrant/internal/plugin" "github.com/hashicorp/vagrant/internal/server/singleprocess" "github.com/mitchellh/go-testing-interface" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" ) -func seededHostMock(name string) *coremocks.Host { - guestMock := &coremocks.Host{} - guestMock.On("Seeds").Return(sdkcore.NewSeeds(), nil) - guestMock.On("Seed", mock.AnythingOfType("")).Return(nil) - guestMock.On("PluginName").Return(name, nil) - return guestMock +type TestGuestPlugin struct { + plugin.TestPluginWithFakeBroker + coremocks.Guest } -func seededGuestMock(name string) *coremocks.Guest { - guestMock := &coremocks.Guest{} - guestMock.On("Seeds").Return(sdkcore.NewSeeds(), nil) - guestMock.On("Seed", mock.AnythingOfType("")).Return(nil) - guestMock.On("PluginName").Return(name, nil) - return guestMock +type TestHostPlugin struct { + plugin.TestPluginWithFakeBroker + coremocks.Host } -func seededSyncedFolderMock(name string) *coremocks.SyncedFolder { - guestMock := &coremocks.SyncedFolder{} - guestMock.On("Seeds").Return(sdkcore.NewSeeds(), nil) - guestMock.On("Seed", mock.AnythingOfType("")).Return(nil) - return guestMock +type TestSyncedFolderPlugin struct { + plugin.TestPluginWithFakeBroker + coremocks.SyncedFolder +} + +func BuildTestGuestPlugin(name string) *TestGuestPlugin { + p := &TestGuestPlugin{} + p.On("SetPluginName", mock.AnythingOfType("string")).Return(nil) + p.On("Seed", mock.AnythingOfType("*core.Seeds")).Return(nil) + p.On("Seeds").Return(core.NewSeeds(), nil) + p.On("PluginName").Return(name, nil) + return p +} + +func BuildTestHostPlugin(name string) *TestHostPlugin { + p := &TestHostPlugin{} + p.On("SetPluginName", mock.AnythingOfType("string")).Return(nil) + p.On("Seed", mock.AnythingOfType("*core.Seeds")).Return(nil) + p.On("Seeds").Return(core.NewSeeds(), nil) + p.On("PluginName").Return(name, nil) + return p +} + +func BuildTestSyncedFolderPlugin() *TestSyncedFolderPlugin { + p := &TestSyncedFolderPlugin{} + p.On("SetPluginName", mock.AnythingOfType("string")).Return(nil) + p.On("Seed", mock.AnythingOfType("*core.Seeds")).Return(nil) + p.On("Seeds").Return(core.NewSeeds(), nil) + return p } func TestBasis(t testing.T, opts ...BasisOption) (b *Basis) { From 85ea3c3d1fb91c7025a96db75ea59fb0bd827234 Mon Sep 17 00:00:00 2001 From: sophia Date: Mon, 2 May 2022 15:36:47 -0500 Subject: [PATCH 6/8] Update machine tests --- internal/core/machine_test.go | 43 ++++++++++++++++------------------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/internal/core/machine_test.go b/internal/core/machine_test.go index 48922f146..2db1518ff 100644 --- a/internal/core/machine_test.go +++ b/internal/core/machine_test.go @@ -128,15 +128,16 @@ func TestMachineConfigedGuest(t *testing.T) { {config: &vagrant_plugin_sdk.Vagrantfile_ConfigVM{Guest: "idontexist"}, errors: true}, } - guestMock := seededGuestMock("myguest") pluginManager := plugin.TestManager(t, plugin.TestPlugin(t, + BuildTestGuestPlugin("myguest"), plugin.WithPluginName("myguest"), - plugin.WithPluginMinimalComponents(component.GuestType, guestMock)), + plugin.WithPluginTypes(component.GuestType), + ), ) - tp := TestProject(t, WithPluginManager(pluginManager)) for _, tc := range tests { + tp := TestProject(t, WithPluginManager(pluginManager)) tm, _ := TestMachine(t, tp, WithTestTargetConfig(&vagrant_plugin_sdk.Vagrantfile_MachineConfig{ ConfigVm: tc.config, @@ -156,32 +157,29 @@ func TestMachineConfigedGuest(t *testing.T) { } func TestMachineNoConfigGuest(t *testing.T) { - guestMock := seededGuestMock("myguest") + guestMock := BuildTestGuestPlugin("myguest") guestMock.On("Detect", mock.AnythingOfType("*core.Machine")).Return(true, nil) - detectPluginInstance := plugin.TestPluginInstance(t, - plugin.WithPluginInstanceName("myguest"), - plugin.WithPluginInstanceType(component.GuestType), - plugin.WithPluginInstanceComponent(guestMock)) detectingPlugin := plugin.TestPlugin(t, + guestMock, plugin.WithPluginName("myguest"), - plugin.WithPluginInstance(detectPluginInstance)) + plugin.WithPluginTypes(component.GuestType), + ) - notGuestMock := seededGuestMock("mynondetectingguest") + notGuestMock := BuildTestGuestPlugin("mynondetectingguest") notGuestMock.On("Detect", mock.AnythingOfType("*core.Machine")).Return(false, nil) nonDetectingPlugin := plugin.TestPlugin(t, + notGuestMock, plugin.WithPluginName("mynondetectingguest"), - plugin.WithPluginMinimalComponents(component.GuestType, notGuestMock)) + plugin.WithPluginTypes(component.GuestType), + ) - guestChildMock := seededGuestMock("myguest-child") + guestChildMock := BuildTestGuestPlugin("myguest-child") guestChildMock.On("Detect", mock.AnythingOfType("*core.Machine")).Return(true, nil) - detectChildPluginInstance := plugin.TestPluginInstance(t, - plugin.WithPluginInstanceName("myguest-child"), - plugin.WithPluginInstanceType(component.GuestType), - plugin.WithPluginInstanceComponent(guestChildMock), - plugin.WithPluginInstanceParent(detectPluginInstance)) + guestChildMock.On("Parent").Return("myguest", nil) detectingChildPlugin := plugin.TestPlugin(t, + guestChildMock, plugin.WithPluginName("myguest-child"), - plugin.WithPluginInstance(detectChildPluginInstance), + plugin.WithPluginTypes(component.GuestType), ) type test struct { @@ -259,14 +257,11 @@ func TestMachineSetState(t *testing.T) { } func syncedFolderPlugin(t *testing.T, name string) *plugin.Plugin { - mock := seededSyncedFolderMock(name) - plugInst := plugin.TestPluginInstance(t, - plugin.WithPluginInstanceName(name), - plugin.WithPluginInstanceType(component.SyncedFolderType), - plugin.WithPluginInstanceComponent(mock)) return plugin.TestPlugin(t, + BuildTestSyncedFolderPlugin(), plugin.WithPluginName(name), - plugin.WithPluginInstance(plugInst)) + plugin.WithPluginTypes(component.SyncedFolderType), + ) } func TestMachineSyncedFolders(t *testing.T) { mySyncedFolder := syncedFolderPlugin(t, "mysyncedfolder") From d135f24f498a312c8f2d1389afb418e578d67cfb Mon Sep 17 00:00:00 2001 From: sophia Date: Mon, 2 May 2022 15:52:36 -0500 Subject: [PATCH 7/8] Add target specialization tests --- internal/core/target_test.go | 37 ++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/internal/core/target_test.go b/internal/core/target_test.go index b79b386f3..5db939c94 100644 --- a/internal/core/target_test.go +++ b/internal/core/target_test.go @@ -4,19 +4,52 @@ import ( "testing" "github.com/hashicorp/vagrant-plugin-sdk/core" + "github.com/hashicorp/vagrant/internal/server/proto/vagrant_server" + "github.com/stretchr/testify/require" ) func TestTargetSpecializeMachine(t *testing.T) { tt, _ := TestMinimalTarget(t) specialized, err := tt.Specialize((*core.Machine)(nil)) - if err != nil { t.Errorf("Specialize function returned an error") } - if _, ok := specialized.(core.Machine); !ok { t.Errorf("Unable to specialize a target to a machine") } + + // Get machine from the cache, should be the same machine + reSpecialized, err := tt.Specialize((*core.Machine)(nil)) + if err != nil { + t.Errorf("Specialize function returned an error") + } + require.Equal(t, reSpecialized, specialized) +} + +func TestTargetSpecializeMultiMachine(t *testing.T) { + p := TestMinimalProject(t) + tt1, _ := TestTarget(t, p, &vagrant_server.Target{Name: "tt1"}) + tt2, _ := TestTarget(t, p, &vagrant_server.Target{Name: "tt2"}) + + specialized, err := tt1.Specialize((*core.Machine)(nil)) + if err != nil { + t.Errorf("Specialize function returned an error") + } + if _, ok := specialized.(core.Machine); !ok { + t.Errorf("Unable to specialize a target to a machine") + } + specializedName, _ := specialized.(core.Machine).Name() + + specialized2, err := tt2.Specialize((*core.Machine)(nil)) + if err != nil { + t.Errorf("Specialize function returned an error") + } + if _, ok := specialized2.(core.Machine); !ok { + t.Errorf("Unable to specialize a target to a machine") + } + specialized2Name, _ := specialized2.(core.Machine).Name() + + require.NotEqual(t, specializedName, specialized2Name) } func TestTargetSpecializeBad(t *testing.T) { From 692dcb1b9258cd810259fc002706a1b52f29d62e Mon Sep 17 00:00:00 2001 From: sophia Date: Mon, 2 May 2022 16:42:40 -0500 Subject: [PATCH 8/8] Allow testing plugins to have parent plugins --- internal/core/basis_test.go | 8 ++++---- internal/core/machine_test.go | 15 ++++++++------- internal/core/testing_basis.go | 23 ++++++++++++++++++++--- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/internal/core/basis_test.go b/internal/core/basis_test.go index 56350665d..edc7619a4 100644 --- a/internal/core/basis_test.go +++ b/internal/core/basis_test.go @@ -10,22 +10,22 @@ import ( func TestBasisPlugins(t *testing.T) { myguest := plugin.TestPlugin(t, - BuildTestGuestPlugin("myguest"), + BuildTestGuestPlugin("myguest", ""), plugin.WithPluginName("myguest"), plugin.WithPluginTypes(component.GuestType), ) myguesttwo := plugin.TestPlugin(t, - BuildTestGuestPlugin("myguesttwo"), + BuildTestGuestPlugin("myguesttwo", ""), plugin.WithPluginName("myguesttwo"), plugin.WithPluginTypes(component.GuestType), ) myhost := plugin.TestPlugin(t, - BuildTestHostPlugin("myhost"), + BuildTestHostPlugin("myhost", ""), plugin.WithPluginName("myhost"), plugin.WithPluginTypes(component.HostType), ) mysf := plugin.TestPlugin(t, - BuildTestSyncedFolderPlugin(), + BuildTestSyncedFolderPlugin(""), plugin.WithPluginName("mysf"), plugin.WithPluginTypes(component.SyncedFolderType), ) diff --git a/internal/core/machine_test.go b/internal/core/machine_test.go index 2db1518ff..b5ca2e7b6 100644 --- a/internal/core/machine_test.go +++ b/internal/core/machine_test.go @@ -130,7 +130,7 @@ func TestMachineConfigedGuest(t *testing.T) { pluginManager := plugin.TestManager(t, plugin.TestPlugin(t, - BuildTestGuestPlugin("myguest"), + BuildTestGuestPlugin("myguest", ""), plugin.WithPluginName("myguest"), plugin.WithPluginTypes(component.GuestType), ), @@ -157,15 +157,16 @@ func TestMachineConfigedGuest(t *testing.T) { } func TestMachineNoConfigGuest(t *testing.T) { - guestMock := BuildTestGuestPlugin("myguest") + guestMock := BuildTestGuestPlugin("myguest", "") guestMock.On("Detect", mock.AnythingOfType("*core.Machine")).Return(true, nil) + guestMock.On("Parent").Return("", nil) detectingPlugin := plugin.TestPlugin(t, guestMock, plugin.WithPluginName("myguest"), plugin.WithPluginTypes(component.GuestType), ) - notGuestMock := BuildTestGuestPlugin("mynondetectingguest") + notGuestMock := BuildTestGuestPlugin("mynondetectingguest", "") notGuestMock.On("Detect", mock.AnythingOfType("*core.Machine")).Return(false, nil) nonDetectingPlugin := plugin.TestPlugin(t, notGuestMock, @@ -173,9 +174,9 @@ func TestMachineNoConfigGuest(t *testing.T) { plugin.WithPluginTypes(component.GuestType), ) - guestChildMock := BuildTestGuestPlugin("myguest-child") + guestChildMock := BuildTestGuestPlugin("myguest-child", "myguest") guestChildMock.On("Detect", mock.AnythingOfType("*core.Machine")).Return(true, nil) - guestChildMock.On("Parent").Return("myguest", nil) + guestChildMock.SetParentComponent(guestMock) detectingChildPlugin := plugin.TestPlugin(t, guestChildMock, plugin.WithPluginName("myguest-child"), @@ -190,7 +191,7 @@ func TestMachineNoConfigGuest(t *testing.T) { tests := []test{ {plugins: []*plugin.Plugin{detectingPlugin}, errors: false, expectedPluginName: "myguest"}, - {plugins: []*plugin.Plugin{detectingChildPlugin}, errors: false, expectedPluginName: "myguest-child"}, + {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}, @@ -258,7 +259,7 @@ func TestMachineSetState(t *testing.T) { func syncedFolderPlugin(t *testing.T, name string) *plugin.Plugin { return plugin.TestPlugin(t, - BuildTestSyncedFolderPlugin(), + BuildTestSyncedFolderPlugin(""), plugin.WithPluginName(name), plugin.WithPluginTypes(component.SyncedFolderType), ) diff --git a/internal/core/testing_basis.go b/internal/core/testing_basis.go index e2de22c32..f1052733b 100644 --- a/internal/core/testing_basis.go +++ b/internal/core/testing_basis.go @@ -16,44 +16,61 @@ import ( "github.com/stretchr/testify/require" ) +type PluginWithParent struct { + parentPlugin interface{} +} + +func (p *PluginWithParent) GetParentComponent() interface{} { + return p.parentPlugin +} +func (p *PluginWithParent) SetParentComponent(in interface{}) { + p.parentPlugin = in +} + type TestGuestPlugin struct { + PluginWithParent plugin.TestPluginWithFakeBroker coremocks.Guest } type TestHostPlugin struct { + PluginWithParent plugin.TestPluginWithFakeBroker coremocks.Host } type TestSyncedFolderPlugin struct { + PluginWithParent plugin.TestPluginWithFakeBroker coremocks.SyncedFolder } -func BuildTestGuestPlugin(name string) *TestGuestPlugin { +func BuildTestGuestPlugin(name string, parent string) *TestGuestPlugin { p := &TestGuestPlugin{} p.On("SetPluginName", mock.AnythingOfType("string")).Return(nil) p.On("Seed", mock.AnythingOfType("*core.Seeds")).Return(nil) p.On("Seeds").Return(core.NewSeeds(), nil) p.On("PluginName").Return(name, nil) + p.On("Parent").Return(parent, nil) return p } -func BuildTestHostPlugin(name string) *TestHostPlugin { +func BuildTestHostPlugin(name string, parent string) *TestHostPlugin { p := &TestHostPlugin{} p.On("SetPluginName", mock.AnythingOfType("string")).Return(nil) p.On("Seed", mock.AnythingOfType("*core.Seeds")).Return(nil) p.On("Seeds").Return(core.NewSeeds(), nil) p.On("PluginName").Return(name, nil) + p.On("Parent").Return(parent, nil) return p } -func BuildTestSyncedFolderPlugin() *TestSyncedFolderPlugin { +func BuildTestSyncedFolderPlugin(parent string) *TestSyncedFolderPlugin { p := &TestSyncedFolderPlugin{} p.On("SetPluginName", mock.AnythingOfType("string")).Return(nil) p.On("Seed", mock.AnythingOfType("*core.Seeds")).Return(nil) p.On("Seeds").Return(core.NewSeeds(), nil) + p.On("Parent").Return(parent, nil) return p }