aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/hashicorp/go-plugin/testing.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/hashicorp/go-plugin/testing.go')
-rw-r--r--vendor/github.com/hashicorp/go-plugin/testing.go86
1 files changed, 82 insertions, 4 deletions
diff --git a/vendor/github.com/hashicorp/go-plugin/testing.go b/vendor/github.com/hashicorp/go-plugin/testing.go
index 9086a1b..df29593 100644
--- a/vendor/github.com/hashicorp/go-plugin/testing.go
+++ b/vendor/github.com/hashicorp/go-plugin/testing.go
@@ -2,9 +2,12 @@ package plugin
2 2
3import ( 3import (
4 "bytes" 4 "bytes"
5 "context"
5 "net" 6 "net"
6 "net/rpc" 7 "net/rpc"
7 "testing" 8
9 "github.com/mitchellh/go-testing-interface"
10 "google.golang.org/grpc"
8) 11)
9 12
10// The testing file contains test helpers that you can use outside of 13// The testing file contains test helpers that you can use outside of
@@ -12,7 +15,7 @@ import (
12 15
13// TestConn is a helper function for returning a client and server 16// TestConn is a helper function for returning a client and server
14// net.Conn connected to each other. 17// net.Conn connected to each other.
15func TestConn(t *testing.T) (net.Conn, net.Conn) { 18func TestConn(t testing.T) (net.Conn, net.Conn) {
16 // Listen to any local port. This listener will be closed 19 // Listen to any local port. This listener will be closed
17 // after a single connection is established. 20 // after a single connection is established.
18 l, err := net.Listen("tcp", "127.0.0.1:0") 21 l, err := net.Listen("tcp", "127.0.0.1:0")
@@ -46,7 +49,7 @@ func TestConn(t *testing.T) (net.Conn, net.Conn) {
46} 49}
47 50
48// TestRPCConn returns a rpc client and server connected to each other. 51// TestRPCConn returns a rpc client and server connected to each other.
49func TestRPCConn(t *testing.T) (*rpc.Client, *rpc.Server) { 52func TestRPCConn(t testing.T) (*rpc.Client, *rpc.Server) {
50 clientConn, serverConn := TestConn(t) 53 clientConn, serverConn := TestConn(t)
51 54
52 server := rpc.NewServer() 55 server := rpc.NewServer()
@@ -58,7 +61,7 @@ func TestRPCConn(t *testing.T) (*rpc.Client, *rpc.Server) {
58 61
59// TestPluginRPCConn returns a plugin RPC client and server that are connected 62// TestPluginRPCConn returns a plugin RPC client and server that are connected
60// together and configured. 63// together and configured.
61func TestPluginRPCConn(t *testing.T, ps map[string]Plugin) (*RPCClient, *RPCServer) { 64func TestPluginRPCConn(t testing.T, ps map[string]Plugin) (*RPCClient, *RPCServer) {
62 // Create two net.Conns we can use to shuttle our control connection 65 // Create two net.Conns we can use to shuttle our control connection
63 clientConn, serverConn := TestConn(t) 66 clientConn, serverConn := TestConn(t)
64 67
@@ -74,3 +77,78 @@ func TestPluginRPCConn(t *testing.T, ps map[string]Plugin) (*RPCClient, *RPCServ
74 77
75 return client, server 78 return client, server
76} 79}
80
81// TestGRPCConn returns a gRPC client conn and grpc server that are connected
82// together and configured. The register function is used to register services
83// prior to the Serve call. This is used to test gRPC connections.
84func TestGRPCConn(t testing.T, register func(*grpc.Server)) (*grpc.ClientConn, *grpc.Server) {
85 // Create a listener
86 l, err := net.Listen("tcp", "127.0.0.1:0")
87 if err != nil {
88 t.Fatalf("err: %s", err)
89 }
90
91 server := grpc.NewServer()
92 register(server)
93 go server.Serve(l)
94
95 // Connect to the server
96 conn, err := grpc.Dial(
97 l.Addr().String(),
98 grpc.WithBlock(),
99 grpc.WithInsecure())
100 if err != nil {
101 t.Fatalf("err: %s", err)
102 }
103
104 // Connection successful, close the listener
105 l.Close()
106
107 return conn, server
108}
109
110// TestPluginGRPCConn returns a plugin gRPC client and server that are connected
111// together and configured. This is used to test gRPC connections.
112func TestPluginGRPCConn(t testing.T, ps map[string]Plugin) (*GRPCClient, *GRPCServer) {
113 // Create a listener
114 l, err := net.Listen("tcp", "127.0.0.1:0")
115 if err != nil {
116 t.Fatalf("err: %s", err)
117 }
118
119 // Start up the server
120 server := &GRPCServer{
121 Plugins: ps,
122 Server: DefaultGRPCServer,
123 Stdout: new(bytes.Buffer),
124 Stderr: new(bytes.Buffer),
125 }
126 if err := server.Init(); err != nil {
127 t.Fatalf("err: %s", err)
128 }
129 go server.Serve(l)
130
131 // Connect to the server
132 conn, err := grpc.Dial(
133 l.Addr().String(),
134 grpc.WithBlock(),
135 grpc.WithInsecure())
136 if err != nil {
137 t.Fatalf("err: %s", err)
138 }
139
140 brokerGRPCClient := newGRPCBrokerClient(conn)
141 broker := newGRPCBroker(brokerGRPCClient, nil)
142 go broker.Run()
143 go brokerGRPCClient.StartStream()
144
145 // Create the client
146 client := &GRPCClient{
147 Conn: conn,
148 Plugins: ps,
149 broker: broker,
150 doneCtx: context.Background(),
151 }
152
153 return client, server
154}