From 946e872f2f8665ba9ab86fc89c80d161cd20f508 Mon Sep 17 00:00:00 2001 From: Paul Banks Date: Wed, 25 Apr 2018 21:22:31 +0100 Subject: [PATCH] Fix tests and listeners to work with Config changes (splitting host and port fields) --- connect/proxy/config.go | 4 ++-- connect/proxy/config_test.go | 7 ++++--- connect/proxy/listener.go | 8 ++++++-- connect/proxy/listener_test.go | 19 ++++++++++++------- connect/proxy/testing.go | 13 +++---------- connect/service.go | 4 ++-- 6 files changed, 29 insertions(+), 26 deletions(-) diff --git a/connect/proxy/config.go b/connect/proxy/config.go index b5a8c6bb48..6fad0bd559 100644 --- a/connect/proxy/config.go +++ b/connect/proxy/config.go @@ -65,7 +65,7 @@ type PublicListenerConfig struct { // BindAddress is the host/IP the public mTLS listener will bind to. BindAddress string `json:"bind_address" hcl:"bind_address" mapstructure:"bind_address"` - BindPort string `json:"bind_port" hcl:"bind_port" mapstructure:"bind_port"` + BindPort int `json:"bind_port" hcl:"bind_port" mapstructure:"bind_port"` // LocalServiceAddress is the host:port for the proxied application. This // should be on loopback or otherwise protected as it's plain TCP. @@ -251,7 +251,7 @@ func NewAgentConfigWatcher(client *api.Client, proxyID string, return nil, err } w.plan = plan - w.plan.Handler = w.handler + w.plan.HybridHandler = w.handler go w.plan.RunWithClientAndLogger(w.client, w.logger) return w, nil } diff --git a/connect/proxy/config_test.go b/connect/proxy/config_test.go index 855eaddf16..e576d5f82a 100644 --- a/connect/proxy/config_test.go +++ b/connect/proxy/config_test.go @@ -24,7 +24,8 @@ func TestParseConfigFile(t *testing.T) { ProxiedServiceID: "web", ProxiedServiceNamespace: "default", PublicListener: PublicListenerConfig{ - BindAddress: ":9999", + BindAddress: "127.0.0.1", + BindPort: 9999, LocalServiceAddress: "127.0.0.1:5000", LocalConnectTimeoutMs: 1000, HandshakeTimeoutMs: 10000, // From defaults @@ -129,7 +130,7 @@ func TestAgentConfigWatcher(t *testing.T) { Proxy: &api.AgentServiceConnectProxy{ Config: map[string]interface{}{ "bind_address": "10.10.10.10", - "bind_port": "1010", + "bind_port": 1010, "local_service_address": "127.0.0.1:5000", "handshake_timeout_ms": 999, "upstreams": []interface{}{ @@ -157,7 +158,7 @@ func TestAgentConfigWatcher(t *testing.T) { ProxiedServiceNamespace: "default", PublicListener: PublicListenerConfig{ BindAddress: "10.10.10.10", - BindPort: "1010", + BindPort: 1010, LocalServiceAddress: "127.0.0.1:5000", HandshakeTimeoutMs: 999, LocalConnectTimeoutMs: 1000, // from applyDefaults diff --git a/connect/proxy/listener.go b/connect/proxy/listener.go index c8e70ac31d..12134f8401 100644 --- a/connect/proxy/listener.go +++ b/connect/proxy/listener.go @@ -4,6 +4,7 @@ import ( "context" "crypto/tls" "errors" + "fmt" "log" "net" "sync/atomic" @@ -44,7 +45,9 @@ func NewPublicListener(svc *connect.Service, cfg PublicListenerConfig, return &Listener{ Service: svc, listenFunc: func() (net.Listener, error) { - return tls.Listen("tcp", cfg.BindAddress, svc.ServerTLSConfig()) + return tls.Listen("tcp", + fmt.Sprintf("%s:%d", cfg.BindAddress, cfg.BindPort), + svc.ServerTLSConfig()) }, dialFunc: func() (net.Conn, error) { return net.DialTimeout("tcp", cfg.LocalServiceAddress, @@ -63,7 +66,8 @@ func NewUpstreamListener(svc *connect.Service, cfg UpstreamConfig, return &Listener{ Service: svc, listenFunc: func() (net.Listener, error) { - return net.Listen("tcp", cfg.LocalBindAddress) + return net.Listen("tcp", + fmt.Sprintf("%s:%d", cfg.LocalBindAddress, cfg.LocalBindPort)) }, dialFunc: func() (net.Conn, error) { if cfg.resolver == nil { diff --git a/connect/proxy/listener_test.go b/connect/proxy/listener_test.go index 8354fbe588..a0bc640d7e 100644 --- a/connect/proxy/listener_test.go +++ b/connect/proxy/listener_test.go @@ -2,6 +2,7 @@ package proxy import ( "context" + "fmt" "log" "net" "os" @@ -9,16 +10,18 @@ import ( agConnect "github.com/hashicorp/consul/agent/connect" "github.com/hashicorp/consul/connect" + "github.com/hashicorp/consul/lib/freeport" "github.com/stretchr/testify/require" ) func TestPublicListener(t *testing.T) { ca := agConnect.TestCA(t, nil) - addrs := TestLocalBindAddrs(t, 2) + ports := freeport.GetT(t, 2) cfg := PublicListenerConfig{ - BindAddress: addrs[0], - LocalServiceAddress: addrs[1], + BindAddress: "127.0.0.1", + BindPort: ports[0], + LocalServiceAddress: TestLocalAddr(ports[1]), HandshakeTimeoutMs: 100, LocalConnectTimeoutMs: 100, } @@ -42,7 +45,7 @@ func TestPublicListener(t *testing.T) { // Proxy and backend are running, play the part of a TLS client using same // cert for now. conn, err := svc.Dial(context.Background(), &connect.StaticResolver{ - Addr: addrs[0], + Addr: TestLocalAddr(ports[0]), CertURI: agConnect.TestSpiffeIDService(t, "db"), }) require.NoError(t, err) @@ -51,7 +54,7 @@ func TestPublicListener(t *testing.T) { func TestUpstreamListener(t *testing.T) { ca := agConnect.TestCA(t, nil) - addrs := TestLocalBindAddrs(t, 1) + ports := freeport.GetT(t, 1) // Run a test server that we can dial. testSvr := connect.NewTestServer(t, "db", ca) @@ -67,7 +70,8 @@ func TestUpstreamListener(t *testing.T) { DestinationNamespace: "default", DestinationName: "db", ConnectTimeoutMs: 100, - LocalBindAddress: addrs[0], + LocalBindAddress: "localhost", + LocalBindPort: ports[0], resolver: &connect.StaticResolver{ Addr: testSvr.Addr, CertURI: agConnect.TestSpiffeIDService(t, "db"), @@ -88,7 +92,8 @@ func TestUpstreamListener(t *testing.T) { // Proxy and fake remote service are running, play the part of the app // connecting to a remote connect service over TCP. - conn, err := net.Dial("tcp", cfg.LocalBindAddress) + conn, err := net.Dial("tcp", + fmt.Sprintf("%s:%d", cfg.LocalBindAddress, cfg.LocalBindPort)) require.NoError(t, err) TestEchoConn(t, conn, "") } diff --git a/connect/proxy/testing.go b/connect/proxy/testing.go index 9ed8c41c4e..f986cfe502 100644 --- a/connect/proxy/testing.go +++ b/connect/proxy/testing.go @@ -7,20 +7,13 @@ import ( "net" "sync/atomic" - "github.com/hashicorp/consul/lib/freeport" "github.com/mitchellh/go-testing-interface" "github.com/stretchr/testify/require" ) -// TestLocalBindAddrs returns n localhost address:port strings with free ports -// for binding test listeners to. -func TestLocalBindAddrs(t testing.T, n int) []string { - ports := freeport.GetT(t, n) - addrs := make([]string, n) - for i, p := range ports { - addrs[i] = fmt.Sprintf("localhost:%d", p) - } - return addrs +// TestLocalAddr makes a localhost address on the given port +func TestLocalAddr(port int) string { + return fmt.Sprintf("localhost:%d", port) } // TestTCPServer is a simple TCP echo server for use during tests. diff --git a/connect/service.go b/connect/service.go index a614f227fd..18e6dd89e7 100644 --- a/connect/service.go +++ b/connect/service.go @@ -86,7 +86,7 @@ func NewServiceWithLogger(serviceID string, client *api.Client, return nil, err } s.rootsWatch = p - s.rootsWatch.Handler = s.rootsWatchHandler + s.rootsWatch.HybridHandler = s.rootsWatchHandler p, err = watch.Parse(map[string]interface{}{ "type": "connect_leaf", @@ -95,7 +95,7 @@ func NewServiceWithLogger(serviceID string, client *api.Client, return nil, err } s.leafWatch = p - s.leafWatch.Handler = s.leafWatchHandler + s.leafWatch.HybridHandler = s.leafWatchHandler //go s.rootsWatch.RunWithClientAndLogger(s.client, s.logger) //go s.leafWatch.RunWithClientAndLogger(s.client, s.logger)