]> arthur.barton.de Git - bup.git/blob - t/test-save-errors
Add bin/bup symlink so bin/ can be added to PATH
[bup.git] / t / test-save-errors
1 #!/usr/bin/env bash
2 . wvtest.sh
3 . wvtest-bup.sh
4 . t/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/cmd/bup-python
37 import os.path, sys
38 sys.path[:0] = [os.path.dirname(os.path.realpath(__file__)) + '/../../../lib']
39 from bup import metadata
40
41 orig_from_path = metadata.from_path
42 def from_path(path, *args, **kw):
43     if path.endswith(b'/5'):
44         raise IOError('intentionally failing metadata read for .../5')
45     return orig_from_path(path, *args, **kw)
46 metadata.from_path = from_path
47
48 exec(open("$top/cmd/bup-save", "rb").read())
49 EOF
50 chmod +x "$tmpdir/bup-save"
51
52 # use it to save the data
53 "$tmpdir/bup-save" -n test "$tmpdir/save"
54
55 # this should work anyway
56 WVPASS bup ls -l "test/latest/$tmpdir/save"
57 # also check the *right* data was returned
58 lsout="$(bup ls -l "test/latest/$tmpdir/save")"
59 for f in 1 2 3 4   6 7 8 9 ; do
60     if ! echo "$lsout" | grep "200${f}-01-01 00:00 $f" ; then
61         WVFAIL echo incorrect date for $f
62     fi
63 done
64 # and ensure we actually failed, and the above script/hack didn't break
65 if ! echo "$lsout" | grep "1970-01-01 00:00 5" ; then
66     WVFAIL echo unexpected date for file 5
67 fi
68
69
70 WVSTART "metadata read error for a folder"
71 WVPASS bup index --clear
72 WVPASS bup index "$tmpdir/save"
73
74 cat > "$tmpdir/bup-save" << EOF
75 #!/usr/bin/env $top/cmd/bup-python
76 import os.path, sys
77 sys.path[:0] = [os.path.dirname(os.path.realpath(__file__)) + '/../../../lib']
78 from bup import metadata
79
80 orig_from_path = metadata.from_path
81 def from_path(path, *args, **kw):
82     if path.endswith(b'/a'):
83         raise IOError('intentionally failing metadata read for .../a')
84     return orig_from_path(path, *args, **kw)
85 metadata.from_path = from_path
86
87 exec(open("$top/cmd/bup-save", "rb").read())
88 EOF
89 chmod +x "$tmpdir/bup-save"
90
91 # use it to save the data
92 "$tmpdir/bup-save" -n test "$tmpdir/save"
93
94 # this should work anyway
95 WVPASS bup ls -l "test/latest/$tmpdir/save"
96 if ! bup ls -l "test/latest/$tmpdir/save/a" | grep '1999-01-01 00:00 1' ; then
97     WVFAIL unexpected date for file a/1
98 fi
99 # and ensure we actually failed, and the above script/hack didn't break
100 if ! bup ls -l "test/latest/$tmpdir/save" | grep "1970-01-01 00:00 a" ; then
101     WVFAIL unexpected date for directory a
102 fi
103
104
105 WVSTART "duplicate entries"
106 WVPASS bup index --clear
107 WVPASS bup index "$tmpdir/save"
108
109 cat > "$tmpdir/bup-save" << EOF
110 #!/usr/bin/env $top/cmd/bup-python
111 import os.path, sys
112 sys.path[:0] = [os.path.dirname(os.path.realpath(__file__)) + '/../../../lib']
113 from bup import index
114
115 Reader = index.Reader
116 class DupReader(index.Reader):
117     def filter(self, *args, **kw):
118         for transname, ent in Reader.filter(self, *args, **kw):
119             # duplicate a file and a folder
120             if ent.name.endswith(b'/5') or ent.name.endswith(b'/a/'):
121                 yield transname, ent
122             yield transname, ent
123 index.Reader = DupReader
124
125 exec(open("$top/cmd/bup-save", "rb").read())
126 EOF
127 chmod +x "$tmpdir/bup-save"
128
129 # use it to save the data
130 "$tmpdir/bup-save" -n test "$tmpdir/save"
131
132 # this should work
133 WVPASS bup ls -l "test/latest/$tmpdir/save"
134
135 # check that there are no duplicates
136 lsout=$(bup ls -l "test/latest/$tmpdir/save")
137 WVPASSEQ "$(echo "$lsout" | sort | uniq -d)" ""
138
139 # and we should get the *right* data for each entry
140 for f in $(seq 9) ; do
141     if ! echo "$lsout" | grep "200${f}-01-01 00:00 $f" ; then
142         WVFAIL echo incorrect metadata for $f
143     fi
144 done
145
146
147 WVPASS rm -rf "$tmpdir"