10 "github.com/mitchellh/go-testing-interface"
11 hclog "github.com/hashicorp/go-hclog"
12 "github.com/hashicorp/go-plugin/internal/plugin"
13 "google.golang.org/grpc"
16 // TestOptions allows specifying options that can affect the behavior of the
18 type TestOptions struct {
19 //ServerStdout causes the given value to be used in place of a blank buffer
20 //for RPCServer's Stdout
21 ServerStdout io.ReadCloser
23 //ServerStderr causes the given value to be used in place of a blank buffer
24 //for RPCServer's Stderr
25 ServerStderr io.ReadCloser
28 // The testing file contains test helpers that you can use outside of
29 // this package for making it easier to test plugins themselves.
31 // TestConn is a helper function for returning a client and server
32 // net.Conn connected to each other.
33 func TestConn(t testing.T) (net.Conn, net.Conn) {
34 // Listen to any local port. This listener will be closed
35 // after a single connection is established.
36 l, err := net.Listen("tcp", "127.0.0.1:0")
38 t.Fatalf("err: %s", err)
41 // Start a goroutine to accept our client connection
42 var serverConn net.Conn
43 doneCh := make(chan struct{})
48 serverConn, err = l.Accept()
50 t.Fatalf("err: %s", err)
54 // Connect to the server
55 clientConn, err := net.Dial("tcp", l.Addr().String())
57 t.Fatalf("err: %s", err)
60 // Wait for the server side to acknowledge it has connected
63 return clientConn, serverConn
66 // TestRPCConn returns a rpc client and server connected to each other.
67 func TestRPCConn(t testing.T) (*rpc.Client, *rpc.Server) {
68 clientConn, serverConn := TestConn(t)
70 server := rpc.NewServer()
71 go server.ServeConn(serverConn)
73 client := rpc.NewClient(clientConn)
77 // TestPluginRPCConn returns a plugin RPC client and server that are connected
78 // together and configured.
79 func TestPluginRPCConn(t testing.T, ps map[string]Plugin, opts *TestOptions) (*RPCClient, *RPCServer) {
80 // Create two net.Conns we can use to shuttle our control connection
81 clientConn, serverConn := TestConn(t)
83 // Start up the server
84 server := &RPCServer{Plugins: ps, Stdout: new(bytes.Buffer), Stderr: new(bytes.Buffer)}
86 if opts.ServerStdout != nil {
87 server.Stdout = opts.ServerStdout
89 if opts.ServerStderr != nil {
90 server.Stderr = opts.ServerStderr
93 go server.ServeConn(serverConn)
95 // Connect the client to the server
96 client, err := NewRPCClient(clientConn, ps)
98 t.Fatalf("err: %s", err)
101 return client, server
104 // TestGRPCConn returns a gRPC client conn and grpc server that are connected
105 // together and configured. The register function is used to register services
106 // prior to the Serve call. This is used to test gRPC connections.
107 func TestGRPCConn(t testing.T, register func(*grpc.Server)) (*grpc.ClientConn, *grpc.Server) {
109 l, err := net.Listen("tcp", "127.0.0.1:0")
111 t.Fatalf("err: %s", err)
114 server := grpc.NewServer()
118 // Connect to the server
119 conn, err := grpc.Dial(
124 t.Fatalf("err: %s", err)
127 // Connection successful, close the listener
133 // TestPluginGRPCConn returns a plugin gRPC client and server that are connected
134 // together and configured. This is used to test gRPC connections.
135 func TestPluginGRPCConn(t testing.T, ps map[string]Plugin) (*GRPCClient, *GRPCServer) {
137 l, err := net.Listen("tcp", "127.0.0.1:0")
139 t.Fatalf("err: %s", err)
142 // Start up the server
143 server := &GRPCServer{
145 DoneCh: make(chan struct{}),
146 Server: DefaultGRPCServer,
147 Stdout: new(bytes.Buffer),
148 Stderr: new(bytes.Buffer),
149 logger: hclog.Default(),
151 if err := server.Init(); err != nil {
152 t.Fatalf("err: %s", err)
156 // Connect to the server
157 conn, err := grpc.Dial(
162 t.Fatalf("err: %s", err)
165 brokerGRPCClient := newGRPCBrokerClient(conn)
166 broker := newGRPCBroker(brokerGRPCClient, nil)
168 go brokerGRPCClient.StartStream()
171 client := &GRPCClient{
175 doneCtx: context.Background(),
176 controller: plugin.NewGRPCControllerClient(conn),
179 return client, server