]> arthur.barton.de Git - bup.git/blob - t/test-save-errors
git: add a test for not keeping midx files open
[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 #!/bin/sh
37 """": # -*-python-*-
38 export PYTHONPATH="$top/lib"
39 exec "$top/cmd/bup-python" "\$0" \${1+"\$@"}
40 """
41 from bup import metadata
42 orig_from_path = metadata.from_path
43 def from_path(path, *args, **kw):
44     if path.endswith(b'/5'):
45         raise IOError('intentionally failing metadata read for .../5')
46     return orig_from_path(path, *args, **kw)
47 metadata.from_path = from_path
48
49 exec(open("$top/cmd/bup-save", "rb").read())
50 EOF
51 chmod +x "$tmpdir/bup-save"
52
53 # use it to save the data
54 "$tmpdir/bup-save" -n test "$tmpdir/save"
55
56 # this should work anyway
57 WVPASS bup ls -l "test/latest/$tmpdir/save"
58 # also check the *right* data was returned
59 lsout="$(bup ls -l "test/latest/$tmpdir/save")"
60 for f in 1 2 3 4   6 7 8 9 ; do
61     if ! echo "$lsout" | grep "200${f}-01-01 00:00 $f" ; then
62         WVFAIL echo incorrect date for $f
63     fi
64 done
65 # and ensure we actually failed, and the above script/hack didn't break
66 if ! echo "$lsout" | grep "1970-01-01 00:00 5" ; then
67     WVFAIL echo unexpected date for file 5
68 fi
69
70
71 WVSTART "metadata read error for a folder"
72 WVPASS bup index --clear
73 WVPASS bup index "$tmpdir/save"
74
75 cat > "$tmpdir/bup-save" << EOF
76 #!/bin/sh
77 """": # -*-python-*-
78 export PYTHONPATH="$top/lib"
79 exec "$top/cmd/bup-python" "\$0" \${1+"\$@"}
80 """
81 from bup import metadata
82 orig_from_path = metadata.from_path
83 def from_path(path, *args, **kw):
84     if path.endswith(b'/a'):
85         raise IOError('intentionally failing metadata read for .../a')
86     return orig_from_path(path, *args, **kw)
87 metadata.from_path = from_path
88
89 exec(open("$top/cmd/bup-save", "rb").read())
90 EOF
91 chmod +x "$tmpdir/bup-save"
92
93 # use it to save the data
94 "$tmpdir/bup-save" -n test "$tmpdir/save"
95
96 # this should work anyway
97 WVPASS bup ls -l "test/latest/$tmpdir/save"
98 if ! bup ls -l "test/latest/$tmpdir/save/a" | grep '1999-01-01 00:00 1' ; then
99     WVFAIL unexpected date for file a/1
100 fi
101 # and ensure we actually failed, and the above script/hack didn't break
102 if ! bup ls -l "test/latest/$tmpdir/save" | grep "1970-01-01 00:00 a" ; then
103     WVFAIL unexpected date for directory a
104 fi
105
106
107 WVSTART "duplicate entries"
108 WVPASS bup index --clear
109 WVPASS bup index "$tmpdir/save"
110
111 cat > "$tmpdir/bup-save" << EOF
112 #!$top/cmd/bup-python
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"