diff --git a/debian/snapshot.go b/debian/snapshot.go index ed0eb340..62e6e048 100644 --- a/debian/snapshot.go +++ b/debian/snapshot.go @@ -46,6 +46,24 @@ func NewSnapshotFromRepository(name string, repo *RemoteRepo) (*Snapshot, error) }, nil } +// NewSnapshotFromPackageList creates snapshot from PackageList +func NewSnapshotFromPackageList(name string, sources []*Snapshot, list *PackageList, description string) *Snapshot { + sourceUUIDs := make([]string, len(sources)) + for i := range sources { + sourceUUIDs[i] = sources[i].UUID + } + + return &Snapshot{ + UUID: uuid.New(), + Name: name, + CreatedAt: time.Now(), + SourceKind: "snapshot", + SourceIDs: sourceUUIDs, + Description: description, + packageRefs: NewPackageRefListFromPackageList(list), + } +} + // String returns string representation of snapshot func (s *Snapshot) String() string { return fmt.Sprintf("[%s]: %s", s.Name, s.Description) diff --git a/debian/snapshot_test.go b/debian/snapshot_test.go index 465f142e..41003150 100644 --- a/debian/snapshot_test.go +++ b/debian/snapshot_test.go @@ -24,12 +24,24 @@ func (s *SnapshotSuite) TestNewSnapshotFromRepository(c *C) { c.Check(snapshot.Name, Equals, "snap1") c.Check(snapshot.NumPackages(), Equals, 3) c.Check(snapshot.RefList().Len(), Equals, 3) + c.Check(snapshot.SourceKind, Equals, "repo") + c.Check(snapshot.SourceIDs, DeepEquals, []string{s.repo.UUID}) s.repo.packageRefs = nil _, err := NewSnapshotFromRepository("snap2", s.repo) c.Check(err, ErrorMatches, ".*not updated") } +func (s *SnapshotSuite) TestNewSnapshotFromPackageList(c *C) { + snap, _ := NewSnapshotFromRepository("snap1", s.repo) + + snapshot := NewSnapshotFromPackageList("snap2", []*Snapshot{snap}, s.list, "Pulled") + c.Check(snapshot.Name, Equals, "snap2") + c.Check(snapshot.NumPackages(), Equals, 3) + c.Check(snapshot.SourceKind, Equals, "snapshot") + c.Check(snapshot.SourceIDs, DeepEquals, []string{snap.UUID}) +} + func (s *SnapshotSuite) TestKey(c *C) { snapshot, _ := NewSnapshotFromRepository("snap1", s.repo) c.Assert(len(snapshot.Key()), Equals, 37)