2022-04-25 14:41:36 +00:00
|
|
|
package cluster
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2022-06-03 17:07:37 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2022-04-25 14:41:36 +00:00
|
|
|
|
2022-11-01 19:03:23 +00:00
|
|
|
"github.com/hashicorp/serf/serf"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/teris-io/shortid"
|
|
|
|
"github.com/testcontainers/testcontainers-go"
|
|
|
|
|
2022-05-19 19:05:41 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2022-06-03 17:07:37 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
2022-11-01 19:03:23 +00:00
|
|
|
libagent "github.com/hashicorp/consul/test/integration/consul-container/libs/agent"
|
2022-04-25 14:41:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Cluster provides an interface for creating and controlling a Consul cluster
|
2022-11-01 19:03:23 +00:00
|
|
|
// in integration tests, with agents running in containers.
|
|
|
|
// These fields are public in the event someone might want to surgically
|
|
|
|
// craft a test case.
|
2022-04-25 14:41:36 +00:00
|
|
|
type Cluster struct {
|
2022-11-01 19:03:23 +00:00
|
|
|
Agents []libagent.Agent
|
|
|
|
CACert string
|
|
|
|
CAKey string
|
|
|
|
ID string
|
|
|
|
Index int
|
|
|
|
Network testcontainers.Network
|
|
|
|
NetworkName string
|
2022-04-25 14:41:36 +00:00
|
|
|
}
|
|
|
|
|
2022-11-01 19:03:23 +00:00
|
|
|
// New creates a Consul cluster. An agent will be started for each of the given
|
2022-04-25 14:41:36 +00:00
|
|
|
// configs and joined to the cluster.
|
2022-11-01 19:03:23 +00:00
|
|
|
//
|
|
|
|
// A cluster has its own docker network for DNS connectivity, but is also joined
|
|
|
|
func New(configs []libagent.Config) (*Cluster, error) {
|
|
|
|
id, err := shortid.Generate()
|
2022-05-19 19:05:41 +00:00
|
|
|
if err != nil {
|
2022-11-01 19:03:23 +00:00
|
|
|
return nil, errors.Wrap(err, "could not cluster id")
|
|
|
|
}
|
|
|
|
|
|
|
|
name := fmt.Sprintf("consul-int-cluster-%s", id)
|
|
|
|
network, err := createNetwork(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not create cluster container network")
|
2022-05-19 19:05:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cluster := Cluster{
|
2022-11-01 19:03:23 +00:00
|
|
|
ID: id,
|
|
|
|
Network: network,
|
|
|
|
NetworkName: name,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cluster.Add(configs); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not start or join all agents")
|
2022-05-19 19:05:41 +00:00
|
|
|
}
|
2022-11-01 19:03:23 +00:00
|
|
|
return &cluster, nil
|
|
|
|
}
|
2022-04-25 14:41:36 +00:00
|
|
|
|
2022-11-01 19:03:23 +00:00
|
|
|
// Add starts an agent with the given configuration and joins it with the existing cluster
|
|
|
|
func (c *Cluster) Add(configs []libagent.Config) error {
|
2022-05-19 19:05:41 +00:00
|
|
|
|
2022-11-01 19:03:23 +00:00
|
|
|
agents := make([]libagent.Agent, len(configs))
|
|
|
|
for idx, conf := range configs {
|
|
|
|
n, err := libagent.NewConsulContainer(context.Background(), conf, c.NetworkName, c.Index)
|
2022-04-25 14:41:36 +00:00
|
|
|
if err != nil {
|
2022-11-01 19:03:23 +00:00
|
|
|
return errors.Wrapf(err, "could not add container index %d", idx)
|
2022-04-25 14:41:36 +00:00
|
|
|
}
|
2022-11-01 19:03:23 +00:00
|
|
|
agents[idx] = n
|
|
|
|
c.Index++
|
2022-04-25 14:41:36 +00:00
|
|
|
}
|
2022-11-01 19:03:23 +00:00
|
|
|
if err := c.Join(agents); err != nil {
|
|
|
|
return errors.Wrapf(err, "could not join agent")
|
2022-04-25 14:41:36 +00:00
|
|
|
}
|
2022-11-01 19:03:23 +00:00
|
|
|
return nil
|
2022-04-25 14:41:36 +00:00
|
|
|
}
|
|
|
|
|
2022-11-01 19:03:23 +00:00
|
|
|
// Join joins the given agent to the cluster.
|
|
|
|
func (c *Cluster) Join(agents []libagent.Agent) error {
|
2022-04-25 14:41:36 +00:00
|
|
|
var joinAddr string
|
2022-11-01 19:03:23 +00:00
|
|
|
if len(c.Agents) >= 1 {
|
|
|
|
joinAddr, _ = c.Agents[0].GetAddr()
|
|
|
|
} else if len(agents) >= 1 {
|
|
|
|
joinAddr, _ = agents[0].GetAddr()
|
2022-04-25 14:41:36 +00:00
|
|
|
}
|
|
|
|
|
2022-11-01 19:03:23 +00:00
|
|
|
for _, n := range agents {
|
|
|
|
err := n.GetClient().Agent().Join(joinAddr, false)
|
2022-04-25 14:41:36 +00:00
|
|
|
if err != nil {
|
2022-11-01 19:03:23 +00:00
|
|
|
return errors.Wrapf(err, "could not join agent %s to %s", n.GetName(), joinAddr)
|
|
|
|
}
|
|
|
|
c.Agents = append(c.Agents, n)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove instructs the agent to leave the cluster then removes it
|
|
|
|
// from the cluster Agent list.
|
|
|
|
func (c *Cluster) Remove(n libagent.Agent) error {
|
|
|
|
err := n.GetClient().Agent().Leave()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not remove agent %s", n.GetName())
|
|
|
|
}
|
|
|
|
|
|
|
|
foundIdx := -1
|
|
|
|
for idx, this := range c.Agents {
|
|
|
|
if this == n {
|
|
|
|
foundIdx = idx
|
|
|
|
break
|
2022-04-25 14:41:36 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-01 19:03:23 +00:00
|
|
|
|
|
|
|
if foundIdx == -1 {
|
|
|
|
return errors.New("could not find agent in cluster")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Agents = append(c.Agents[:foundIdx], c.Agents[foundIdx+1:]...)
|
2022-04-25 14:41:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-01 19:03:23 +00:00
|
|
|
// Terminate will attempt to terminate all agents in the cluster and its network. If any agent
|
2022-04-25 14:41:36 +00:00
|
|
|
// termination fails, Terminate will abort and return an error.
|
|
|
|
func (c *Cluster) Terminate() error {
|
2022-11-01 19:03:23 +00:00
|
|
|
for _, n := range c.Agents {
|
2022-04-25 14:41:36 +00:00
|
|
|
err := n.Terminate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-11-01 19:03:23 +00:00
|
|
|
|
|
|
|
// Testcontainers seems to clean this the network.
|
|
|
|
// Trigger it now will throw an error while the containers are still shutting down
|
|
|
|
//if err := c.Network.Remove(context.Background()); err != nil {
|
|
|
|
// return errors.Wrapf(err, "could not terminate cluster network %s", c.ID)
|
|
|
|
//}
|
2022-04-25 14:41:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-01 19:03:23 +00:00
|
|
|
// Leader returns the cluster leader agent, or an error if no leader is
|
2022-04-25 14:41:36 +00:00
|
|
|
// available.
|
2022-11-01 19:03:23 +00:00
|
|
|
func (c *Cluster) Leader() (libagent.Agent, error) {
|
|
|
|
if len(c.Agents) < 1 {
|
|
|
|
return nil, fmt.Errorf("no agent available")
|
2022-04-25 14:41:36 +00:00
|
|
|
}
|
2022-11-01 19:03:23 +00:00
|
|
|
n0 := c.Agents[0]
|
2022-05-19 19:05:41 +00:00
|
|
|
|
2022-11-01 19:03:23 +00:00
|
|
|
leaderAdd, err := getLeader(n0.GetClient())
|
2022-04-25 14:41:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-19 19:05:41 +00:00
|
|
|
|
2022-11-01 19:03:23 +00:00
|
|
|
for _, n := range c.Agents {
|
2022-04-25 14:41:36 +00:00
|
|
|
addr, _ := n.GetAddr()
|
|
|
|
if strings.Contains(leaderAdd, addr) {
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("leader not found")
|
|
|
|
}
|
2022-05-19 19:05:41 +00:00
|
|
|
|
2022-11-01 19:03:23 +00:00
|
|
|
func getLeader(client *api.Client) (string, error) {
|
|
|
|
leaderAdd, err := client.Status().Leader()
|
2022-05-19 19:05:41 +00:00
|
|
|
if err != nil {
|
2022-11-01 19:03:23 +00:00
|
|
|
return "", errors.Wrap(err, "could not query leader")
|
2022-05-19 19:05:41 +00:00
|
|
|
}
|
2022-11-01 19:03:23 +00:00
|
|
|
if leaderAdd == "" {
|
|
|
|
return "", errors.New("no leader available")
|
2022-05-19 19:05:41 +00:00
|
|
|
}
|
2022-11-01 19:03:23 +00:00
|
|
|
return leaderAdd, nil
|
2022-05-19 19:05:41 +00:00
|
|
|
}
|
|
|
|
|
2022-11-01 19:03:23 +00:00
|
|
|
// Followers returns the cluster following servers.
|
|
|
|
func (c *Cluster) Followers() ([]libagent.Agent, error) {
|
|
|
|
var followers []libagent.Agent
|
|
|
|
|
|
|
|
leader, err := c.Leader()
|
2022-05-19 19:05:41 +00:00
|
|
|
if err != nil {
|
2022-11-01 19:03:23 +00:00
|
|
|
return nil, fmt.Errorf("could not determine leader: %w", err)
|
2022-05-19 19:05:41 +00:00
|
|
|
}
|
2022-11-01 19:03:23 +00:00
|
|
|
|
|
|
|
for _, n := range c.Agents {
|
|
|
|
if n != leader && n.IsServer() {
|
|
|
|
followers = append(followers, n)
|
|
|
|
}
|
2022-05-19 19:05:41 +00:00
|
|
|
}
|
2022-11-01 19:03:23 +00:00
|
|
|
return followers, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Servers returns the handle to server agents
|
|
|
|
func (c *Cluster) Servers() ([]libagent.Agent, error) {
|
|
|
|
var servers []libagent.Agent
|
|
|
|
|
|
|
|
for _, n := range c.Agents {
|
|
|
|
if n.IsServer() {
|
|
|
|
servers = append(servers, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return servers, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clients returns the handle to client agents
|
|
|
|
func (c *Cluster) Clients() ([]libagent.Agent, error) {
|
|
|
|
var clients []libagent.Agent
|
|
|
|
|
|
|
|
for _, n := range c.Agents {
|
|
|
|
if !n.IsServer() {
|
|
|
|
clients = append(clients, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return clients, nil
|
2022-05-19 19:05:41 +00:00
|
|
|
}
|
2022-06-03 17:07:37 +00:00
|
|
|
|
|
|
|
const retryTimeout = 20 * time.Second
|
|
|
|
const retryFrequency = 500 * time.Millisecond
|
|
|
|
|
|
|
|
func LongFailer() *retry.Timer {
|
|
|
|
return &retry.Timer{Timeout: retryTimeout, Wait: retryFrequency}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WaitForLeader(t *testing.T, cluster *Cluster, client *api.Client) {
|
|
|
|
retry.RunWith(LongFailer(), t, func(r *retry.R) {
|
|
|
|
leader, err := cluster.Leader()
|
|
|
|
require.NoError(r, err)
|
|
|
|
require.NotEmpty(r, leader)
|
|
|
|
})
|
|
|
|
|
|
|
|
if client != nil {
|
|
|
|
waitForLeaderFromClient(t, client)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func waitForLeaderFromClient(t *testing.T, client *api.Client) {
|
|
|
|
retry.RunWith(LongFailer(), t, func(r *retry.R) {
|
2022-11-01 19:03:23 +00:00
|
|
|
leader, err := getLeader(client)
|
2022-06-03 17:07:37 +00:00
|
|
|
require.NoError(r, err)
|
|
|
|
require.NotEmpty(r, leader)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func WaitForMembers(t *testing.T, client *api.Client, expectN int) {
|
|
|
|
retry.RunWith(LongFailer(), t, func(r *retry.R) {
|
|
|
|
members, err := client.Agent().Members(false)
|
2022-11-01 19:03:23 +00:00
|
|
|
var activeMembers int
|
|
|
|
for _, member := range members {
|
|
|
|
if serf.MemberStatus(member.Status) == serf.StatusAlive {
|
|
|
|
activeMembers++
|
|
|
|
}
|
|
|
|
}
|
2022-06-03 17:07:37 +00:00
|
|
|
require.NoError(r, err)
|
2022-11-01 19:03:23 +00:00
|
|
|
require.Equal(r, activeMembers, expectN)
|
2022-06-03 17:07:37 +00:00
|
|
|
})
|
|
|
|
}
|