Adds missing autopilot snapshot test and avoids snapshotting nil. (#3333)

This commit is contained in:
James Phillips 2017-07-28 15:48:42 -07:00 committed by GitHub
parent e0643758f3
commit 8f1f762ddd
2 changed files with 46 additions and 0 deletions

View File

@ -691,6 +691,9 @@ func (s *consulSnapshot) persistAutopilot(sink raft.SnapshotSink,
if err != nil {
return err
}
if autopilot == nil {
return nil
}
sink.Write([]byte{byte(structs.AutopilotRequestType)})
if err := encoder.Encode(autopilot); err != nil {

View File

@ -6,6 +6,7 @@ import (
"time"
"github.com/hashicorp/consul/agent/consul/structs"
"github.com/pascaldekloe/goe/verify"
)
func TestStateStore_Autopilot(t *testing.T) {
@ -92,3 +93,45 @@ func TestStateStore_AutopilotCAS(t *testing.T) {
t.Fatalf("bad: %#v", config)
}
}
func TestStateStore_Autopilot_Snapshot_Restore(t *testing.T) {
s := testStateStore(t)
before := &structs.AutopilotConfig{
CleanupDeadServers: true,
}
if err := s.AutopilotSetConfig(99, before); err != nil {
t.Fatal(err)
}
snap := s.Snapshot()
defer snap.Close()
after := &structs.AutopilotConfig{
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)
}
verify.Values(t, "", before, snapped)
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)
}
verify.Values(t, "", before, res)
}