2017-12-18 21:16:23 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-01-30 03:53:34 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2017-12-18 21:16:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStateStore_Autopilot(t *testing.T) {
|
|
|
|
s := testStateStore(t)
|
|
|
|
|
2018-01-30 03:53:34 +00:00
|
|
|
expected := &structs.AutopilotConfig{
|
2017-12-18 21:16:23 +00:00
|
|
|
CleanupDeadServers: true,
|
|
|
|
LastContactThreshold: 5 * time.Second,
|
|
|
|
MaxTrailingLogs: 500,
|
|
|
|
ServerStabilizationTime: 100 * time.Second,
|
2018-01-30 03:53:34 +00:00
|
|
|
EnableRedundancyZones: true,
|
2017-12-18 21:16:23 +00:00
|
|
|
DisableUpgradeMigration: true,
|
2018-01-30 03:53:34 +00:00
|
|
|
EnableCustomUpgrades: true,
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.AutopilotSetConfig(0, expected); err != nil {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStateStore_AutopilotCAS(t *testing.T) {
|
|
|
|
s := testStateStore(t)
|
|
|
|
|
2018-01-30 03:53:34 +00:00
|
|
|
expected := &structs.AutopilotConfig{
|
2017-12-18 21:16:23 +00:00
|
|
|
CleanupDeadServers: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2018-01-30 03:53:34 +00:00
|
|
|
ok, err := s.AutopilotCASConfig(2, 0, &structs.AutopilotConfig{
|
2017-12-18 21:16:23 +00:00
|
|
|
CleanupDeadServers: false,
|
|
|
|
})
|
|
|
|
if ok || err != nil {
|
|
|
|
t.Fatalf("expected (false, nil), got: (%v, %#v)", ok, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the index is untouched and the entry
|
|
|
|
// has not been updated.
|
|
|
|
idx, config, err := s.AutopilotConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if idx != 1 {
|
|
|
|
t.Fatalf("bad: %d", idx)
|
|
|
|
}
|
|
|
|
if !config.CleanupDeadServers {
|
|
|
|
t.Fatalf("bad: %#v", config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do another CAS, this time with the correct index
|
2018-01-30 03:53:34 +00:00
|
|
|
ok, err = s.AutopilotCASConfig(2, 1, &structs.AutopilotConfig{
|
2017-12-18 21:16:23 +00:00
|
|
|
CleanupDeadServers: false,
|
|
|
|
})
|
|
|
|
if !ok || err != nil {
|
|
|
|
t.Fatalf("expected (true, nil), got: (%v, %#v)", ok, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the config was updated
|
|
|
|
idx, config, err = s.AutopilotConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if idx != 2 {
|
|
|
|
t.Fatalf("bad: %d", idx)
|
|
|
|
}
|
|
|
|
if config.CleanupDeadServers {
|
|
|
|
t.Fatalf("bad: %#v", config)
|
|
|
|
}
|
|
|
|
}
|