consul: Snapshot KVS store support

This commit is contained in:
Armon Dadgar 2014-03-31 13:31:31 -07:00
parent aba2d997d3
commit 47807c5d19
2 changed files with 41 additions and 1 deletions

View File

@ -901,3 +901,17 @@ func (s *StateSnapshot) NodeChecks(node string) structs.HealthChecks {
_, checks := s.store.parseHealthChecks(s.lastIndex, res, err) _, checks := s.store.parseHealthChecks(s.lastIndex, res, err)
return checks 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
}

View File

@ -550,6 +550,16 @@ func TestStoreSnapshot(t *testing.T) {
t.Fatalf("err: %v") 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 // Take a snapshot
snap, err := store.Snapshot() snap, err := store.Snapshot()
if err != nil { if err != nil {
@ -558,7 +568,7 @@ func TestStoreSnapshot(t *testing.T) {
defer snap.Close() defer snap.Close()
// Check the last nodes // Check the last nodes
if idx := snap.LastIndex(); idx != 13 { if idx := snap.LastIndex(); idx != 15 {
t.Fatalf("bad: %v", idx) t.Fatalf("bad: %v", idx)
} }
@ -591,6 +601,12 @@ func TestStoreSnapshot(t *testing.T) {
t.Fatalf("bad: %v", checks[0]) 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! // Make some changes!
if err := store.EnsureService(14, "foo", &structs.NodeService{"db", "db", "slave", 8000}); err != nil { if err := store.EnsureService(14, "foo", &structs.NodeService{"db", "db", "slave", 8000}); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
@ -612,6 +628,10 @@ func TestStoreSnapshot(t *testing.T) {
t.Fatalf("err: %v") t.Fatalf("err: %v")
} }
if err := store.KVSDelete(18, "/web/a"); err != nil {
t.Fatalf("err: %v")
}
// Check snapshot has old values // Check snapshot has old values
nodes = snap.Nodes() nodes = snap.Nodes()
if len(nodes) != 2 { if len(nodes) != 2 {
@ -639,6 +659,12 @@ func TestStoreSnapshot(t *testing.T) {
if !reflect.DeepEqual(checks[0], check) { if !reflect.DeepEqual(checks[0], check) {
t.Fatalf("bad: %v", checks[0]) 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) { func TestEnsureCheck(t *testing.T) {