From acc42e3483659a86914791ed64c518c51e0afd83 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Fri, 20 Dec 2013 20:13:22 +0400 Subject: [PATCH] Start of snapshot work. --- debian/snapshot.go | 52 +++++++++++++++++++++++++++++++++++++++++ debian/snapshot_test.go | 33 ++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 debian/snapshot.go create mode 100644 debian/snapshot_test.go diff --git a/debian/snapshot.go b/debian/snapshot.go new file mode 100644 index 00000000..f2ea02b6 --- /dev/null +++ b/debian/snapshot.go @@ -0,0 +1,52 @@ +package debian + +import ( + "code.google.com/p/go-uuid/uuid" + "fmt" + "time" +) + +// Snapshot is immutable state of repository: list of packages +type Snapshot struct { + // Persisten internal ID + UUID string + // Human-readable name + Name string + // Date of creation + CreatedAt time.Time + + // Source: kind + ID + SourceKind string + SourceIDs []string + // Description of how snapshot was created + Description string + + packageRefs *PackageRefList +} + +// NewSnapshotFromRepository creates snapshot from current state of repository +func NewSnapshotFromRepository(name string, repo *RemoteRepo) *Snapshot { + if repo.packageRefs == nil { + panic("repo.packageRefs == nil") + } + + return &Snapshot{ + UUID: uuid.New(), + Name: name, + CreatedAt: time.Now(), + SourceKind: "repo", + SourceIDs: []string{repo.UUID}, + Description: fmt.Sprintf("Snapshot from mirror %s", repo), + packageRefs: repo.packageRefs, + } +} + +// NumPackages returns number of packages in snapshot +func (s *Snapshot) NumPackages() int { + return s.packageRefs.Len() +} + +// Key is a unique id in DB +func (s *Snapshot) Key() []byte { + return []byte("S" + s.UUID) +} diff --git a/debian/snapshot_test.go b/debian/snapshot_test.go new file mode 100644 index 00000000..665f4947 --- /dev/null +++ b/debian/snapshot_test.go @@ -0,0 +1,33 @@ +package debian + +import ( + . "launchpad.net/gocheck" +) + +type SnapshotSuite struct { + PackageListMixinSuite + repo *RemoteRepo +} + +var _ = Suite(&SnapshotSuite{}) + +func (s *SnapshotSuite) SetUpTest(c *C) { + s.SetUpPackages() + s.repo, _ = NewRemoteRepo("yandex", "http://mirror.yandex.ru/debian/", "squeeze", []string{"main"}, []string{}) + s.repo.packageRefs = s.reflist +} + +func (s *SnapshotSuite) TestNewSnapshotFromRepository(c *C) { + snapshot := NewSnapshotFromRepository("snap1", s.repo) + c.Check(snapshot.Name, Equals, "snap1") + c.Check(snapshot.NumPackages(), Equals, 3) + + s.repo.packageRefs = nil + c.Check(func() { NewSnapshotFromRepository("snap2", s.repo) }, PanicMatches, "repo.packageRefs == nil") +} + +func (s *SnapshotSuite) TestKey(c *C) { + snapshot := NewSnapshotFromRepository("snap1", s.repo) + c.Assert(len(snapshot.Key()), Equals, 37) + c.Assert(snapshot.Key()[0], Equals, byte('S')) +}