Skip to content

Commit

Permalink
add tests from mTLS in grpc and netrpc
Browse files Browse the repository at this point in the history
Verify that we can connect and call methods using both protocols in the
test fixtures.
  • Loading branch information
jbardin committed Nov 30, 2018
1 parent 7505a0d commit ff43c1c
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
102 changes: 102 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,108 @@ func TestClient_versionedClient(t *testing.T) {
}
}

func TestClient_mtlsClient(t *testing.T) {
process := helperProcess("test-mtls")
c := NewClient(&ClientConfig{
AutoMTLS: true,
Cmd: process,
HandshakeConfig: testVersionedHandshake,
VersionedPlugins: map[int]PluginSet{
2: testGRPCPluginMap,
},
AllowedProtocols: []Protocol{ProtocolGRPC},
})
defer c.Kill()

if _, err := c.Start(); err != nil {
t.Fatalf("err: %s", err)
}

if v := c.Protocol(); v != ProtocolGRPC {
t.Fatalf("bad: %s", v)
}

// Grab the RPC client
client, err := c.Client()
if err != nil {
t.Fatalf("err should be nil, got %s", err)
}

if c.NegotiatedVersion() != 2 {
t.Fatal("using incorrect version", c.NegotiatedVersion())
}

// Grab the impl
raw, err := client.Dispense("test")
if err != nil {
t.Fatalf("err should be nil, got %s", err)
}

tester, ok := raw.(testInterface)
if !ok {
t.Fatalf("bad: %#v", raw)
}

n := tester.Double(3)
if n != 6 {
t.Fatal("invalid response", n)
}

c.process.Kill()

select {
case <-c.doneCtx.Done():
case <-time.After(time.Second * 2):
t.Fatal("Context was not closed")
}
}

func TestClient_mtlsNetRPCClient(t *testing.T) {
process := helperProcess("test-interface-mtls")
c := NewClient(&ClientConfig{
AutoMTLS: true,
Cmd: process,
HandshakeConfig: testVersionedHandshake,
Plugins: testPluginMap,
AllowedProtocols: []Protocol{ProtocolNetRPC},
})
defer c.Kill()

if _, err := c.Start(); err != nil {
t.Fatalf("err: %s", err)
}

// Grab the RPC client
client, err := c.Client()
if err != nil {
t.Fatalf("err should be nil, got %s", err)
}

// Grab the impl
raw, err := client.Dispense("test")
if err != nil {
t.Fatalf("err should be nil, got %s", err)
}

tester, ok := raw.(testInterface)
if !ok {
t.Fatalf("bad: %#v", raw)
}

n := tester.Double(3)
if n != 6 {
t.Fatal("invalid response", n)
}

c.process.Kill()

select {
case <-c.doneCtx.Done():
case <-time.After(time.Second * 2):
t.Fatal("Context was not closed")
}
}

func TestClient_logger(t *testing.T) {
t.Run("net/rpc", func(t *testing.T) { testClient_logger(t, "netrpc") })
t.Run("grpc", func(t *testing.T) { testClient_logger(t, "grpc") })
Expand Down
25 changes: 25 additions & 0 deletions plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,17 @@ func TestHelperProcess(*testing.T) {

// Shouldn't reach here but make sure we exit anyways
os.Exit(0)

case "test-interface-mtls":
// Serve!
Serve(&ServeConfig{
HandshakeConfig: testVersionedHandshake,
Plugins: testPluginMap,
})

// Shouldn't reach here but make sure we exit anyways
os.Exit(0)

case "test-versioned-plugins":
// Serve!
Serve(&ServeConfig{
Expand Down Expand Up @@ -590,6 +601,20 @@ func TestHelperProcess(*testing.T) {
TLSProvider: helperTLSProvider,
})

// Shouldn't reach here but make sure we exit anyways
os.Exit(0)
case "test-mtls":
// Serve 2 plugins over different protocols
Serve(&ServeConfig{
HandshakeConfig: HandshakeConfig{
ProtocolVersion: 2,
MagicCookieKey: "TEST_MAGIC_COOKIE",
MagicCookieValue: "test",
},
Plugins: testGRPCPluginMap,
GRPCServer: DefaultGRPCServer,
})

// Shouldn't reach here but make sure we exit anyways
os.Exit(0)
default:
Expand Down

0 comments on commit ff43c1c

Please sign in to comment.