Update testing plugins

This commit is contained in:
sophia 2022-05-02 12:33:18 -05:00
parent db14bf00e2
commit c712afad8f

View File

@ -2,13 +2,42 @@ package plugin
import ( import (
"github.com/hashicorp/go-hclog" "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" "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{ plugin := &Plugin{
Location: "test", Location: "test",
Client: client.(plugin.ClientProtocol),
logger: hclog.New(&hclog.LoggerOptions{}), logger: hclog.New(&hclog.LoggerOptions{}),
cleaner: cleanup.New(),
} }
return plugin return plugin
} }
@ -16,8 +45,11 @@ func TestMinimalPlugin(t testing.T) *Plugin {
// TestPlugin returns a fully in-memory and side-effect free Plugin that // 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 // can be used for testing. Additional options can be given to provide your own
// factories, configuration, etc. // factories, configuration, etc.
func TestPlugin(t testing.T, opts ...PluginProperty) (plugin *Plugin) { func TestPlugin(t testing.T, plg interface{}, opts ...PluginProperty) (plugin *Plugin) {
plugin = TestMinimalPlugin(t) mockClient := &MockClientProtocol{
plg: &TestPluginWithFakeBroker{client: plg},
}
plugin = TestMinimalPlugin(t, mockClient)
for _, opt := range opts { for _, opt := range opts {
if err := opt(plugin); err != nil { if err := opt(plugin); err != nil {
t.Error(err) t.Error(err)
@ -34,3 +66,10 @@ func WithPluginName(name string) PluginProperty {
return return
} }
} }
func WithPluginTypes(types ...component.Type) PluginProperty {
return func(p *Plugin) (err error) {
p.Types = types
return
}
}