2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-01-11 21:34:27 +00:00
|
|
|
package topology
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-03-02 23:21:25 +00:00
|
|
|
"github.com/testcontainers/testcontainers-go"
|
2023-01-11 21:34:27 +00:00
|
|
|
|
2023-01-20 22:02:44 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
|
2023-01-11 21:34:27 +00:00
|
|
|
libassert "github.com/hashicorp/consul/test/integration/consul-container/libs/assert"
|
|
|
|
libcluster "github.com/hashicorp/consul/test/integration/consul-container/libs/cluster"
|
|
|
|
libservice "github.com/hashicorp/consul/test/integration/consul-container/libs/service"
|
|
|
|
"github.com/hashicorp/consul/test/integration/consul-container/libs/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
AcceptingPeerName = "accepting-to-dialer"
|
|
|
|
DialingPeerName = "dialing-to-acceptor"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BuiltCluster struct {
|
|
|
|
Cluster *libcluster.Cluster
|
|
|
|
Context *libcluster.BuildContext
|
|
|
|
Service libservice.Service
|
2023-02-13 19:09:12 +00:00
|
|
|
Container libservice.Service
|
2023-01-27 16:25:48 +00:00
|
|
|
Gateway libservice.Service
|
2023-01-11 21:34:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BasicPeeringTwoClustersSetup sets up a scenario for testing peering, which consists of
|
|
|
|
//
|
|
|
|
// - an accepting cluster with 3 servers and 1 client agent. The client should be used to
|
|
|
|
// host a service for export: staticServerSvc.
|
|
|
|
// - an dialing cluster with 1 server and 1 client. The client should be used to host a
|
|
|
|
// service connecting to staticServerSvc.
|
|
|
|
// - Create the peering, export the service from accepting cluster, and verify service
|
|
|
|
// connectivity.
|
|
|
|
//
|
|
|
|
// It returns objects of the accepting cluster, dialing cluster, staticServerSvc, and staticClientSvcSidecar
|
|
|
|
func BasicPeeringTwoClustersSetup(
|
|
|
|
t *testing.T,
|
|
|
|
consulVersion string,
|
2023-02-15 15:26:43 +00:00
|
|
|
peeringThroughMeshgateway bool,
|
2023-01-11 21:34:27 +00:00
|
|
|
) (*BuiltCluster, *BuiltCluster) {
|
2023-03-02 23:21:25 +00:00
|
|
|
acceptingCluster, acceptingCtx, acceptingClient := NewCluster(t, &ClusterConfig{
|
|
|
|
NumServers: 3,
|
|
|
|
NumClients: 1,
|
|
|
|
BuildOpts: &libcluster.BuildOptions{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
ConsulVersion: consulVersion,
|
|
|
|
InjectAutoEncryption: true,
|
|
|
|
},
|
|
|
|
ApplyDefaultProxySettings: true,
|
2023-02-07 19:13:19 +00:00
|
|
|
})
|
2023-03-02 23:21:25 +00:00
|
|
|
|
|
|
|
dialingCluster, dialingCtx, dialingClient := NewCluster(t, &ClusterConfig{
|
|
|
|
NumServers: 1,
|
|
|
|
NumClients: 1,
|
|
|
|
BuildOpts: &libcluster.BuildOptions{
|
|
|
|
Datacenter: "dc2",
|
|
|
|
ConsulVersion: consulVersion,
|
|
|
|
InjectAutoEncryption: true,
|
|
|
|
},
|
|
|
|
ApplyDefaultProxySettings: true,
|
2023-02-07 19:13:19 +00:00
|
|
|
})
|
2023-02-15 15:26:43 +00:00
|
|
|
|
|
|
|
// Create the mesh gateway for dataplane traffic and peering control plane traffic (if enabled)
|
2023-03-15 17:51:36 +00:00
|
|
|
gwCfg := libservice.GatewayConfig{
|
|
|
|
Name: "mesh",
|
|
|
|
Kind: "mesh",
|
|
|
|
}
|
|
|
|
acceptingClusterGateway, err := libservice.NewGatewayService(context.Background(), gwCfg, acceptingCluster.Clients()[0])
|
2023-02-15 15:26:43 +00:00
|
|
|
require.NoError(t, err)
|
2023-03-15 17:51:36 +00:00
|
|
|
dialingClusterGateway, err := libservice.NewGatewayService(context.Background(), gwCfg, dialingCluster.Clients()[0])
|
2023-02-15 15:26:43 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Enable peering control plane traffic through mesh gateway
|
|
|
|
if peeringThroughMeshgateway {
|
|
|
|
req := &api.MeshConfigEntry{
|
|
|
|
Peering: &api.PeeringMeshConfig{
|
|
|
|
PeerThroughMeshGateways: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
configCluster := func(cli *api.Client) error {
|
2023-03-02 22:40:07 +00:00
|
|
|
libassert.CatalogServiceExists(t, cli, "mesh", nil)
|
2023-02-15 15:26:43 +00:00
|
|
|
ok, _, err := cli.ConfigEntries().Set(req, &api.WriteOptions{})
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("config entry is not set")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error writing config entry: %s", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err = configCluster(dialingClient)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = configCluster(acceptingClient)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2023-01-11 21:34:27 +00:00
|
|
|
require.NoError(t, dialingCluster.PeerWithCluster(acceptingClient, AcceptingPeerName, DialingPeerName))
|
|
|
|
|
|
|
|
libassert.PeeringStatus(t, acceptingClient, AcceptingPeerName, api.PeeringStateActive)
|
|
|
|
// libassert.PeeringExports(t, acceptingClient, acceptingPeerName, 1)
|
|
|
|
|
|
|
|
// Register an static-server service in acceptingCluster and export to dialing cluster
|
2023-02-13 19:09:12 +00:00
|
|
|
var serverService, serverSidecarService libservice.Service
|
2023-01-11 21:34:27 +00:00
|
|
|
{
|
|
|
|
clientNode := acceptingCluster.Clients()[0]
|
|
|
|
|
|
|
|
// Create a service and proxy instance
|
|
|
|
var err error
|
2023-02-03 15:20:22 +00:00
|
|
|
// Create a service and proxy instance
|
|
|
|
serviceOpts := libservice.ServiceOpts{
|
2023-02-09 14:45:31 +00:00
|
|
|
Name: libservice.StaticServerServiceName,
|
|
|
|
ID: "static-server",
|
|
|
|
Meta: map[string]string{"version": ""},
|
|
|
|
HTTPPort: 8080,
|
|
|
|
GRPCPort: 8079,
|
2023-02-03 15:20:22 +00:00
|
|
|
}
|
2023-02-13 19:09:12 +00:00
|
|
|
serverService, serverSidecarService, err = libservice.CreateAndRegisterStaticServerAndSidecar(clientNode, &serviceOpts)
|
2023-01-11 21:34:27 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-03-02 22:40:07 +00:00
|
|
|
libassert.CatalogServiceExists(t, acceptingClient, libservice.StaticServerServiceName, nil)
|
|
|
|
libassert.CatalogServiceExists(t, acceptingClient, "static-server-sidecar-proxy", nil)
|
2023-01-11 21:34:27 +00:00
|
|
|
|
2023-02-13 19:09:12 +00:00
|
|
|
require.NoError(t, serverService.Export("default", AcceptingPeerName, acceptingClient))
|
2023-01-11 21:34:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Register an static-client service in dialing cluster and set upstream to static-server service
|
|
|
|
var clientSidecarService *libservice.ConnectContainer
|
|
|
|
{
|
|
|
|
clientNode := dialingCluster.Clients()[0]
|
|
|
|
|
|
|
|
// Create a service and proxy instance
|
|
|
|
var err error
|
|
|
|
clientSidecarService, err = libservice.CreateAndRegisterStaticClientSidecar(clientNode, DialingPeerName, true)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-03-02 22:40:07 +00:00
|
|
|
libassert.CatalogServiceExists(t, dialingClient, "static-client-sidecar-proxy", nil)
|
2023-01-27 16:25:48 +00:00
|
|
|
|
2023-01-11 21:34:27 +00:00
|
|
|
}
|
|
|
|
|
2023-01-25 17:27:36 +00:00
|
|
|
_, adminPort := clientSidecarService.GetAdminAddr()
|
|
|
|
libassert.AssertUpstreamEndpointStatus(t, adminPort, fmt.Sprintf("static-server.default.%s.external", DialingPeerName), "HEALTHY", 1)
|
2023-01-11 21:34:27 +00:00
|
|
|
_, port := clientSidecarService.GetAddr()
|
2023-01-20 22:02:44 +00:00
|
|
|
libassert.HTTPServiceEchoes(t, "localhost", port, "")
|
2023-03-02 22:40:07 +00:00
|
|
|
libassert.AssertFortioName(t, fmt.Sprintf("http://localhost:%d", port), libservice.StaticServerServiceName, "")
|
2023-01-11 21:34:27 +00:00
|
|
|
|
|
|
|
return &BuiltCluster{
|
|
|
|
Cluster: acceptingCluster,
|
|
|
|
Context: acceptingCtx,
|
|
|
|
Service: serverSidecarService,
|
2023-02-13 19:09:12 +00:00
|
|
|
Container: serverSidecarService,
|
2023-01-27 16:25:48 +00:00
|
|
|
Gateway: acceptingClusterGateway,
|
2023-01-11 21:34:27 +00:00
|
|
|
},
|
|
|
|
&BuiltCluster{
|
|
|
|
Cluster: dialingCluster,
|
|
|
|
Context: dialingCtx,
|
|
|
|
Service: nil,
|
|
|
|
Container: clientSidecarService,
|
2023-01-27 16:25:48 +00:00
|
|
|
Gateway: dialingClusterGateway,
|
2023-01-11 21:34:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-02 23:21:25 +00:00
|
|
|
type ClusterConfig struct {
|
|
|
|
NumServers int
|
|
|
|
NumClients int
|
|
|
|
ApplyDefaultProxySettings bool
|
|
|
|
BuildOpts *libcluster.BuildOptions
|
|
|
|
Cmd string
|
|
|
|
LogConsumer *TestLogConsumer
|
|
|
|
Ports []int
|
2023-01-11 21:34:27 +00:00
|
|
|
}
|
|
|
|
|
2023-03-02 23:21:25 +00:00
|
|
|
// NewCluster creates a cluster with peering enabled. It also creates
|
2023-01-11 21:34:27 +00:00
|
|
|
// and registers a mesh-gateway at the client agent. The API client returned is
|
|
|
|
// pointed at the client agent.
|
2023-02-13 19:09:12 +00:00
|
|
|
// - proxy-defaults.protocol = tcp
|
2023-03-02 23:21:25 +00:00
|
|
|
func NewCluster(
|
2023-01-11 21:34:27 +00:00
|
|
|
t *testing.T,
|
2023-03-02 23:21:25 +00:00
|
|
|
config *ClusterConfig,
|
2023-01-11 21:34:27 +00:00
|
|
|
) (*libcluster.Cluster, *libcluster.BuildContext, *api.Client) {
|
2023-03-02 23:21:25 +00:00
|
|
|
var (
|
|
|
|
cluster *libcluster.Cluster
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
require.NotEmpty(t, config.BuildOpts.Datacenter)
|
|
|
|
require.True(t, config.NumServers > 0)
|
2023-01-11 21:34:27 +00:00
|
|
|
|
|
|
|
opts := libcluster.BuildOptions{
|
2023-03-02 23:21:25 +00:00
|
|
|
Datacenter: config.BuildOpts.Datacenter,
|
|
|
|
InjectAutoEncryption: config.BuildOpts.InjectAutoEncryption,
|
2023-01-11 21:34:27 +00:00
|
|
|
InjectGossipEncryption: true,
|
|
|
|
AllowHTTPAnyway: true,
|
2023-03-02 23:21:25 +00:00
|
|
|
ConsulVersion: config.BuildOpts.ConsulVersion,
|
|
|
|
ACLEnabled: config.BuildOpts.ACLEnabled,
|
2023-03-18 20:43:22 +00:00
|
|
|
LogStore: config.BuildOpts.LogStore,
|
2023-01-11 21:34:27 +00:00
|
|
|
}
|
|
|
|
ctx := libcluster.NewBuildContext(t, opts)
|
|
|
|
|
|
|
|
serverConf := libcluster.NewConfigBuilder(ctx).
|
2023-03-02 23:21:25 +00:00
|
|
|
Bootstrap(config.NumServers).
|
2023-01-11 21:34:27 +00:00
|
|
|
Peering(true).
|
|
|
|
ToAgentConfig(t)
|
2023-02-07 19:13:19 +00:00
|
|
|
t.Logf("%s server config: \n%s", opts.Datacenter, serverConf.JSON)
|
2023-01-11 21:34:27 +00:00
|
|
|
|
2023-03-02 23:21:25 +00:00
|
|
|
// optional
|
|
|
|
if config.LogConsumer != nil {
|
|
|
|
serverConf.LogConsumer = config.LogConsumer
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("Cluster config:\n%s", serverConf.JSON)
|
|
|
|
|
|
|
|
// optional custom cmd
|
|
|
|
if config.Cmd != "" {
|
|
|
|
serverConf.Cmd = append(serverConf.Cmd, config.Cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Ports != nil {
|
|
|
|
cluster, err = libcluster.New(t, []libcluster.Config{*serverConf}, config.Ports...)
|
|
|
|
} else {
|
|
|
|
cluster, err = libcluster.NewN(t, *serverConf, config.NumServers)
|
|
|
|
}
|
2023-01-11 21:34:27 +00:00
|
|
|
require.NoError(t, err)
|
2023-03-29 16:51:21 +00:00
|
|
|
// builder generates certs for us, so copy them back
|
|
|
|
if opts.InjectAutoEncryption {
|
|
|
|
cluster.CACert = serverConf.CACert
|
|
|
|
}
|
2023-01-11 21:34:27 +00:00
|
|
|
|
|
|
|
var retryJoin []string
|
2023-03-02 23:21:25 +00:00
|
|
|
for i := 0; i < config.NumServers; i++ {
|
2023-01-11 21:34:27 +00:00
|
|
|
retryJoin = append(retryJoin, fmt.Sprintf("agent-%d", i))
|
|
|
|
}
|
|
|
|
|
2023-03-01 20:45:27 +00:00
|
|
|
// Add numClients static clients to register the service
|
2023-02-07 19:13:19 +00:00
|
|
|
configbuiilder := libcluster.NewConfigBuilder(ctx).
|
2023-01-11 21:34:27 +00:00
|
|
|
Client().
|
|
|
|
Peering(true).
|
2023-02-07 19:13:19 +00:00
|
|
|
RetryJoin(retryJoin...)
|
|
|
|
clientConf := configbuiilder.ToAgentConfig(t)
|
|
|
|
t.Logf("%s client config: \n%s", opts.Datacenter, clientConf.JSON)
|
2023-01-11 21:34:27 +00:00
|
|
|
|
2023-03-02 23:21:25 +00:00
|
|
|
require.NoError(t, cluster.AddN(*clientConf, config.NumClients, true))
|
2023-01-11 21:34:27 +00:00
|
|
|
|
|
|
|
// Use the client agent as the HTTP endpoint since we will not rotate it in many tests.
|
2023-03-02 23:21:25 +00:00
|
|
|
var client *api.Client
|
|
|
|
if config.NumClients > 0 {
|
|
|
|
clientNode := cluster.Agents[config.NumServers]
|
|
|
|
client = clientNode.GetClient()
|
|
|
|
} else {
|
|
|
|
client = cluster.Agents[0].GetClient()
|
|
|
|
}
|
2023-01-11 21:34:27 +00:00
|
|
|
libcluster.WaitForLeader(t, cluster, client)
|
2023-03-02 23:21:25 +00:00
|
|
|
libcluster.WaitForMembers(t, client, config.NumServers+config.NumClients)
|
2023-01-11 21:34:27 +00:00
|
|
|
|
|
|
|
// Default Proxy Settings
|
2023-03-02 23:21:25 +00:00
|
|
|
if config.ApplyDefaultProxySettings {
|
|
|
|
ok, err := utils.ApplyDefaultProxySettings(client)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, ok)
|
|
|
|
}
|
2023-01-11 21:34:27 +00:00
|
|
|
|
|
|
|
return cluster, ctx, client
|
|
|
|
}
|
2023-03-02 23:21:25 +00:00
|
|
|
|
|
|
|
type TestLogConsumer struct {
|
|
|
|
Msgs []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *TestLogConsumer) Accept(l testcontainers.Log) {
|
|
|
|
g.Msgs = append(g.Msgs, string(l.Content))
|
|
|
|
}
|