]> arthur.barton.de Git - bup.git/blob - t/test-meta.sh
test-meta.sh: use $foo rather than ${foo} where possible.
[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 -- "make install" to a temp directory,
13 # then check that bup meta can reproduce the metadata correctly
14 # (according to coreutils stat) via create, extract, start-extract,
15 # and finish-extract.  The current tests are crude, and this does not
16 # test devices, varying 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
35   then
36     rm -rf "$@"
37   else
38     # Go to greater lengths to deal with any test detritus.
39     for f in "$@"
40     do
41       test -e "$@" || continue
42       chattr -fR = "$@" || true
43       setfacl -Rb "$@"
44       rm -r "$@"
45     done
46   fi
47 }
48
49 test-src-create-extract()
50 {
51   # Test bup meta create/extract for ./src -> ./src-restore.
52   # Also writes to ./src-stat and ./src-restore-stat.
53   (
54     (cd src && WVPASS genstat) > src-stat
55     WVPASS bup meta --create --recurse --file src.meta src
56     # Test extract.
57     force-delete src-restore
58     mkdir src-restore
59     cd src-restore
60     WVPASS bup meta --extract --file ../src.meta
61     WVPASS test -d src
62     (cd src && genstat >../../src-restore-stat) || WVFAIL
63     WVPASS diff -U5 ../src-stat ../src-restore-stat
64     # Test start/finish extract.
65     force-delete src
66     WVPASS bup meta --start-extract --file ../src.meta
67     WVPASS test -d src
68     WVPASS bup meta --finish-extract --file ../src.meta
69     (cd src && genstat >../../src-restore-stat) || WVFAIL
70     WVPASS diff -U5 ../src-stat ../src-restore-stat
71   )
72 }
73
74 if actually-root
75 then
76   umount "$TOP/bupmeta.tmp/testfs" || true
77 fi
78
79 force-delete "$BUP_DIR"
80 force-delete "$TOP/bupmeta.tmp"
81
82 # Create a test tree.
83 (
84   mkdir -p "$TOP/bupmeta.tmp"
85   make DESTDIR="$TOP/bupmeta.tmp/src" install
86   mkdir "$TOP/bupmeta.tmp/src/misc"
87   cp -a cmd/bup-* "$TOP/bupmeta.tmp/src/misc/"
88 ) || WVFAIL
89
90 # Use the test tree to check bup meta.
91 WVSTART 'meta - general'
92 (
93   cd "$TOP/bupmeta.tmp"
94   test-src-create-extract
95 )
96
97 # Root-only tests: ACLs, Linux attr, Linux xattr, etc.
98 if actually-root
99 then
100   (
101     cleanup_at_exit()
102     {
103       cd "$TOP"
104       umount "$TOP/bupmeta.tmp/testfs" || true
105     }
106
107     trap cleanup_at_exit EXIT
108
109     WVSTART 'meta - general (as root)'
110     WVPASS cd "$TOP/bupmeta.tmp"
111     umount testfs || true
112     dd if=/dev/zero of=testfs.img bs=1M count=32
113     mke2fs -F -j -m 0 testfs.img
114     mkdir testfs
115     mount -o loop,acl,user_xattr testfs.img testfs
116     # Hide, so that tests can't create risks.
117     chown root:root testfs
118     chmod 0700 testfs
119
120     cp -a src testfs/src
121     (cd testfs && test-src-create-extract)
122
123     WVSTART 'meta - atime (as root)'
124     force-delete testfs/src
125     mkdir testfs/src
126     (
127       mkdir testfs/src/foo
128       touch testfs/src/bar
129       PYTHONPATH="$TOP/lib" \
130         python -c "from bup.xstat import lutime, FSTime; \
131                    x = FSTime.from_secs(42);\
132                    lutime('testfs/src/foo', (x, x));\
133                    lutime('testfs/src/bar', (x, x));"
134       cd testfs
135       WVPASS bup meta -v --create --recurse --file src.meta src
136       bup meta -tvf src.meta
137       # Test extract.
138       force-delete src-restore
139       mkdir src-restore
140       cd src-restore
141       WVPASS bup meta --extract --file ../src.meta
142       WVPASSEQ "$(bup xstat --include-fields=atime src/foo)" "atime: 42"
143       WVPASSEQ "$(bup xstat --include-fields=atime src/bar)" "atime: 42"
144       # Test start/finish extract.
145       force-delete src
146       WVPASS bup meta --start-extract --file ../src.meta
147       WVPASS test -d src
148       WVPASS bup meta --finish-extract --file ../src.meta
149       WVPASSEQ "$(bup xstat --include-fields=atime src/foo)" "atime: 42"
150       WVPASSEQ "$(bup xstat --include-fields=atime src/bar)" "atime: 42"
151     )
152
153     WVSTART 'meta - Linux attr (as root)'
154     force-delete testfs/src
155     mkdir testfs/src
156     (
157       touch testfs/src/foo
158       mkdir testfs/src/bar
159       chattr +acdeijstuADST testfs/src/foo
160       chattr +acdeijstuADST testfs/src/bar
161       (cd testfs && test-src-create-extract)
162     )
163
164     WVSTART 'meta - Linux xattr (as root)'
165     force-delete testfs/src
166     mkdir testfs/src
167     (
168       touch testfs/src/foo
169       mkdir testfs/src/bar
170       attr -s foo -V bar testfs/src/foo
171       attr -s foo -V bar testfs/src/bar
172       (cd testfs && test-src-create-extract)
173     )
174
175     WVSTART 'meta - POSIX.1e ACLs (as root)'
176     force-delete testfs/src
177     mkdir testfs/src
178     (
179       touch testfs/src/foo
180       mkdir testfs/src/bar
181       setfacl -m u:root:r testfs/src/foo
182       setfacl -m u:root:r testfs/src/bar
183       (cd testfs && test-src-create-extract)
184     )
185   )
186 fi
187
188 exit 0