import (
"bytes"
"context"
+ "io"
"net"
"net/rpc"
"github.com/mitchellh/go-testing-interface"
+ hclog "github.com/hashicorp/go-hclog"
+ "github.com/hashicorp/go-plugin/internal/plugin"
"google.golang.org/grpc"
)
+// TestOptions allows specifying options that can affect the behavior of the
+// test functions
+type TestOptions struct {
+ //ServerStdout causes the given value to be used in place of a blank buffer
+ //for RPCServer's Stdout
+ ServerStdout io.ReadCloser
+
+ //ServerStderr causes the given value to be used in place of a blank buffer
+ //for RPCServer's Stderr
+ ServerStderr io.ReadCloser
+}
+
// The testing file contains test helpers that you can use outside of
// this package for making it easier to test plugins themselves.
// TestPluginRPCConn returns a plugin RPC client and server that are connected
// together and configured.
-func TestPluginRPCConn(t testing.T, ps map[string]Plugin) (*RPCClient, *RPCServer) {
+func TestPluginRPCConn(t testing.T, ps map[string]Plugin, opts *TestOptions) (*RPCClient, *RPCServer) {
// Create two net.Conns we can use to shuttle our control connection
clientConn, serverConn := TestConn(t)
// Start up the server
server := &RPCServer{Plugins: ps, Stdout: new(bytes.Buffer), Stderr: new(bytes.Buffer)}
+ if opts != nil {
+ if opts.ServerStdout != nil {
+ server.Stdout = opts.ServerStdout
+ }
+ if opts.ServerStderr != nil {
+ server.Stderr = opts.ServerStderr
+ }
+ }
go server.ServeConn(serverConn)
// Connect the client to the server
// Start up the server
server := &GRPCServer{
Plugins: ps,
+ DoneCh: make(chan struct{}),
Server: DefaultGRPCServer,
Stdout: new(bytes.Buffer),
Stderr: new(bytes.Buffer),
+ logger: hclog.Default(),
}
if err := server.Init(); err != nil {
t.Fatalf("err: %s", err)
// Create the client
client := &GRPCClient{
- Conn: conn,
- Plugins: ps,
- broker: broker,
- doneCtx: context.Background(),
+ Conn: conn,
+ Plugins: ps,
+ broker: broker,
+ doneCtx: context.Background(),
+ controller: plugin.NewGRPCControllerClient(conn),
}
return client, server