Find snapshot by source.

This commit is contained in:
Andrey Smirnov
2014-01-29 18:00:53 +04:00
parent e49afbcd2d
commit 772035ad44
2 changed files with 28 additions and 0 deletions
+12
View File
@@ -207,6 +207,18 @@ func (collection *SnapshotCollection) ByRemoteRepoSource(repo *RemoteRepo) []*Sn
return result return result
} }
// BySnapshotSource looks up snapshots that have specified snapshot as a source
func (collection *SnapshotCollection) BySnapshotSource(snapshot *Snapshot) []*Snapshot {
result := make([]*Snapshot, 0)
for _, s := range collection.list {
if s.SourceKind == "snapshot" && utils.StrSliceHasItem(s.SourceIDs, snapshot.UUID) {
result = append(result, s)
}
}
return result
}
// ForEach runs method for each snapshot // ForEach runs method for each snapshot
func (collection *SnapshotCollection) ForEach(handler func(*Snapshot) error) error { func (collection *SnapshotCollection) ForEach(handler func(*Snapshot) error) error {
var err error var err error
+16
View File
@@ -171,6 +171,22 @@ func (s *SnapshotCollectionSuite) TestFindByRemoteRepoSource(c *C) {
c.Check(s.collection.ByRemoteRepoSource(repo3), DeepEquals, []*Snapshot{}) c.Check(s.collection.ByRemoteRepoSource(repo3), DeepEquals, []*Snapshot{})
} }
func (s *SnapshotCollectionSuite) TestFindSnapshotSource(c *C) {
snapshot3 := NewSnapshotFromRefList("snap3", []*Snapshot{s.snapshot1, s.snapshot2}, s.reflist, "desc1")
snapshot4 := NewSnapshotFromRefList("snap4", []*Snapshot{s.snapshot1}, s.reflist, "desc2")
snapshot5 := NewSnapshotFromRefList("snap5", []*Snapshot{snapshot3}, s.reflist, "desc3")
c.Assert(s.collection.Add(s.snapshot1), IsNil)
c.Assert(s.collection.Add(s.snapshot2), IsNil)
c.Assert(s.collection.Add(snapshot3), IsNil)
c.Assert(s.collection.Add(snapshot4), IsNil)
c.Assert(s.collection.Add(snapshot5), IsNil)
c.Check(s.collection.BySnapshotSource(s.snapshot1), DeepEquals, []*Snapshot{snapshot3, snapshot4})
c.Check(s.collection.BySnapshotSource(s.snapshot2), DeepEquals, []*Snapshot{snapshot3})
c.Check(s.collection.BySnapshotSource(snapshot5), DeepEquals, []*Snapshot{})
}
func (s *SnapshotCollectionSuite) TestDrop(c *C) { func (s *SnapshotCollectionSuite) TestDrop(c *C) {
s.collection.Add(s.snapshot1) s.collection.Add(s.snapshot1)
s.collection.Add(s.snapshot2) s.collection.Add(s.snapshot2)