]> arthur.barton.de Git - bup.git/blob - t/test-meta.sh
Save metadata during "bup save".
[bup.git] / t / test-meta.sh
1 #!/usr/bin/env bash
2 . wvtest.sh
3
4 TOP="$(pwd)"
5 export BUP_DIR="$TOP/buptest.tmp"
6
7 bup()
8 {
9     "$TOP/bup" "$@"
10 }
11
12 # Very simple metadata tests -- create a test tree then check that bup
13 # meta can reproduce the metadata correctly (according to bup xstat)
14 # via create, extract, start-extract, and finish-extract.  The current
15 # tests are crude, and this does not fully test devices, varying
16 # users/groups, acls, attrs, etc.
17
18 genstat()
19 {
20     (
21         export PATH="$TOP:$PATH" # pick up bup
22         # Skip atime (test elsewhere) to avoid the observer effect.
23         find . | sort | xargs bup xstat --exclude-fields ctime,atime,size
24     )
25 }
26
27 actually-root()
28 {
29     test "$(whoami)" == root -a -z "$FAKEROOTKEY"
30 }
31
32 force-delete()
33 {
34     if ! actually-root; then
35         rm -rf "$@"
36     else
37         # Go to greater lengths to deal with any test detritus.
38         for f in "$@"; do
39             test -e "$@" || continue
40             chattr -fR = "$@" || true
41             setfacl -Rb "$@"
42             rm -r "$@"
43         done
44     fi
45 }
46
47 test-src-create-extract()
48 {
49     # Test bup meta create/extract for ./src -> ./src-restore.
50     # Also writes to ./src-stat and ./src-restore-stat.
51     (
52         (cd src && WVPASS genstat) > src-stat
53         WVPASS bup meta --create --recurse --file src.meta src
54         # Test extract.
55         force-delete src-restore
56         mkdir src-restore
57         cd src-restore
58         WVPASS bup meta --extract --file ../src.meta
59         WVPASS test -d src
60         (cd src && genstat >../../src-restore-stat) || WVFAIL
61         WVPASS diff -U5 ../src-stat ../src-restore-stat
62         # Test start/finish extract.
63         force-delete src
64         WVPASS bup meta --start-extract --file ../src.meta
65         WVPASS test -d src
66         WVPASS bup meta --finish-extract --file ../src.meta
67         (cd src && genstat >../../src-restore-stat) || WVFAIL
68         WVPASS diff -U5 ../src-stat ../src-restore-stat
69     )
70 }
71
72 if actually-root; then
73     umount "$TOP/bupmeta.tmp/testfs" || true
74 fi
75
76 force-delete "$BUP_DIR"
77 force-delete "$TOP/bupmeta.tmp"
78
79 # Create a test tree.
80 (
81     set -e
82     rm -rf "$TOP/bupmeta.tmp/src"
83     mkdir -p "$TOP/bupmeta.tmp/src"
84     cp -pPR Documentation cmd lib t "$TOP/bupmeta.tmp"/src
85
86     # Regression test for metadata sort order.  Previously, these two
87     # entries would sort in the wrong order because the metadata
88     # entries were being sorted by mangled name, but the index isn't.
89     dd if=/dev/zero of="$TOP/bupmeta.tmp"/src/foo bs=1k count=33
90     touch -d 2011-11-11 "$TOP/bupmeta.tmp"/src/foo
91     touch -d 2011-12-12 "$TOP/bupmeta.tmp"/src/foo-bar
92
93     t/mksock "$TOP/bupmeta.tmp/src/test-socket" || true
94 ) || WVFAIL
95
96 # Use the test tree to check bup meta.
97 WVSTART 'meta - general'
98 (
99     cd "$TOP/bupmeta.tmp"
100     test-src-create-extract
101
102     # Test a top-level file (not dir).
103     touch src-file
104     WVPASS bup meta -cf src-file.meta src-file
105     mkdir dest
106     cd dest
107     WVPASS bup meta -xf ../src-file.meta
108 )
109
110 # Root-only tests: ACLs, Linux attr, Linux xattr, etc.
111 if actually-root; then
112     (
113         # These tests are only likely to work under Linux for now
114         # (patches welcome).
115         [[ $(uname) =~ Linux ]] || exit 0
116
117         cleanup_at_exit()
118         {
119             cd "$TOP"
120             umount "$TOP/bupmeta.tmp/testfs" || true
121         }
122
123         trap cleanup_at_exit EXIT
124
125         WVSTART 'meta - general (as root)'
126         WVPASS cd "$TOP/bupmeta.tmp"
127         umount testfs || true
128         dd if=/dev/zero of=testfs.img bs=1M count=32
129         mke2fs -F -j -m 0 testfs.img
130         mkdir testfs
131         mount -o loop,acl,user_xattr testfs.img testfs
132         # Hide, so that tests can't create risks.
133         chown root:root testfs
134         chmod 0700 testfs
135
136         #cp -a src testfs/src
137         cp -pPR src testfs/src
138         (cd testfs && test-src-create-extract)
139
140         WVSTART 'meta - atime (as root)'
141         force-delete testfs/src
142         mkdir testfs/src
143         (
144             mkdir testfs/src/foo
145             touch testfs/src/bar
146             PYTHONPATH="$TOP/lib" \
147                 python -c "from bup import xstat; \
148                 x = xstat.timespec_to_nsecs((42, 0));\
149                    xstat.utime('testfs/src/foo', (x, x));\
150                    xstat.utime('testfs/src/bar', (x, x));"
151             cd testfs
152             WVPASS bup meta -v --create --recurse --file src.meta src
153             bup meta -tvf src.meta
154             # Test extract.
155             force-delete src-restore
156             mkdir src-restore
157             cd src-restore
158             WVPASS bup meta --extract --file ../src.meta
159             WVPASSEQ "$(bup xstat --include-fields=atime src/foo)" "atime: 42"
160             WVPASSEQ "$(bup xstat --include-fields=atime src/bar)" "atime: 42"
161             # Test start/finish extract.
162             force-delete src
163             WVPASS bup meta --start-extract --file ../src.meta
164             WVPASS test -d src
165             WVPASS bup meta --finish-extract --file ../src.meta
166             WVPASSEQ "$(bup xstat --include-fields=atime src/foo)" "atime: 42"
167             WVPASSEQ "$(bup xstat --include-fields=atime src/bar)" "atime: 42"
168         )
169
170         WVSTART 'meta - Linux attr (as root)'
171         force-delete testfs/src
172         mkdir testfs/src
173         (
174             touch testfs/src/foo
175             mkdir testfs/src/bar
176             chattr +acdeijstuADST testfs/src/foo
177             chattr +acdeijstuADST testfs/src/bar
178             (cd testfs && test-src-create-extract)
179         )
180
181         WVSTART 'meta - Linux xattr (as root)'
182         force-delete testfs/src
183         mkdir testfs/src
184         (
185             touch testfs/src/foo
186             mkdir testfs/src/bar
187             attr -s foo -V bar testfs/src/foo
188             attr -s foo -V bar testfs/src/bar
189             (cd testfs && test-src-create-extract)
190         )
191
192         WVSTART 'meta - POSIX.1e ACLs (as root)'
193         force-delete testfs/src
194         mkdir testfs/src
195         (
196             touch testfs/src/foo
197             mkdir testfs/src/bar
198             setfacl -m u:root:r testfs/src/foo
199             setfacl -m u:root:r testfs/src/bar
200             (cd testfs && test-src-create-extract)
201         )
202     )
203 fi
204
205 exit 0