diff --git a/consul/state_store.go b/consul/state_store.go index aa8a59fe0b..2d01be2c7d 100644 --- a/consul/state_store.go +++ b/consul/state_store.go @@ -901,3 +901,17 @@ func (s *StateSnapshot) NodeChecks(node string) structs.HealthChecks { _, checks := s.store.parseHealthChecks(s.lastIndex, res, err) return checks } + +// KVSDump is used to list all KV entries +func (s *StateSnapshot) KVSDump() structs.DirEntries { + res, err := s.store.kvsTable.GetTxn(s.tx, "id") + if err != nil { + s.store.logger.Printf("[ERR] consul.state: Failed to get KVS entries: %v", err) + return nil + } + ents := make(structs.DirEntries, len(res)) + for idx, r := range res { + ents[idx] = r.(*structs.DirEntry) + } + return ents +} diff --git a/consul/state_store_test.go b/consul/state_store_test.go index f37c8b5470..e03e0ad76d 100644 --- a/consul/state_store_test.go +++ b/consul/state_store_test.go @@ -550,6 +550,16 @@ func TestStoreSnapshot(t *testing.T) { t.Fatalf("err: %v") } + // Add some KVS entries + d := &structs.DirEntry{Key: "/web/a", Flags: 42, Value: []byte("test")} + if err := store.KVSSet(14, d); err != nil { + t.Fatalf("err: %v", err) + } + d = &structs.DirEntry{Key: "/web/b", Flags: 42, Value: []byte("test")} + if err := store.KVSSet(15, d); err != nil { + t.Fatalf("err: %v", err) + } + // Take a snapshot snap, err := store.Snapshot() if err != nil { @@ -558,7 +568,7 @@ func TestStoreSnapshot(t *testing.T) { defer snap.Close() // Check the last nodes - if idx := snap.LastIndex(); idx != 13 { + if idx := snap.LastIndex(); idx != 15 { t.Fatalf("bad: %v", idx) } @@ -591,6 +601,12 @@ func TestStoreSnapshot(t *testing.T) { t.Fatalf("bad: %v", checks[0]) } + // Check we have the entries + ents := snap.KVSDump() + if len(ents) != 2 { + t.Fatalf("missing KVS entries!") + } + // Make some changes! if err := store.EnsureService(14, "foo", &structs.NodeService{"db", "db", "slave", 8000}); err != nil { t.Fatalf("err: %v", err) @@ -612,6 +628,10 @@ func TestStoreSnapshot(t *testing.T) { t.Fatalf("err: %v") } + if err := store.KVSDelete(18, "/web/a"); err != nil { + t.Fatalf("err: %v") + } + // Check snapshot has old values nodes = snap.Nodes() if len(nodes) != 2 { @@ -639,6 +659,12 @@ func TestStoreSnapshot(t *testing.T) { if !reflect.DeepEqual(checks[0], check) { t.Fatalf("bad: %v", checks[0]) } + + // Check we have the entries + ents = snap.KVSDump() + if len(ents) != 2 { + t.Fatalf("missing KVS entries!") + } } func TestEnsureCheck(t *testing.T) {