420286d64a2892f2d0d87092363ced6b7ecddf0a
[platform/upstream/btrfs-progs.git] / tests / common
1 #!/bin/bash
2 #
3 # Common routines for all tests
4 #
5
6 _fail()
7 {
8         echo "$*" | tee -a $RESULTS
9         exit 1
10 }
11
12 # log a message to the results file
13 _log()
14 {
15         echo "$*" | tee -a $RESULTS
16 }
17
18 _not_run()
19 {
20         echo "    [NOTRUN] $*"
21         exit 0
22 }
23
24 run_check()
25 {
26         echo "############### $@" >> $RESULTS 2>&1
27         if [ "$TEST_LOG" = 'tty' ]; then echo "CMD: $@" > /dev/tty; fi
28         if [ "$1" = 'root_helper' ]; then
29                 "$@" >> $RESULTS 2>&1 || _fail "failed: $@"
30         else
31                 $INSTRUMENT "$@" >> $RESULTS 2>&1 || _fail "failed: $@"
32         fi
33 }
34
35 # same as run_check but the stderr+stdout output is duplicated on stdout and
36 # can be processed further
37 run_check_stdout()
38 {
39         echo "############### $@" >> $RESULTS 2>&1
40         if [ "$TEST_LOG" = 'tty' ]; then echo "CMD(stdout): $@" > /dev/tty; fi
41         if [ "$1" = 'root_helper' ]; then
42                 "$@" 2>&1 | tee -a $RESULTS || _fail "failed: $@"
43         else
44                 $INSTRUMENT "$@" 2>&1 | tee -a $RESULTS || _fail "failed: $@"
45         fi
46 }
47
48 # same as run_check but does not fail the test, output is logged
49 run_mayfail()
50 {
51         local ret
52
53         echo "############### $@" >> $RESULTS 2>&1
54         if [ "$TEST_LOG" = 'tty' ]; then echo "CMD(mayfail): $@" > /dev/tty; fi
55         if [ "$1" = 'root_helper' ]; then
56                 "$@" >> $RESULTS 2>&1
57         else
58                 $INSTRUMENT "$@" >> $RESULTS 2>&1
59         fi
60         ret=$?
61         if [ $ret != 0 ]; then
62                 echo "failed (ignored, ret=$ret): $@" >> $RESULTS
63                 if [ $ret == 139 ]; then
64                         _fail "mayfail: returned code 139 (SEGFAULT), not ignored"
65                 elif [ $ret == 134 ]; then
66                         _fail "mayfail: returned code 134 (SIGABRT), not ignored"
67                 fi
68                 return $ret
69         fi
70 }
71
72 # first argument is error message to print if it fails, otherwise
73 # same as run_check but expects the command to fail, output is logged
74 run_mustfail()
75 {
76         local msg
77
78         msg="$1"
79         shift
80
81         echo "############### $@" >> $RESULTS 2>&1
82         if [ "$TEST_LOG" = 'tty' ]; then echo "CMD(mustfail): $@" > /dev/tty; fi
83         if [ "$1" = 'root_helper' ]; then
84                 "$@" >> $RESULTS 2>&1
85         else
86                 $INSTRUMENT "$@" >> $RESULTS 2>&1
87         fi
88         if [ $? != 0 ]; then
89                 echo "failed (expected): $@" >> $RESULTS
90                 return 0
91         else
92                 echo "succeeded (unexpected!): $@" >> $RESULTS
93                 _fail "unexpected success: $msg"
94                 return 1
95         fi
96 }
97
98 check_prereq()
99 {
100         if ! [ -f $TOP/$1 ]; then
101                 _fail "Failed prerequisites: $1";
102         fi
103 }
104
105 check_global_prereq()
106 {
107         which $1 &> /dev/null
108         if [ $? -ne 0 ]; then
109                 _fail "Failed system wide prerequisities: $1";
110         fi
111 }
112
113 check_image()
114 {
115         local image
116
117         image=$1
118         echo "testing image $(basename $image)" >> $RESULTS
119         $TOP/btrfs check $image >> $RESULTS 2>&1
120         [ $? -eq 0 ] && _fail "btrfs check should have detected corruption"
121
122         run_check $TOP/btrfs check --repair $image
123         run_check $TOP/btrfs check $image
124 }
125
126 # Extract a usable image from packed formats
127 # - raw btrfs filesystem images, suffix .raw
128 # - dtto compressed by XZ, suffix .raw.xz
129 # - meta-dump images with suffix .img
130 # - dtto compressed by XZ, suffix .img.xz
131 # - compressed send stream, .stream.xz
132 extract_image()
133 {
134         local image
135         local cleanme
136
137         image="$1"
138         case "$image" in
139         *.img)
140                 rm -f $image.restored
141                 : ;;
142         *.img.xz)
143                 xz --decompress --keep "$image" || \
144                         _fail "failed to decompress image $image" >&2
145                 image=${image%%.xz}
146                 rm -f $image.restored
147                 cleanme=$image
148                 ;;
149         *.raw)
150                 cp --sparse=auto $image $image.restored
151                 ;;
152         *.raw.xz)
153                 xz --decompress --keep "$image" || \
154                         _fail "failed to decompress image $image" >&2
155                 image=${image%%.xz}
156                 mv "$image" "$image".restored
157                 ;;
158         *.stream.xz)
159                 xz --decompress --keep "$image" || \
160                         _fail "failed to decompress file $image" >&2
161                 image=${image%%.xz}
162                 mv "$image" "$image".restored
163                 ;;
164         esac
165
166         if ! [ -f $image.restored ]; then
167                 echo "restoring image $(basename $image)" >> $RESULTS
168                 $TOP/btrfs-image -r $image $image.restored \
169                         &>> $RESULTS \
170                         || _fail "failed to restore image $image" >&2
171         fi
172
173         [ -f "$cleanme" ] && rm -f "$cleanme"
174
175         echo "$image.restored"
176 }
177
178 # Process all image dumps in a given directory
179 check_all_images()
180 {
181         local dir
182         local extracted
183
184         dir="$1"
185         for image in $(find $dir \( -iname '*.img' -o   \
186                                 -iname '*.img.xz' -o    \
187                                 -iname '*.raw' -o       \
188                                 -iname '*.raw.xz' \) | sort)
189         do
190                 extracted=$(extract_image "$image")
191                 check_image "$extracted"
192                 rm -f "$extracted"
193         done
194 }
195
196 # some tests need to mount the recovered image and do verifications call
197 # 'setup_root_helper' and then check for have_root_helper == 1 if the test
198 # needs to fail otherwise; using sudo by default for now
199 SUDO_HELPER=
200 NEED_SUDO_VALIDATE=unknown
201 export SUDO_HELPER
202 export NEED_SUDO_VALIDATE
203 root_helper()
204 {
205         if [ $UID -eq 0 ]; then
206                 "$@"
207         else
208                 if [ "$NEED_SUDO_VALIDATE" = 'yes' ]; then
209                         sudo -v -n &>/dev/null || \
210                                 _not_run "Need to validate sudo credentials"
211                         sudo -n "$@"
212                 elif [ "$NEED_SUDO_VALIDATE" = 'no' ]; then
213                         sudo -n /bin/true &> /dev/null || \
214                                 _not_run "Need to validate sudo user settings"
215                         sudo -n "$@"
216                 else
217                         # should not happen
218                         _not_run "Need to validate root privileges"
219                 fi
220         fi
221 }
222
223 setup_root_helper()
224 {
225         if [ $UID -eq 0 -o -n "$SUDO_HELPER" ]; then
226                 return
227         fi
228
229         # Test for old sudo or special settings, which make sudo -v fail even
230         # if user setting is NOPASSWD
231         sudo -n /bin/true &>/dev/null && NEED_SUDO_VALIDATE=no
232
233         # Newer sudo or default sudo setting
234         sudo -v -n &>/dev/null && NEED_SUDO_VALIDATE=yes
235
236         if [ "$NEED_SUDO_VALIDATE" = 'unknown' ]; then
237                 _not_run "Need to validate root privileges"
238         fi
239         SUDO_HELPER=root_helper
240 }
241
242 prepare_test_dev()
243 {
244         # num[K/M/G/T...]
245         local size="$1"
246
247         [[ "$TEST_DEV" ]] && return
248         [[ "$size" ]] || size='2G'
249
250         echo "\$TEST_DEV not given, use $TOP/test/test.img as fallback" >> \
251                 $RESULTS
252         TEST_DEV="$TOP/tests/test.img"
253
254         truncate -s "$size" "$TEST_DEV" || _not_run "create file for loop device failed"
255 }
256
257 run_check_mount_test_dev()
258 {
259         setup_root_helper
260
261         local loop_opt
262         if [[ -b "$TEST_DEV" ]]; then
263                 loop_opt=""
264         elif [[ -f "$TEST_DEV" ]]; then
265                 loop_opt="-o loop"
266         else
267                 _fail "Invalid \$TEST_DEV: $TEST_DEV"
268         fi
269
270         [[ -d "$TEST_MNT" ]] || {
271                 _fail "Invalid \$TEST_MNT: $TEST_MNT"
272         }
273
274         run_check $SUDO_HELPER mount $loop_opt "$@" "$TEST_DEV" "$TEST_MNT"
275 }
276
277 run_check_umount_test_dev()
278 {
279         setup_root_helper
280         run_check $SUDO_HELPER umount "$@" "$TEST_DEV"
281 }
282
283 init_env()
284 {
285         TEST_MNT="${TEST_MNT:-$TOP/tests/mnt}"
286         export TEST_MNT
287         mkdir -p "$TEST_MNT" || { echo "Failed mkdir -p $TEST_MNT"; exit 1; }
288 }
289 init_env