2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-07-13 15:33:48 +00:00
|
|
|
package internal
|
2020-09-11 18:15:02 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-09-15 18:11:48 +00:00
|
|
|
"net"
|
2020-09-11 18:15:02 +00:00
|
|
|
"strings"
|
2020-10-14 20:47:16 +00:00
|
|
|
"sync/atomic"
|
2020-09-11 18:15:02 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-08-24 21:28:44 +00:00
|
|
|
"github.com/google/tcpproxy"
|
2020-10-14 20:47:16 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2021-08-24 21:28:44 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-10-13 22:45:17 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2023-01-05 10:21:27 +00:00
|
|
|
"github.com/hashicorp/consul/agent/grpc-internal/balancer"
|
2022-07-13 15:33:48 +00:00
|
|
|
"github.com/hashicorp/consul/agent/grpc-internal/resolver"
|
2022-10-11 22:00:32 +00:00
|
|
|
"github.com/hashicorp/consul/agent/grpc-middleware/testutil/testservice"
|
2020-09-11 18:15:02 +00:00
|
|
|
"github.com/hashicorp/consul/agent/metadata"
|
2021-08-24 21:28:44 +00:00
|
|
|
"github.com/hashicorp/consul/ipaddr"
|
|
|
|
"github.com/hashicorp/consul/sdk/freeport"
|
2023-01-05 10:21:27 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2020-10-14 20:47:16 +00:00
|
|
|
"github.com/hashicorp/consul/tlsutil"
|
2021-12-06 15:55:54 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
2020-09-11 18:15:02 +00:00
|
|
|
)
|
|
|
|
|
2021-01-06 16:21:22 +00:00
|
|
|
// useTLSForDcAlwaysTrue tell GRPC to always return the TLS is enabled
|
|
|
|
func useTLSForDcAlwaysTrue(_ string) bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-09-15 18:11:48 +00:00
|
|
|
func TestNewDialer_WithTLSWrapper(t *testing.T) {
|
2021-11-29 17:19:43 +00:00
|
|
|
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
2020-09-15 18:11:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
t.Cleanup(logError(t, lis.Close))
|
|
|
|
|
2023-05-11 16:08:57 +00:00
|
|
|
builder := resolver.NewServerResolverBuilder(newConfig(t, "dc1", "server"))
|
|
|
|
builder.AddServer(types.AreaLAN, &metadata.Server{
|
2020-09-15 18:11:48 +00:00
|
|
|
Name: "server-1",
|
|
|
|
ID: "ID1",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Addr: lis.Addr(),
|
|
|
|
UseTLS: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
var called bool
|
|
|
|
wrapper := func(_ string, conn net.Conn) (net.Conn, error) {
|
|
|
|
called = true
|
|
|
|
return conn, nil
|
|
|
|
}
|
2021-08-24 21:28:44 +00:00
|
|
|
dial := newDialer(
|
|
|
|
ClientConnPoolConfig{
|
|
|
|
Servers: builder,
|
|
|
|
TLSWrapper: wrapper,
|
|
|
|
UseTLSForDC: useTLSForDcAlwaysTrue,
|
|
|
|
DialingFromServer: true,
|
|
|
|
DialingFromDatacenter: "dc1",
|
|
|
|
},
|
|
|
|
&gatewayResolverDep{},
|
|
|
|
)
|
2020-09-15 18:11:48 +00:00
|
|
|
ctx := context.Background()
|
2021-08-24 21:28:44 +00:00
|
|
|
conn, err := dial(ctx, resolver.DCPrefix("dc1", lis.Addr().String()))
|
2020-09-15 18:11:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, conn.Close())
|
|
|
|
require.True(t, called, "expected TLSWrapper to be called")
|
2020-09-11 18:15:02 +00:00
|
|
|
}
|
|
|
|
|
2021-08-24 21:28:44 +00:00
|
|
|
func TestNewDialer_WithALPNWrapper(t *testing.T) {
|
2021-11-29 17:19:43 +00:00
|
|
|
lis1, err := net.Listen("tcp", "127.0.0.1:0")
|
2021-08-24 21:28:44 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
t.Cleanup(logError(t, lis1.Close))
|
|
|
|
|
2021-11-29 17:19:43 +00:00
|
|
|
lis2, err := net.Listen("tcp", "127.0.0.1:0")
|
2021-08-24 21:28:44 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
t.Cleanup(logError(t, lis2.Close))
|
|
|
|
|
|
|
|
// Send all of the traffic to dc2's server
|
|
|
|
var p tcpproxy.Proxy
|
2021-11-30 22:32:41 +00:00
|
|
|
gwAddr := ipaddr.FormatAddressPort("127.0.0.1", freeport.GetOne(t))
|
2021-11-29 17:19:43 +00:00
|
|
|
p.AddRoute(gwAddr, tcpproxy.To(lis2.Addr().String()))
|
2021-08-24 21:28:44 +00:00
|
|
|
p.AddStopACMESearch(gwAddr)
|
|
|
|
require.NoError(t, p.Start())
|
|
|
|
defer func() {
|
|
|
|
p.Close()
|
|
|
|
p.Wait()
|
|
|
|
}()
|
|
|
|
|
2023-05-11 16:08:57 +00:00
|
|
|
builder := resolver.NewServerResolverBuilder(newConfig(t, "dc1", "server"))
|
2021-12-06 15:55:54 +00:00
|
|
|
builder.AddServer(types.AreaWAN, &metadata.Server{
|
2021-08-24 21:28:44 +00:00
|
|
|
Name: "server-1",
|
|
|
|
ID: "ID1",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Addr: lis1.Addr(),
|
|
|
|
UseTLS: true,
|
|
|
|
})
|
2021-12-06 15:55:54 +00:00
|
|
|
builder.AddServer(types.AreaWAN, &metadata.Server{
|
2021-08-24 21:28:44 +00:00
|
|
|
Name: "server-2",
|
|
|
|
ID: "ID2",
|
|
|
|
Datacenter: "dc2",
|
|
|
|
Addr: lis2.Addr(),
|
|
|
|
UseTLS: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
var calledTLS bool
|
|
|
|
wrapperTLS := func(_ string, conn net.Conn) (net.Conn, error) {
|
|
|
|
calledTLS = true
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
var calledALPN bool
|
|
|
|
wrapperALPN := func(_, _, _ string, conn net.Conn) (net.Conn, error) {
|
|
|
|
calledALPN = true
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
gwResolverDep := &gatewayResolverDep{
|
|
|
|
GatewayResolver: func(addr string) string {
|
|
|
|
return gwAddr
|
|
|
|
},
|
|
|
|
}
|
|
|
|
dial := newDialer(
|
|
|
|
ClientConnPoolConfig{
|
|
|
|
Servers: builder,
|
|
|
|
TLSWrapper: wrapperTLS,
|
|
|
|
ALPNWrapper: wrapperALPN,
|
|
|
|
UseTLSForDC: useTLSForDcAlwaysTrue,
|
|
|
|
DialingFromServer: true,
|
|
|
|
DialingFromDatacenter: "dc1",
|
|
|
|
},
|
|
|
|
gwResolverDep,
|
|
|
|
)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
conn, err := dial(ctx, resolver.DCPrefix("dc2", lis2.Addr().String()))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, conn.Close())
|
|
|
|
|
|
|
|
assert.False(t, calledTLS, "expected TLSWrapper not to be called")
|
|
|
|
assert.True(t, calledALPN, "expected ALPNWrapper to be called")
|
|
|
|
}
|
|
|
|
|
2020-10-14 20:47:16 +00:00
|
|
|
func TestNewDialer_IntegrationWithTLSEnabledHandler(t *testing.T) {
|
2021-11-01 15:40:16 +00:00
|
|
|
// if this test is failing because of expired certificates
|
|
|
|
// use the procedure in test/CA-GENERATION.md
|
2023-05-11 16:08:57 +00:00
|
|
|
res := resolver.NewServerResolverBuilder(newConfig(t, "dc1", "server"))
|
2023-02-28 10:18:38 +00:00
|
|
|
bb := balancer.NewBuilder(res.Authority(), testutil.Logger(t))
|
|
|
|
registerWithGRPC(t, res, bb)
|
2020-10-14 20:47:16 +00:00
|
|
|
|
|
|
|
tlsConf, err := tlsutil.NewConfigurator(tlsutil.Config{
|
2022-03-18 10:46:58 +00:00
|
|
|
InternalRPC: tlsutil.ProtocolConfig{
|
|
|
|
VerifyIncoming: true,
|
2022-07-13 15:33:48 +00:00
|
|
|
CAFile: "../../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../../test/hostname/Alice.crt",
|
|
|
|
KeyFile: "../../test/hostname/Alice.key",
|
2022-03-18 10:46:58 +00:00
|
|
|
VerifyOutgoing: true,
|
|
|
|
},
|
2020-10-14 20:47:16 +00:00
|
|
|
}, hclog.New(nil))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-10-16 15:59:32 +00:00
|
|
|
srv := newSimpleTestServer(t, "server-1", "dc1", tlsConf)
|
2021-08-24 21:28:44 +00:00
|
|
|
|
|
|
|
md := srv.Metadata()
|
2023-05-11 16:08:57 +00:00
|
|
|
res.AddServer(types.AreaLAN, md)
|
2020-10-14 20:47:16 +00:00
|
|
|
t.Cleanup(srv.shutdown)
|
|
|
|
|
2023-05-11 16:08:57 +00:00
|
|
|
{
|
|
|
|
// Put a duplicate instance of this on the WAN that will
|
|
|
|
// fail if we accidentally use it.
|
|
|
|
srv := newPanicTestServer(t, hclog.Default(), "server-1", "dc1", nil)
|
|
|
|
res.AddServer(types.AreaWAN, srv.Metadata())
|
|
|
|
t.Cleanup(srv.shutdown)
|
|
|
|
}
|
|
|
|
|
2021-08-24 21:28:44 +00:00
|
|
|
pool := NewClientConnPool(ClientConnPoolConfig{
|
|
|
|
Servers: res,
|
|
|
|
TLSWrapper: TLSWrapper(tlsConf.OutgoingRPCWrapper()),
|
|
|
|
UseTLSForDC: tlsConf.UseTLS,
|
|
|
|
DialingFromServer: true,
|
|
|
|
DialingFromDatacenter: "dc1",
|
|
|
|
})
|
2020-10-14 20:47:16 +00:00
|
|
|
|
|
|
|
conn, err := pool.ClientConn("dc1")
|
|
|
|
require.NoError(t, err)
|
|
|
|
client := testservice.NewSimpleClient(conn)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
t.Cleanup(cancel)
|
|
|
|
|
|
|
|
resp, err := client.Something(ctx, &testservice.Req{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "server-1", resp.ServerName)
|
|
|
|
require.True(t, atomic.LoadInt32(&srv.rpc.tlsConnEstablished) > 0)
|
2021-08-24 21:28:44 +00:00
|
|
|
require.True(t, atomic.LoadInt32(&srv.rpc.alpnConnEstablished) == 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDialer_IntegrationWithTLSEnabledHandler_viaMeshGateway(t *testing.T) {
|
2021-11-01 15:40:16 +00:00
|
|
|
// if this test is failing because of expired certificates
|
|
|
|
// use the procedure in test/CA-GENERATION.md
|
2021-11-30 22:32:41 +00:00
|
|
|
gwAddr := ipaddr.FormatAddressPort("127.0.0.1", freeport.GetOne(t))
|
2021-08-24 21:28:44 +00:00
|
|
|
|
2023-05-11 16:08:57 +00:00
|
|
|
res := resolver.NewServerResolverBuilder(newConfig(t, "dc2", "server"))
|
2023-02-28 10:18:38 +00:00
|
|
|
bb := balancer.NewBuilder(res.Authority(), testutil.Logger(t))
|
|
|
|
registerWithGRPC(t, res, bb)
|
2021-08-24 21:28:44 +00:00
|
|
|
|
|
|
|
tlsConf, err := tlsutil.NewConfigurator(tlsutil.Config{
|
2022-03-18 10:46:58 +00:00
|
|
|
InternalRPC: tlsutil.ProtocolConfig{
|
|
|
|
VerifyIncoming: true,
|
2022-07-13 15:33:48 +00:00
|
|
|
CAFile: "../../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../../test/hostname/Bob.crt",
|
|
|
|
KeyFile: "../../test/hostname/Bob.key",
|
2022-03-18 10:46:58 +00:00
|
|
|
VerifyOutgoing: true,
|
|
|
|
VerifyServerHostname: true,
|
|
|
|
},
|
|
|
|
Domain: "consul",
|
|
|
|
NodeName: "bob",
|
2021-08-24 21:28:44 +00:00
|
|
|
}, hclog.New(nil))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-10-16 15:59:32 +00:00
|
|
|
srv := newSimpleTestServer(t, "bob", "dc1", tlsConf)
|
2021-08-24 21:28:44 +00:00
|
|
|
|
|
|
|
// Send all of the traffic to dc1's server
|
|
|
|
var p tcpproxy.Proxy
|
|
|
|
p.AddRoute(gwAddr, tcpproxy.To(srv.addr.String()))
|
|
|
|
p.AddStopACMESearch(gwAddr)
|
|
|
|
require.NoError(t, p.Start())
|
|
|
|
defer func() {
|
|
|
|
p.Close()
|
|
|
|
p.Wait()
|
|
|
|
}()
|
|
|
|
|
|
|
|
md := srv.Metadata()
|
2021-12-06 15:55:54 +00:00
|
|
|
res.AddServer(types.AreaWAN, md)
|
2021-08-24 21:28:44 +00:00
|
|
|
t.Cleanup(srv.shutdown)
|
|
|
|
|
|
|
|
clientTLSConf, err := tlsutil.NewConfigurator(tlsutil.Config{
|
2022-03-18 10:46:58 +00:00
|
|
|
InternalRPC: tlsutil.ProtocolConfig{
|
|
|
|
VerifyIncoming: true,
|
2022-07-13 15:33:48 +00:00
|
|
|
CAFile: "../../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../../test/hostname/Betty.crt",
|
|
|
|
KeyFile: "../../test/hostname/Betty.key",
|
2022-03-18 10:46:58 +00:00
|
|
|
VerifyOutgoing: true,
|
|
|
|
VerifyServerHostname: true,
|
|
|
|
},
|
|
|
|
Domain: "consul",
|
|
|
|
NodeName: "betty",
|
2021-08-24 21:28:44 +00:00
|
|
|
}, hclog.New(nil))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
pool := NewClientConnPool(ClientConnPoolConfig{
|
|
|
|
Servers: res,
|
|
|
|
TLSWrapper: TLSWrapper(clientTLSConf.OutgoingRPCWrapper()),
|
|
|
|
ALPNWrapper: ALPNWrapper(clientTLSConf.OutgoingALPNRPCWrapper()),
|
|
|
|
UseTLSForDC: tlsConf.UseTLS,
|
|
|
|
DialingFromServer: true,
|
|
|
|
DialingFromDatacenter: "dc2",
|
|
|
|
})
|
|
|
|
pool.SetGatewayResolver(func(addr string) string {
|
|
|
|
return gwAddr
|
|
|
|
})
|
|
|
|
|
|
|
|
conn, err := pool.ClientConn("dc1")
|
|
|
|
require.NoError(t, err)
|
|
|
|
client := testservice.NewSimpleClient(conn)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
t.Cleanup(cancel)
|
|
|
|
|
|
|
|
resp, err := client.Something(ctx, &testservice.Req{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "bob", resp.ServerName)
|
|
|
|
require.True(t, atomic.LoadInt32(&srv.rpc.tlsConnEstablished) == 0)
|
|
|
|
require.True(t, atomic.LoadInt32(&srv.rpc.alpnConnEstablished) > 0)
|
2020-10-14 20:47:16 +00:00
|
|
|
}
|
2020-09-15 18:11:48 +00:00
|
|
|
|
2020-09-11 18:15:02 +00:00
|
|
|
func TestClientConnPool_IntegrationWithGRPCResolver_Failover(t *testing.T) {
|
|
|
|
count := 4
|
2023-05-11 16:08:57 +00:00
|
|
|
res := resolver.NewServerResolverBuilder(newConfig(t, "dc1", "server"))
|
2023-02-28 10:18:38 +00:00
|
|
|
bb := balancer.NewBuilder(res.Authority(), testutil.Logger(t))
|
|
|
|
registerWithGRPC(t, res, bb)
|
2021-08-24 21:28:44 +00:00
|
|
|
pool := NewClientConnPool(ClientConnPoolConfig{
|
|
|
|
Servers: res,
|
|
|
|
UseTLSForDC: useTLSForDcAlwaysTrue,
|
|
|
|
DialingFromServer: true,
|
|
|
|
DialingFromDatacenter: "dc1",
|
|
|
|
})
|
2020-09-11 18:15:02 +00:00
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
name := fmt.Sprintf("server-%d", i)
|
2023-05-11 16:08:57 +00:00
|
|
|
{
|
|
|
|
srv := newSimpleTestServer(t, name, "dc1", nil)
|
|
|
|
res.AddServer(types.AreaLAN, srv.Metadata())
|
|
|
|
t.Cleanup(srv.shutdown)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// Put a duplicate instance of this on the WAN that will
|
|
|
|
// fail if we accidentally use it.
|
|
|
|
srv := newPanicTestServer(t, hclog.Default(), name, "dc1", nil)
|
|
|
|
res.AddServer(types.AreaWAN, srv.Metadata())
|
|
|
|
t.Cleanup(srv.shutdown)
|
|
|
|
}
|
2020-09-11 18:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := pool.ClientConn("dc1")
|
|
|
|
require.NoError(t, err)
|
|
|
|
client := testservice.NewSimpleClient(conn)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
t.Cleanup(cancel)
|
|
|
|
|
|
|
|
first, err := client.Something(ctx, &testservice.Req{})
|
|
|
|
require.NoError(t, err)
|
2020-09-14 20:16:44 +00:00
|
|
|
|
2023-05-11 16:08:57 +00:00
|
|
|
res.RemoveServer(types.AreaLAN, &metadata.Server{ID: first.ServerName, Datacenter: "dc1"})
|
2020-09-11 18:15:02 +00:00
|
|
|
|
|
|
|
resp, err := client.Something(ctx, &testservice.Req{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEqual(t, resp.ServerName, first.ServerName)
|
|
|
|
}
|
|
|
|
|
2021-07-22 18:58:08 +00:00
|
|
|
func TestClientConnPool_ForwardToLeader_Failover(t *testing.T) {
|
|
|
|
count := 3
|
2023-05-11 16:08:57 +00:00
|
|
|
res := resolver.NewServerResolverBuilder(newConfig(t, "dc1", "server"))
|
2023-02-28 10:18:38 +00:00
|
|
|
bb := balancer.NewBuilder(res.Authority(), testutil.Logger(t))
|
|
|
|
registerWithGRPC(t, res, bb)
|
2021-08-24 21:28:44 +00:00
|
|
|
pool := NewClientConnPool(ClientConnPoolConfig{
|
|
|
|
Servers: res,
|
|
|
|
UseTLSForDC: useTLSForDcAlwaysTrue,
|
|
|
|
DialingFromServer: true,
|
|
|
|
DialingFromDatacenter: "dc1",
|
|
|
|
})
|
2021-07-22 18:58:08 +00:00
|
|
|
|
|
|
|
var servers []testServer
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
name := fmt.Sprintf("server-%d", i)
|
2023-05-11 16:08:57 +00:00
|
|
|
{
|
|
|
|
srv := newSimpleTestServer(t, name, "dc1", nil)
|
|
|
|
res.AddServer(types.AreaLAN, srv.Metadata())
|
|
|
|
servers = append(servers, srv)
|
|
|
|
t.Cleanup(srv.shutdown)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// Put a duplicate instance of this on the WAN that will
|
|
|
|
// fail if we accidentally use it.
|
|
|
|
srv := newPanicTestServer(t, hclog.Default(), name, "dc1", nil)
|
|
|
|
res.AddServer(types.AreaWAN, srv.Metadata())
|
|
|
|
t.Cleanup(srv.shutdown)
|
|
|
|
}
|
2021-07-22 18:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the leader address to the first server.
|
2021-08-24 21:28:44 +00:00
|
|
|
srv0 := servers[0].Metadata()
|
|
|
|
res.UpdateLeaderAddr(srv0.Datacenter, srv0.Addr.String())
|
2021-07-22 18:58:08 +00:00
|
|
|
|
|
|
|
conn, err := pool.ClientConnLeader()
|
|
|
|
require.NoError(t, err)
|
|
|
|
client := testservice.NewSimpleClient(conn)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
t.Cleanup(cancel)
|
|
|
|
|
|
|
|
first, err := client.Something(ctx, &testservice.Req{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, first.ServerName, servers[0].name)
|
|
|
|
|
|
|
|
// Update the leader address and make another request.
|
2021-08-24 21:28:44 +00:00
|
|
|
srv1 := servers[1].Metadata()
|
|
|
|
res.UpdateLeaderAddr(srv1.Datacenter, srv1.Addr.String())
|
2021-07-22 18:58:08 +00:00
|
|
|
|
|
|
|
resp, err := client.Something(ctx, &testservice.Req{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, resp.ServerName, servers[1].name)
|
|
|
|
}
|
|
|
|
|
2023-05-11 16:08:57 +00:00
|
|
|
func newConfig(t *testing.T, dc, agentType string) resolver.Config {
|
2020-10-14 20:47:16 +00:00
|
|
|
n := t.Name()
|
2020-09-11 18:15:02 +00:00
|
|
|
s := strings.Replace(n, "/", "", -1)
|
|
|
|
s = strings.Replace(s, "_", "", -1)
|
2023-05-11 16:08:57 +00:00
|
|
|
return resolver.Config{
|
|
|
|
Datacenter: dc,
|
|
|
|
AgentType: agentType,
|
|
|
|
Authority: strings.ToLower(s),
|
|
|
|
}
|
2020-09-11 18:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientConnPool_IntegrationWithGRPCResolver_MultiDC(t *testing.T) {
|
|
|
|
dcs := []string{"dc1", "dc2", "dc3"}
|
|
|
|
|
2023-05-11 16:08:57 +00:00
|
|
|
res := resolver.NewServerResolverBuilder(newConfig(t, "dc1", "server"))
|
2023-02-28 10:18:38 +00:00
|
|
|
bb := balancer.NewBuilder(res.Authority(), testutil.Logger(t))
|
|
|
|
registerWithGRPC(t, res, bb)
|
2023-05-11 16:08:57 +00:00
|
|
|
|
2021-08-24 21:28:44 +00:00
|
|
|
pool := NewClientConnPool(ClientConnPoolConfig{
|
|
|
|
Servers: res,
|
|
|
|
UseTLSForDC: useTLSForDcAlwaysTrue,
|
|
|
|
DialingFromServer: true,
|
|
|
|
DialingFromDatacenter: "dc1",
|
|
|
|
})
|
2020-09-11 18:15:02 +00:00
|
|
|
|
|
|
|
for _, dc := range dcs {
|
|
|
|
name := "server-0-" + dc
|
2021-10-16 15:59:32 +00:00
|
|
|
srv := newSimpleTestServer(t, name, dc, nil)
|
2023-05-11 16:08:57 +00:00
|
|
|
if dc == "dc1" {
|
|
|
|
res.AddServer(types.AreaLAN, srv.Metadata())
|
|
|
|
// Put a duplicate instance of this on the WAN that will
|
|
|
|
// fail if we accidentally use it.
|
|
|
|
srvBad := newPanicTestServer(t, hclog.Default(), name, dc, nil)
|
|
|
|
res.AddServer(types.AreaWAN, srvBad.Metadata())
|
|
|
|
t.Cleanup(srvBad.shutdown)
|
|
|
|
} else {
|
|
|
|
res.AddServer(types.AreaWAN, srv.Metadata())
|
|
|
|
}
|
2020-09-11 18:15:02 +00:00
|
|
|
t.Cleanup(srv.shutdown)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
t.Cleanup(cancel)
|
|
|
|
|
|
|
|
for _, dc := range dcs {
|
|
|
|
conn, err := pool.ClientConn(dc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
client := testservice.NewSimpleClient(conn)
|
|
|
|
|
|
|
|
resp, err := client.Something(ctx, &testservice.Req{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, resp.Datacenter, dc)
|
|
|
|
}
|
|
|
|
}
|
2021-06-01 22:31:52 +00:00
|
|
|
|
2023-02-28 10:18:38 +00:00
|
|
|
func registerWithGRPC(t *testing.T, rb *resolver.ServerResolverBuilder, bb *balancer.Builder) {
|
|
|
|
resolver.Register(rb)
|
|
|
|
bb.Register()
|
2021-06-01 22:31:52 +00:00
|
|
|
t.Cleanup(func() {
|
2023-02-28 10:18:38 +00:00
|
|
|
resolver.Deregister(rb.Authority())
|
|
|
|
bb.Deregister()
|
2021-06-01 22:31:52 +00:00
|
|
|
})
|
|
|
|
}
|