]> arthur.barton.de Git - bup.git/blob - test/ext/test-save-errors
Update base_version to 0.34~ for 0.34 development
[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 # Inject save errors while reading metadata via --import-py-module.
33 WVPASS rm -rf "$tmpdir/mod"
34 WVPASS mkdir -p "$tmpdir/mod"
35 cat > "$tmpdir/mod/bup_fail_on_5.py" << EOF
36 from bup import metadata
37
38 orig_from_path = metadata.from_path
39 def from_path(path, *args, **kw):
40     if path.endswith(b'/5'):
41         raise IOError('intentionally failing metadata read for .../5')
42     return orig_from_path(path, *args, **kw)
43 metadata.from_path = from_path
44 EOF
45
46 PYTHONPATH="$tmpdir/mod" \
47           bup --import-py-module bup_fail_on_5 save -n test "$tmpdir/save"
48
49 # this should work anyway
50 WVPASS bup ls -l "test/latest/$tmpdir/save"
51 # also check the *right* data was returned
52 lsout="$(bup ls -l "test/latest/$tmpdir/save")"
53 for f in 1 2 3 4   6 7 8 9 ; do
54     if ! echo "$lsout" | grep "200${f}-01-01 00:00 $f" ; then
55         WVFAIL echo incorrect date for $f
56     fi
57 done
58 # and ensure we actually failed, and the above script/hack didn't break
59 if echo "$lsout" | grep " 5$" ; then
60     WVFAIL echo unexpectedly stored data for file 5
61 fi
62
63
64 WVSTART "metadata read error for a folder"
65 WVPASS bup index --clear
66 WVPASS bup index "$tmpdir/save"
67
68 # Inject save errors while reading metadata via --import-py-module.
69 WVPASS rm -rf "$tmpdir/mod"
70 WVPASS mkdir -p "$tmpdir/mod"
71 cat > "$tmpdir/mod/bup_fail_on_a.py" << EOF
72 from bup import metadata
73
74 orig_from_path = metadata.from_path
75 def from_path(path, *args, **kw):
76     if path.endswith(b'/a'):
77         raise IOError('intentionally failing metadata read for .../a')
78     return orig_from_path(path, *args, **kw)
79 metadata.from_path = from_path
80 EOF
81
82 PYTHONPATH="$tmpdir/mod" \
83           bup --import-py-module bup_fail_on_a save -n test "$tmpdir/save"
84
85 # this should work anyway
86 WVPASS bup ls -l "test/latest/$tmpdir/save"
87 if ! bup ls -l "test/latest/$tmpdir/save/a" | grep '1999-01-01 00:00 1' ; then
88     WVFAIL unexpected date for file a/1
89 fi
90 # and ensure we actually failed, and the above script/hack didn't break
91 if ! bup ls -l "test/latest/$tmpdir/save" | grep "1970-01-01 00:00 a" ; then
92     WVFAIL unexpected date for directory a
93 fi
94
95
96 WVSTART "duplicate entries"
97 WVPASS bup index --clear
98 WVPASS bup index "$tmpdir/save"
99
100 # Inject save errors while reading metadata via --import-py-module.
101 WVPASS rm -rf "$tmpdir/mod"
102 WVPASS mkdir -p "$tmpdir/mod"
103 cat > "$tmpdir/mod/bup_dup_reader_path.py" << EOF
104 from bup import index
105
106 Reader = index.Reader
107 class DupReader(index.Reader):
108     def filter(self, *args, **kw):
109         for transname, ent in Reader.filter(self, *args, **kw):
110             # duplicate a file and a folder
111             if ent.name.endswith(b'/5') or ent.name.endswith(b'/a/'):
112                 yield transname, ent
113             yield transname, ent
114 index.Reader = DupReader
115 EOF
116
117 PYTHONPATH="$tmpdir/mod" \
118           bup --import-py-module bup_dup_reader_path save -n test "$tmpdir/save"
119
120 # this should work
121 WVPASS bup ls -l "test/latest/$tmpdir/save"
122
123 # check that there are no duplicates
124 lsout=$(bup ls -l "test/latest/$tmpdir/save")
125 WVPASSEQ "$(echo "$lsout" | sort | uniq -d)" ""
126
127 # and we should get the *right* data for each entry
128 for f in $(seq 9) ; do
129     if ! echo "$lsout" | grep "200${f}-01-01 00:00 $f" ; then
130         WVFAIL echo incorrect metadata for $f
131     fi
132 done
133
134
135 WVPASS rm -rf "$tmpdir"