From 8f1f762dddefa475d66f9992f9f364cff042c11c Mon Sep 17 00:00:00 2001 From: James Phillips Date: Fri, 28 Jul 2017 15:48:42 -0700 Subject: [PATCH] Adds missing autopilot snapshot test and avoids snapshotting nil. (#3333) --- agent/consul/fsm.go | 3 ++ agent/consul/state/autopilot_test.go | 43 ++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/agent/consul/fsm.go b/agent/consul/fsm.go index 8258ba22b..a18e7ffec 100644 --- a/agent/consul/fsm.go +++ b/agent/consul/fsm.go @@ -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 { diff --git a/agent/consul/state/autopilot_test.go b/agent/consul/state/autopilot_test.go index 545c7e4b2..14a772aef 100644 --- a/agent/consul/state/autopilot_test.go +++ b/agent/consul/state/autopilot_test.go @@ -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) +}