2014-12-05 20:45:49 +00:00
|
|
|
package restic_test
|
2014-09-23 20:39:12 +00:00
|
|
|
|
|
|
|
import (
|
2017-06-05 21:56:59 +00:00
|
|
|
"context"
|
2014-12-06 21:01:54 +00:00
|
|
|
"encoding/json"
|
2022-10-08 19:29:51 +00:00
|
|
|
"errors"
|
2014-09-23 20:39:12 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-09-26 15:18:42 +00:00
|
|
|
"strconv"
|
2014-09-23 20:39:12 +00:00
|
|
|
"testing"
|
2014-12-06 21:01:54 +00:00
|
|
|
|
2022-06-12 12:38:19 +00:00
|
|
|
"github.com/restic/restic/internal/archiver"
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/repository"
|
2017-07-24 15:42:25 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest "github.com/restic/restic/internal/test"
|
2021-08-07 20:52:05 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2014-09-23 20:39:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var testFiles = []struct {
|
|
|
|
name string
|
|
|
|
content []byte
|
|
|
|
}{
|
|
|
|
{"foo", []byte("bar")},
|
|
|
|
{"bar/foo2", []byte("bar2")},
|
|
|
|
{"bar/bla/blubb", []byte("This is just a test!\n")},
|
|
|
|
}
|
|
|
|
|
2015-04-30 00:59:06 +00:00
|
|
|
func createTempDir(t *testing.T) string {
|
2022-12-02 18:36:43 +00:00
|
|
|
tempdir, err := os.MkdirTemp(rtest.TestTempDir, "restic-test-")
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2014-09-23 20:39:12 +00:00
|
|
|
|
|
|
|
for _, test := range testFiles {
|
|
|
|
file := filepath.Join(tempdir, test.name)
|
|
|
|
dir := filepath.Dir(file)
|
|
|
|
if dir != "." {
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, os.MkdirAll(dir, 0755))
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Create(file)
|
|
|
|
defer func() {
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, f.Close())
|
2014-09-23 20:39:12 +00:00
|
|
|
}()
|
|
|
|
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2014-09-23 20:39:12 +00:00
|
|
|
|
|
|
|
_, err = f.Write(test.content)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return tempdir
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTree(t *testing.T) {
|
2015-04-30 00:59:06 +00:00
|
|
|
dir := createTempDir(t)
|
2014-09-23 20:39:12 +00:00
|
|
|
defer func() {
|
2017-10-02 13:06:39 +00:00
|
|
|
if rtest.TestCleanupTempDirs {
|
|
|
|
rtest.RemoveAll(t, dir)
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2014-12-06 21:01:54 +00:00
|
|
|
|
|
|
|
var testNodes = []restic.Node{
|
2017-05-16 23:28:39 +00:00
|
|
|
{Name: "normal"},
|
|
|
|
{Name: "with backslashes \\zzz"},
|
|
|
|
{Name: "test utf-8 föbärß"},
|
|
|
|
{Name: "test invalid \x00\x01\x02\x03\x04"},
|
|
|
|
{Name: "test latin1 \x75\x6d\x6c\xe4\xfc\x74\xf6\x6e\xdf\x6e\x6c\x6c"},
|
2014-12-06 21:01:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeMarshal(t *testing.T) {
|
|
|
|
for i, n := range testNodes {
|
|
|
|
data, err := json.Marshal(&n)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2014-12-06 21:01:54 +00:00
|
|
|
|
|
|
|
var node restic.Node
|
|
|
|
err = json.Unmarshal(data, &node)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2014-12-06 21:01:54 +00:00
|
|
|
|
|
|
|
if n.Name != node.Name {
|
|
|
|
t.Fatalf("Node %d: Names are not equal, want: %q got: %q", i, n.Name, node.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-10 22:40:10 +00:00
|
|
|
|
|
|
|
func TestNodeComparison(t *testing.T) {
|
|
|
|
fi, err := os.Lstat("tree_test.go")
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2015-01-10 22:40:10 +00:00
|
|
|
|
2017-02-02 11:23:13 +00:00
|
|
|
node, err := restic.NodeFromFileInfo("tree_test.go", fi)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2015-01-10 22:40:10 +00:00
|
|
|
|
|
|
|
n2 := *node
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.Assert(t, node.Equals(n2), "nodes aren't equal")
|
2015-01-10 22:40:10 +00:00
|
|
|
|
2015-04-30 00:59:06 +00:00
|
|
|
n2.Size--
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.Assert(t, !node.Equals(n2), "nodes are equal")
|
2015-01-10 22:40:10 +00:00
|
|
|
}
|
2015-04-26 15:44:38 +00:00
|
|
|
|
2022-06-12 12:38:19 +00:00
|
|
|
func TestEmptyLoadTree(t *testing.T) {
|
2022-12-11 09:41:22 +00:00
|
|
|
repo := repository.TestRepository(t)
|
2015-04-26 15:44:38 +00:00
|
|
|
|
2021-08-07 20:52:05 +00:00
|
|
|
var wg errgroup.Group
|
|
|
|
repo.StartPackUploader(context.TODO(), &wg)
|
2015-04-26 15:44:38 +00:00
|
|
|
// save tree
|
2021-09-26 15:18:42 +00:00
|
|
|
tree := restic.NewTree(0)
|
2022-06-12 12:38:19 +00:00
|
|
|
id, err := restic.SaveTree(context.TODO(), repo, tree)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2015-04-26 15:44:38 +00:00
|
|
|
|
|
|
|
// save packs
|
2017-11-22 11:27:29 +00:00
|
|
|
rtest.OK(t, repo.Flush(context.Background()))
|
2015-04-26 15:44:38 +00:00
|
|
|
|
|
|
|
// load tree again
|
2022-06-12 12:38:19 +00:00
|
|
|
tree2, err := restic.LoadTree(context.TODO(), repo, id)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2015-04-26 15:44:38 +00:00
|
|
|
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.Assert(t, tree.Equals(tree2),
|
2015-04-26 15:44:38 +00:00
|
|
|
"trees are not equal: want %v, got %v",
|
|
|
|
tree, tree2)
|
|
|
|
}
|
2021-09-26 15:18:42 +00:00
|
|
|
|
2022-05-21 11:33:08 +00:00
|
|
|
func TestTreeEqualSerialization(t *testing.T) {
|
|
|
|
files := []string{"node.go", "tree.go", "tree_test.go"}
|
|
|
|
for i := 1; i <= len(files); i++ {
|
|
|
|
tree := restic.NewTree(i)
|
|
|
|
builder := restic.NewTreeJSONBuilder()
|
|
|
|
|
|
|
|
for _, fn := range files[:i] {
|
|
|
|
fi, err := os.Lstat(fn)
|
|
|
|
rtest.OK(t, err)
|
|
|
|
node, err := restic.NodeFromFileInfo(fn, fi)
|
|
|
|
rtest.OK(t, err)
|
|
|
|
|
|
|
|
rtest.OK(t, tree.Insert(node))
|
|
|
|
rtest.OK(t, builder.AddNode(node))
|
|
|
|
|
|
|
|
rtest.Assert(t, tree.Insert(node) != nil, "no error on duplicate node")
|
|
|
|
rtest.Assert(t, builder.AddNode(node) != nil, "no error on duplicate node")
|
2022-10-08 19:29:51 +00:00
|
|
|
rtest.Assert(t, errors.Is(builder.AddNode(node), restic.ErrTreeNotOrdered), "wrong error returned")
|
2022-05-21 11:33:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
treeBytes, err := json.Marshal(tree)
|
|
|
|
treeBytes = append(treeBytes, '\n')
|
|
|
|
rtest.OK(t, err)
|
|
|
|
|
|
|
|
stiBytes, err := builder.Finalize()
|
|
|
|
rtest.OK(t, err)
|
|
|
|
|
|
|
|
// compare serialization of an individual node and the SaveTreeIterator
|
|
|
|
rtest.Equals(t, treeBytes, stiBytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-26 15:18:42 +00:00
|
|
|
func BenchmarkBuildTree(b *testing.B) {
|
|
|
|
const size = 100 // Directories of this size are not uncommon.
|
|
|
|
|
|
|
|
nodes := make([]restic.Node, size)
|
|
|
|
for i := range nodes {
|
|
|
|
// Archiver.SaveTree inputs in sorted order, so do that here too.
|
|
|
|
nodes[i].Name = strconv.Itoa(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
t := restic.NewTree(size)
|
|
|
|
|
|
|
|
for i := range nodes {
|
|
|
|
_ = t.Insert(&nodes[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-12 12:38:19 +00:00
|
|
|
|
|
|
|
func TestLoadTree(t *testing.T) {
|
|
|
|
repository.TestAllVersions(t, testLoadTree)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testLoadTree(t *testing.T, version uint) {
|
|
|
|
if rtest.BenchArchiveDirectory == "" {
|
|
|
|
t.Skip("benchdir not set, skipping")
|
|
|
|
}
|
|
|
|
|
|
|
|
// archive a few files
|
2022-12-11 09:41:22 +00:00
|
|
|
repo := repository.TestRepositoryWithVersion(t, version)
|
2022-06-12 12:38:19 +00:00
|
|
|
sn := archiver.TestSnapshot(t, repo, rtest.BenchArchiveDirectory, nil)
|
|
|
|
rtest.OK(t, repo.Flush(context.Background()))
|
|
|
|
|
|
|
|
_, err := restic.LoadTree(context.TODO(), repo, *sn.Tree)
|
|
|
|
rtest.OK(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLoadTree(t *testing.B) {
|
|
|
|
repository.BenchmarkAllVersions(t, benchmarkLoadTree)
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkLoadTree(t *testing.B, version uint) {
|
|
|
|
if rtest.BenchArchiveDirectory == "" {
|
|
|
|
t.Skip("benchdir not set, skipping")
|
|
|
|
}
|
|
|
|
|
|
|
|
// archive a few files
|
2022-12-11 09:41:22 +00:00
|
|
|
repo := repository.TestRepositoryWithVersion(t, version)
|
2022-06-12 12:38:19 +00:00
|
|
|
sn := archiver.TestSnapshot(t, repo, rtest.BenchArchiveDirectory, nil)
|
|
|
|
rtest.OK(t, repo.Flush(context.Background()))
|
|
|
|
|
|
|
|
t.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < t.N; i++ {
|
|
|
|
_, err := restic.LoadTree(context.TODO(), repo, *sn.Tree)
|
|
|
|
rtest.OK(t, err)
|
|
|
|
}
|
|
|
|
}
|
2023-07-16 13:43:27 +00:00
|
|
|
|
|
|
|
func TestFindTreeDirectory(t *testing.T) {
|
|
|
|
repo := repository.TestRepository(t)
|
2023-07-16 13:55:05 +00:00
|
|
|
sn := restic.TestCreateSnapshot(t, repo, parseTimeUTC("2017-07-07 07:07:08"), 3)
|
2023-07-16 13:43:27 +00:00
|
|
|
|
|
|
|
for _, exp := range []struct {
|
2023-07-16 14:50:54 +00:00
|
|
|
subfolder string
|
|
|
|
id restic.ID
|
|
|
|
err error
|
2023-07-16 13:43:27 +00:00
|
|
|
}{
|
|
|
|
{"", restic.TestParseID("c25199703a67455b34cc0c6e49a8ac8861b268a5dd09dc5b2e31e7380973fc97"), nil},
|
|
|
|
{"/", restic.TestParseID("c25199703a67455b34cc0c6e49a8ac8861b268a5dd09dc5b2e31e7380973fc97"), nil},
|
|
|
|
{".", restic.TestParseID("c25199703a67455b34cc0c6e49a8ac8861b268a5dd09dc5b2e31e7380973fc97"), nil},
|
|
|
|
{"..", restic.ID{}, errors.New("path ..: not found")},
|
|
|
|
{"file-1", restic.ID{}, errors.New("path file-1: not a directory")},
|
|
|
|
{"dir-21", restic.TestParseID("76172f9dec15d7e4cb98d2993032e99f06b73b2f02ffea3b7cfd9e6b4d762712"), nil},
|
|
|
|
{"/dir-21", restic.TestParseID("76172f9dec15d7e4cb98d2993032e99f06b73b2f02ffea3b7cfd9e6b4d762712"), nil},
|
|
|
|
{"dir-21/", restic.TestParseID("76172f9dec15d7e4cb98d2993032e99f06b73b2f02ffea3b7cfd9e6b4d762712"), nil},
|
|
|
|
{"dir-21/dir-24", restic.TestParseID("74626b3fb2bd4b3e572b81a4059b3e912bcf2a8f69fecd9c187613b7173f13b1"), nil},
|
|
|
|
} {
|
|
|
|
t.Run("", func(t *testing.T) {
|
2023-07-16 14:50:54 +00:00
|
|
|
id, err := restic.FindTreeDirectory(context.TODO(), repo, sn.Tree, exp.subfolder)
|
2023-07-16 13:43:27 +00:00
|
|
|
if exp.err == nil {
|
|
|
|
rtest.OK(t, err)
|
|
|
|
rtest.Assert(t, exp.id == *id, "unexpected id, expected %v, got %v", exp.id, id)
|
|
|
|
} else {
|
|
|
|
rtest.Assert(t, exp.err.Error() == err.Error(), "unexpected err, expected %v, got %v", exp.err, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := restic.FindTreeDirectory(context.TODO(), repo, nil, "")
|
|
|
|
rtest.Assert(t, err != nil, "missing error on null tree id")
|
|
|
|
}
|