From 19ebc1b786bf66f9c7e1c6a1e76c7eb0f0eac21c Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Mon, 8 May 2023 20:49:41 +0200 Subject: [PATCH] restore: Add basic test for progress bar accounting of hardlinks --- internal/restorer/restorer_unix_test.go | 55 +++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/internal/restorer/restorer_unix_test.go b/internal/restorer/restorer_unix_test.go index e9c521e36..4c5f2a5b8 100644 --- a/internal/restorer/restorer_unix_test.go +++ b/internal/restorer/restorer_unix_test.go @@ -9,10 +9,12 @@ import ( "path/filepath" "syscall" "testing" + "time" "github.com/restic/restic/internal/repository" "github.com/restic/restic/internal/restic" rtest "github.com/restic/restic/internal/test" + restoreui "github.com/restic/restic/internal/ui/restore" ) func TestRestorerRestoreEmptyHardlinkedFileds(t *testing.T) { @@ -66,3 +68,56 @@ func getBlockCount(t *testing.T, filename string) int64 { } return st.Blocks } + +type printerMock struct { + filesFinished, filesTotal, allBytesWritten, allBytesTotal uint64 +} + +func (p *printerMock) Update(filesFinished, filesTotal, allBytesWritten, allBytesTotal uint64, duration time.Duration) { +} +func (p *printerMock) Finish(filesFinished, filesTotal, allBytesWritten, allBytesTotal uint64, duration time.Duration) { + p.filesFinished = filesFinished + p.filesTotal = filesTotal + p.allBytesWritten = allBytesWritten + p.allBytesTotal = allBytesTotal +} + +func TestRestorerProgressBar(t *testing.T) { + repo := repository.TestRepository(t) + + sn, _ := saveSnapshot(t, repo, Snapshot{ + Nodes: map[string]Node{ + "dirtest": Dir{ + Nodes: map[string]Node{ + "file1": File{Links: 2, Inode: 1, Data: "foo"}, + "file2": File{Links: 2, Inode: 1, Data: "foo"}, + }, + }, + "file2": File{Links: 1, Inode: 2, Data: "example"}, + }, + }) + + mock := &printerMock{} + progress := restoreui.NewProgress(mock, 0) + res := NewRestorer(context.TODO(), repo, sn, false, progress) + res.SelectFilter = func(item string, dstpath string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool) { + return true, true + } + + tempdir := rtest.TempDir(t) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + err := res.RestoreTo(ctx, tempdir) + rtest.OK(t, err) + progress.Finish() + + const filesFinished = 4 + const filesTotal = filesFinished + const allBytesWritten = 10 + const allBytesTotal = allBytesWritten + rtest.Assert(t, mock.filesFinished == filesFinished, "filesFinished: expected %v, got %v", filesFinished, mock.filesFinished) + rtest.Assert(t, mock.filesTotal == filesTotal, "filesTotal: expected %v, got %v", filesTotal, mock.filesTotal) + rtest.Assert(t, mock.allBytesWritten == allBytesWritten, "allBytesWritten: expected %v, got %v", allBytesWritten, mock.allBytesWritten) + rtest.Assert(t, mock.allBytesTotal == allBytesTotal, "allBytesTotal: expected %v, got %v", allBytesTotal, mock.allBytesTotal) +}