2017-03-30 19:35:50 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-10-29 14:04:41 +00:00
|
|
|
msgpackrpc "github.com/hashicorp/net-rpc-msgpackrpc"
|
2017-03-30 19:35:50 +00:00
|
|
|
"github.com/hashicorp/raft"
|
2020-09-25 17:46:38 +00:00
|
|
|
autopilot "github.com/hashicorp/raft-autopilot"
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-08-06 22:00:58 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/acl"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2017-03-30 19:35:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestOperator_Autopilot_GetConfiguration(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2017-03-30 19:35:50 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.AutopilotConfig.CleanupDeadServers = false
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2017-03-30 19:35:50 +00:00
|
|
|
|
|
|
|
arg := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
2020-09-25 17:46:38 +00:00
|
|
|
var reply structs.AutopilotConfig
|
2017-03-30 19:35:50 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Operator.AutopilotGetConfiguration", &arg, &reply)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if reply.CleanupDeadServers {
|
|
|
|
t.Fatalf("bad: %#v", reply)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOperator_Autopilot_GetConfiguration_ACLDeny(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2017-03-30 19:35:50 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
2021-08-06 22:00:58 +00:00
|
|
|
c.PrimaryDatacenter = "dc1"
|
2018-10-19 16:04:07 +00:00
|
|
|
c.ACLsEnabled = true
|
2017-03-30 19:35:50 +00:00
|
|
|
c.ACLMasterToken = "root"
|
2021-08-06 22:39:39 +00:00
|
|
|
c.ACLResolverSettings.ACLDefaultPolicy = "deny"
|
2017-03-30 19:35:50 +00:00
|
|
|
c.AutopilotConfig.CleanupDeadServers = false
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2017-03-30 19:35:50 +00:00
|
|
|
|
|
|
|
// Try to get config without permissions
|
|
|
|
arg := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
2020-09-25 17:46:38 +00:00
|
|
|
var reply structs.AutopilotConfig
|
2017-03-30 19:35:50 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Operator.AutopilotGetConfiguration", &arg, &reply)
|
2017-08-23 14:52:48 +00:00
|
|
|
if !acl.IsErrPermissionDenied(err) {
|
2017-03-30 19:35:50 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-09-03 20:13:11 +00:00
|
|
|
token := createToken(t, codec, `operator = "read"`)
|
2017-03-30 19:35:50 +00:00
|
|
|
|
|
|
|
// Now we can read and verify the config
|
|
|
|
arg.Token = token
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Operator.AutopilotGetConfiguration", &arg, &reply)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if reply.CleanupDeadServers {
|
|
|
|
t.Fatalf("bad: %#v", reply)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOperator_Autopilot_SetConfiguration(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2017-03-30 19:35:50 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.AutopilotConfig.CleanupDeadServers = false
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2017-03-30 19:35:50 +00:00
|
|
|
|
|
|
|
// Change the autopilot config from the default
|
|
|
|
arg := structs.AutopilotSetConfigRequest{
|
|
|
|
Datacenter: "dc1",
|
2020-09-25 17:46:38 +00:00
|
|
|
Config: structs.AutopilotConfig{
|
2017-03-30 19:35:50 +00:00
|
|
|
CleanupDeadServers: true,
|
2019-10-29 14:04:41 +00:00
|
|
|
MinQuorum: 3,
|
2017-03-30 19:35:50 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
var reply *bool
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Operator.AutopilotSetConfiguration", &arg, &reply)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure it's changed
|
|
|
|
state := s1.fsm.State()
|
|
|
|
_, config, err := state.AutopilotConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-10-29 14:04:41 +00:00
|
|
|
if !config.CleanupDeadServers && config.MinQuorum != 3 {
|
2017-03-30 19:35:50 +00:00
|
|
|
t.Fatalf("bad: %#v", config)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOperator_Autopilot_SetConfiguration_ACLDeny(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2017-03-30 19:35:50 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
2021-08-06 22:00:58 +00:00
|
|
|
c.PrimaryDatacenter = "dc1"
|
2018-10-19 16:04:07 +00:00
|
|
|
c.ACLsEnabled = true
|
2017-03-30 19:35:50 +00:00
|
|
|
c.ACLMasterToken = "root"
|
2021-08-06 22:39:39 +00:00
|
|
|
c.ACLResolverSettings.ACLDefaultPolicy = "deny"
|
2017-03-30 19:35:50 +00:00
|
|
|
c.AutopilotConfig.CleanupDeadServers = false
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2017-03-30 19:35:50 +00:00
|
|
|
|
|
|
|
// Try to set config without permissions
|
|
|
|
arg := structs.AutopilotSetConfigRequest{
|
|
|
|
Datacenter: "dc1",
|
2020-09-25 17:46:38 +00:00
|
|
|
Config: structs.AutopilotConfig{
|
2017-03-30 19:35:50 +00:00
|
|
|
CleanupDeadServers: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var reply *bool
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Operator.AutopilotSetConfiguration", &arg, &reply)
|
2017-08-23 14:52:48 +00:00
|
|
|
if !acl.IsErrPermissionDenied(err) {
|
2017-03-30 19:35:50 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-09-03 20:13:11 +00:00
|
|
|
token := createToken(t, codec, `operator = "write"`)
|
2017-03-30 19:35:50 +00:00
|
|
|
|
|
|
|
// Now we can update the config
|
|
|
|
arg.Token = token
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Operator.AutopilotSetConfiguration", &arg, &reply)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure it's changed
|
|
|
|
state := s1.fsm.State()
|
|
|
|
_, config, err := state.AutopilotConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !config.CleanupDeadServers {
|
|
|
|
t.Fatalf("bad: %#v", config)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOperator_ServerHealth(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2017-03-30 19:35:50 +00:00
|
|
|
conf := func(c *Config) {
|
|
|
|
c.Datacenter = "dc1"
|
|
|
|
c.Bootstrap = false
|
|
|
|
c.BootstrapExpect = 3
|
|
|
|
c.RaftConfig.ProtocolVersion = 3
|
|
|
|
c.ServerHealthInterval = 100 * time.Millisecond
|
|
|
|
c.AutopilotInterval = 100 * time.Millisecond
|
|
|
|
}
|
|
|
|
dir1, s1 := testServerWithConfig(t, conf)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
dir2, s2 := testServerWithConfig(t, conf)
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer s2.Shutdown()
|
2017-05-05 10:29:49 +00:00
|
|
|
joinLAN(t, s2, s1)
|
2017-03-30 19:35:50 +00:00
|
|
|
|
|
|
|
dir3, s3 := testServerWithConfig(t, conf)
|
|
|
|
defer os.RemoveAll(dir3)
|
|
|
|
defer s3.Shutdown()
|
2017-05-05 10:29:49 +00:00
|
|
|
joinLAN(t, s3, s1)
|
2017-03-30 19:35:50 +00:00
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2017-05-04 22:52:53 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2017-03-30 19:35:50 +00:00
|
|
|
arg := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
2020-09-25 17:46:38 +00:00
|
|
|
var reply structs.AutopilotHealthReply
|
2017-03-30 19:35:50 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Operator.ServerHealth", &arg, &reply)
|
|
|
|
if err != nil {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("err: %v", err)
|
2017-03-30 19:35:50 +00:00
|
|
|
}
|
|
|
|
if !reply.Healthy {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("bad: %v", reply)
|
2017-03-30 19:35:50 +00:00
|
|
|
}
|
|
|
|
if reply.FailureTolerance != 1 {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("bad: %v", reply)
|
2017-03-30 19:35:50 +00:00
|
|
|
}
|
|
|
|
if len(reply.Servers) != 3 {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("bad: %v", reply)
|
2017-03-30 19:35:50 +00:00
|
|
|
}
|
|
|
|
// Leader should have LastContact == 0, others should be positive
|
|
|
|
for _, s := range reply.Servers {
|
|
|
|
isLeader := s1.raft.Leader() == raft.ServerAddress(s.Address)
|
|
|
|
if isLeader && s.LastContact != 0 {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("bad: %v", reply)
|
2017-03-30 19:35:50 +00:00
|
|
|
}
|
|
|
|
if !isLeader && s.LastContact <= 0 {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("bad: %v", reply)
|
2017-03-30 19:35:50 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
})
|
2017-03-30 19:35:50 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 17:46:38 +00:00
|
|
|
func TestOperator_AutopilotState(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2020-09-25 17:46:38 +00:00
|
|
|
conf := func(c *Config) {
|
2017-03-30 19:35:50 +00:00
|
|
|
c.Datacenter = "dc1"
|
2020-09-25 17:46:38 +00:00
|
|
|
c.Bootstrap = false
|
|
|
|
c.BootstrapExpect = 3
|
|
|
|
c.RaftConfig.ProtocolVersion = 3
|
|
|
|
c.ServerHealthInterval = 100 * time.Millisecond
|
|
|
|
c.AutopilotInterval = 100 * time.Millisecond
|
|
|
|
}
|
|
|
|
dir1, s1 := testServerWithConfig(t, conf)
|
2017-03-30 19:35:50 +00:00
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
2020-09-25 17:46:38 +00:00
|
|
|
dir2, s2 := testServerWithConfig(t, conf)
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer s2.Shutdown()
|
|
|
|
joinLAN(t, s2, s1)
|
|
|
|
|
|
|
|
dir3, s3 := testServerWithConfig(t, conf)
|
|
|
|
defer os.RemoveAll(dir3)
|
|
|
|
defer s3.Shutdown()
|
|
|
|
joinLAN(t, s3, s1)
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
arg := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
var reply autopilot.State
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Operator.AutopilotState", &arg, &reply)
|
|
|
|
require.NoError(r, err)
|
|
|
|
require.True(r, reply.Healthy)
|
|
|
|
require.Equal(r, 1, reply.FailureTolerance)
|
|
|
|
require.Len(r, reply.Servers, 3)
|
|
|
|
|
|
|
|
// Leader should have LastContact == 0, others should be positive
|
|
|
|
for _, s := range reply.Servers {
|
|
|
|
isLeader := s1.raft.Leader() == s.Server.Address
|
|
|
|
if isLeader {
|
|
|
|
require.Zero(r, s.Stats.LastContact)
|
|
|
|
} else {
|
|
|
|
require.NotEqual(r, time.Duration(0), s.Stats.LastContact)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2017-03-30 19:35:50 +00:00
|
|
|
}
|