2016-02-12 18:02:16 +00:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-07-17 20:41:40 +00:00
|
|
|
"context"
|
|
|
|
"io"
|
2016-02-12 18:02:16 +00:00
|
|
|
"net"
|
|
|
|
"net/rpc"
|
2017-07-21 19:11:21 +00:00
|
|
|
|
2017-07-25 21:21:59 +00:00
|
|
|
"github.com/mitchellh/go-testing-interface"
|
2019-10-04 17:23:35 +00:00
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/hashicorp/go-plugin/internal/plugin"
|
2017-07-21 19:11:21 +00:00
|
|
|
"google.golang.org/grpc"
|
2016-02-12 18:02:16 +00:00
|
|
|
)
|
|
|
|
|
2018-07-17 20:41:40 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2016-02-12 18:02:16 +00:00
|
|
|
// The testing file contains test helpers that you can use outside of
|
|
|
|
// this package for making it easier to test plugins themselves.
|
|
|
|
|
|
|
|
// TestConn is a helper function for returning a client and server
|
|
|
|
// net.Conn connected to each other.
|
2017-07-25 21:21:59 +00:00
|
|
|
func TestConn(t testing.T) (net.Conn, net.Conn) {
|
2016-02-12 18:02:16 +00:00
|
|
|
// Listen to any local port. This listener will be closed
|
|
|
|
// after a single connection is established.
|
|
|
|
l, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start a goroutine to accept our client connection
|
|
|
|
var serverConn net.Conn
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(doneCh)
|
|
|
|
defer l.Close()
|
|
|
|
var err error
|
|
|
|
serverConn, err = l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Connect to the server
|
|
|
|
clientConn, err := net.Dial("tcp", l.Addr().String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the server side to acknowledge it has connected
|
|
|
|
<-doneCh
|
|
|
|
|
|
|
|
return clientConn, serverConn
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestRPCConn returns a rpc client and server connected to each other.
|
2017-07-25 21:21:59 +00:00
|
|
|
func TestRPCConn(t testing.T) (*rpc.Client, *rpc.Server) {
|
2016-02-12 18:02:16 +00:00
|
|
|
clientConn, serverConn := TestConn(t)
|
|
|
|
|
|
|
|
server := rpc.NewServer()
|
|
|
|
go server.ServeConn(serverConn)
|
|
|
|
|
|
|
|
client := rpc.NewClient(clientConn)
|
|
|
|
return client, server
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestPluginRPCConn returns a plugin RPC client and server that are connected
|
|
|
|
// together and configured.
|
2018-07-17 20:41:40 +00:00
|
|
|
func TestPluginRPCConn(t testing.T, ps map[string]Plugin, opts *TestOptions) (*RPCClient, *RPCServer) {
|
2016-02-12 18:02:16 +00:00
|
|
|
// 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)}
|
2018-07-17 20:41:40 +00:00
|
|
|
if opts != nil {
|
|
|
|
if opts.ServerStdout != nil {
|
|
|
|
server.Stdout = opts.ServerStdout
|
|
|
|
}
|
|
|
|
if opts.ServerStderr != nil {
|
|
|
|
server.Stderr = opts.ServerStderr
|
|
|
|
}
|
|
|
|
}
|
2016-02-12 18:02:16 +00:00
|
|
|
go server.ServeConn(serverConn)
|
|
|
|
|
|
|
|
// Connect the client to the server
|
|
|
|
client, err := NewRPCClient(clientConn, ps)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return client, server
|
|
|
|
}
|
2017-07-21 19:11:21 +00:00
|
|
|
|
2018-07-17 20:41:40 +00:00
|
|
|
// TestGRPCConn returns a gRPC client conn and grpc server that are connected
|
|
|
|
// together and configured. The register function is used to register services
|
|
|
|
// prior to the Serve call. This is used to test gRPC connections.
|
|
|
|
func TestGRPCConn(t testing.T, register func(*grpc.Server)) (*grpc.ClientConn, *grpc.Server) {
|
|
|
|
// Create a listener
|
|
|
|
l, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
server := grpc.NewServer()
|
|
|
|
register(server)
|
|
|
|
go server.Serve(l)
|
|
|
|
|
|
|
|
// Connect to the server
|
|
|
|
conn, err := grpc.Dial(
|
|
|
|
l.Addr().String(),
|
|
|
|
grpc.WithBlock(),
|
|
|
|
grpc.WithInsecure())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connection successful, close the listener
|
|
|
|
l.Close()
|
|
|
|
|
|
|
|
return conn, server
|
|
|
|
}
|
|
|
|
|
2017-07-21 19:11:21 +00:00
|
|
|
// TestPluginGRPCConn returns a plugin gRPC client and server that are connected
|
|
|
|
// together and configured. This is used to test gRPC connections.
|
2017-07-25 21:21:59 +00:00
|
|
|
func TestPluginGRPCConn(t testing.T, ps map[string]Plugin) (*GRPCClient, *GRPCServer) {
|
2017-07-21 19:11:21 +00:00
|
|
|
// Create a listener
|
|
|
|
l, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start up the server
|
|
|
|
server := &GRPCServer{
|
|
|
|
Plugins: ps,
|
2018-12-12 15:54:19 +00:00
|
|
|
DoneCh: make(chan struct{}),
|
2017-07-21 19:11:21 +00:00
|
|
|
Server: DefaultGRPCServer,
|
|
|
|
Stdout: new(bytes.Buffer),
|
|
|
|
Stderr: new(bytes.Buffer),
|
2018-12-12 15:54:19 +00:00
|
|
|
logger: hclog.Default(),
|
2017-07-21 19:11:21 +00:00
|
|
|
}
|
|
|
|
if err := server.Init(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
go server.Serve(l)
|
|
|
|
|
|
|
|
// Connect to the server
|
|
|
|
conn, err := grpc.Dial(
|
|
|
|
l.Addr().String(),
|
|
|
|
grpc.WithBlock(),
|
|
|
|
grpc.WithInsecure())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-07-17 20:41:40 +00:00
|
|
|
brokerGRPCClient := newGRPCBrokerClient(conn)
|
|
|
|
broker := newGRPCBroker(brokerGRPCClient, nil)
|
|
|
|
go broker.Run()
|
|
|
|
go brokerGRPCClient.StartStream()
|
2017-07-21 19:11:21 +00:00
|
|
|
|
|
|
|
// Create the client
|
|
|
|
client := &GRPCClient{
|
2018-12-12 15:54:19 +00:00
|
|
|
Conn: conn,
|
|
|
|
Plugins: ps,
|
|
|
|
broker: broker,
|
|
|
|
doneCtx: context.Background(),
|
2019-10-04 17:23:35 +00:00
|
|
|
controller: plugin.NewGRPCControllerClient(conn),
|
2017-07-21 19:11:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return client, server
|
|
|
|
}
|