]> arthur.barton.de Git - bup.git/blob - test/ext/test-save-errors
tests: move all tests to test/
[bup.git] / test / ext / test-save-errors
1 #!/usr/bin/env bash
2 . wvtest.sh
3 . wvtest-bup.sh
4 . dev/lib.sh
5
6 set -o pipefail
7
8 top="$(WVPASS pwd)" || exit $?
9 tmpdir="$(WVPASS wvmktempdir)" || exit $?
10 export BUP_DIR="$tmpdir/bup"
11
12 bup() { "$top/bup" "$@"; }
13
14 WVPASS cd "$tmpdir"
15
16 # necessary for 0 == 1970-01-01 00:00
17 export TZ=UTC
18
19 WVSTART "init"
20 WVPASS bup init
21
22 mkdir "$tmpdir/save"
23 for f in $(seq 9) ; do
24     touch -t 200${f}01010000 "$tmpdir/save/$f"
25 done
26 mkdir "$tmpdir/save/a"
27 touch -t 199901010000 "$tmpdir/save/a/1"
28
29 WVSTART "metadata read error for a file"
30 WVPASS bup index "$tmpdir/save"
31
32 # now do a hack to inject save errors while reading metadata
33 # essentially, we create a bup-save command for ourselves
34 # that gets an error for the .../5 file in metadata.from_path()
35 cat > "$tmpdir/bup-save" << EOF
36 #!/usr/bin/env $top/dev/bup-python
37 from bup import metadata
38
39 orig_from_path = metadata.from_path
40 def from_path(path, *args, **kw):
41     if path.endswith(b'/5'):
42         raise IOError('intentionally failing metadata read for .../5')
43     return orig_from_path(path, *args, **kw)
44 metadata.from_path = from_path
45
46 exec(open("$top/lib/cmd/bup-save", "rb").read())
47 EOF
48 chmod +x "$tmpdir/bup-save"
49
50 # use it to save the data
51 "$tmpdir/bup-save" -n test "$tmpdir/save"
52
53 # this should work anyway
54 WVPASS bup ls -l "test/latest/$tmpdir/save"
55 # also check the *right* data was returned
56 lsout="$(bup ls -l "test/latest/$tmpdir/save")"
57 for f in 1 2 3 4   6 7 8 9 ; do
58     if ! echo "$lsout" | grep "200${f}-01-01 00:00 $f" ; then
59         WVFAIL echo incorrect date for $f
60     fi
61 done
62 # and ensure we actually failed, and the above script/hack didn't break
63 if ! echo "$lsout" | grep "1970-01-01 00:00 5" ; then
64     WVFAIL echo unexpected date for file 5
65 fi
66
67
68 WVSTART "metadata read error for a folder"
69 WVPASS bup index --clear
70 WVPASS bup index "$tmpdir/save"
71
72 cat > "$tmpdir/bup-save" << EOF
73 #!/usr/bin/env $top/dev/bup-python
74 from bup import metadata
75
76 orig_from_path = metadata.from_path
77 def from_path(path, *args, **kw):
78     if path.endswith(b'/a'):
79         raise IOError('intentionally failing metadata read for .../a')
80     return orig_from_path(path, *args, **kw)
81 metadata.from_path = from_path
82
83 exec(open("$top/lib/cmd/bup-save", "rb").read())
84 EOF
85 chmod +x "$tmpdir/bup-save"
86
87 # use it to save the data
88 "$tmpdir/bup-save" -n test "$tmpdir/save"
89
90 # this should work anyway
91 WVPASS bup ls -l "test/latest/$tmpdir/save"
92 if ! bup ls -l "test/latest/$tmpdir/save/a" | grep '1999-01-01 00:00 1' ; then
93     WVFAIL unexpected date for file a/1
94 fi
95 # and ensure we actually failed, and the above script/hack didn't break
96 if ! bup ls -l "test/latest/$tmpdir/save" | grep "1970-01-01 00:00 a" ; then
97     WVFAIL unexpected date for directory a
98 fi
99
100
101 WVSTART "duplicate entries"
102 WVPASS bup index --clear
103 WVPASS bup index "$tmpdir/save"
104
105 cat > "$tmpdir/bup-save" << EOF
106 #!/usr/bin/env $top/dev/bup-python
107 from bup import index
108
109 Reader = index.Reader
110 class DupReader(index.Reader):
111     def filter(self, *args, **kw):
112         for transname, ent in Reader.filter(self, *args, **kw):
113             # duplicate a file and a folder
114             if ent.name.endswith(b'/5') or ent.name.endswith(b'/a/'):
115                 yield transname, ent
116             yield transname, ent
117 index.Reader = DupReader
118
119 exec(open("$top/lib/cmd/bup-save", "rb").read())
120 EOF
121 chmod +x "$tmpdir/bup-save"
122
123 # use it to save the data
124 "$tmpdir/bup-save" -n test "$tmpdir/save"
125
126 # this should work
127 WVPASS bup ls -l "test/latest/$tmpdir/save"
128
129 # check that there are no duplicates
130 lsout=$(bup ls -l "test/latest/$tmpdir/save")
131 WVPASSEQ "$(echo "$lsout" | sort | uniq -d)" ""
132
133 # and we should get the *right* data for each entry
134 for f in $(seq 9) ; do
135     if ! echo "$lsout" | grep "200${f}-01-01 00:00 $f" ; then
136         WVFAIL echo incorrect metadata for $f
137     fi
138 done
139
140
141 WVPASS rm -rf "$tmpdir"