1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
package plugin
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"log"
"net/rpc"
"os"
"os/exec"
"testing"
"time"
"github.com/hashicorp/go-plugin/test/grpc"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
// Test that NetRPCUnsupportedPlugin implements the correct interfaces.
var _ Plugin = new(NetRPCUnsupportedPlugin)
// testAPIVersion is the ProtocolVersion we use for testing.
var testHandshake = HandshakeConfig{
ProtocolVersion: 1,
MagicCookieKey: "TEST_MAGIC_COOKIE",
MagicCookieValue: "test",
}
// testInterface is the test interface we use for plugins.
type testInterface interface {
Double(int) int
}
// testInterfacePlugin is the implementation of Plugin to create
// RPC client/server implementations for testInterface.
type testInterfacePlugin struct{}
func (p *testInterfacePlugin) Server(b *MuxBroker) (interface{}, error) {
return &testInterfaceServer{Impl: new(testInterfaceImpl)}, nil
}
func (p *testInterfacePlugin) Client(b *MuxBroker, c *rpc.Client) (interface{}, error) {
return &testInterfaceClient{Client: c}, nil
}
func (p *testInterfacePlugin) GRPCServer(s *grpc.Server) error {
grpctest.RegisterTestServer(s, &testGRPCServer{Impl: new(testInterfaceImpl)})
return nil
}
func (p *testInterfacePlugin) GRPCClient(c *grpc.ClientConn) (interface{}, error) {
return &testGRPCClient{Client: grpctest.NewTestClient(c)}, nil
}
// testInterfaceImpl implements testInterface concretely
type testInterfaceImpl struct{}
func (i *testInterfaceImpl) Double(v int) int { return v * 2 }
// testInterfaceClient implements testInterface to communicate over RPC
type testInterfaceClient struct {
Client *rpc.Client
}
func (impl *testInterfaceClient) Double(v int) int {
var resp int
err := impl.Client.Call("Plugin.Double", v, &resp)
if err != nil {
panic(err)
}
return resp
}
// testInterfaceServer is the RPC server for testInterfaceClient
type testInterfaceServer struct {
Broker *MuxBroker
Impl testInterface
}
func (s *testInterfaceServer) Double(arg int, resp *int) error {
*resp = s.Impl.Double(arg)
return nil
}
// testPluginMap can be used for tests as a plugin map
var testPluginMap = map[string]Plugin{
"test": new(testInterfacePlugin),
}
// testGRPCServer is the implementation of our GRPC service.
type testGRPCServer struct {
Impl testInterface
}
func (s *testGRPCServer) Double(
ctx context.Context,
req *grpctest.TestRequest) (*grpctest.TestResponse, error) {
return &grpctest.TestResponse{
Output: int32(s.Impl.Double(int(req.Input))),
}, nil
}
// testGRPCClient is an implementation of TestInterface that communicates
// over gRPC.
type testGRPCClient struct {
Client grpctest.TestClient
}
func (c *testGRPCClient) Double(v int) int {
resp, err := c.Client.Double(context.Background(), &grpctest.TestRequest{
Input: int32(v),
})
if err != nil {
panic(err)
}
return int(resp.Output)
}
func helperProcess(s ...string) *exec.Cmd {
cs := []string{"-test.run=TestHelperProcess", "--"}
cs = append(cs, s...)
env := []string{
"GO_WANT_HELPER_PROCESS=1",
"PLUGIN_MIN_PORT=10000",
"PLUGIN_MAX_PORT=25000",
}
cmd := exec.Command(os.Args[0], cs...)
cmd.Env = append(env, os.Environ()...)
return cmd
}
// This is not a real test. This is just a helper process kicked off by
// tests.
func TestHelperProcess(*testing.T) {
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
return
}
defer os.Exit(0)
args := os.Args
for len(args) > 0 {
if args[0] == "--" {
args = args[1:]
break
}
args = args[1:]
}
if len(args) == 0 {
fmt.Fprintf(os.Stderr, "No command\n")
os.Exit(2)
}
cmd, args := args[0], args[1:]
switch cmd {
case "bad-version":
// If we have an arg, we write there on start
if len(args) > 0 {
path := args[0]
err := ioutil.WriteFile(path, []byte("foo"), 0644)
if err != nil {
panic(err)
}
}
fmt.Printf("%d|%d1|tcp|:1234\n", CoreProtocolVersion, testHandshake.ProtocolVersion)
<-make(chan int)
case "invalid-rpc-address":
fmt.Println("lolinvalid")
case "mock":
fmt.Printf("%d|%d|tcp|:1234\n", CoreProtocolVersion, testHandshake.ProtocolVersion)
<-make(chan int)
case "start-timeout":
time.Sleep(1 * time.Minute)
os.Exit(1)
case "stderr":
fmt.Printf("%d|%d|tcp|:1234\n", CoreProtocolVersion, testHandshake.ProtocolVersion)
log.Println("HELLO")
log.Println("WORLD")
case "stdin":
fmt.Printf("%d|%d|tcp|:1234\n", CoreProtocolVersion, testHandshake.ProtocolVersion)
data := make([]byte, 5)
if _, err := os.Stdin.Read(data); err != nil {
log.Printf("stdin read error: %s", err)
os.Exit(100)
}
if string(data) == "hello" {
os.Exit(0)
}
os.Exit(1)
case "cleanup":
// Create a defer to write the file. This tests that we get cleaned
// up properly versus just calling os.Exit
path := args[0]
defer func() {
err := ioutil.WriteFile(path, []byte("foo"), 0644)
if err != nil {
panic(err)
}
}()
Serve(&ServeConfig{
HandshakeConfig: testHandshake,
Plugins: testPluginMap,
})
// Exit
return
case "test-grpc":
Serve(&ServeConfig{
HandshakeConfig: testHandshake,
Plugins: testPluginMap,
GRPCServer: DefaultGRPCServer,
})
// Shouldn't reach here but make sure we exit anyways
os.Exit(0)
case "test-grpc-tls":
// Serve!
Serve(&ServeConfig{
HandshakeConfig: testHandshake,
Plugins: testPluginMap,
GRPCServer: DefaultGRPCServer,
TLSProvider: helperTLSProvider,
})
// Shouldn't reach here but make sure we exit anyways
os.Exit(0)
case "test-interface":
Serve(&ServeConfig{
HandshakeConfig: testHandshake,
Plugins: testPluginMap,
})
// Shouldn't reach here but make sure we exit anyways
os.Exit(0)
case "test-interface-daemon":
// Serve!
Serve(&ServeConfig{
HandshakeConfig: testHandshake,
Plugins: testPluginMap,
})
// Shouldn't reach here but make sure we exit anyways
os.Exit(0)
case "test-interface-tls":
// Serve!
Serve(&ServeConfig{
HandshakeConfig: testHandshake,
Plugins: testPluginMap,
TLSProvider: helperTLSProvider,
})
// Shouldn't reach here but make sure we exit anyways
os.Exit(0)
default:
fmt.Fprintf(os.Stderr, "Unknown command: %q\n", cmd)
os.Exit(2)
}
}
func helperTLSProvider() (*tls.Config, error) {
serverCert, err := tls.X509KeyPair([]byte(TestClusterServerCert), []byte(TestClusterServerKey))
if err != nil {
return nil, err
}
rootCAs := x509.NewCertPool()
rootCAs.AppendCertsFromPEM([]byte(TestClusterCACert))
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{serverCert},
RootCAs: rootCAs,
ClientCAs: rootCAs,
ClientAuth: tls.VerifyClientCertIfGiven,
ServerName: "127.0.0.1",
}
tlsConfig.BuildNameToCertificate()
return tlsConfig, nil
}
const (
TestClusterCACert = `-----BEGIN CERTIFICATE-----
MIIDPjCCAiagAwIBAgIUfIKsF2VPT7sdFcKOHJH2Ii6K4MwwDQYJKoZIhvcNAQEL
BQAwFjEUMBIGA1UEAxMLbXl2YXVsdC5jb20wIBcNMTYwNTAyMTYwNTQyWhgPMjA2
NjA0MjAxNjA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