backup: add --dry-run/-n flag to show what would happen.
This can be used to check how large a backup is or validate exclusions.
It does not actually write any data to the underlying backend. This is
implemented as a simple overlay backend that accepts writes without
forwarding them, passes through reads, and generally does the minimal
necessary to pretend that progress is actually happening.
Fixes #1542
Example usage:
$ restic -vv --dry-run . | grep add
new /changelog/unreleased/issue-1542, saved in 0.000s (350 B added)
modified /cmd/restic/cmd_backup.go, saved in 0.000s (16.543 KiB added)
modified /cmd/restic/global.go, saved in 0.000s (0 B added)
new /internal/backend/dry/dry_backend_test.go, saved in 0.000s (3.866 KiB added)
new /internal/backend/dry/dry_backend.go, saved in 0.000s (3.744 KiB added)
modified /internal/backend/test/tests.go, saved in 0.000s (0 B added)
modified /internal/repository/repository.go, saved in 0.000s (20.707 KiB added)
modified /internal/ui/backup.go, saved in 0.000s (9.110 KiB added)
modified /internal/ui/jsonstatus/status.go, saved in 0.001s (11.055 KiB added)
modified /restic, saved in 0.131s (25.542 MiB added)
Would add to the repo: 25.892 MiB
2019-06-13 03:39:13 +00:00
|
|
|
package dryrun_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/restic/restic/internal/restic"
|
|
|
|
|
|
|
|
"github.com/restic/restic/internal/backend/dryrun"
|
|
|
|
"github.com/restic/restic/internal/backend/mem"
|
|
|
|
)
|
|
|
|
|
|
|
|
// make sure that Backend implements backend.Backend
|
|
|
|
var _ restic.Backend = &dryrun.Backend{}
|
|
|
|
|
|
|
|
func newBackends() (*dryrun.Backend, restic.Backend) {
|
|
|
|
m := mem.New()
|
|
|
|
return dryrun.New(m), m
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDry(t *testing.T) {
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
2021-02-23 15:09:29 +00:00
|
|
|
d, m := newBackends()
|
backup: add --dry-run/-n flag to show what would happen.
This can be used to check how large a backup is or validate exclusions.
It does not actually write any data to the underlying backend. This is
implemented as a simple overlay backend that accepts writes without
forwarding them, passes through reads, and generally does the minimal
necessary to pretend that progress is actually happening.
Fixes #1542
Example usage:
$ restic -vv --dry-run . | grep add
new /changelog/unreleased/issue-1542, saved in 0.000s (350 B added)
modified /cmd/restic/cmd_backup.go, saved in 0.000s (16.543 KiB added)
modified /cmd/restic/global.go, saved in 0.000s (0 B added)
new /internal/backend/dry/dry_backend_test.go, saved in 0.000s (3.866 KiB added)
new /internal/backend/dry/dry_backend.go, saved in 0.000s (3.744 KiB added)
modified /internal/backend/test/tests.go, saved in 0.000s (0 B added)
modified /internal/repository/repository.go, saved in 0.000s (20.707 KiB added)
modified /internal/ui/backup.go, saved in 0.000s (9.110 KiB added)
modified /internal/ui/jsonstatus/status.go, saved in 0.001s (11.055 KiB added)
modified /restic, saved in 0.131s (25.542 MiB added)
Would add to the repo: 25.892 MiB
2019-06-13 03:39:13 +00:00
|
|
|
// Since the dry backend is a mostly write-only overlay, the standard backend test suite
|
|
|
|
// won't pass. Instead, perform a series of operations over the backend, testing the state
|
|
|
|
// at each step.
|
|
|
|
steps := []struct {
|
|
|
|
be restic.Backend
|
|
|
|
op string
|
|
|
|
fname string
|
|
|
|
content string
|
|
|
|
wantErr string
|
|
|
|
}{
|
|
|
|
{d, "loc", "", "DRY:RAM", ""},
|
2021-02-23 15:09:29 +00:00
|
|
|
{d, "delete", "", "", ""},
|
backup: add --dry-run/-n flag to show what would happen.
This can be used to check how large a backup is or validate exclusions.
It does not actually write any data to the underlying backend. This is
implemented as a simple overlay backend that accepts writes without
forwarding them, passes through reads, and generally does the minimal
necessary to pretend that progress is actually happening.
Fixes #1542
Example usage:
$ restic -vv --dry-run . | grep add
new /changelog/unreleased/issue-1542, saved in 0.000s (350 B added)
modified /cmd/restic/cmd_backup.go, saved in 0.000s (16.543 KiB added)
modified /cmd/restic/global.go, saved in 0.000s (0 B added)
new /internal/backend/dry/dry_backend_test.go, saved in 0.000s (3.866 KiB added)
new /internal/backend/dry/dry_backend.go, saved in 0.000s (3.744 KiB added)
modified /internal/backend/test/tests.go, saved in 0.000s (0 B added)
modified /internal/repository/repository.go, saved in 0.000s (20.707 KiB added)
modified /internal/ui/backup.go, saved in 0.000s (9.110 KiB added)
modified /internal/ui/jsonstatus/status.go, saved in 0.001s (11.055 KiB added)
modified /restic, saved in 0.131s (25.542 MiB added)
Would add to the repo: 25.892 MiB
2019-06-13 03:39:13 +00:00
|
|
|
{d, "stat", "a", "", "not found"},
|
|
|
|
{d, "list", "", "", ""},
|
|
|
|
{d, "save", "", "", "invalid"},
|
|
|
|
{d, "test", "a", "", ""},
|
2021-02-23 15:09:29 +00:00
|
|
|
{m, "save", "a", "baz", ""}, // save a directly to the mem backend
|
|
|
|
{d, "save", "b", "foob", ""}, // b is not saved
|
|
|
|
{d, "save", "b", "xxx", ""}, // no error as b is not saved
|
backup: add --dry-run/-n flag to show what would happen.
This can be used to check how large a backup is or validate exclusions.
It does not actually write any data to the underlying backend. This is
implemented as a simple overlay backend that accepts writes without
forwarding them, passes through reads, and generally does the minimal
necessary to pretend that progress is actually happening.
Fixes #1542
Example usage:
$ restic -vv --dry-run . | grep add
new /changelog/unreleased/issue-1542, saved in 0.000s (350 B added)
modified /cmd/restic/cmd_backup.go, saved in 0.000s (16.543 KiB added)
modified /cmd/restic/global.go, saved in 0.000s (0 B added)
new /internal/backend/dry/dry_backend_test.go, saved in 0.000s (3.866 KiB added)
new /internal/backend/dry/dry_backend.go, saved in 0.000s (3.744 KiB added)
modified /internal/backend/test/tests.go, saved in 0.000s (0 B added)
modified /internal/repository/repository.go, saved in 0.000s (20.707 KiB added)
modified /internal/ui/backup.go, saved in 0.000s (9.110 KiB added)
modified /internal/ui/jsonstatus/status.go, saved in 0.001s (11.055 KiB added)
modified /restic, saved in 0.131s (25.542 MiB added)
Would add to the repo: 25.892 MiB
2019-06-13 03:39:13 +00:00
|
|
|
{d, "test", "a", "1", ""},
|
2021-02-23 15:09:29 +00:00
|
|
|
{d, "test", "b", "", ""},
|
backup: add --dry-run/-n flag to show what would happen.
This can be used to check how large a backup is or validate exclusions.
It does not actually write any data to the underlying backend. This is
implemented as a simple overlay backend that accepts writes without
forwarding them, passes through reads, and generally does the minimal
necessary to pretend that progress is actually happening.
Fixes #1542
Example usage:
$ restic -vv --dry-run . | grep add
new /changelog/unreleased/issue-1542, saved in 0.000s (350 B added)
modified /cmd/restic/cmd_backup.go, saved in 0.000s (16.543 KiB added)
modified /cmd/restic/global.go, saved in 0.000s (0 B added)
new /internal/backend/dry/dry_backend_test.go, saved in 0.000s (3.866 KiB added)
new /internal/backend/dry/dry_backend.go, saved in 0.000s (3.744 KiB added)
modified /internal/backend/test/tests.go, saved in 0.000s (0 B added)
modified /internal/repository/repository.go, saved in 0.000s (20.707 KiB added)
modified /internal/ui/backup.go, saved in 0.000s (9.110 KiB added)
modified /internal/ui/jsonstatus/status.go, saved in 0.001s (11.055 KiB added)
modified /restic, saved in 0.131s (25.542 MiB added)
Would add to the repo: 25.892 MiB
2019-06-13 03:39:13 +00:00
|
|
|
{d, "stat", "", "", "invalid"},
|
|
|
|
{d, "stat", "a", "a 3", ""},
|
|
|
|
{d, "load", "a", "baz", ""},
|
2021-02-23 15:09:29 +00:00
|
|
|
{d, "load", "b", "", "not found"},
|
|
|
|
{d, "list", "", "a", ""},
|
|
|
|
{d, "remove", "c", "", ""},
|
backup: add --dry-run/-n flag to show what would happen.
This can be used to check how large a backup is or validate exclusions.
It does not actually write any data to the underlying backend. This is
implemented as a simple overlay backend that accepts writes without
forwarding them, passes through reads, and generally does the minimal
necessary to pretend that progress is actually happening.
Fixes #1542
Example usage:
$ restic -vv --dry-run . | grep add
new /changelog/unreleased/issue-1542, saved in 0.000s (350 B added)
modified /cmd/restic/cmd_backup.go, saved in 0.000s (16.543 KiB added)
modified /cmd/restic/global.go, saved in 0.000s (0 B added)
new /internal/backend/dry/dry_backend_test.go, saved in 0.000s (3.866 KiB added)
new /internal/backend/dry/dry_backend.go, saved in 0.000s (3.744 KiB added)
modified /internal/backend/test/tests.go, saved in 0.000s (0 B added)
modified /internal/repository/repository.go, saved in 0.000s (20.707 KiB added)
modified /internal/ui/backup.go, saved in 0.000s (9.110 KiB added)
modified /internal/ui/jsonstatus/status.go, saved in 0.001s (11.055 KiB added)
modified /restic, saved in 0.131s (25.542 MiB added)
Would add to the repo: 25.892 MiB
2019-06-13 03:39:13 +00:00
|
|
|
{d, "stat", "b", "", "not found"},
|
|
|
|
{d, "list", "", "a", ""},
|
2021-02-23 15:09:29 +00:00
|
|
|
{d, "remove", "a", "", ""}, // a is in fact not removed
|
|
|
|
{d, "list", "", "a", ""},
|
|
|
|
{m, "remove", "a", "", ""}, // remove a from the mem backend
|
|
|
|
{d, "list", "", "", ""},
|
backup: add --dry-run/-n flag to show what would happen.
This can be used to check how large a backup is or validate exclusions.
It does not actually write any data to the underlying backend. This is
implemented as a simple overlay backend that accepts writes without
forwarding them, passes through reads, and generally does the minimal
necessary to pretend that progress is actually happening.
Fixes #1542
Example usage:
$ restic -vv --dry-run . | grep add
new /changelog/unreleased/issue-1542, saved in 0.000s (350 B added)
modified /cmd/restic/cmd_backup.go, saved in 0.000s (16.543 KiB added)
modified /cmd/restic/global.go, saved in 0.000s (0 B added)
new /internal/backend/dry/dry_backend_test.go, saved in 0.000s (3.866 KiB added)
new /internal/backend/dry/dry_backend.go, saved in 0.000s (3.744 KiB added)
modified /internal/backend/test/tests.go, saved in 0.000s (0 B added)
modified /internal/repository/repository.go, saved in 0.000s (20.707 KiB added)
modified /internal/ui/backup.go, saved in 0.000s (9.110 KiB added)
modified /internal/ui/jsonstatus/status.go, saved in 0.001s (11.055 KiB added)
modified /restic, saved in 0.131s (25.542 MiB added)
Would add to the repo: 25.892 MiB
2019-06-13 03:39:13 +00:00
|
|
|
{d, "close", "", "", ""},
|
|
|
|
{d, "close", "", "", ""},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, step := range steps {
|
|
|
|
var err error
|
|
|
|
var boolRes bool
|
|
|
|
|
2021-02-23 15:09:29 +00:00
|
|
|
handle := restic.Handle{Type: restic.PackFile, Name: step.fname}
|
backup: add --dry-run/-n flag to show what would happen.
This can be used to check how large a backup is or validate exclusions.
It does not actually write any data to the underlying backend. This is
implemented as a simple overlay backend that accepts writes without
forwarding them, passes through reads, and generally does the minimal
necessary to pretend that progress is actually happening.
Fixes #1542
Example usage:
$ restic -vv --dry-run . | grep add
new /changelog/unreleased/issue-1542, saved in 0.000s (350 B added)
modified /cmd/restic/cmd_backup.go, saved in 0.000s (16.543 KiB added)
modified /cmd/restic/global.go, saved in 0.000s (0 B added)
new /internal/backend/dry/dry_backend_test.go, saved in 0.000s (3.866 KiB added)
new /internal/backend/dry/dry_backend.go, saved in 0.000s (3.744 KiB added)
modified /internal/backend/test/tests.go, saved in 0.000s (0 B added)
modified /internal/repository/repository.go, saved in 0.000s (20.707 KiB added)
modified /internal/ui/backup.go, saved in 0.000s (9.110 KiB added)
modified /internal/ui/jsonstatus/status.go, saved in 0.001s (11.055 KiB added)
modified /restic, saved in 0.131s (25.542 MiB added)
Would add to the repo: 25.892 MiB
2019-06-13 03:39:13 +00:00
|
|
|
switch step.op {
|
|
|
|
case "save":
|
|
|
|
err = step.be.Save(ctx, handle, restic.NewByteReader([]byte(step.content)))
|
|
|
|
case "test":
|
|
|
|
boolRes, err = step.be.Test(ctx, handle)
|
|
|
|
if boolRes != (step.content != "") {
|
|
|
|
t.Errorf("%d. Test(%q) = %v, want %v", i, step.fname, boolRes, step.content != "")
|
|
|
|
}
|
|
|
|
case "list":
|
|
|
|
fileList := []string{}
|
2021-02-23 15:09:29 +00:00
|
|
|
err = step.be.List(ctx, restic.PackFile, func(fi restic.FileInfo) error {
|
backup: add --dry-run/-n flag to show what would happen.
This can be used to check how large a backup is or validate exclusions.
It does not actually write any data to the underlying backend. This is
implemented as a simple overlay backend that accepts writes without
forwarding them, passes through reads, and generally does the minimal
necessary to pretend that progress is actually happening.
Fixes #1542
Example usage:
$ restic -vv --dry-run . | grep add
new /changelog/unreleased/issue-1542, saved in 0.000s (350 B added)
modified /cmd/restic/cmd_backup.go, saved in 0.000s (16.543 KiB added)
modified /cmd/restic/global.go, saved in 0.000s (0 B added)
new /internal/backend/dry/dry_backend_test.go, saved in 0.000s (3.866 KiB added)
new /internal/backend/dry/dry_backend.go, saved in 0.000s (3.744 KiB added)
modified /internal/backend/test/tests.go, saved in 0.000s (0 B added)
modified /internal/repository/repository.go, saved in 0.000s (20.707 KiB added)
modified /internal/ui/backup.go, saved in 0.000s (9.110 KiB added)
modified /internal/ui/jsonstatus/status.go, saved in 0.001s (11.055 KiB added)
modified /restic, saved in 0.131s (25.542 MiB added)
Would add to the repo: 25.892 MiB
2019-06-13 03:39:13 +00:00
|
|
|
fileList = append(fileList, fi.Name)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
sort.Strings(fileList)
|
|
|
|
files := strings.Join(fileList, " ")
|
|
|
|
if files != step.content {
|
|
|
|
t.Errorf("%d. List = %q, want %q", i, files, step.content)
|
|
|
|
}
|
|
|
|
case "loc":
|
|
|
|
loc := step.be.Location()
|
|
|
|
if loc != step.content {
|
|
|
|
t.Errorf("%d. Location = %q, want %q", i, loc, step.content)
|
|
|
|
}
|
|
|
|
case "delete":
|
|
|
|
err = step.be.Delete(ctx)
|
|
|
|
case "remove":
|
|
|
|
err = step.be.Remove(ctx, handle)
|
|
|
|
case "stat":
|
|
|
|
var fi restic.FileInfo
|
|
|
|
fi, err = step.be.Stat(ctx, handle)
|
|
|
|
if err == nil {
|
|
|
|
fis := fmt.Sprintf("%s %d", fi.Name, fi.Size)
|
|
|
|
if fis != step.content {
|
|
|
|
t.Errorf("%d. Stat = %q, want %q", i, fis, step.content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "load":
|
|
|
|
data := ""
|
|
|
|
err = step.be.Load(ctx, handle, 100, 0, func(rd io.Reader) error {
|
|
|
|
buf, err := ioutil.ReadAll(rd)
|
|
|
|
data = string(buf)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if data != step.content {
|
|
|
|
t.Errorf("%d. Load = %q, want %q", i, data, step.content)
|
|
|
|
}
|
|
|
|
case "close":
|
|
|
|
err = step.be.Close()
|
|
|
|
default:
|
|
|
|
t.Fatalf("%d. unknown step operation %q", i, step.op)
|
|
|
|
}
|
|
|
|
if step.wantErr != "" {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("%d. %s error = nil, want %q", i, step.op, step.wantErr)
|
|
|
|
} else if !strings.Contains(err.Error(), step.wantErr) {
|
|
|
|
t.Errorf("%d. %s error = %q, doesn't contain %q", i, step.op, err, step.wantErr)
|
|
|
|
} else if step.wantErr == "not found" && !step.be.IsNotExist(err) {
|
|
|
|
t.Errorf("%d. IsNotExist(%s error) = false, want true", i, step.op)
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if err != nil {
|
|
|
|
t.Errorf("%d. %s error = %q, want nil", i, step.op, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|