Adds ACL snapshot/restore test.

This commit is contained in:
James Phillips 2015-10-06 20:25:24 -07:00
parent 21bc8e0996
commit bc34ae2f5c
1 changed files with 76 additions and 4 deletions

View File

@ -115,8 +115,8 @@ func verifyWatch(t *testing.T, watch Watch, fn func()) {
select { select {
case <-ch: case <-ch:
case <-time.After(100 * time.Millisecond): case <-time.After(1 * time.Second):
t.Fatalf("watch was not notified") t.Fatalf("watch was not notified in time")
} }
} }
@ -1800,6 +1800,9 @@ func TestStateStore_ACLList(t *testing.T) {
// Query the ACLs // Query the ACLs
idx, res, err = s.ACLList() idx, res, err = s.ACLList()
if err != nil {
t.Fatalf("err: %s", err)
}
if idx != 2 { if idx != 2 {
t.Fatalf("bad index: %d", idx) t.Fatalf("bad index: %d", idx)
} }
@ -1810,6 +1813,75 @@ func TestStateStore_ACLList(t *testing.T) {
} }
} }
func TestStateStore_ACL_Snapshot_Restore(t *testing.T) {
s := testStateStore(t)
// Insert some ACLs.
acls := structs.ACLs{
&structs.ACL{
ID: "acl1",
Type: structs.ACLTypeClient,
Rules: "rules1",
RaftIndex: structs.RaftIndex{
CreateIndex: 1,
ModifyIndex: 1,
},
},
&structs.ACL{
ID: "acl2",
Type: structs.ACLTypeClient,
Rules: "rules2",
RaftIndex: structs.RaftIndex{
CreateIndex: 2,
ModifyIndex: 2,
},
},
}
for _, acl := range acls {
if err := s.ACLSet(acl.ModifyIndex, acl); err != nil {
t.Fatalf("err: %s", err)
}
}
// Snapshot the ACLs.
snap := s.Snapshot()
defer snap.Close()
// Verify the snapshot.
if idx := snap.LastIndex(); idx != 2 {
t.Fatalf("bad index: %d", idx)
}
dump, err := snap.ACLDump()
if err != nil {
t.Fatalf("err: %s", err)
}
if !reflect.DeepEqual(dump, acls) {
t.Fatalf("bad: %#v", dump)
}
// Restore the values into a new state store.
func() {
s := testStateStore(t)
for _, acl := range dump {
if err := s.ACLRestore(acl); err != nil {
t.Fatalf("err: %s", err)
}
}
// Read the restored ACLs back out and verify that they match.
idx, res, err := s.ACLList()
if err != nil {
t.Fatalf("err: %s", err)
}
if idx != 2 {
t.Fatalf("bad index: %d", idx)
}
if !reflect.DeepEqual(res, acls) {
t.Fatalf("bad: %#v", res)
}
}()
}
func TestStateStore_ACLDelete(t *testing.T) { func TestStateStore_ACLDelete(t *testing.T) {
s := testStateStore(t) s := testStateStore(t)
@ -1852,18 +1924,18 @@ func TestStateStore_ACLDelete(t *testing.T) {
func TestStateStore_ACL_Watches(t *testing.T) { func TestStateStore_ACL_Watches(t *testing.T) {
s := testStateStore(t) s := testStateStore(t)
// Call functions that update the ACLs table and make sure a watch fires
// each time.
verifyWatch(t, s.GetTableWatch("acls"), func() { verifyWatch(t, s.GetTableWatch("acls"), func() {
if err := s.ACLSet(1, &structs.ACL{ID: "acl1"}); err != nil { if err := s.ACLSet(1, &structs.ACL{ID: "acl1"}); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
}) })
verifyWatch(t, s.GetTableWatch("acls"), func() { verifyWatch(t, s.GetTableWatch("acls"), func() {
if err := s.ACLDelete(2, "acl1"); err != nil { if err := s.ACLDelete(2, "acl1"); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
}) })
verifyWatch(t, s.GetTableWatch("acls"), func() { verifyWatch(t, s.GetTableWatch("acls"), func() {
if err := s.ACLRestore(&structs.ACL{ID: "acl1"}); err != nil { if err := s.ACLRestore(&structs.ACL{ID: "acl1"}); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)