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
|
package backup
import (
"bytes"
"code.laria.me/petrific/cache"
"code.laria.me/petrific/fs"
"code.laria.me/petrific/logging"
"code.laria.me/petrific/objects"
"code.laria.me/petrific/storage"
"code.laria.me/petrific/storage/memory"
"testing"
)
func wantObject(
t *testing.T,
s storage.Storage,
id objects.ObjectId,
want []byte,
) {
have, err := s.Get(id)
if err != nil {
t.Errorf("Could not get %s: %s", id, err)
return
}
if !bytes.Equal(want, have) {
t.Errorf("Wrong result for %s: (size=%d) %#v", id, len(have), have)
}
}
func TestWriteLargeFile(t *testing.T) {
s := memory.NewMemoryStorage()
id, err := WriteFile(s, bytes.NewReader(content_largefile))
if err != nil {
t.Fatalf("Unexpected error when writing file: %s", err)
}
if !id.Equals(objid_largefile) {
t.Errorf("Unexpected file id: %s", id)
}
wantObject(t, s, objid_largefile, obj_largefile)
wantObject(t, s, objid_largefile_blob0, obj_largefile_blob0)
wantObject(t, s, objid_largefile_blob1, obj_largefile_blob1)
}
func mkfile(t *testing.T, d fs.Dir, name string, exec bool, content []byte) {
f, err := d.CreateChildFile(name, exec)
if err != nil {
t.Fatalf("Could not create file %s: %s", name, err)
}
wc, err := f.OpenWritable()
if err != nil {
t.Fatalf("Could not create file %s: %s", name, err)
}
defer wc.Close()
if _, err := wc.Write(content); err != nil {
t.Fatalf("Could not create file %s: %s", name, err)
}
}
func TestWriteDir(t *testing.T) {
s := memory.NewMemoryStorage()
root := fs.NewMemoryFSRoot("root")
mkfile(t, root, "foo", false, []byte("foo"))
mkfile(t, root, "bar", true, []byte(""))
if _, err := root.CreateChildSymlink("baz", "foo"); err != nil {
t.Fatalf("Failed creating symlink baz: %s", err)
}
d, err := root.CreateChildDir("sub")
if err != nil {
t.Fatalf("Failed creating dir: %s", err)
}
mkfile(t, d, "a", false, []byte(""))
if _, err = d.CreateChildDir("b"); err != nil {
t.Fatalf("Failed creating dir: %s", err)
}
id, err := WriteDir(s, "", root, cache.NopCache{}, logging.NewNopLog())
if err != nil {
t.Fatalf("Could not WriteDir: %s", err)
}
if id.String() != "sha3-256:09e881f57befa1eacec744e3857a36f0d9d5dd1fa72ba96564b467a3d7d0c0d5" {
t.Errorf("Unexpected dir id: %s", id)
}
wantObject(t, s, objid_emptyfile, obj_emptyfile)
wantObject(t, s, objid_fooblob, obj_fooblob)
wantObject(t, s, objid_foofile, obj_foofile)
wantObject(t, s, objid_emptytree, obj_emptytree)
wantObject(t, s, objid_subtree, obj_subtree)
wantObject(t, s, objid_testtree, obj_testtree)
}
|