From 772035ad446f1c3af157aeafdc2656c33ebf7a72 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Wed, 29 Jan 2014 18:00:53 +0400 Subject: [PATCH] Find snapshot by source. --- debian/snapshot.go | 12 ++++++++++++ debian/snapshot_test.go | 16 ++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/debian/snapshot.go b/debian/snapshot.go index 221385eb..4cd0119a 100644 --- a/debian/snapshot.go +++ b/debian/snapshot.go @@ -207,6 +207,18 @@ func (collection *SnapshotCollection) ByRemoteRepoSource(repo *RemoteRepo) []*Sn 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 func (collection *SnapshotCollection) ForEach(handler func(*Snapshot) error) error { var err error diff --git a/debian/snapshot_test.go b/debian/snapshot_test.go index 55c9f624..0e9b75f3 100644 --- a/debian/snapshot_test.go +++ b/debian/snapshot_test.go @@ -171,6 +171,22 @@ func (s *SnapshotCollectionSuite) TestFindByRemoteRepoSource(c *C) { 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) { s.collection.Add(s.snapshot1) s.collection.Add(s.snapshot2)