Start of snapshot work.

This commit is contained in:
Andrey Smirnov
2013-12-20 20:13:22 +04:00
parent e38cb8b39e
commit acc42e3483
2 changed files with 85 additions and 0 deletions
+52
View File
@@ -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)
}
+33
View File
@@ -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'))
}