From c712afad8faa5399cb827bfc0e00321b6de08b50 Mon Sep 17 00:00:00 2001 From: sophia Date: Mon, 2 May 2022 12:33:18 -0500 Subject: [PATCH] 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 + } +}