2022-10-28 15:44:34 +00:00
|
|
|
package restore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-07-27 23:06:26 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2022-10-28 15:44:34 +00:00
|
|
|
"github.com/restic/restic/internal/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
type printerTraceEntry struct {
|
2024-05-31 11:43:57 +00:00
|
|
|
progress State
|
2022-10-28 15:44:34 +00:00
|
|
|
|
|
|
|
duration time.Duration
|
|
|
|
isFinished bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type printerTrace []printerTraceEntry
|
|
|
|
|
2024-05-31 18:38:51 +00:00
|
|
|
type itemTraceEntry struct {
|
|
|
|
action ItemAction
|
|
|
|
item string
|
|
|
|
size uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
type itemTrace []itemTraceEntry
|
2024-07-27 23:06:26 +00:00
|
|
|
|
|
|
|
type errorTraceEntry struct {
|
|
|
|
item string
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
type errorTrace []errorTraceEntry
|
|
|
|
|
2022-10-28 15:44:34 +00:00
|
|
|
type mockPrinter struct {
|
2024-07-27 23:06:26 +00:00
|
|
|
trace printerTrace
|
|
|
|
items itemTrace
|
|
|
|
errors errorTrace
|
2022-10-28 15:44:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const mockFinishDuration = 42 * time.Second
|
|
|
|
|
2024-05-31 11:43:57 +00:00
|
|
|
func (p *mockPrinter) Update(progress State, duration time.Duration) {
|
|
|
|
p.trace = append(p.trace, printerTraceEntry{progress, duration, false})
|
2022-10-28 15:44:34 +00:00
|
|
|
}
|
2024-07-27 23:06:26 +00:00
|
|
|
func (p *mockPrinter) Error(item string, err error) error {
|
|
|
|
p.errors = append(p.errors, errorTraceEntry{item, err})
|
|
|
|
return nil
|
|
|
|
}
|
2024-05-31 18:38:51 +00:00
|
|
|
func (p *mockPrinter) CompleteItem(action ItemAction, item string, size uint64) {
|
|
|
|
p.items = append(p.items, itemTraceEntry{action, item, size})
|
|
|
|
}
|
2024-05-31 11:43:57 +00:00
|
|
|
func (p *mockPrinter) Finish(progress State, _ time.Duration) {
|
|
|
|
p.trace = append(p.trace, printerTraceEntry{progress, mockFinishDuration, true})
|
2022-10-28 15:44:34 +00:00
|
|
|
}
|
|
|
|
|
2024-07-27 23:06:26 +00:00
|
|
|
func testProgress(fn func(progress *Progress) bool) (printerTrace, itemTrace, errorTrace) {
|
2022-10-28 15:44:34 +00:00
|
|
|
printer := &mockPrinter{}
|
|
|
|
progress := NewProgress(printer, 0)
|
|
|
|
final := fn(progress)
|
|
|
|
progress.update(0, final)
|
|
|
|
trace := append(printerTrace{}, printer.trace...)
|
2024-05-31 18:38:51 +00:00
|
|
|
items := append(itemTrace{}, printer.items...)
|
2024-07-27 23:06:26 +00:00
|
|
|
errors := append(errorTrace{}, printer.errors...)
|
2022-10-28 15:44:34 +00:00
|
|
|
// cleanup to avoid goroutine leak, but copy trace first
|
|
|
|
progress.Finish()
|
2024-07-27 23:06:26 +00:00
|
|
|
return trace, items, errors
|
2022-10-28 15:44:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNew(t *testing.T) {
|
2024-07-27 23:06:26 +00:00
|
|
|
result, items, _ := testProgress(func(progress *Progress) bool {
|
2022-10-28 15:44:34 +00:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
test.Equals(t, printerTrace{
|
2024-12-01 14:29:11 +00:00
|
|
|
printerTraceEntry{State{0, 0, 0, 0, 0, 0, 0}, 0, false},
|
2022-10-28 15:44:34 +00:00
|
|
|
}, result)
|
2024-05-31 18:38:51 +00:00
|
|
|
test.Equals(t, itemTrace{}, items)
|
2022-10-28 15:44:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddFile(t *testing.T) {
|
|
|
|
fileSize := uint64(100)
|
|
|
|
|
2024-07-27 23:06:26 +00:00
|
|
|
result, items, _ := testProgress(func(progress *Progress) bool {
|
2022-10-28 15:44:34 +00:00
|
|
|
progress.AddFile(fileSize)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
test.Equals(t, printerTrace{
|
2024-12-01 14:29:11 +00:00
|
|
|
printerTraceEntry{State{0, 1, 0, 0, 0, fileSize, 0}, 0, false},
|
2022-10-28 15:44:34 +00:00
|
|
|
}, result)
|
2024-05-31 18:38:51 +00:00
|
|
|
test.Equals(t, itemTrace{}, items)
|
2022-10-28 15:44:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFirstProgressOnAFile(t *testing.T) {
|
|
|
|
expectedBytesWritten := uint64(5)
|
|
|
|
expectedBytesTotal := uint64(100)
|
|
|
|
|
2024-07-27 23:06:26 +00:00
|
|
|
result, items, _ := testProgress(func(progress *Progress) bool {
|
2022-10-28 15:44:34 +00:00
|
|
|
progress.AddFile(expectedBytesTotal)
|
2024-06-29 19:24:45 +00:00
|
|
|
progress.AddProgress("test", ActionFileUpdated, expectedBytesWritten, expectedBytesTotal)
|
2022-10-28 15:44:34 +00:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
test.Equals(t, printerTrace{
|
2024-12-01 14:29:11 +00:00
|
|
|
printerTraceEntry{State{0, 1, 0, 0, expectedBytesWritten, expectedBytesTotal, 0}, 0, false},
|
2022-10-28 15:44:34 +00:00
|
|
|
}, result)
|
2024-05-31 18:38:51 +00:00
|
|
|
test.Equals(t, itemTrace{}, items)
|
2022-10-28 15:44:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLastProgressOnAFile(t *testing.T) {
|
|
|
|
fileSize := uint64(100)
|
|
|
|
|
2024-07-27 23:06:26 +00:00
|
|
|
result, items, _ := testProgress(func(progress *Progress) bool {
|
2022-10-28 15:44:34 +00:00
|
|
|
progress.AddFile(fileSize)
|
2024-06-29 19:24:45 +00:00
|
|
|
progress.AddProgress("test", ActionFileUpdated, 30, fileSize)
|
|
|
|
progress.AddProgress("test", ActionFileUpdated, 35, fileSize)
|
|
|
|
progress.AddProgress("test", ActionFileUpdated, 35, fileSize)
|
2022-10-28 15:44:34 +00:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
test.Equals(t, printerTrace{
|
2024-12-01 14:29:11 +00:00
|
|
|
printerTraceEntry{State{1, 1, 0, 0, fileSize, fileSize, 0}, 0, false},
|
2022-10-28 15:44:34 +00:00
|
|
|
}, result)
|
2024-05-31 18:38:51 +00:00
|
|
|
test.Equals(t, itemTrace{
|
|
|
|
itemTraceEntry{action: ActionFileUpdated, item: "test", size: fileSize},
|
|
|
|
}, items)
|
2022-10-28 15:44:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLastProgressOnLastFile(t *testing.T) {
|
|
|
|
fileSize := uint64(100)
|
|
|
|
|
2024-07-27 23:06:26 +00:00
|
|
|
result, items, _ := testProgress(func(progress *Progress) bool {
|
2022-10-28 15:44:34 +00:00
|
|
|
progress.AddFile(fileSize)
|
|
|
|
progress.AddFile(50)
|
2024-06-29 19:24:45 +00:00
|
|
|
progress.AddProgress("test1", ActionFileUpdated, 50, 50)
|
|
|
|
progress.AddProgress("test2", ActionFileUpdated, 50, fileSize)
|
|
|
|
progress.AddProgress("test2", ActionFileUpdated, 50, fileSize)
|
2022-10-28 15:44:34 +00:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
test.Equals(t, printerTrace{
|
2024-12-01 14:29:11 +00:00
|
|
|
printerTraceEntry{State{2, 2, 0, 0, 50 + fileSize, 50 + fileSize, 0}, 0, false},
|
2022-10-28 15:44:34 +00:00
|
|
|
}, result)
|
2024-05-31 18:38:51 +00:00
|
|
|
test.Equals(t, itemTrace{
|
|
|
|
itemTraceEntry{action: ActionFileUpdated, item: "test1", size: 50},
|
|
|
|
itemTraceEntry{action: ActionFileUpdated, item: "test2", size: fileSize},
|
|
|
|
}, items)
|
2022-10-28 15:44:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSummaryOnSuccess(t *testing.T) {
|
|
|
|
fileSize := uint64(100)
|
|
|
|
|
2024-07-27 23:06:26 +00:00
|
|
|
result, _, _ := testProgress(func(progress *Progress) bool {
|
2022-10-28 15:44:34 +00:00
|
|
|
progress.AddFile(fileSize)
|
|
|
|
progress.AddFile(50)
|
2024-06-29 19:24:45 +00:00
|
|
|
progress.AddProgress("test1", ActionFileUpdated, 50, 50)
|
|
|
|
progress.AddProgress("test2", ActionFileUpdated, fileSize, fileSize)
|
2022-10-28 15:44:34 +00:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
test.Equals(t, printerTrace{
|
2024-12-01 14:29:11 +00:00
|
|
|
printerTraceEntry{State{2, 2, 0, 0, 50 + fileSize, 50 + fileSize, 0}, mockFinishDuration, true},
|
2022-10-28 15:44:34 +00:00
|
|
|
}, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSummaryOnErrors(t *testing.T) {
|
|
|
|
fileSize := uint64(100)
|
|
|
|
|
2024-07-27 23:06:26 +00:00
|
|
|
result, _, _ := testProgress(func(progress *Progress) bool {
|
2022-10-28 15:44:34 +00:00
|
|
|
progress.AddFile(fileSize)
|
|
|
|
progress.AddFile(50)
|
2024-06-29 19:24:45 +00:00
|
|
|
progress.AddProgress("test1", ActionFileUpdated, 50, 50)
|
|
|
|
progress.AddProgress("test2", ActionFileUpdated, fileSize/2, fileSize)
|
2022-10-28 15:44:34 +00:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
test.Equals(t, printerTrace{
|
2024-12-01 14:29:11 +00:00
|
|
|
printerTraceEntry{State{1, 2, 0, 0, 50 + fileSize/2, 50 + fileSize, 0}, mockFinishDuration, true},
|
2024-05-31 12:12:06 +00:00
|
|
|
}, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSkipFile(t *testing.T) {
|
|
|
|
fileSize := uint64(100)
|
|
|
|
|
2024-07-27 23:06:26 +00:00
|
|
|
result, items, _ := testProgress(func(progress *Progress) bool {
|
2024-05-31 18:38:51 +00:00
|
|
|
progress.AddSkippedFile("test", fileSize)
|
2024-05-31 12:12:06 +00:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
test.Equals(t, printerTrace{
|
2024-12-01 14:29:11 +00:00
|
|
|
printerTraceEntry{State{0, 0, 1, 0, 0, 0, fileSize}, mockFinishDuration, true},
|
2022-10-28 15:44:34 +00:00
|
|
|
}, result)
|
2024-05-31 18:38:51 +00:00
|
|
|
test.Equals(t, itemTrace{
|
|
|
|
itemTraceEntry{ActionFileUnchanged, "test", fileSize},
|
|
|
|
}, items)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProgressTypes(t *testing.T) {
|
|
|
|
fileSize := uint64(100)
|
|
|
|
|
2024-07-27 23:06:26 +00:00
|
|
|
_, items, _ := testProgress(func(progress *Progress) bool {
|
2024-05-31 18:38:51 +00:00
|
|
|
progress.AddFile(fileSize)
|
|
|
|
progress.AddFile(0)
|
2024-06-29 19:24:45 +00:00
|
|
|
progress.AddProgress("dir", ActionDirRestored, fileSize, fileSize)
|
|
|
|
progress.AddProgress("new", ActionFileRestored, 0, 0)
|
2024-12-01 14:29:11 +00:00
|
|
|
progress.ReportDeletion("del")
|
2024-05-31 18:38:51 +00:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
test.Equals(t, itemTrace{
|
|
|
|
itemTraceEntry{ActionDirRestored, "dir", fileSize},
|
|
|
|
itemTraceEntry{ActionFileRestored, "new", 0},
|
2024-06-29 19:29:42 +00:00
|
|
|
itemTraceEntry{ActionDeleted, "del", 0},
|
2024-05-31 18:38:51 +00:00
|
|
|
}, items)
|
2022-10-28 15:44:34 +00:00
|
|
|
}
|
2024-07-27 23:06:26 +00:00
|
|
|
|
|
|
|
func TestProgressError(t *testing.T) {
|
|
|
|
err1 := errors.New("err1")
|
|
|
|
err2 := errors.New("err2")
|
|
|
|
_, _, errors := testProgress(func(progress *Progress) bool {
|
|
|
|
test.Equals(t, progress.Error("first", err1), nil)
|
|
|
|
test.Equals(t, progress.Error("second", err2), nil)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
test.Equals(t, errorTrace{
|
|
|
|
errorTraceEntry{"first", err1},
|
|
|
|
errorTraceEntry{"second", err2},
|
|
|
|
}, errors)
|
|
|
|
}
|