2022-05-09 19:00:46 +00:00
|
|
|
package upgrade
|
2022-04-25 14:41:36 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-05-18 19:52:26 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-04-25 14:41:36 +00:00
|
|
|
|
2022-05-18 19:52:26 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2022-06-03 17:07:37 +00:00
|
|
|
libcluster "github.com/hashicorp/consul/integration/consul-container/libs/cluster"
|
2022-04-25 14:41:36 +00:00
|
|
|
"github.com/hashicorp/consul/integration/consul-container/libs/node"
|
|
|
|
"github.com/hashicorp/consul/integration/consul-container/libs/utils"
|
|
|
|
)
|
|
|
|
|
2022-05-09 19:00:46 +00:00
|
|
|
// Test health check GRPC call using Target Servers and Latest GA Clients
|
|
|
|
func TestTargetServersWithLatestGAClients(t *testing.T) {
|
2022-05-18 19:52:26 +00:00
|
|
|
const (
|
|
|
|
numServers = 3
|
|
|
|
numClients = 1
|
|
|
|
)
|
|
|
|
|
2022-06-03 17:07:37 +00:00
|
|
|
cluster := serversCluster(t, numServers, *utils.TargetImage)
|
|
|
|
defer terminate(t, cluster)
|
2022-04-25 14:41:36 +00:00
|
|
|
|
2022-06-03 17:07:37 +00:00
|
|
|
clients := clientsCreate(t, numClients, *utils.LatestImage, cluster.EncryptKey)
|
2022-05-18 19:52:26 +00:00
|
|
|
|
|
|
|
require.NoError(t, cluster.AddNodes(clients))
|
|
|
|
|
2022-04-25 14:41:36 +00:00
|
|
|
client := cluster.Nodes[0].GetClient()
|
2022-05-18 19:52:26 +00:00
|
|
|
|
2022-06-03 17:07:37 +00:00
|
|
|
libcluster.WaitForLeader(t, cluster, client)
|
|
|
|
libcluster.WaitForMembers(t, client, 4)
|
2022-05-18 19:52:26 +00:00
|
|
|
|
2022-04-25 14:41:36 +00:00
|
|
|
serviceName := "api"
|
2022-05-18 19:52:26 +00:00
|
|
|
index := serviceCreate(t, client, serviceName)
|
2022-04-25 14:41:36 +00:00
|
|
|
|
|
|
|
ch := make(chan []*api.ServiceEntry)
|
|
|
|
errCh := make(chan error)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
service, q, err := client.Health().Service(serviceName, "", false, &api.QueryOptions{WaitIndex: index})
|
2022-05-18 19:52:26 +00:00
|
|
|
if err == nil && q.QueryBackend != api.QueryBackendStreaming {
|
2022-04-25 14:41:36 +00:00
|
|
|
err = fmt.Errorf("invalid backend for this test %s", q.QueryBackend)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
errCh <- err
|
|
|
|
} else {
|
|
|
|
ch <- service
|
|
|
|
}
|
|
|
|
}()
|
2022-05-18 19:52:26 +00:00
|
|
|
|
|
|
|
require.NoError(t, client.Agent().ServiceRegister(
|
|
|
|
&api.AgentServiceRegistration{Name: serviceName, Port: 9998},
|
|
|
|
))
|
|
|
|
|
2022-04-25 14:41:36 +00:00
|
|
|
timer := time.NewTimer(1 * time.Second)
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
require.NoError(t, err)
|
|
|
|
case service := <-ch:
|
|
|
|
require.Len(t, service, 1)
|
|
|
|
require.Equal(t, serviceName, service[0].Service.Service)
|
|
|
|
require.Equal(t, 9998, service[0].Service.Port)
|
|
|
|
case <-timer.C:
|
|
|
|
t.Fatalf("test timeout")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test health check GRPC call using Mixed (majority latest) Servers and Latest GA Clients
|
|
|
|
func TestMixedServersMajorityLatestGAClient(t *testing.T) {
|
|
|
|
var configs []node.Config
|
|
|
|
configs = append(configs,
|
|
|
|
node.Config{
|
|
|
|
HCL: `node_name="` + utils.RandName("consul-server") + `"
|
|
|
|
log_level="TRACE"
|
|
|
|
server=true`,
|
|
|
|
Cmd: []string{"agent", "-client=0.0.0.0"},
|
2022-06-03 17:07:37 +00:00
|
|
|
Version: *utils.TargetImage,
|
2022-04-25 14:41:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
for i := 1; i < 3; i++ {
|
|
|
|
configs = append(configs,
|
|
|
|
node.Config{
|
|
|
|
HCL: `node_name="` + utils.RandName("consul-server") + `"
|
|
|
|
log_level="TRACE"
|
|
|
|
bootstrap_expect=3
|
|
|
|
server=true`,
|
|
|
|
Cmd: []string{"agent", "-client=0.0.0.0"},
|
2022-06-03 17:07:37 +00:00
|
|
|
Version: *utils.LatestImage,
|
2022-04-25 14:41:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-06-03 17:07:37 +00:00
|
|
|
cluster, err := libcluster.New(configs)
|
2022-04-25 14:41:36 +00:00
|
|
|
require.NoError(t, err)
|
2022-06-03 17:07:37 +00:00
|
|
|
defer terminate(t, cluster)
|
2022-04-25 14:41:36 +00:00
|
|
|
|
2022-05-18 19:52:26 +00:00
|
|
|
const (
|
|
|
|
numClients = 1
|
|
|
|
)
|
|
|
|
|
2022-06-03 17:07:37 +00:00
|
|
|
clients := clientsCreate(t, numClients, *utils.LatestImage, cluster.EncryptKey)
|
2022-05-18 19:52:26 +00:00
|
|
|
|
|
|
|
require.NoError(t, cluster.AddNodes(clients))
|
|
|
|
|
2022-04-25 14:41:36 +00:00
|
|
|
client := clients[0].GetClient()
|
2022-05-18 19:52:26 +00:00
|
|
|
|
2022-06-03 17:07:37 +00:00
|
|
|
libcluster.WaitForLeader(t, cluster, client)
|
|
|
|
libcluster.WaitForMembers(t, client, 4)
|
2022-04-25 14:41:36 +00:00
|
|
|
|
|
|
|
serviceName := "api"
|
2022-05-18 19:52:26 +00:00
|
|
|
index := serviceCreate(t, client, serviceName)
|
2022-04-25 14:41:36 +00:00
|
|
|
|
|
|
|
ch := make(chan []*api.ServiceEntry)
|
|
|
|
errCh := make(chan error)
|
|
|
|
go func() {
|
|
|
|
service, q, err := client.Health().Service(serviceName, "", false, &api.QueryOptions{WaitIndex: index})
|
2022-05-18 19:52:26 +00:00
|
|
|
if err == nil && q.QueryBackend != api.QueryBackendStreaming {
|
2022-04-25 14:41:36 +00:00
|
|
|
err = fmt.Errorf("invalid backend for this test %s", q.QueryBackend)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
errCh <- err
|
|
|
|
} else {
|
|
|
|
ch <- service
|
|
|
|
}
|
|
|
|
}()
|
2022-05-18 19:52:26 +00:00
|
|
|
|
|
|
|
require.NoError(t, client.Agent().ServiceRegister(
|
|
|
|
&api.AgentServiceRegistration{Name: serviceName, Port: 9998},
|
|
|
|
))
|
|
|
|
|
2022-04-25 14:41:36 +00:00
|
|
|
timer := time.NewTimer(1 * time.Second)
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
require.NoError(t, err)
|
|
|
|
case service := <-ch:
|
|
|
|
require.Len(t, service, 1)
|
|
|
|
require.Equal(t, serviceName, service[0].Service.Service)
|
|
|
|
require.Equal(t, 9998, service[0].Service.Port)
|
|
|
|
case <-timer.C:
|
|
|
|
t.Fatalf("test timeout")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-09 19:00:46 +00:00
|
|
|
// Test health check GRPC call using Mixed (majority target) Servers and Latest GA Clients
|
|
|
|
func TestMixedServersMajorityTargetGAClient(t *testing.T) {
|
2022-04-25 14:41:36 +00:00
|
|
|
var configs []node.Config
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
configs = append(configs,
|
|
|
|
node.Config{
|
|
|
|
HCL: `node_name="` + utils.RandName("consul-server") + `"
|
|
|
|
log_level="TRACE"
|
|
|
|
bootstrap_expect=3
|
|
|
|
server=true`,
|
|
|
|
Cmd: []string{"agent", "-client=0.0.0.0"},
|
2022-06-03 17:07:37 +00:00
|
|
|
Version: *utils.TargetImage,
|
2022-04-25 14:41:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
configs = append(configs,
|
|
|
|
node.Config{
|
|
|
|
HCL: `node_name="` + utils.RandName("consul-server") + `"
|
|
|
|
log_level="TRACE"
|
|
|
|
server=true`,
|
|
|
|
Cmd: []string{"agent", "-client=0.0.0.0"},
|
2022-06-03 17:07:37 +00:00
|
|
|
Version: *utils.LatestImage,
|
2022-04-25 14:41:36 +00:00
|
|
|
})
|
2022-05-09 19:00:46 +00:00
|
|
|
|
2022-06-03 17:07:37 +00:00
|
|
|
cluster, err := libcluster.New(configs)
|
2022-04-25 14:41:36 +00:00
|
|
|
require.NoError(t, err)
|
2022-06-03 17:07:37 +00:00
|
|
|
defer terminate(t, cluster)
|
2022-04-25 14:41:36 +00:00
|
|
|
|
2022-05-18 19:52:26 +00:00
|
|
|
const (
|
|
|
|
numClients = 1
|
|
|
|
)
|
|
|
|
|
2022-06-03 17:07:37 +00:00
|
|
|
clients := clientsCreate(t, numClients, *utils.LatestImage, cluster.EncryptKey)
|
2022-05-18 19:52:26 +00:00
|
|
|
|
|
|
|
require.NoError(t, cluster.AddNodes(clients))
|
|
|
|
|
2022-04-25 14:41:36 +00:00
|
|
|
client := clients[0].GetClient()
|
2022-05-18 19:52:26 +00:00
|
|
|
|
2022-06-03 17:07:37 +00:00
|
|
|
libcluster.WaitForLeader(t, cluster, client)
|
|
|
|
libcluster.WaitForMembers(t, client, 4)
|
2022-04-25 14:41:36 +00:00
|
|
|
|
|
|
|
serviceName := "api"
|
2022-05-18 19:52:26 +00:00
|
|
|
index := serviceCreate(t, client, serviceName)
|
2022-04-25 14:41:36 +00:00
|
|
|
|
|
|
|
ch := make(chan []*api.ServiceEntry)
|
|
|
|
errCh := make(chan error)
|
|
|
|
go func() {
|
|
|
|
service, q, err := client.Health().Service(serviceName, "", false, &api.QueryOptions{WaitIndex: index})
|
2022-05-18 19:52:26 +00:00
|
|
|
if err == nil && q.QueryBackend != api.QueryBackendStreaming {
|
2022-04-25 14:41:36 +00:00
|
|
|
err = fmt.Errorf("invalid backend for this test %s", q.QueryBackend)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
errCh <- err
|
|
|
|
} else {
|
|
|
|
ch <- service
|
|
|
|
}
|
|
|
|
}()
|
2022-05-18 19:52:26 +00:00
|
|
|
|
|
|
|
require.NoError(t, client.Agent().ServiceRegister(
|
|
|
|
&api.AgentServiceRegistration{Name: serviceName, Port: 9998},
|
|
|
|
))
|
|
|
|
|
2022-04-25 14:41:36 +00:00
|
|
|
timer := time.NewTimer(1 * time.Second)
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
require.NoError(t, err)
|
|
|
|
case service := <-ch:
|
|
|
|
require.Len(t, service, 1)
|
|
|
|
require.Equal(t, serviceName, service[0].Service.Service)
|
|
|
|
require.Equal(t, 9998, service[0].Service.Port)
|
|
|
|
case <-timer.C:
|
|
|
|
t.Fatalf("test timeout")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-19 19:05:41 +00:00
|
|
|
func clientsCreate(t *testing.T, numClients int, version string, serfKey string) []node.Node {
|
2022-04-25 14:41:36 +00:00
|
|
|
clients := make([]node.Node, numClients)
|
|
|
|
for i := 0; i < numClients; i++ {
|
2022-05-18 19:52:26 +00:00
|
|
|
var err error
|
2022-04-25 14:41:36 +00:00
|
|
|
clients[i], err = node.NewConsulContainer(context.Background(),
|
|
|
|
node.Config{
|
2022-05-19 19:05:41 +00:00
|
|
|
HCL: fmt.Sprintf(`
|
|
|
|
node_name = %q
|
|
|
|
log_level = "TRACE"
|
|
|
|
encrypt = %q`, utils.RandName("consul-client"), serfKey),
|
2022-04-25 14:41:36 +00:00
|
|
|
Cmd: []string{"agent", "-client=0.0.0.0"},
|
2022-05-09 18:36:49 +00:00
|
|
|
Version: version,
|
2022-04-25 14:41:36 +00:00
|
|
|
})
|
2022-05-18 19:52:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-04-25 14:41:36 +00:00
|
|
|
}
|
2022-05-18 19:52:26 +00:00
|
|
|
return clients
|
2022-04-25 14:41:36 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 19:52:26 +00:00
|
|
|
func serviceCreate(t *testing.T, client *api.Client, serviceName string) uint64 {
|
2022-04-25 14:41:36 +00:00
|
|
|
err := client.Agent().ServiceRegister(&api.AgentServiceRegistration{Name: serviceName, Port: 9999})
|
|
|
|
require.NoError(t, err)
|
2022-05-18 19:52:26 +00:00
|
|
|
|
2022-04-25 14:41:36 +00:00
|
|
|
service, meta, err := client.Catalog().Service(serviceName, "", &api.QueryOptions{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, service, 1)
|
|
|
|
require.Equal(t, serviceName, service[0].ServiceName)
|
|
|
|
require.Equal(t, 9999, service[0].ServicePort)
|
2022-05-18 19:52:26 +00:00
|
|
|
|
|
|
|
return meta.LastIndex
|
2022-04-25 14:41:36 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 17:07:37 +00:00
|
|
|
func serversCluster(t *testing.T, numServers int, version string) *libcluster.Cluster {
|
2022-04-25 14:41:36 +00:00
|
|
|
var configs []node.Config
|
|
|
|
for i := 0; i < numServers; i++ {
|
|
|
|
configs = append(configs, node.Config{
|
|
|
|
HCL: `node_name="` + utils.RandName("consul-server") + `"
|
|
|
|
log_level="TRACE"
|
|
|
|
bootstrap_expect=3
|
|
|
|
server=true`,
|
|
|
|
Cmd: []string{"agent", "-client=0.0.0.0"},
|
2022-05-09 19:00:46 +00:00
|
|
|
Version: version,
|
2022-04-25 14:41:36 +00:00
|
|
|
})
|
|
|
|
}
|
2022-06-03 17:07:37 +00:00
|
|
|
cluster, err := libcluster.New(configs)
|
2022-04-25 14:41:36 +00:00
|
|
|
require.NoError(t, err)
|
2022-05-18 19:52:26 +00:00
|
|
|
|
2022-06-03 17:07:37 +00:00
|
|
|
libcluster.WaitForLeader(t, cluster, nil)
|
|
|
|
libcluster.WaitForMembers(t, cluster.Nodes[0].GetClient(), numServers)
|
2022-05-18 19:52:26 +00:00
|
|
|
|
|
|
|
return cluster
|
2022-04-25 14:41:36 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 17:07:37 +00:00
|
|
|
func terminate(t *testing.T, cluster *libcluster.Cluster) {
|
2022-04-25 14:41:36 +00:00
|
|
|
err := cluster.Terminate()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|