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"
|
2014-09-23 20:39:12 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2014-12-06 21:01:54 +00:00
|
|
|
|
2016-02-14 14:29:28 +00:00
|
|
|
"restic"
|
2016-09-04 11:24:51 +00:00
|
|
|
"restic/repository"
|
2016-02-14 14:29:28 +00:00
|
|
|
. "restic/test"
|
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 {
|
2015-06-13 10:35:19 +00:00
|
|
|
tempdir, err := ioutil.TempDir(TestTempDir, "restic-test-")
|
2015-04-09 19:15:48 +00:00
|
|
|
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 != "." {
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, os.MkdirAll(dir, 0755))
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Create(file)
|
|
|
|
defer func() {
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, f.Close())
|
2014-09-23 20:39:12 +00:00
|
|
|
}()
|
|
|
|
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2014-09-23 20:39:12 +00:00
|
|
|
|
|
|
|
_, err = f.Write(test.content)
|
2015-04-09 19:15:48 +00:00
|
|
|
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() {
|
2016-01-23 18:12:02 +00:00
|
|
|
if TestCleanupTempDirs {
|
2015-08-18 19:05:49 +00:00
|
|
|
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)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2014-12-06 21:01:54 +00:00
|
|
|
|
|
|
|
var node restic.Node
|
|
|
|
err = json.Unmarshal(data, &node)
|
2015-04-09 19:15:48 +00:00
|
|
|
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")
|
2015-04-09 19:15:48 +00:00
|
|
|
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)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-01-10 22:40:10 +00:00
|
|
|
|
|
|
|
n2 := *node
|
2015-04-09 19:15:48 +00:00
|
|
|
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--
|
2015-04-09 19:15:48 +00:00
|
|
|
Assert(t, !node.Equals(n2), "nodes are equal")
|
2015-01-10 22:40:10 +00:00
|
|
|
}
|
2015-04-26 15:44:38 +00:00
|
|
|
|
|
|
|
func TestLoadTree(t *testing.T) {
|
2016-09-04 11:24:51 +00:00
|
|
|
repo, cleanup := repository.TestRepository(t)
|
2016-09-04 10:52:43 +00:00
|
|
|
defer cleanup()
|
2015-04-26 15:44:38 +00:00
|
|
|
|
|
|
|
// save tree
|
|
|
|
tree := restic.NewTree()
|
2017-06-05 21:56:59 +00:00
|
|
|
id, err := repo.SaveTree(context.TODO(), tree)
|
2015-04-26 15:44:38 +00:00
|
|
|
OK(t, err)
|
|
|
|
|
|
|
|
// save packs
|
2015-05-09 11:32:52 +00:00
|
|
|
OK(t, repo.Flush())
|
2015-04-26 15:44:38 +00:00
|
|
|
|
|
|
|
// load tree again
|
2017-06-05 21:56:59 +00:00
|
|
|
tree2, err := repo.LoadTree(context.TODO(), id)
|
2015-04-26 15:44:38 +00:00
|
|
|
OK(t, err)
|
|
|
|
|
|
|
|
Assert(t, tree.Equals(tree2),
|
|
|
|
"trees are not equal: want %v, got %v",
|
|
|
|
tree, tree2)
|
|
|
|
}
|