]> arthur.barton.de Git - bup.git/blob - t/test-meta.sh
fixed cp problem in bup/t/test-meta.sh
[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
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     rm -rf "$TOP/bupmeta.tmp/src"
82     mkdir -p "$TOP/bupmeta.tmp/src"
83     #cp -a Documentation cmd lib t "$TOP/bupmeta.tmp"/src
84     cp -pPR Documentation cmd lib t "$TOP/bupmeta.tmp"/src
85 ) || WVFAIL
86
87 # Use the test tree to check bup meta.
88 WVSTART 'meta - general'
89 (
90     cd "$TOP/bupmeta.tmp"
91     test-src-create-extract
92 )
93
94 # Root-only tests: ACLs, Linux attr, Linux xattr, etc.
95 if actually-root; then
96     (
97         cleanup_at_exit()
98         {
99             cd "$TOP"
100             umount "$TOP/bupmeta.tmp/testfs" || true
101         }
102
103         trap cleanup_at_exit EXIT
104
105         WVSTART 'meta - general (as root)'
106         WVPASS cd "$TOP/bupmeta.tmp"
107         umount testfs || true
108         dd if=/dev/zero of=testfs.img bs=1M count=32
109         mke2fs -F -j -m 0 testfs.img
110         mkdir testfs
111         mount -o loop,acl,user_xattr testfs.img testfs
112         # Hide, so that tests can't create risks.
113         chown root:root testfs
114         chmod 0700 testfs
115
116         #cp -a src testfs/src
117         cp -pPR src testfs/src
118         (cd testfs && test-src-create-extract)
119
120         WVSTART 'meta - atime (as root)'
121         force-delete testfs/src
122         mkdir testfs/src
123         (
124             mkdir testfs/src/foo
125             touch testfs/src/bar
126             PYTHONPATH="$TOP/lib" \
127                 python -c "from bup.xstat import lutime, FSTime; \
128                 x = FSTime.from_secs(42);\
129                    lutime('testfs/src/foo', (x, x));\
130                    lutime('testfs/src/bar', (x, x));"
131             cd testfs
132             WVPASS bup meta -v --create --recurse --file src.meta src
133             bup meta -tvf src.meta
134             # Test extract.
135             force-delete src-restore
136             mkdir src-restore
137             cd src-restore
138             WVPASS bup meta --extract --file ../src.meta
139             WVPASSEQ "$(bup xstat --include-fields=atime src/foo)" "atime: 42"
140             WVPASSEQ "$(bup xstat --include-fields=atime src/bar)" "atime: 42"
141             # Test start/finish extract.
142             force-delete src
143             WVPASS bup meta --start-extract --file ../src.meta
144             WVPASS test -d src
145             WVPASS bup meta --finish-extract --file ../src.meta
146             WVPASSEQ "$(bup xstat --include-fields=atime src/foo)" "atime: 42"
147             WVPASSEQ "$(bup xstat --include-fields=atime src/bar)" "atime: 42"
148         )
149
150         WVSTART 'meta - Linux attr (as root)'
151         force-delete testfs/src
152         mkdir testfs/src
153         (
154             touch testfs/src/foo
155             mkdir testfs/src/bar
156             chattr +acdeijstuADST testfs/src/foo
157             chattr +acdeijstuADST testfs/src/bar
158             (cd testfs && test-src-create-extract)
159         )
160
161         WVSTART 'meta - Linux xattr (as root)'
162         force-delete testfs/src
163         mkdir testfs/src
164         (
165             touch testfs/src/foo
166             mkdir testfs/src/bar
167             attr -s foo -V bar testfs/src/foo
168             attr -s foo -V bar testfs/src/bar
169             (cd testfs && test-src-create-extract)
170         )
171
172         WVSTART 'meta - POSIX.1e ACLs (as root)'
173         force-delete testfs/src
174         mkdir testfs/src
175         (
176             touch testfs/src/foo
177             mkdir testfs/src/bar
178             setfacl -m u:root:r testfs/src/foo
179             setfacl -m u:root:r testfs/src/bar
180             (cd testfs && test-src-create-extract)
181         )
182     )
183 fi
184
185 exit 0