aboutsummaryrefslogtreecommitdiff
path: root/backup/restore_test.go
blob: 9e6e5255ddd90d6571e74bfba4618fe0d2e67128 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package backup

import (
	"bytes"
	"code.laria.me/petrific/fs"
	"code.laria.me/petrific/objects"
	"code.laria.me/petrific/storage"
	"io"
	"testing"
)

func withChildOfType(t *testing.T, root fs.Dir, name string, ft fs.FileType, do func(*testing.T, fs.File)) {
	f, err := root.GetChild(name)
	if err != nil {
		t.Errorf("Could not GetChild(%s): %s", name, err)
		return
	}

	if f.Type() != ft {
		t.Errorf("Child '%s' has type %s, expected %s", name, f.Type(), ft)
		return
	}

	do(t, f)
}

func wantFileWithContent(want []byte, exec bool) func(*testing.T, fs.File) {
	return func(t *testing.T, f fs.File) {
		rf := f.(fs.RegularFile)

		if rf.Executable() != exec {
			t.Errorf("Child '%s' has executable bit %b, expected %b", f.Name(), rf.Executable(), exec)
		}

		rwc, err := rf.Open()
		if err != nil {
			t.Errorf("could not open child '%s': %s", f.Name(), err)
		}
		defer rwc.Close()

		buf := new(bytes.Buffer)
		if _, err := io.Copy(buf, rwc); err != nil {
			t.Errorf("Could not read content of child '%s': %s", err)
			return
		}

		have := buf.Bytes()
		if !bytes.Equal(have, want) {
			t.Errorf("Unexpected content of child '%s': %s", f.Name(), have)
		}
	}
}

func wantDir(n int, fx func(*testing.T, fs.Dir)) func(*testing.T, fs.File) {
	return func(t *testing.T, f fs.File) {
		d := f.(fs.Dir)

		children, err := d.Readdir()
		if err != nil {
			t.Errorf("Could not Readdir() '%s': %s", f.Name(), err)
			return
		}

		if len(children) != n {
			t.Errorf("Expected '%s' to have %d children, got %d", f.Name(), n, len(children))
			return
		}

		fx(t, d)
	}
}

func TestRestoreDir(t *testing.T) {
	s := storage.NewMemoryStorage()

	s.Set(objid_emptyfile, objects.OTFile, obj_emptyfile)
	s.Set(objid_fooblob, objects.OTBlob, obj_fooblob)
	s.Set(objid_foofile, objects.OTFile, obj_foofile)
	s.Set(objid_emptytree, objects.OTTree, obj_emptytree)
	s.Set(objid_subtree, objects.OTTree, obj_subtree)
	s.Set(objid_testtree, objects.OTTree, obj_testtree)

	root := fs.NewMemoryFSRoot("")

	if err := RestoreDir(s, objid_testtree, root); err != nil {
		t.Fatalf("Unexpected error from RestoreDir(): %s", err)
	}

	wantDir(4, func(t *testing.T, root fs.Dir) {
		withChildOfType(t, root, "foo", fs.FFile, wantFileWithContent([]byte("foo"), false))
		withChildOfType(t, root, "bar", fs.FFile, wantFileWithContent([]byte{}, true))
		withChildOfType(t, root, "baz", fs.FSymlink, func(t *testing.T, f fs.File) {
			target, err := f.(fs.Symlink).Readlink()
			if err != nil {
				t.Errorf("Could not Readlink() child 'baz': %s", err)
				return
			}

			if target != "foo" {
				t.Errorf("Unexpected target for baz: %s", target)
			}
		})

		withChildOfType(t, root, "sub", fs.FDir, wantDir(2, func(t *testing.T, d fs.Dir) {
			withChildOfType(t, d, "a", fs.FFile, wantFileWithContent([]byte{}, false))
			withChildOfType(t, d, "b", fs.FDir, wantDir(0, func(t *testing.T, d fs.Dir) {}))
		}))
	})(t, root)
}

func TestRestoreLargeFile(t *testing.T) {
	s := storage.NewMemoryStorage()
	s.Set(objid_largefile_blob0, objects.OTBlob, obj_largefile_blob0)
	s.Set(objid_largefile_blob1, objects.OTBlob, obj_largefile_blob1)
	s.Set(objid_largefile, objects.OTFile, obj_largefile)

	buf := new(bytes.Buffer)

	if err := RestoreFile(s, objid_largefile, buf); err != nil {
		t.Fatalf("Unexpected error while restoring file: %s", err)
	}

	have := buf.Bytes()
	if !bytes.Equal(have, content_largefile) {
		t.Errorf("Unexpected restoration result: %s", have)
	}
}