From 8dc6a14766c1bdf8c9efe5e5910055e1c42e8cc1 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Wed, 26 Apr 2017 00:33:09 +0300 Subject: [PATCH] Allow snapshot to be created from empty local repo Fixes #288 --- deb/snapshot.go | 14 ++++++++------ deb/snapshot_test.go | 12 +++++++++--- system/t05_snapshot/CreateSnapshot9Test_gold | 4 +++- system/t05_snapshot/create.py | 1 - system/t12_api/publish.py | 3 --- system/t12_api/snapshots.py | 6 +++++- 6 files changed, 25 insertions(+), 15 deletions(-) diff --git a/deb/snapshot.go b/deb/snapshot.go index c9bde8db..2162f73f 100644 --- a/deb/snapshot.go +++ b/deb/snapshot.go @@ -53,11 +53,7 @@ func NewSnapshotFromRepository(name string, repo *RemoteRepo) (*Snapshot, error) // NewSnapshotFromLocalRepo creates snapshot from current state of local repository func NewSnapshotFromLocalRepo(name string, repo *LocalRepo) (*Snapshot, error) { - if repo.packageRefs == nil { - return nil, errors.New("local repo doesn't have packages") - } - - return &Snapshot{ + snap := &Snapshot{ UUID: uuid.New(), Name: name, CreatedAt: time.Now(), @@ -65,7 +61,13 @@ func NewSnapshotFromLocalRepo(name string, repo *LocalRepo) (*Snapshot, error) { SourceIDs: []string{repo.UUID}, Description: fmt.Sprintf("Snapshot from local repo %s", repo), packageRefs: repo.packageRefs, - }, nil + } + + if snap.packageRefs == nil { + snap.packageRefs = NewPackageRefList() + } + + return snap, nil } // NewSnapshotFromPackageList creates snapshot from PackageList diff --git a/deb/snapshot_test.go b/deb/snapshot_test.go index aac5cb6c..7571fd54 100644 --- a/deb/snapshot_test.go +++ b/deb/snapshot_test.go @@ -37,11 +37,17 @@ func (s *SnapshotSuite) TestNewSnapshotFromRepository(c *C) { func (s *SnapshotSuite) TestNewSnapshotFromLocalRepo(c *C) { localRepo := NewLocalRepo("lala", "hoorah!") - _, err := NewSnapshotFromLocalRepo("snap2", localRepo) - c.Check(err, ErrorMatches, "local repo doesn't have packages") + snapshot, err := NewSnapshotFromLocalRepo("snap2", localRepo) + c.Assert(err, IsNil) + c.Check(snapshot.Name, Equals, "snap2") + c.Check(snapshot.NumPackages(), Equals, 0) + c.Check(snapshot.RefList().Len(), Equals, 0) + c.Check(snapshot.SourceKind, Equals, "local") + c.Check(snapshot.SourceIDs, DeepEquals, []string{localRepo.UUID}) localRepo.UpdateRefList(s.reflist) - snapshot, _ := NewSnapshotFromLocalRepo("snap1", localRepo) + snapshot, err = NewSnapshotFromLocalRepo("snap1", localRepo) + c.Assert(err, IsNil) c.Check(snapshot.Name, Equals, "snap1") c.Check(snapshot.NumPackages(), Equals, 3) c.Check(snapshot.RefList().Len(), Equals, 3) diff --git a/system/t05_snapshot/CreateSnapshot9Test_gold b/system/t05_snapshot/CreateSnapshot9Test_gold index 4ff183dd..50c34a68 100644 --- a/system/t05_snapshot/CreateSnapshot9Test_gold +++ b/system/t05_snapshot/CreateSnapshot9Test_gold @@ -1 +1,3 @@ -ERROR: unable to create snapshot: local repo doesn't have packages + +Snapshot snap9 successfully created. +You can run 'aptly publish snapshot snap9' to publish snapshot as Debian repository. diff --git a/system/t05_snapshot/create.py b/system/t05_snapshot/create.py index ec641613..6f2b9322 100644 --- a/system/t05_snapshot/create.py +++ b/system/t05_snapshot/create.py @@ -106,4 +106,3 @@ class CreateSnapshot9Test(BaseTest): "aptly repo create local-repo", ] runCmd = "aptly snapshot create snap9 from repo local-repo" - expectedCode = 1 diff --git a/system/t12_api/publish.py b/system/t12_api/publish.py index cc178232..1e07be4b 100644 --- a/system/t12_api/publish.py +++ b/system/t12_api/publish.py @@ -105,9 +105,6 @@ class PublishSnapshotAPITest(APITest): snapshot_name = self.random_name() self.check_equal(self.post("/api/repos", json={"Name": repo_name}).status_code, 201) - resp = self.post("/api/repos/" + repo_name + '/snapshots', json={'Name': snapshot_name}) - self.check_equal(resp.status_code, 400) - d = self.random_name() self.check_equal(self.upload("/api/files/" + d, "libboost-program-options-dev_1.49.0.1_i386.deb").status_code, 200) diff --git a/system/t12_api/snapshots.py b/system/t12_api/snapshots.py index 994ef604..6d093038 100644 --- a/system/t12_api/snapshots.py +++ b/system/t12_api/snapshots.py @@ -97,8 +97,12 @@ class SnapshotsAPITestCreateFromRepo(APITest): self.check_equal(self.post("/api/repos", json={"Name": repo_name}).status_code, 201) resp = self.post("/api/repos/" + repo_name + '/snapshots', json={'Name': snapshot_name}) - self.check_equal(resp.status_code, 400) + self.check_equal(resp.status_code, 201) + self.check_equal([], + self.get("/api/snapshots/" + snapshot_name + "/packages", params={"format": "details"}).json()) + + snapshot_name = self.random_name() d = self.random_name() self.check_equal(self.upload("/api/files/" + d, "libboost-program-options-dev_1.49.0.1_i386.deb").status_code, 200)