Make snapshot creation errorable.

This commit is contained in:
Andrey Smirnov
2013-12-23 13:04:42 +04:00
parent 0f1f0a542c
commit bed85d078f
2 changed files with 12 additions and 10 deletions
+4 -3
View File
@@ -3,6 +3,7 @@ package debian
import ( import (
"bytes" "bytes"
"code.google.com/p/go-uuid/uuid" "code.google.com/p/go-uuid/uuid"
"errors"
"fmt" "fmt"
"github.com/smira/aptly/database" "github.com/smira/aptly/database"
"github.com/ugorji/go/codec" "github.com/ugorji/go/codec"
@@ -29,9 +30,9 @@ type Snapshot struct {
} }
// NewSnapshotFromRepository creates snapshot from current state of repository // NewSnapshotFromRepository creates snapshot from current state of repository
func NewSnapshotFromRepository(name string, repo *RemoteRepo) *Snapshot { func NewSnapshotFromRepository(name string, repo *RemoteRepo) (*Snapshot, error) {
if repo.packageRefs == nil { if repo.packageRefs == nil {
panic("repo.packageRefs == nil") return nil, errors.New("mirror not updated")
} }
return &Snapshot{ return &Snapshot{
@@ -42,7 +43,7 @@ func NewSnapshotFromRepository(name string, repo *RemoteRepo) *Snapshot {
SourceIDs: []string{repo.UUID}, SourceIDs: []string{repo.UUID},
Description: fmt.Sprintf("Snapshot from mirror %s", repo), Description: fmt.Sprintf("Snapshot from mirror %s", repo),
packageRefs: repo.packageRefs, packageRefs: repo.packageRefs,
} }, nil
} }
// String returns string representation of snapshot // String returns string representation of snapshot
+8 -7
View File
@@ -19,29 +19,30 @@ func (s *SnapshotSuite) SetUpTest(c *C) {
} }
func (s *SnapshotSuite) TestNewSnapshotFromRepository(c *C) { func (s *SnapshotSuite) TestNewSnapshotFromRepository(c *C) {
snapshot := NewSnapshotFromRepository("snap1", s.repo) snapshot, _ := NewSnapshotFromRepository("snap1", s.repo)
c.Check(snapshot.Name, Equals, "snap1") c.Check(snapshot.Name, Equals, "snap1")
c.Check(snapshot.NumPackages(), Equals, 3) c.Check(snapshot.NumPackages(), Equals, 3)
s.repo.packageRefs = nil s.repo.packageRefs = nil
c.Check(func() { NewSnapshotFromRepository("snap2", s.repo) }, PanicMatches, "repo.packageRefs == nil") _, err := NewSnapshotFromRepository("snap2", s.repo)
c.Check(err, ErrorMatches, ".*not updated")
} }
func (s *SnapshotSuite) TestKey(c *C) { func (s *SnapshotSuite) TestKey(c *C) {
snapshot := NewSnapshotFromRepository("snap1", s.repo) snapshot, _ := NewSnapshotFromRepository("snap1", s.repo)
c.Assert(len(snapshot.Key()), Equals, 37) c.Assert(len(snapshot.Key()), Equals, 37)
c.Assert(snapshot.Key()[0], Equals, byte('S')) c.Assert(snapshot.Key()[0], Equals, byte('S'))
} }
func (s *SnapshotSuite) TestRefKey(c *C) { func (s *SnapshotSuite) TestRefKey(c *C) {
snapshot := NewSnapshotFromRepository("snap1", s.repo) snapshot, _ := NewSnapshotFromRepository("snap1", s.repo)
c.Assert(len(snapshot.RefKey()), Equals, 37) c.Assert(len(snapshot.RefKey()), Equals, 37)
c.Assert(snapshot.RefKey()[0], Equals, byte('E')) c.Assert(snapshot.RefKey()[0], Equals, byte('E'))
c.Assert(snapshot.RefKey()[1:], DeepEquals, snapshot.Key()[1:]) c.Assert(snapshot.RefKey()[1:], DeepEquals, snapshot.Key()[1:])
} }
func (s *SnapshotSuite) TestEncodeDecode(c *C) { func (s *SnapshotSuite) TestEncodeDecode(c *C) {
snapshot := NewSnapshotFromRepository("snap1", s.repo) snapshot, _ := NewSnapshotFromRepository("snap1", s.repo)
s.repo.packageRefs = s.reflist s.repo.packageRefs = s.reflist
snapshot2 := &Snapshot{} snapshot2 := &Snapshot{}
@@ -67,11 +68,11 @@ func (s *SnapshotCollectionSuite) SetUpTest(c *C) {
s.repo1, _ = NewRemoteRepo("yandex", "http://mirror.yandex.ru/debian/", "squeeze", []string{"main"}, []string{}) s.repo1, _ = NewRemoteRepo("yandex", "http://mirror.yandex.ru/debian/", "squeeze", []string{"main"}, []string{})
s.repo1.packageRefs = s.reflist s.repo1.packageRefs = s.reflist
s.snapshot1 = NewSnapshotFromRepository("snap1", s.repo1) s.snapshot1, _ = NewSnapshotFromRepository("snap1", s.repo1)
s.repo2, _ = NewRemoteRepo("android", "http://mirror.yandex.ru/debian/", "lenny", []string{"main"}, []string{}) s.repo2, _ = NewRemoteRepo("android", "http://mirror.yandex.ru/debian/", "lenny", []string{"main"}, []string{})
s.repo2.packageRefs = s.reflist s.repo2.packageRefs = s.reflist
s.snapshot2 = NewSnapshotFromRepository("snap2", s.repo2) s.snapshot2, _ = NewSnapshotFromRepository("snap2", s.repo2)
} }
func (s *SnapshotCollectionSuite) TearDownTest(c *C) { func (s *SnapshotCollectionSuite) TearDownTest(c *C) {