Files
restic/internal/dump/common_test.go
T

168 lines
4.5 KiB
Go

package dump
import (
"archive/tar"
"bytes"
"context"
"io"
"testing"
"github.com/restic/restic/internal/archiver"
"github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/data"
"github.com/restic/restic/internal/errors"
"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"
)
func prepareTempdirRepoSrc(t testing.TB, src archiver.TestDir) (string, restic.Repository, backend.Backend) {
tempdir := rtest.TempDir(t)
repo, _, be := repository.TestRepositoryWithVersion(t, 0)
archiver.TestCreateFiles(t, tempdir, src)
return tempdir, repo, be
}
type CheckDump func(t *testing.T, testDir string, testDump *bytes.Buffer) error
func WriteTest(t *testing.T, format string, cd CheckDump) {
tests := []struct {
name string
args archiver.TestDir
target string
}{
{
name: "single file in root",
args: archiver.TestDir{
"file": archiver.TestFile{Content: "string"},
},
target: "/",
},
{
name: "multiple files in root",
args: archiver.TestDir{
"file1": archiver.TestFile{Content: "string"},
"file2": archiver.TestFile{Content: "string"},
},
target: "/",
},
{
name: "multiple files and folders in root",
args: archiver.TestDir{
"file1": archiver.TestFile{Content: "string"},
"file2": archiver.TestFile{Content: "string"},
"firstDir": archiver.TestDir{
"another": archiver.TestFile{Content: "string"},
},
"secondDir": archiver.TestDir{
"another2": archiver.TestFile{Content: "string"},
},
},
target: "/",
},
{
name: "file and symlink in root",
args: archiver.TestDir{
"file1": archiver.TestFile{Content: "string"},
"file2": archiver.TestSymlink{Target: "file1"},
},
target: "/",
},
{
name: "directory only",
args: archiver.TestDir{
"firstDir": archiver.TestDir{
"secondDir": archiver.TestDir{},
},
},
target: "/",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := t.Context()
tmpdir, repo, be := prepareTempdirRepoSrc(t, tt.args)
arch := archiver.New(repo, fs.Track{FS: fs.NewLocal()}, archiver.Options{})
back := rtest.Chdir(t, tmpdir)
defer back()
sn, _, _, err := arch.Snapshot(ctx, []string{"."}, archiver.SnapshotOptions{})
rtest.OK(t, err)
tree, err := data.LoadTree(ctx, repo, *sn.Tree)
rtest.OK(t, err)
dst := &bytes.Buffer{}
d := New(format, repo, dst)
if err := d.DumpTree(ctx, tree, tt.target); err != nil {
t.Fatalf("Dumper.Run error = %v", err)
}
if err := cd(t, tmpdir, dst); err != nil {
t.Errorf("WriteDump() = does not match: %v", err)
}
// test that dump returns an error if the repository is broken
tree, err = data.LoadTree(ctx, repo, *sn.Tree)
rtest.OK(t, err)
rtest.OK(t, be.Delete(ctx))
// use new dumper as the old one has the blobs cached
d = New(format, repo, dst)
err = d.DumpTree(ctx, tree, tt.target)
rtest.Assert(t, err != nil, "expected error, got nil")
})
}
}
func TestDumpSkipsUnorderedNodes(t *testing.T) {
ctx := t.Context()
repo := repository.TestRepository(t)
var rootID restic.ID
rtest.OK(t, repo.WithBlobUploader(ctx, func(ctx context.Context, uploader restic.BlobSaverWithAsync) error {
file := func(name string) *data.Node {
return &data.Node{Name: name, Type: data.NodeTypeFile, Content: restic.IDs{}}
}
// "z" stays in order after the skipped file "a".
childID := data.TestSaveUnorderedTree(t, ctx, uploader, []*data.Node{file("m"), file("a"), file("z")})
skippedID := data.TestSaveUnorderedTree(t, ctx, uploader, []*data.Node{file("kept")})
rootID = data.TestSaveUnorderedTree(t, ctx, uploader, []*data.Node{
file("m"),
{Name: "b", Type: data.NodeTypeDir, Subtree: &skippedID},
{Name: "sub", Type: data.NodeTypeDir, Subtree: &childID},
file("a"),
file("z"),
})
return nil
}))
tree, err := data.LoadTree(ctx, repo, rootID)
rtest.OK(t, err)
var reported []string
dst := &bytes.Buffer{}
d := New("tar", repo, dst)
d.Error = func(nodePath string, err error) {
rtest.Assert(t, errors.Is(err, data.ErrTreeNotOrdered), "unexpected error %v", err)
reported = append(reported, nodePath)
}
rtest.OK(t, d.DumpTree(ctx, tree, "/"))
rtest.Equals(t, []string{"/b", "/sub/a", "/a"}, reported)
var names []string
tr := tar.NewReader(dst)
for {
hdr, err := tr.Next()
if errors.Is(err, io.EOF) {
break
}
rtest.OK(t, err)
names = append(names, hdr.Name)
}
rtest.Equals(t, []string{"m", "sub/", "sub/m", "sub/z", "z"}, names)
}