]> arthur.barton.de Git - bup.git/blob - t/test-meta.sh
Add atime tests and fix atime capture in metadata.from_path().
[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 force-delete()
29 {
30   if test "$(whoami)" != root
31   then
32     rm -rf "$@"
33   else
34     # Go to greater lengths to deal with any test detritus.
35     for f in "$@"
36     do
37       test -e "$@" || continue
38       chattr -fR = "$@" || true
39       setfacl -Rb "$@"
40       rm -r "$@"
41     done
42   fi
43 }
44
45 test-src-create-extract()
46 {
47   # Test bup meta create/extract for ./src -> ./src-restore.
48   # Also writes to ./src-stat and ./src-restore-stat.
49   (
50     (cd src && WVPASS genstat) > src-stat
51     WVPASS bup meta --create --recurse --file src.meta src
52     # Test extract.
53     force-delete src-restore
54     mkdir src-restore
55     cd src-restore
56     WVPASS bup meta --extract --file ../src.meta
57     WVPASS test -d src
58     (cd src && genstat >../../src-restore-stat) || WVFAIL
59     WVPASS diff -u5 ../src-stat ../src-restore-stat
60     # Test start/finish extract.
61     force-delete src
62     WVPASS bup meta --start-extract --file ../src.meta
63     WVPASS test -d src
64     WVPASS bup meta --finish-extract --file ../src.meta
65     (cd src && genstat >../../src-restore-stat) || WVFAIL
66     WVPASS diff -u5 ../src-stat ../src-restore-stat
67   )
68 }
69
70 if test "$(whoami)" == root
71 then
72   umount "${TOP}/bupmeta.tmp/testfs" || true
73 fi
74
75 force-delete "${BUP_DIR}"
76 force-delete "${TOP}/bupmeta.tmp"
77
78 # Create a test tree.
79 (
80   mkdir -p "${TOP}/bupmeta.tmp"
81   make DESTDIR="${TOP}/bupmeta.tmp/src" install
82   mkdir "${TOP}/bupmeta.tmp/src/misc"
83   cp -a cmd/bup-* "${TOP}/bupmeta.tmp/src/misc/"
84 ) || WVFAIL
85
86 # Use the test tree to check bup meta.
87 WVSTART 'meta - general'
88 (
89   cd "${TOP}/bupmeta.tmp"
90   test-src-create-extract
91 )
92
93 # Root-only tests: ACLs, Linux attr, Linux xattr, etc.
94 if test "$(whoami)" == root
95 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=test-fs.img bs=1M count=32
109     mke2fs -F -j -m 0 test-fs.img
110     mkdir testfs
111     mount -o loop,acl,user_xattr test-fs.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     (cd testfs && test-src-create-extract)
118
119     WVSTART 'meta - atime'
120     force-delete testfs/src
121     mkdir testfs/src
122     (
123       mkdir testfs/src/foo
124       touch testfs/src/bar
125       PYTHONPATH="${TOP}/lib" \
126         python -c "from bup.xstat import lutime, FSTime; \
127                    x = FSTime.from_secs(42);\
128                    lutime('testfs/src/foo', (x, x));\
129                    lutime('testfs/src/bar', (x, x));"
130       cd testfs
131       WVPASS bup meta -v --create --recurse --file src.meta src
132       bup meta -tvf src.meta
133       # Test extract.
134       force-delete src-restore
135       mkdir src-restore
136       cd src-restore
137       WVPASS bup meta --extract --file ../src.meta
138       WVPASSEQ "$(bup xstat --include-fields=atime src/foo)" "atime: 42"
139       WVPASSEQ "$(bup xstat --include-fields=atime src/bar)" "atime: 42"
140       # Test start/finish extract.
141       force-delete src
142       WVPASS bup meta --start-extract --file ../src.meta
143       WVPASS test -d src
144       WVPASS bup meta --finish-extract --file ../src.meta
145       WVPASSEQ "$(bup xstat --include-fields=atime src/foo)" "atime: 42"
146       WVPASSEQ "$(bup xstat --include-fields=atime src/bar)" "atime: 42"
147     )
148
149     WVSTART 'meta - Linux attr (as root)'
150     force-delete testfs/src
151     mkdir testfs/src
152     (
153       touch testfs/src/foo
154       mkdir testfs/src/bar
155       chattr +acdeijstuADST testfs/src/foo
156       chattr +acdeijstuADST testfs/src/bar
157       (cd testfs && test-src-create-extract)
158     )
159
160     WVSTART 'meta - Linux xattr (as root)'
161     force-delete testfs/src
162     mkdir testfs/src
163     (
164       touch testfs/src/foo
165       mkdir testfs/src/bar
166       attr -s foo -V bar testfs/src/foo
167       attr -s foo -V bar testfs/src/bar
168       (cd testfs && test-src-create-extract)
169     )
170
171     WVSTART 'meta - POSIX.1e ACLs (as root)'
172     force-delete testfs/src
173     mkdir testfs/src
174     (
175       touch testfs/src/foo
176       mkdir testfs/src/bar
177       setfacl -m u:root:r testfs/src/foo
178       setfacl -m u:root:r testfs/src/bar
179       (cd testfs && test-src-create-extract)
180     )
181   )
182 fi
183
184 force-delete "${BUP_DIR}"
185 force-delete "$TOP/bupmeta.tmp"
186
187 exit 0