|
|
@ -6,6 +6,7 @@ import (
|
|
|
|
"testing"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/testcontainers/testcontainers-go"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
|
|
|
|
|
|
|
@ -43,16 +44,26 @@ func BasicPeeringTwoClustersSetup(
|
|
|
|
consulVersion string,
|
|
|
|
consulVersion string,
|
|
|
|
peeringThroughMeshgateway bool,
|
|
|
|
peeringThroughMeshgateway bool,
|
|
|
|
) (*BuiltCluster, *BuiltCluster) {
|
|
|
|
) (*BuiltCluster, *BuiltCluster) {
|
|
|
|
// acceptingCluster, acceptingCtx, acceptingClient := NewPeeringCluster(t, "dc1", 3, consulVersion, true)
|
|
|
|
acceptingCluster, acceptingCtx, acceptingClient := NewCluster(t, &ClusterConfig{
|
|
|
|
acceptingCluster, acceptingCtx, acceptingClient := NewPeeringCluster(t, 3, 1, &libcluster.BuildOptions{
|
|
|
|
NumServers: 3,
|
|
|
|
Datacenter: "dc1",
|
|
|
|
NumClients: 1,
|
|
|
|
ConsulVersion: consulVersion,
|
|
|
|
BuildOpts: &libcluster.BuildOptions{
|
|
|
|
InjectAutoEncryption: true,
|
|
|
|
Datacenter: "dc1",
|
|
|
|
|
|
|
|
ConsulVersion: consulVersion,
|
|
|
|
|
|
|
|
InjectAutoEncryption: true,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
ApplyDefaultProxySettings: true,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
dialingCluster, dialingCtx, dialingClient := NewPeeringCluster(t, 1, 1, &libcluster.BuildOptions{
|
|
|
|
|
|
|
|
Datacenter: "dc2",
|
|
|
|
dialingCluster, dialingCtx, dialingClient := NewCluster(t, &ClusterConfig{
|
|
|
|
ConsulVersion: consulVersion,
|
|
|
|
NumServers: 1,
|
|
|
|
InjectAutoEncryption: true,
|
|
|
|
NumClients: 1,
|
|
|
|
|
|
|
|
BuildOpts: &libcluster.BuildOptions{
|
|
|
|
|
|
|
|
Datacenter: "dc2",
|
|
|
|
|
|
|
|
ConsulVersion: consulVersion,
|
|
|
|
|
|
|
|
InjectAutoEncryption: true,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
ApplyDefaultProxySettings: true,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// Create the mesh gateway for dataplane traffic and peering control plane traffic (if enabled)
|
|
|
|
// Create the mesh gateway for dataplane traffic and peering control plane traffic (if enabled)
|
|
|
@ -151,92 +162,68 @@ func BasicPeeringTwoClustersSetup(
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewDialingCluster creates a cluster for peering with a single dev agent
|
|
|
|
type ClusterConfig struct {
|
|
|
|
// TODO: note: formerly called CreatingPeeringClusterAndSetup
|
|
|
|
NumServers int
|
|
|
|
//
|
|
|
|
NumClients int
|
|
|
|
// Deprecated: use NewPeeringCluster mostly
|
|
|
|
ApplyDefaultProxySettings bool
|
|
|
|
func NewDialingCluster(
|
|
|
|
BuildOpts *libcluster.BuildOptions
|
|
|
|
t *testing.T,
|
|
|
|
Cmd string
|
|
|
|
version string,
|
|
|
|
LogConsumer *TestLogConsumer
|
|
|
|
dialingPeerName string,
|
|
|
|
Ports []int
|
|
|
|
) (*libcluster.Cluster, *api.Client, libservice.Service) {
|
|
|
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
t.Logf("creating the dialing cluster")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
opts := libcluster.BuildOptions{
|
|
|
|
|
|
|
|
Datacenter: "dc2",
|
|
|
|
|
|
|
|
InjectAutoEncryption: true,
|
|
|
|
|
|
|
|
InjectGossipEncryption: true,
|
|
|
|
|
|
|
|
AllowHTTPAnyway: true,
|
|
|
|
|
|
|
|
ConsulVersion: version,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := libcluster.NewBuildContext(t, opts)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
conf := libcluster.NewConfigBuilder(ctx).
|
|
|
|
|
|
|
|
Peering(true).
|
|
|
|
|
|
|
|
ToAgentConfig(t)
|
|
|
|
|
|
|
|
t.Logf("dc2 server config: \n%s", conf.JSON)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cluster, err := libcluster.NewN(t, *conf, 1)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
node := cluster.Agents[0]
|
|
|
|
|
|
|
|
client := node.GetClient()
|
|
|
|
|
|
|
|
libcluster.WaitForLeader(t, cluster, client)
|
|
|
|
|
|
|
|
libcluster.WaitForMembers(t, client, 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Default Proxy Settings
|
|
|
|
|
|
|
|
ok, err := utils.ApplyDefaultProxySettings(client)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create the mesh gateway for dataplane traffic
|
|
|
|
|
|
|
|
_, err = libservice.NewGatewayService(context.Background(), "mesh", "mesh", node)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create a service and proxy instance
|
|
|
|
|
|
|
|
clientProxyService, err := libservice.CreateAndRegisterStaticClientSidecar(node, dialingPeerName, true)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
libassert.CatalogServiceExists(t, client, "static-client-sidecar-proxy", nil)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return cluster, client, clientProxyService
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewPeeringCluster creates a cluster with peering enabled. It also creates
|
|
|
|
// NewCluster creates a cluster with peering enabled. It also creates
|
|
|
|
// and registers a mesh-gateway at the client agent. The API client returned is
|
|
|
|
// and registers a mesh-gateway at the client agent. The API client returned is
|
|
|
|
// pointed at the client agent.
|
|
|
|
// pointed at the client agent.
|
|
|
|
// - proxy-defaults.protocol = tcp
|
|
|
|
// - proxy-defaults.protocol = tcp
|
|
|
|
func NewPeeringCluster(
|
|
|
|
func NewCluster(
|
|
|
|
t *testing.T,
|
|
|
|
t *testing.T,
|
|
|
|
numServers int,
|
|
|
|
config *ClusterConfig,
|
|
|
|
numClients int,
|
|
|
|
|
|
|
|
buildOpts *libcluster.BuildOptions,
|
|
|
|
|
|
|
|
) (*libcluster.Cluster, *libcluster.BuildContext, *api.Client) {
|
|
|
|
) (*libcluster.Cluster, *libcluster.BuildContext, *api.Client) {
|
|
|
|
require.NotEmpty(t, buildOpts.Datacenter)
|
|
|
|
var (
|
|
|
|
require.True(t, numServers > 0)
|
|
|
|
cluster *libcluster.Cluster
|
|
|
|
|
|
|
|
err error
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
require.NotEmpty(t, config.BuildOpts.Datacenter)
|
|
|
|
|
|
|
|
require.True(t, config.NumServers > 0)
|
|
|
|
|
|
|
|
|
|
|
|
opts := libcluster.BuildOptions{
|
|
|
|
opts := libcluster.BuildOptions{
|
|
|
|
Datacenter: buildOpts.Datacenter,
|
|
|
|
Datacenter: config.BuildOpts.Datacenter,
|
|
|
|
InjectAutoEncryption: buildOpts.InjectAutoEncryption,
|
|
|
|
InjectAutoEncryption: config.BuildOpts.InjectAutoEncryption,
|
|
|
|
InjectGossipEncryption: true,
|
|
|
|
InjectGossipEncryption: true,
|
|
|
|
AllowHTTPAnyway: true,
|
|
|
|
AllowHTTPAnyway: true,
|
|
|
|
ConsulVersion: buildOpts.ConsulVersion,
|
|
|
|
ConsulVersion: config.BuildOpts.ConsulVersion,
|
|
|
|
ACLEnabled: buildOpts.ACLEnabled,
|
|
|
|
ACLEnabled: config.BuildOpts.ACLEnabled,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx := libcluster.NewBuildContext(t, opts)
|
|
|
|
ctx := libcluster.NewBuildContext(t, opts)
|
|
|
|
|
|
|
|
|
|
|
|
serverConf := libcluster.NewConfigBuilder(ctx).
|
|
|
|
serverConf := libcluster.NewConfigBuilder(ctx).
|
|
|
|
Bootstrap(numServers).
|
|
|
|
Bootstrap(config.NumServers).
|
|
|
|
Peering(true).
|
|
|
|
Peering(true).
|
|
|
|
ToAgentConfig(t)
|
|
|
|
ToAgentConfig(t)
|
|
|
|
t.Logf("%s server config: \n%s", opts.Datacenter, serverConf.JSON)
|
|
|
|
t.Logf("%s server config: \n%s", opts.Datacenter, serverConf.JSON)
|
|
|
|
|
|
|
|
|
|
|
|
cluster, err := libcluster.NewN(t, *serverConf, numServers)
|
|
|
|
// 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)
|
|
|
|
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
|
|
var retryJoin []string
|
|
|
|
var retryJoin []string
|
|
|
|
for i := 0; i < numServers; i++ {
|
|
|
|
for i := 0; i < config.NumServers; i++ {
|
|
|
|
retryJoin = append(retryJoin, fmt.Sprintf("agent-%d", i))
|
|
|
|
retryJoin = append(retryJoin, fmt.Sprintf("agent-%d", i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -248,18 +235,33 @@ func NewPeeringCluster(
|
|
|
|
clientConf := configbuiilder.ToAgentConfig(t)
|
|
|
|
clientConf := configbuiilder.ToAgentConfig(t)
|
|
|
|
t.Logf("%s client config: \n%s", opts.Datacenter, clientConf.JSON)
|
|
|
|
t.Logf("%s client config: \n%s", opts.Datacenter, clientConf.JSON)
|
|
|
|
|
|
|
|
|
|
|
|
require.NoError(t, cluster.AddN(*clientConf, numClients, true))
|
|
|
|
require.NoError(t, cluster.AddN(*clientConf, config.NumClients, true))
|
|
|
|
|
|
|
|
|
|
|
|
// Use the client agent as the HTTP endpoint since we will not rotate it in many tests.
|
|
|
|
// Use the client agent as the HTTP endpoint since we will not rotate it in many tests.
|
|
|
|
clientNode := cluster.Agents[numServers]
|
|
|
|
var client *api.Client
|
|
|
|
client := clientNode.GetClient()
|
|
|
|
if config.NumClients > 0 {
|
|
|
|
|
|
|
|
clientNode := cluster.Agents[config.NumServers]
|
|
|
|
|
|
|
|
client = clientNode.GetClient()
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
client = cluster.Agents[0].GetClient()
|
|
|
|
|
|
|
|
}
|
|
|
|
libcluster.WaitForLeader(t, cluster, client)
|
|
|
|
libcluster.WaitForLeader(t, cluster, client)
|
|
|
|
libcluster.WaitForMembers(t, client, numServers+numClients)
|
|
|
|
libcluster.WaitForMembers(t, client, config.NumServers+config.NumClients)
|
|
|
|
|
|
|
|
|
|
|
|
// Default Proxy Settings
|
|
|
|
// Default Proxy Settings
|
|
|
|
ok, err := utils.ApplyDefaultProxySettings(client)
|
|
|
|
if config.ApplyDefaultProxySettings {
|
|
|
|
require.NoError(t, err)
|
|
|
|
ok, err := utils.ApplyDefaultProxySettings(client)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return cluster, ctx, client
|
|
|
|
return cluster, ctx, client
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type TestLogConsumer struct {
|
|
|
|
|
|
|
|
Msgs []string
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (g *TestLogConsumer) Accept(l testcontainers.Log) {
|
|
|
|
|
|
|
|
g.Msgs = append(g.Msgs, string(l.Content))
|
|
|
|
|
|
|
|
}
|
|
|
|