2018-07-21 18:56:24 +00:00
|
|
|
package restorer
|
2017-11-25 19:36:32 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/restic/restic/internal/fs"
|
|
|
|
"github.com/restic/restic/internal/repository"
|
|
|
|
"github.com/restic/restic/internal/restic"
|
|
|
|
rtest "github.com/restic/restic/internal/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Node interface{}
|
|
|
|
|
|
|
|
type Snapshot struct {
|
2020-03-06 22:48:22 +00:00
|
|
|
Nodes map[string]Node
|
2017-11-25 19:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type File struct {
|
2018-09-27 12:59:33 +00:00
|
|
|
Data string
|
|
|
|
Links uint64
|
|
|
|
Inode uint64
|
2017-11-25 19:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Dir struct {
|
|
|
|
Nodes map[string]Node
|
2018-01-07 13:50:47 +00:00
|
|
|
Mode os.FileMode
|
2017-11-25 19:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func saveFile(t testing.TB, repo restic.Repository, node File) restic.ID {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2020-06-06 20:20:44 +00:00
|
|
|
id, _, err := repo.SaveBlob(ctx, restic.DataBlob, []byte(node.Data), restic.ID{}, false)
|
2017-11-25 19:36:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
2018-04-08 00:32:19 +00:00
|
|
|
func saveDir(t testing.TB, repo restic.Repository, nodes map[string]Node, inode uint64) restic.ID {
|
2017-11-25 19:36:32 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
tree := &restic.Tree{}
|
|
|
|
for name, n := range nodes {
|
2018-04-08 00:32:19 +00:00
|
|
|
inode++
|
2017-11-25 19:36:32 +00:00
|
|
|
switch node := n.(type) {
|
|
|
|
case File:
|
2018-09-27 12:59:33 +00:00
|
|
|
fi := n.(File).Inode
|
|
|
|
if fi == 0 {
|
|
|
|
fi = inode
|
|
|
|
}
|
|
|
|
lc := n.(File).Links
|
|
|
|
if lc == 0 {
|
|
|
|
lc = 1
|
|
|
|
}
|
|
|
|
fc := []restic.ID{}
|
|
|
|
if len(n.(File).Data) > 0 {
|
|
|
|
fc = append(fc, saveFile(t, repo, node))
|
|
|
|
}
|
2017-11-25 19:36:32 +00:00
|
|
|
tree.Insert(&restic.Node{
|
|
|
|
Type: "file",
|
|
|
|
Mode: 0644,
|
|
|
|
Name: name,
|
|
|
|
UID: uint32(os.Getuid()),
|
|
|
|
GID: uint32(os.Getgid()),
|
2018-09-27 12:59:33 +00:00
|
|
|
Content: fc,
|
2018-04-08 00:32:19 +00:00
|
|
|
Size: uint64(len(n.(File).Data)),
|
2018-09-27 12:59:33 +00:00
|
|
|
Inode: fi,
|
|
|
|
Links: lc,
|
2017-11-25 19:36:32 +00:00
|
|
|
})
|
|
|
|
case Dir:
|
2018-09-27 12:59:33 +00:00
|
|
|
id := saveDir(t, repo, node.Nodes, inode)
|
2018-01-07 13:50:47 +00:00
|
|
|
|
|
|
|
mode := node.Mode
|
|
|
|
if mode == 0 {
|
|
|
|
mode = 0755
|
|
|
|
}
|
|
|
|
|
2017-11-25 19:36:32 +00:00
|
|
|
tree.Insert(&restic.Node{
|
|
|
|
Type: "dir",
|
2018-01-07 13:50:47 +00:00
|
|
|
Mode: mode,
|
2017-11-25 19:36:32 +00:00
|
|
|
Name: name,
|
|
|
|
UID: uint32(os.Getuid()),
|
|
|
|
GID: uint32(os.Getgid()),
|
|
|
|
Subtree: &id,
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
t.Fatalf("unknown node type %T", node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := repo.SaveTree(ctx, tree)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
2018-07-21 18:56:38 +00:00
|
|
|
func saveSnapshot(t testing.TB, repo restic.Repository, snapshot Snapshot) (*restic.Snapshot, restic.ID) {
|
2017-11-25 19:36:32 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2018-09-27 12:59:33 +00:00
|
|
|
treeID := saveDir(t, repo, snapshot.Nodes, 1000)
|
2017-11-25 19:36:32 +00:00
|
|
|
|
2017-12-03 12:32:50 +00:00
|
|
|
err := repo.Flush(ctx)
|
2017-11-25 19:36:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sn, err := restic.NewSnapshot([]string{"test"}, nil, "", time.Now())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sn.Tree = &treeID
|
|
|
|
id, err := repo.SaveJSONUnpacked(ctx, restic.SnapshotFile, sn)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-07-21 18:56:38 +00:00
|
|
|
return sn, id
|
2017-11-25 19:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// toSlash converts the OS specific path dir to a slash-separated path.
|
|
|
|
func toSlash(dir string) string {
|
|
|
|
data := strings.Split(dir, string(filepath.Separator))
|
|
|
|
return strings.Join(data, "/")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRestorer(t *testing.T) {
|
|
|
|
var tests = []struct {
|
|
|
|
Snapshot
|
|
|
|
Files map[string]string
|
2018-09-15 00:55:30 +00:00
|
|
|
ErrorsMust map[string]map[string]struct{}
|
|
|
|
ErrorsMay map[string]map[string]struct{}
|
2018-07-21 12:06:19 +00:00
|
|
|
Select func(item string, dstpath string, node *restic.Node) (selectForRestore bool, childMayBeSelected bool)
|
2017-11-25 19:36:32 +00:00
|
|
|
}{
|
|
|
|
// valid test cases
|
|
|
|
{
|
|
|
|
Snapshot: Snapshot{
|
|
|
|
Nodes: map[string]Node{
|
2018-09-27 12:59:33 +00:00
|
|
|
"foo": File{Data: "content: foo\n"},
|
2017-11-25 19:36:32 +00:00
|
|
|
"dirtest": Dir{
|
|
|
|
Nodes: map[string]Node{
|
2018-09-27 12:59:33 +00:00
|
|
|
"file": File{Data: "content: file\n"},
|
2017-11-25 19:36:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Files: map[string]string{
|
|
|
|
"foo": "content: foo\n",
|
|
|
|
"dirtest/file": "content: file\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Snapshot: Snapshot{
|
|
|
|
Nodes: map[string]Node{
|
2018-09-27 12:59:33 +00:00
|
|
|
"top": File{Data: "toplevel file"},
|
2017-11-25 19:36:32 +00:00
|
|
|
"dir": Dir{
|
|
|
|
Nodes: map[string]Node{
|
2018-09-27 12:59:33 +00:00
|
|
|
"file": File{Data: "file in dir"},
|
2017-11-25 19:36:32 +00:00
|
|
|
"subdir": Dir{
|
|
|
|
Nodes: map[string]Node{
|
2018-09-27 12:59:33 +00:00
|
|
|
"file": File{Data: "file in subdir"},
|
2017-11-25 19:36:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Files: map[string]string{
|
|
|
|
"top": "toplevel file",
|
|
|
|
"dir/file": "file in dir",
|
|
|
|
"dir/subdir/file": "file in subdir",
|
|
|
|
},
|
|
|
|
},
|
2018-01-07 13:50:47 +00:00
|
|
|
{
|
|
|
|
Snapshot: Snapshot{
|
|
|
|
Nodes: map[string]Node{
|
|
|
|
"dir": Dir{
|
|
|
|
Mode: 0444,
|
2018-01-07 14:13:24 +00:00
|
|
|
},
|
2018-09-27 12:59:33 +00:00
|
|
|
"file": File{Data: "top-level file"},
|
2018-01-07 14:13:24 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Files: map[string]string{
|
|
|
|
"file": "top-level file",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Snapshot: Snapshot{
|
|
|
|
Nodes: map[string]Node{
|
|
|
|
"dir": Dir{
|
|
|
|
Mode: 0555,
|
2018-01-07 13:50:47 +00:00
|
|
|
Nodes: map[string]Node{
|
2018-09-27 12:59:33 +00:00
|
|
|
"file": File{Data: "file in dir"},
|
2018-01-07 13:50:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Files: map[string]string{
|
|
|
|
"dir/file": "file in dir",
|
|
|
|
},
|
|
|
|
},
|
2018-07-14 19:19:42 +00:00
|
|
|
{
|
|
|
|
Snapshot: Snapshot{
|
|
|
|
Nodes: map[string]Node{
|
2018-09-27 12:59:33 +00:00
|
|
|
"topfile": File{Data: "top-level file"},
|
2018-07-14 19:19:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Files: map[string]string{
|
|
|
|
"topfile": "top-level file",
|
|
|
|
},
|
|
|
|
},
|
2018-07-21 12:06:19 +00:00
|
|
|
{
|
|
|
|
Snapshot: Snapshot{
|
|
|
|
Nodes: map[string]Node{
|
|
|
|
"dir": Dir{
|
|
|
|
Nodes: map[string]Node{
|
2018-09-27 12:59:33 +00:00
|
|
|
"file": File{Data: "content: file\n"},
|
2018-07-21 12:06:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Files: map[string]string{
|
|
|
|
"dir/file": "content: file\n",
|
|
|
|
},
|
|
|
|
Select: func(item, dstpath string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool) {
|
|
|
|
switch item {
|
|
|
|
case filepath.FromSlash("/dir"):
|
|
|
|
childMayBeSelected = true
|
|
|
|
case filepath.FromSlash("/dir/file"):
|
|
|
|
selectedForRestore = true
|
|
|
|
childMayBeSelected = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return selectedForRestore, childMayBeSelected
|
|
|
|
},
|
|
|
|
},
|
2017-11-25 19:36:32 +00:00
|
|
|
|
|
|
|
// test cases with invalid/constructed names
|
|
|
|
{
|
|
|
|
Snapshot: Snapshot{
|
|
|
|
Nodes: map[string]Node{
|
2018-09-27 12:59:33 +00:00
|
|
|
`..\test`: File{Data: "foo\n"},
|
|
|
|
`..\..\foo\..\bar\..\xx\test2`: File{Data: "test2\n"},
|
2017-11-25 19:36:32 +00:00
|
|
|
},
|
|
|
|
},
|
2018-09-15 00:55:30 +00:00
|
|
|
ErrorsMay: map[string]map[string]struct{}{
|
|
|
|
`/`: {
|
|
|
|
`invalid child node name ..\test`: struct{}{},
|
|
|
|
`invalid child node name ..\..\foo\..\bar\..\xx\test2`: struct{}{},
|
|
|
|
},
|
2017-11-25 19:36:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Snapshot: Snapshot{
|
|
|
|
Nodes: map[string]Node{
|
2018-09-27 12:59:33 +00:00
|
|
|
`../test`: File{Data: "foo\n"},
|
|
|
|
`../../foo/../bar/../xx/test2`: File{Data: "test2\n"},
|
2017-11-25 19:36:32 +00:00
|
|
|
},
|
|
|
|
},
|
2018-09-15 00:55:30 +00:00
|
|
|
ErrorsMay: map[string]map[string]struct{}{
|
|
|
|
`/`: {
|
|
|
|
`invalid child node name ../test`: struct{}{},
|
|
|
|
`invalid child node name ../../foo/../bar/../xx/test2`: struct{}{},
|
|
|
|
},
|
2017-11-25 19:36:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Snapshot: Snapshot{
|
|
|
|
Nodes: map[string]Node{
|
2018-09-27 12:59:33 +00:00
|
|
|
"top": File{Data: "toplevel file"},
|
2017-11-25 19:36:32 +00:00
|
|
|
"x": Dir{
|
|
|
|
Nodes: map[string]Node{
|
2018-09-27 12:59:33 +00:00
|
|
|
"file1": File{Data: "file1"},
|
2017-11-25 19:36:32 +00:00
|
|
|
"..": Dir{
|
|
|
|
Nodes: map[string]Node{
|
2018-09-27 12:59:33 +00:00
|
|
|
"file2": File{Data: "file2"},
|
2017-11-25 19:36:32 +00:00
|
|
|
"..": Dir{
|
|
|
|
Nodes: map[string]Node{
|
2018-09-27 12:59:33 +00:00
|
|
|
"file2": File{Data: "file2"},
|
2017-11-25 19:36:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Files: map[string]string{
|
|
|
|
"top": "toplevel file",
|
|
|
|
},
|
2018-09-15 00:55:30 +00:00
|
|
|
ErrorsMust: map[string]map[string]struct{}{
|
|
|
|
`/x`: {
|
|
|
|
`invalid child node name ..`: struct{}{},
|
|
|
|
},
|
2017-11-25 19:36:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
repo, cleanup := repository.TestRepository(t)
|
|
|
|
defer cleanup()
|
|
|
|
_, id := saveSnapshot(t, repo, test.Snapshot)
|
|
|
|
t.Logf("snapshot saved as %v", id.Str())
|
|
|
|
|
2018-07-21 18:56:24 +00:00
|
|
|
res, err := NewRestorer(repo, id)
|
2017-11-25 19:36:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tempdir, cleanup := rtest.TempDir(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
2018-07-14 19:19:42 +00:00
|
|
|
// make sure we're creating a new subdir of the tempdir
|
|
|
|
tempdir = filepath.Join(tempdir, "target")
|
|
|
|
|
2017-11-25 19:36:32 +00:00
|
|
|
res.SelectFilter = func(item, dstpath string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool) {
|
|
|
|
t.Logf("restore %v to %v", item, dstpath)
|
|
|
|
if !fs.HasPathPrefix(tempdir, dstpath) {
|
|
|
|
t.Errorf("would restore %v to %v, which is not within the target dir %v",
|
|
|
|
item, dstpath, tempdir)
|
|
|
|
return false, false
|
|
|
|
}
|
2018-07-21 12:06:19 +00:00
|
|
|
|
|
|
|
if test.Select != nil {
|
|
|
|
return test.Select(item, dstpath, node)
|
|
|
|
}
|
|
|
|
|
2017-11-25 19:36:32 +00:00
|
|
|
return true, true
|
|
|
|
}
|
|
|
|
|
2018-09-15 00:55:30 +00:00
|
|
|
errors := make(map[string]map[string]struct{})
|
|
|
|
res.Error = func(location string, err error) error {
|
|
|
|
location = toSlash(location)
|
|
|
|
t.Logf("restore returned error for %q: %v", location, err)
|
|
|
|
if errors[location] == nil {
|
|
|
|
errors[location] = make(map[string]struct{})
|
|
|
|
}
|
|
|
|
errors[location][err.Error()] = struct{}{}
|
2017-11-25 19:36:32 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2018-05-11 04:45:14 +00:00
|
|
|
err = res.RestoreTo(ctx, tempdir)
|
2017-11-25 19:36:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-09-15 00:55:30 +00:00
|
|
|
for location, expectedErrors := range test.ErrorsMust {
|
|
|
|
actualErrors, ok := errors[location]
|
2017-11-25 19:36:32 +00:00
|
|
|
if !ok {
|
2018-09-15 00:55:30 +00:00
|
|
|
t.Errorf("expected error(s) for %v, found none", location)
|
2017-11-25 19:36:32 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-09-15 00:55:30 +00:00
|
|
|
rtest.Equals(t, expectedErrors, actualErrors)
|
2017-11-25 19:36:32 +00:00
|
|
|
|
2018-09-15 00:55:30 +00:00
|
|
|
delete(errors, location)
|
2017-11-25 19:36:32 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 00:55:30 +00:00
|
|
|
for location, expectedErrors := range test.ErrorsMay {
|
|
|
|
actualErrors, ok := errors[location]
|
2017-11-25 19:36:32 +00:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-09-15 00:55:30 +00:00
|
|
|
rtest.Equals(t, expectedErrors, actualErrors)
|
2017-11-25 19:36:32 +00:00
|
|
|
|
2018-09-15 00:55:30 +00:00
|
|
|
delete(errors, location)
|
2017-11-25 19:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for filename, err := range errors {
|
|
|
|
t.Errorf("unexpected error for %v found: %v", filename, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for filename, content := range test.Files {
|
|
|
|
data, err := ioutil.ReadFile(filepath.Join(tempdir, filepath.FromSlash(filename)))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to read file %v: %v", filename, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(data, []byte(content)) {
|
|
|
|
t.Errorf("file %v has wrong content: want %q, got %q", filename, content, data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-11-26 17:36:48 +00:00
|
|
|
|
|
|
|
func TestRestorerRelative(t *testing.T) {
|
|
|
|
var tests = []struct {
|
|
|
|
Snapshot
|
|
|
|
Files map[string]string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Snapshot: Snapshot{
|
|
|
|
Nodes: map[string]Node{
|
2018-09-27 12:59:33 +00:00
|
|
|
"foo": File{Data: "content: foo\n"},
|
2017-11-26 17:36:48 +00:00
|
|
|
"dirtest": Dir{
|
|
|
|
Nodes: map[string]Node{
|
2018-09-27 12:59:33 +00:00
|
|
|
"file": File{Data: "content: file\n"},
|
2017-11-26 17:36:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Files: map[string]string{
|
|
|
|
"foo": "content: foo\n",
|
|
|
|
"dirtest/file": "content: file\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
repo, cleanup := repository.TestRepository(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
_, id := saveSnapshot(t, repo, test.Snapshot)
|
|
|
|
t.Logf("snapshot saved as %v", id.Str())
|
|
|
|
|
2018-07-21 18:56:24 +00:00
|
|
|
res, err := NewRestorer(repo, id)
|
2017-11-26 17:36:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tempdir, cleanup := rtest.TempDir(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
2020-02-17 12:24:09 +00:00
|
|
|
cleanup = rtest.Chdir(t, tempdir)
|
2017-11-26 17:36:48 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
errors := make(map[string]string)
|
2018-09-15 00:55:30 +00:00
|
|
|
res.Error = func(location string, err error) error {
|
|
|
|
t.Logf("restore returned error for %q: %v", location, err)
|
|
|
|
errors[location] = err.Error()
|
2017-11-26 17:36:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2018-05-11 04:45:14 +00:00
|
|
|
err = res.RestoreTo(ctx, "restore")
|
2017-11-26 17:36:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for filename, err := range errors {
|
|
|
|
t.Errorf("unexpected error for %v found: %v", filename, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for filename, content := range test.Files {
|
|
|
|
data, err := ioutil.ReadFile(filepath.Join(tempdir, "restore", filepath.FromSlash(filename)))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to read file %v: %v", filename, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(data, []byte(content)) {
|
|
|
|
t.Errorf("file %v has wrong content: want %q, got %q", filename, content, data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-07-21 19:55:57 +00:00
|
|
|
|
|
|
|
type TraverseTreeCheck func(testing.TB) treeVisitor
|
|
|
|
|
|
|
|
type TreeVisit struct {
|
|
|
|
funcName string // name of the function
|
|
|
|
location string // location passed to the function
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkVisitOrder(list []TreeVisit) TraverseTreeCheck {
|
|
|
|
var pos int
|
|
|
|
|
|
|
|
return func(t testing.TB) treeVisitor {
|
|
|
|
check := func(funcName string) func(*restic.Node, string, string) error {
|
|
|
|
return func(node *restic.Node, target, location string) error {
|
|
|
|
if pos >= len(list) {
|
|
|
|
t.Errorf("step %v, %v(%v): expected no more than %d function calls", pos, funcName, location, len(list))
|
|
|
|
pos++
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
v := list[pos]
|
|
|
|
|
|
|
|
if v.funcName != funcName {
|
|
|
|
t.Errorf("step %v, location %v: want function %v, but %v was called",
|
|
|
|
pos, location, v.funcName, funcName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if location != filepath.FromSlash(v.location) {
|
|
|
|
t.Errorf("step %v: want location %v, got %v", pos, list[pos].location, location)
|
|
|
|
}
|
|
|
|
|
|
|
|
pos++
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return treeVisitor{
|
|
|
|
enterDir: check("enterDir"),
|
|
|
|
visitNode: check("visitNode"),
|
|
|
|
leaveDir: check("leaveDir"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRestorerTraverseTree(t *testing.T) {
|
|
|
|
var tests = []struct {
|
|
|
|
Snapshot
|
|
|
|
Select func(item string, dstpath string, node *restic.Node) (selectForRestore bool, childMayBeSelected bool)
|
|
|
|
Visitor TraverseTreeCheck
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// select everything
|
|
|
|
Snapshot: Snapshot{
|
|
|
|
Nodes: map[string]Node{
|
|
|
|
"dir": Dir{Nodes: map[string]Node{
|
2018-09-27 12:59:33 +00:00
|
|
|
"otherfile": File{Data: "x"},
|
2018-07-21 19:55:57 +00:00
|
|
|
"subdir": Dir{Nodes: map[string]Node{
|
2018-09-27 12:59:33 +00:00
|
|
|
"file": File{Data: "content: file\n"},
|
2018-07-21 19:55:57 +00:00
|
|
|
}},
|
|
|
|
}},
|
2018-09-27 12:59:33 +00:00
|
|
|
"foo": File{Data: "content: foo\n"},
|
2018-07-21 19:55:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Select: func(item string, dstpath string, node *restic.Node) (selectForRestore bool, childMayBeSelected bool) {
|
|
|
|
return true, true
|
|
|
|
},
|
|
|
|
Visitor: checkVisitOrder([]TreeVisit{
|
|
|
|
{"enterDir", "/dir"},
|
|
|
|
{"visitNode", "/dir/otherfile"},
|
|
|
|
{"enterDir", "/dir/subdir"},
|
|
|
|
{"visitNode", "/dir/subdir/file"},
|
|
|
|
{"leaveDir", "/dir/subdir"},
|
|
|
|
{"leaveDir", "/dir"},
|
|
|
|
{"visitNode", "/foo"},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
|
|
|
|
// select only the top-level file
|
|
|
|
{
|
|
|
|
Snapshot: Snapshot{
|
|
|
|
Nodes: map[string]Node{
|
|
|
|
"dir": Dir{Nodes: map[string]Node{
|
2018-09-27 12:59:33 +00:00
|
|
|
"otherfile": File{Data: "x"},
|
2018-07-21 19:55:57 +00:00
|
|
|
"subdir": Dir{Nodes: map[string]Node{
|
2018-09-27 12:59:33 +00:00
|
|
|
"file": File{Data: "content: file\n"},
|
2018-07-21 19:55:57 +00:00
|
|
|
}},
|
|
|
|
}},
|
2018-09-27 12:59:33 +00:00
|
|
|
"foo": File{Data: "content: foo\n"},
|
2018-07-21 19:55:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Select: func(item string, dstpath string, node *restic.Node) (selectForRestore bool, childMayBeSelected bool) {
|
|
|
|
if item == "/foo" {
|
|
|
|
return true, false
|
|
|
|
}
|
|
|
|
return false, false
|
|
|
|
},
|
|
|
|
Visitor: checkVisitOrder([]TreeVisit{
|
|
|
|
{"visitNode", "/foo"},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Snapshot: Snapshot{
|
|
|
|
Nodes: map[string]Node{
|
2018-09-27 12:59:33 +00:00
|
|
|
"aaa": File{Data: "content: foo\n"},
|
2018-07-21 19:55:57 +00:00
|
|
|
"dir": Dir{Nodes: map[string]Node{
|
2018-09-27 12:59:33 +00:00
|
|
|
"otherfile": File{Data: "x"},
|
2018-07-21 19:55:57 +00:00
|
|
|
"subdir": Dir{Nodes: map[string]Node{
|
2018-09-27 12:59:33 +00:00
|
|
|
"file": File{Data: "content: file\n"},
|
2018-07-21 19:55:57 +00:00
|
|
|
}},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Select: func(item string, dstpath string, node *restic.Node) (selectForRestore bool, childMayBeSelected bool) {
|
|
|
|
if item == "/aaa" {
|
|
|
|
return true, false
|
|
|
|
}
|
|
|
|
return false, false
|
|
|
|
},
|
|
|
|
Visitor: checkVisitOrder([]TreeVisit{
|
|
|
|
{"visitNode", "/aaa"},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
|
|
|
|
// select dir/
|
|
|
|
{
|
|
|
|
Snapshot: Snapshot{
|
|
|
|
Nodes: map[string]Node{
|
|
|
|
"dir": Dir{Nodes: map[string]Node{
|
2018-09-27 12:59:33 +00:00
|
|
|
"otherfile": File{Data: "x"},
|
2018-07-21 19:55:57 +00:00
|
|
|
"subdir": Dir{Nodes: map[string]Node{
|
2018-09-27 12:59:33 +00:00
|
|
|
"file": File{Data: "content: file\n"},
|
2018-07-21 19:55:57 +00:00
|
|
|
}},
|
|
|
|
}},
|
2018-09-27 12:59:33 +00:00
|
|
|
"foo": File{Data: "content: foo\n"},
|
2018-07-21 19:55:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Select: func(item string, dstpath string, node *restic.Node) (selectForRestore bool, childMayBeSelected bool) {
|
|
|
|
if strings.HasPrefix(item, "/dir") {
|
|
|
|
return true, true
|
|
|
|
}
|
|
|
|
return false, false
|
|
|
|
},
|
|
|
|
Visitor: checkVisitOrder([]TreeVisit{
|
|
|
|
{"enterDir", "/dir"},
|
|
|
|
{"visitNode", "/dir/otherfile"},
|
|
|
|
{"enterDir", "/dir/subdir"},
|
|
|
|
{"visitNode", "/dir/subdir/file"},
|
|
|
|
{"leaveDir", "/dir/subdir"},
|
|
|
|
{"leaveDir", "/dir"},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
|
|
|
|
// select only dir/otherfile
|
|
|
|
{
|
|
|
|
Snapshot: Snapshot{
|
|
|
|
Nodes: map[string]Node{
|
|
|
|
"dir": Dir{Nodes: map[string]Node{
|
2018-09-27 12:59:33 +00:00
|
|
|
"otherfile": File{Data: "x"},
|
2018-07-21 19:55:57 +00:00
|
|
|
"subdir": Dir{Nodes: map[string]Node{
|
2018-09-27 12:59:33 +00:00
|
|
|
"file": File{Data: "content: file\n"},
|
2018-07-21 19:55:57 +00:00
|
|
|
}},
|
|
|
|
}},
|
2018-09-27 12:59:33 +00:00
|
|
|
"foo": File{Data: "content: foo\n"},
|
2018-07-21 19:55:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Select: func(item string, dstpath string, node *restic.Node) (selectForRestore bool, childMayBeSelected bool) {
|
|
|
|
switch item {
|
|
|
|
case "/dir":
|
|
|
|
return false, true
|
|
|
|
case "/dir/otherfile":
|
|
|
|
return true, false
|
|
|
|
default:
|
|
|
|
return false, false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Visitor: checkVisitOrder([]TreeVisit{
|
|
|
|
{"visitNode", "/dir/otherfile"},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
repo, cleanup := repository.TestRepository(t)
|
|
|
|
defer cleanup()
|
|
|
|
sn, id := saveSnapshot(t, repo, test.Snapshot)
|
|
|
|
|
|
|
|
res, err := NewRestorer(repo, id)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
res.SelectFilter = test.Select
|
|
|
|
|
|
|
|
tempdir, cleanup := rtest.TempDir(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// make sure we're creating a new subdir of the tempdir
|
|
|
|
target := filepath.Join(tempdir, "target")
|
|
|
|
|
|
|
|
err = res.traverseTree(ctx, target, string(filepath.Separator), *sn.Tree, test.Visitor(t))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|