2017-02-24 04:32:13 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2017-07-18 02:05:24 +00:00
|
|
|
"time"
|
2017-02-24 04:32:13 +00:00
|
|
|
|
2017-12-12 00:38:52 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/autopilot"
|
2020-06-02 16:41:25 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-02-24 04:32:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStateStore_Autopilot(t *testing.T) {
|
|
|
|
s := testStateStore(t)
|
|
|
|
|
2017-12-12 00:38:52 +00:00
|
|
|
expected := &autopilot.Config{
|
2017-07-18 02:05:24 +00:00
|
|
|
CleanupDeadServers: true,
|
|
|
|
LastContactThreshold: 5 * time.Second,
|
|
|
|
MaxTrailingLogs: 500,
|
|
|
|
ServerStabilizationTime: 100 * time.Second,
|
|
|
|
RedundancyZoneTag: "az",
|
|
|
|
DisableUpgradeMigration: true,
|
|
|
|
UpgradeVersionTag: "build",
|
2017-02-24 04:32:13 +00:00
|
|
|
}
|
|
|
|
|
2017-02-24 21:08:49 +00:00
|
|
|
if err := s.AutopilotSetConfig(0, expected); err != nil {
|
2017-02-24 04:32:13 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
idx, config, err := s.AutopilotConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if idx != 0 {
|
|
|
|
t.Fatalf("bad: %d", idx)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(expected, config) {
|
|
|
|
t.Fatalf("bad: %#v, %#v", expected, config)
|
|
|
|
}
|
|
|
|
}
|
2017-02-24 21:08:49 +00:00
|
|
|
|
|
|
|
func TestStateStore_AutopilotCAS(t *testing.T) {
|
|
|
|
s := testStateStore(t)
|
|
|
|
|
2017-12-12 00:38:52 +00:00
|
|
|
expected := &autopilot.Config{
|
2017-02-28 22:23:14 +00:00
|
|
|
CleanupDeadServers: true,
|
2017-02-24 21:08:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.AutopilotSetConfig(0, expected); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := s.AutopilotSetConfig(1, expected); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do a CAS with an index lower than the entry
|
2017-12-12 00:38:52 +00:00
|
|
|
ok, err := s.AutopilotCASConfig(2, 0, &autopilot.Config{
|
2017-02-28 22:23:14 +00:00
|
|
|
CleanupDeadServers: false,
|
2017-02-24 21:08:49 +00:00
|
|
|
})
|
|
|
|
if ok || err != nil {
|
|
|
|
t.Fatalf("expected (false, nil), got: (%v, %#v)", ok, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the index is untouched and the entry
|
2017-02-28 22:23:14 +00:00
|
|
|
// has not been updated.
|
2017-02-24 21:08:49 +00:00
|
|
|
idx, config, err := s.AutopilotConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if idx != 1 {
|
|
|
|
t.Fatalf("bad: %d", idx)
|
|
|
|
}
|
2017-02-28 22:23:14 +00:00
|
|
|
if !config.CleanupDeadServers {
|
2017-02-24 21:08:49 +00:00
|
|
|
t.Fatalf("bad: %#v", config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do another CAS, this time with the correct index
|
2017-12-12 00:38:52 +00:00
|
|
|
ok, err = s.AutopilotCASConfig(2, 1, &autopilot.Config{
|
2017-02-28 22:23:14 +00:00
|
|
|
CleanupDeadServers: false,
|
2017-02-24 21:08:49 +00:00
|
|
|
})
|
|
|
|
if !ok || err != nil {
|
|
|
|
t.Fatalf("expected (true, nil), got: (%v, %#v)", ok, err)
|
|
|
|
}
|
|
|
|
|
2017-02-28 22:23:14 +00:00
|
|
|
// Make sure the config was updated
|
2017-02-24 21:08:49 +00:00
|
|
|
idx, config, err = s.AutopilotConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if idx != 2 {
|
|
|
|
t.Fatalf("bad: %d", idx)
|
|
|
|
}
|
2017-02-28 22:23:14 +00:00
|
|
|
if config.CleanupDeadServers {
|
2017-02-24 21:08:49 +00:00
|
|
|
t.Fatalf("bad: %#v", config)
|
|
|
|
}
|
|
|
|
}
|
2017-07-28 22:48:42 +00:00
|
|
|
|
|
|
|
func TestStateStore_Autopilot_Snapshot_Restore(t *testing.T) {
|
|
|
|
s := testStateStore(t)
|
2017-12-12 00:38:52 +00:00
|
|
|
before := &autopilot.Config{
|
2017-07-28 22:48:42 +00:00
|
|
|
CleanupDeadServers: true,
|
|
|
|
}
|
|
|
|
if err := s.AutopilotSetConfig(99, before); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
snap := s.Snapshot()
|
|
|
|
defer snap.Close()
|
|
|
|
|
2017-12-12 00:38:52 +00:00
|
|
|
after := &autopilot.Config{
|
2017-07-28 22:48:42 +00:00
|
|
|
CleanupDeadServers: false,
|
|
|
|
}
|
|
|
|
if err := s.AutopilotSetConfig(100, after); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
snapped, err := snap.Autopilot()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2020-06-02 16:41:25 +00:00
|
|
|
require.Equal(t, snapped, before, "autopilot snapshot")
|
2017-07-28 22:48:42 +00:00
|
|
|
|
|
|
|
s2 := testStateStore(t)
|
|
|
|
restore := s2.Restore()
|
|
|
|
if err := restore.Autopilot(snapped); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
restore.Commit()
|
|
|
|
|
|
|
|
idx, res, err := s2.AutopilotConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 99 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
2020-06-02 16:41:25 +00:00
|
|
|
require.Equal(t, res, before, "autopilot config")
|
2017-07-28 22:48:42 +00:00
|
|
|
}
|