]> arthur.barton.de Git - bup.git/blob - t/test-meta.sh
Add initial (trivial) root-only metadata tests for attr and xattr.
[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 WVSTART "meta"
19
20 genstat()
21 {
22   (
23     export PATH="${TOP}:${PATH}" # pick up bup
24     # Skip atime (test elsewhere) to avoid the observer effect.
25     find . | sort | xargs bup xstat --exclude-fields ctime,atime
26   )
27 }
28
29 force-delete()
30 {
31   if test "$(whoami)" != root
32   then
33     rm -rf "$@"
34   else
35     # Go to greater lengths to deal with any test detritus.
36     for f in "$@"
37     do
38       test -e "$@" || continue
39       chattr -fR = "$@" || true
40       setfacl -Rb "$@"
41       rm -r "$@"
42     done
43   fi
44 }
45
46 test-src-create-extract()
47 {
48   # Test bup meta create/extract for ./src -> ./src-restore.
49   # Also writes to ./src-stat and ./src-restore-stat.
50   (
51     (cd src && WVPASS genstat) > src-stat
52     WVPASS bup meta --create --recurse --file src.meta src
53     # Test extract.
54     force-delete src-restore
55     mkdir src-restore
56     cd src-restore
57     WVPASS bup meta --extract --file ../src.meta
58     WVPASS test -d src
59     (cd src && genstat >../../src-restore-stat) || WVFAIL
60     WVPASS diff -u5 ../src-stat ../src-restore-stat
61     # Test start/finish extract.
62     force-delete src
63     WVPASS bup meta --start-extract --file ../src.meta
64     WVPASS test -d src
65     WVPASS bup meta --finish-extract --file ../src.meta
66     (cd src && genstat >../../src-restore-stat) || WVFAIL
67     WVPASS diff -u5 ../src-stat ../src-restore-stat
68   )
69 }
70
71 if test "$(whoami)" == root
72 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   mkdir -p "${TOP}/bupmeta.tmp"
82   make DESTDIR="${TOP}/bupmeta.tmp/src" install
83   mkdir "${TOP}/bupmeta.tmp/src/misc"
84   cp -a cmd/bup-* "${TOP}/bupmeta.tmp/src/misc/"
85 ) || WVFAIL
86
87 # Use the test tree to check bup meta.
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     WVPASS cd "${TOP}/bupmeta.tmp"
106     umount testfs || true
107     dd if=/dev/zero of=test-fs.img bs=1M count=32
108     mke2fs -F -j -m 0 test-fs.img
109     mkdir testfs
110     mount -o loop,acl,user_xattr test-fs.img testfs
111     # Hide, so that tests can't create risks.
112     chown root:root testfs
113     chmod 0700 testfs
114
115     cp -a src testfs/src
116     (cd testfs && test-src-create-extract)
117
118     # Test Linux attr.
119     force-delete testfs/src
120     mkdir testfs/src
121     (
122       touch testfs/src/foo
123       mkdir testfs/src/bar
124       chattr +acdeijstuADST testfs/src/foo
125       chattr +acdeijstuADST testfs/src/bar
126       (cd testfs && test-src-create-extract)
127     )
128
129     # Test Linux xattr.
130     force-delete testfs/src
131     mkdir testfs/src
132     (
133       touch testfs/src/foo
134       mkdir testfs/src/bar
135       attr -s foo -V bar testfs/src/foo
136       attr -s foo -V bar testfs/src/bar
137       (cd testfs && test-src-create-extract)
138     )
139   )
140 fi
141
142 force-delete "${BUP_DIR}"
143 force-delete "$TOP/bupmeta.tmp"
144
145 exit 0