From baad39b57983722f9ddadb3d8ac5eefc720650e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Sto=C5=82owski?= Date: Fri, 2 Oct 2020 10:40:20 +0200 Subject: Add export-import roundtrip test. --- overlord/snapshotstate/backend/backend.go | 3 +- overlord/snapshotstate/backend/backend_test.go | 46 ++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/overlord/snapshotstate/backend/backend.go b/overlord/snapshotstate/backend/backend.go index 17c0c0ea18..6043df8f13 100644 --- a/overlord/snapshotstate/backend/backend.go +++ b/overlord/snapshotstate/backend/backend.go @@ -447,7 +447,7 @@ func addDirToZip(ctx context.Context, snapshot *client.Snapshot, w *zip.Writer, } // Import a snapshot from the export file format -func Import(ctx context.Context, id uint64, r io.Reader) (int64, []string, error) { +func Import(ctx context.Context, id uint64, r io.Reader) (size int64, snapNames []string, err error) { comment := fmt.Sprintf("snapshot %d", id) // prepare cache location to unpack the import file @@ -479,7 +479,6 @@ func Import(ctx context.Context, id uint64, r io.Reader) (int64, []string, error } defer dir.Close() - var snapNames []string var readErr error for readErr == nil { var names []string diff --git a/overlord/snapshotstate/backend/backend_test.go b/overlord/snapshotstate/backend/backend_test.go index 7f1d65e8ed..65cb00a65e 100644 --- a/overlord/snapshotstate/backend/backend_test.go +++ b/overlord/snapshotstate/backend/backend_test.go @@ -1047,6 +1047,52 @@ func (s *snapshotSuite) TestImportCheckErorr(c *check.C) { c.Assert(err, check.ErrorMatches, `validation failed for "5_foo_1.0_199.zip": zip: not a valid zip file`) } +func (s *snapshotSuite) TestImportExportRoundtrip(c *check.C) { + err := os.MkdirAll(dirs.SnapshotsDir, 0755) + c.Assert(err, check.IsNil) + + ctx := context.TODO() + + epoch := snap.E("42*") + info := &snap.Info{SideInfo: snap.SideInfo{RealName: "hello-snap", Revision: snap.R(42), SnapID: "hello-id"}, Version: "v1.33", Epoch: epoch} + cfg := map[string]interface{}{"some-setting": false} + shID := uint64(12) + + shw, err := backend.Save(ctx, shID, info, cfg, []string{"snapuser"}, &backend.Flags{}) + c.Assert(err, check.IsNil) + c.Check(shw.SetID, check.Equals, shID) + + c.Check(backend.Filename(shw), check.Equals, filepath.Join(dirs.SnapshotsDir, "12_hello-snap_v1.33_42.zip")) + c.Check(hashkeys(shw), check.DeepEquals, []string{"archive.tgz", "user/snapuser.tgz"}) + + export, err := backend.NewSnapshotExport(ctx, shw.SetID) + c.Assert(err, check.IsNil) + c.Assert(export.Init(), check.IsNil) + + buf := bytes.NewBuffer(nil) + c.Assert(export.StreamTo(buf), check.IsNil) + c.Check(buf.Len(), check.Equals, int(export.Size())) + + // now import it + c.Assert(os.Remove(filepath.Join(dirs.SnapshotsDir, "12_hello-snap_v1.33_42.zip")), check.IsNil) + + _, names, err := backend.Import(ctx, 123, buf) + c.Assert(err, check.IsNil) + c.Check(names, check.DeepEquals, []string{"hello-snap"}) + + sets, err := backend.List(ctx, 0, nil) + c.Assert(err, check.IsNil) + c.Assert(sets, check.HasLen, 1) + c.Check(sets[0].ID, check.Equals, uint64(123)) + + rdr, err := backend.Open(filepath.Join(dirs.SnapshotsDir, "123_hello-snap_v1.33_42.zip"), backend.ExtractFnameSetID) + defer rdr.Close() + c.Check(err, check.IsNil) + c.Check(rdr.SetID, check.Equals, uint64(123)) + c.Check(rdr.Snap, check.Equals, "hello-snap") + c.Check(rdr.IsValid(), check.Equals, true) +} + func (s *snapshotSuite) TestEstimateSnapshotSize(c *check.C) { restore := backend.MockUsersForUsernames(func(usernames []string) ([]*user.User, error) { return []*user.User{{HomeDir: filepath.Join(s.root, "home/user1")}}, nil -- cgit v1.2.3