From 99094d70b0f6713334c1d6e911cae8e0824484f8 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 19 May 2018 10:47:30 -0700 Subject: [PATCH] connect/proxy: add a full proxy test, parallel --- connect/proxy/config_test.go | 6 +++ connect/proxy/conn_test.go | 6 +++ connect/proxy/listener_test.go | 16 ++++--- connect/proxy/proxy_test.go | 79 ++++++++++++++++++++++++++++++++++ connect/proxy/testing.go | 19 +++++--- 5 files changed, 114 insertions(+), 12 deletions(-) create mode 100644 connect/proxy/proxy_test.go diff --git a/connect/proxy/config_test.go b/connect/proxy/config_test.go index 87bb43c81..2e214c6dd 100644 --- a/connect/proxy/config_test.go +++ b/connect/proxy/config_test.go @@ -15,6 +15,8 @@ import ( ) func TestParseConfigFile(t *testing.T) { + t.Parallel() + cfg, err := ParseConfigFile("testdata/config-kitchensink.hcl") require.Nil(t, err) @@ -54,6 +56,8 @@ func TestParseConfigFile(t *testing.T) { } func TestUpstreamResolverFromClient(t *testing.T) { + t.Parallel() + tests := []struct { name string cfg UpstreamConfig @@ -115,6 +119,8 @@ func TestUpstreamResolverFromClient(t *testing.T) { } func TestAgentConfigWatcher(t *testing.T) { + t.Parallel() + a := agent.NewTestAgent("agent_smith", "") defer a.Shutdown() diff --git a/connect/proxy/conn_test.go b/connect/proxy/conn_test.go index 4de428ad0..29dcdd3e3 100644 --- a/connect/proxy/conn_test.go +++ b/connect/proxy/conn_test.go @@ -64,6 +64,8 @@ func testConnPipelineSetup(t *testing.T) (net.Conn, net.Conn, *Conn, func()) { } func TestConn(t *testing.T) { + t.Parallel() + src, dst, c, stop := testConnPipelineSetup(t) defer stop() @@ -117,6 +119,8 @@ func TestConn(t *testing.T) { } func TestConnSrcClosing(t *testing.T) { + t.Parallel() + src, dst, c, stop := testConnPipelineSetup(t) defer stop() @@ -155,6 +159,8 @@ func TestConnSrcClosing(t *testing.T) { } func TestConnDstClosing(t *testing.T) { + t.Parallel() + src, dst, c, stop := testConnPipelineSetup(t) defer stop() diff --git a/connect/proxy/listener_test.go b/connect/proxy/listener_test.go index a0bc640d7..d63f5818b 100644 --- a/connect/proxy/listener_test.go +++ b/connect/proxy/listener_test.go @@ -15,23 +15,23 @@ import ( ) func TestPublicListener(t *testing.T) { + t.Parallel() + ca := agConnect.TestCA(t, nil) - ports := freeport.GetT(t, 2) + ports := freeport.GetT(t, 1) + + testApp := NewTestTCPServer(t) + defer testApp.Close() cfg := PublicListenerConfig{ BindAddress: "127.0.0.1", BindPort: ports[0], - LocalServiceAddress: TestLocalAddr(ports[1]), + LocalServiceAddress: testApp.Addr().String(), HandshakeTimeoutMs: 100, LocalConnectTimeoutMs: 100, } - testApp, err := NewTestTCPServer(t, cfg.LocalServiceAddress) - require.NoError(t, err) - defer testApp.Close() - svc := connect.TestService(t, "db", ca) - l := NewPublicListener(svc, cfg, log.New(os.Stderr, "", log.LstdFlags)) // Run proxy @@ -53,6 +53,8 @@ func TestPublicListener(t *testing.T) { } func TestUpstreamListener(t *testing.T) { + t.Parallel() + ca := agConnect.TestCA(t, nil) ports := freeport.GetT(t, 1) diff --git a/connect/proxy/proxy_test.go b/connect/proxy/proxy_test.go new file mode 100644 index 000000000..681f802c4 --- /dev/null +++ b/connect/proxy/proxy_test.go @@ -0,0 +1,79 @@ +package proxy + +import ( + "context" + "log" + "net" + "os" + "testing" + + "github.com/hashicorp/consul/agent" + agConnect "github.com/hashicorp/consul/agent/connect" + "github.com/hashicorp/consul/api" + "github.com/hashicorp/consul/connect" + "github.com/hashicorp/consul/lib/freeport" + "github.com/hashicorp/consul/testutil/retry" + "github.com/stretchr/testify/require" +) + +func TestProxy_public(t *testing.T) { + t.Parallel() + + require := require.New(t) + ports := freeport.GetT(t, 1) + + a := agent.NewTestAgent(t.Name(), "") + defer a.Shutdown() + client := a.Client() + + // Register the service so we can get a leaf cert + _, err := client.Catalog().Register(&api.CatalogRegistration{ + Datacenter: "dc1", + Node: "local", + Address: "127.0.0.1", + Service: &api.AgentService{ + Service: "echo", + }, + }, nil) + require.NoError(err) + + // Start the backend service that is being proxied + testApp := NewTestTCPServer(t) + defer testApp.Close() + + // Start the proxy + p, err := New(client, NewStaticConfigWatcher(&Config{ + ProxiedServiceName: "echo", + PublicListener: PublicListenerConfig{ + BindAddress: "127.0.0.1", + BindPort: ports[0], + LocalServiceAddress: testApp.Addr().String(), + }, + }), testLogger(t)) + require.NoError(err) + defer p.Close() + go p.Serve() + + // Create a test connection to the proxy. We retry here a few times + // since this is dependent on the agent actually starting up and setting + // up the CA. + var conn net.Conn + svc, err := connect.NewService("echo", client) + require.NoError(err) + retry.Run(t, func(r *retry.R) { + conn, err = svc.Dial(context.Background(), &connect.StaticResolver{ + Addr: TestLocalAddr(ports[0]), + CertURI: agConnect.TestSpiffeIDService(t, "echo"), + }) + if err != nil { + r.Fatalf("err: %s", err) + } + }) + + // Connection works, test it is the right one + TestEchoConn(t, conn, "") +} + +func testLogger(t *testing.T) *log.Logger { + return log.New(os.Stderr, "", log.LstdFlags) +} diff --git a/connect/proxy/testing.go b/connect/proxy/testing.go index f986cfe50..6b1b2636f 100644 --- a/connect/proxy/testing.go +++ b/connect/proxy/testing.go @@ -7,6 +7,7 @@ import ( "net" "sync/atomic" + "github.com/hashicorp/consul/lib/freeport" "github.com/mitchellh/go-testing-interface" "github.com/stretchr/testify/require" ) @@ -26,17 +27,20 @@ type TestTCPServer struct { // NewTestTCPServer opens as a listening socket on the given address and returns // a TestTCPServer serving requests to it. The server is already started and can // be stopped by calling Close(). -func NewTestTCPServer(t testing.T, addr string) (*TestTCPServer, error) { +func NewTestTCPServer(t testing.T) *TestTCPServer { + port := freeport.GetT(t, 1) + addr := TestLocalAddr(port[0]) + l, err := net.Listen("tcp", addr) - if err != nil { - return nil, err - } + require.NoError(t, err) + log.Printf("test tcp server listening on %s", addr) s := &TestTCPServer{ l: l, } go s.accept() - return s, nil + + return s } // Close stops the server @@ -47,6 +51,11 @@ func (s *TestTCPServer) Close() { } } +// Addr returns the address that this server is listening on. +func (s *TestTCPServer) Addr() net.Addr { + return s.l.Addr() +} + func (s *TestTCPServer) accept() error { for { conn, err := s.l.Accept()