Fix tests and listeners to work with Config changes (splitting host and port fields)

This commit is contained in:
Paul Banks 2018-04-25 21:22:31 +01:00 committed by Mitchell Hashimoto
parent 072b2a79ca
commit 2b1660fdf7
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
6 changed files with 29 additions and 26 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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 {

View File

@ -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, "")
}

View File

@ -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.

View File

@ -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)