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