Update testing plugins
This commit is contained in:
parent
db14bf00e2
commit
c712afad8f
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user