btrfs-progs: tests: don't treat segfault as ignorable error
[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                 fi
66                 return $ret
67         fi
68 }
69
70 # first argument is error message to print if it fails, otherwise
71 # same as run_check but expects the command to fail, output is logged
72 run_mustfail()
73 {
74         local msg
75
76         msg="$1"
77         shift
78
79         echo "############### $@" >> $RESULTS 2>&1
80         if [ "$TEST_LOG" = 'tty' ]; then echo "CMD(mustfail): $@" > /dev/tty; fi
81         if [ "$1" = 'root_helper' ]; then
82                 "$@" >> $RESULTS 2>&1
83         else
84                 $INSTRUMENT "$@" >> $RESULTS 2>&1
85         fi
86         if [ $? != 0 ]; then
87                 echo "failed (expected): $@" >> $RESULTS
88                 return 0
89         else
90                 echo "succeeded (unexpected!): $@" >> $RESULTS
91                 _fail "unexpected success: $msg"
92                 return 1
93         fi
94 }
95
96 check_prereq()
97 {
98         if ! [ -f $TOP/$1 ]; then
99                 _fail "Failed prerequisites: $1";
100         fi
101 }
102
103 check_global_prereq()
104 {
105         which $1 &> /dev/null
106         if [ $? -ne 0 ]; then
107                 _fail "Failed system wide prerequisities: $1";
108         fi
109 }
110
111 check_image()
112 {
113         local image
114
115         image=$1
116         echo "testing image $(basename $image)" >> $RESULTS
117         $TOP/btrfs check $image >> $RESULTS 2>&1
118         [ $? -eq 0 ] && _fail "btrfs check should have detected corruption"
119
120         run_check $TOP/btrfs check --repair $image
121         run_check $TOP/btrfs check $image
122 }
123
124 # Extract a usable image from packed formats
125 # - raw btrfs filesystem images, suffix .raw
126 # - dtto compressed by XZ, suffix .raw.xz
127 # - meta-dump images with suffix .img
128 # - dtto compressed by XZ, suffix .img.xz
129 extract_image()
130 {
131         local image
132         local cleanme
133
134         image="$1"
135         case "$image" in
136         *.img)
137                 rm -f $image.restored
138                 : ;;
139         *.img.xz)
140                 xz --decompress --keep "$image" || \
141                         _fail "failed to decompress image $image" >&2
142                 image=${image%%.xz}
143                 rm -f $image.restored
144                 cleanme=$image
145                 ;;
146         *.raw)
147                 cp --sparse=auto $image $image.restored
148                 ;;
149         *.raw.xz)
150                 xz --decompress --keep "$image" || \
151                         _fail "failed to decompress image $image" >&2
152                 image=${image%%.xz}
153                 mv "$image" "$image".restored
154                 ;;
155         esac
156
157         if ! [ -f $image.restored ]; then
158                 echo "restoring image $(basename $image)" >> $RESULTS
159                 $TOP/btrfs-image -r $image $image.restored \
160                         &>> $RESULTS \
161                         || _fail "failed to restore image $image" >&2
162         fi
163
164         [ -f "$cleanme" ] && rm -f "$cleanme"
165
166         echo "$image.restored"
167 }
168
169 # Process all image dumps in a given directory
170 check_all_images()
171 {
172         local dir
173         local extracted
174
175         dir="$1"
176         for image in $(find $dir \( -iname '*.img' -o   \
177                                 -iname '*.img.xz' -o    \
178                                 -iname '*.raw' -o       \
179                                 -iname '*.raw.xz' \) | sort)
180         do
181                 extracted=$(extract_image "$image")
182                 check_image "$extracted"
183                 rm -f "$extracted"
184         done
185 }
186
187 # some tests need to mount the recovered image and do verifications call
188 # 'setup_root_helper' and then check for have_root_helper == 1 if the test
189 # needs to fail otherwise; using sudo by default for now
190 SUDO_HELPER=
191 NEED_SUDO_VALIDATE=unknown
192 export SUDO_HELPER
193 export NEED_SUDO_VALIDATE
194 root_helper()
195 {
196         if [ $UID -eq 0 ]; then
197                 "$@"
198         else
199                 if [ "$NEED_SUDO_VALIDATE" = 'yes' ]; then
200                         sudo -v -n &>/dev/null || \
201                                 _not_run "Need to validate sudo credentials"
202                         sudo -n "$@"
203                 elif [ "$NEED_SUDO_VALIDATE" = 'no' ]; then
204                         sudo -n /bin/true &> /dev/null || \
205                                 _not_run "Need to validate sudo user settings"
206                         sudo -n "$@"
207                 else
208                         # should not happen
209                         _not_run "Need to validate root privileges"
210                 fi
211         fi
212 }
213
214 setup_root_helper()
215 {
216         if [ $UID -eq 0 -o -n "$SUDO_HELPER" ]; then
217                 return
218         fi
219
220         # Test for old sudo or special settings, which make sudo -v fail even
221         # if user setting is NOPASSWD
222         sudo -n /bin/true &>/dev/null && NEED_SUDO_VALIDATE=no
223
224         # Newer sudo or default sudo setting
225         sudo -v -n &>/dev/null && NEED_SUDO_VALIDATE=yes
226
227         if [ "$NEED_SUDO_VALIDATE" = 'unknown' ]; then
228                 _not_run "Need to validate root privileges"
229         fi
230         SUDO_HELPER=root_helper
231 }
232
233 prepare_test_dev()
234 {
235         # num[K/M/G/T...]
236         local size="$1"
237
238         [[ "$TEST_DEV" ]] && return
239         [[ "$size" ]] || size='2G'
240
241         echo "\$TEST_DEV not given, use $TOP/test/test.img as fallback" >> \
242                 $RESULTS
243         TEST_DEV="$TOP/tests/test.img"
244
245         truncate -s "$size" "$TEST_DEV" || _not_run "create file for loop device failed"
246 }
247
248 run_check_mount_test_dev()
249 {
250         setup_root_helper
251
252         local loop_opt
253         if [[ -b "$TEST_DEV" ]]; then
254                 loop_opt=""
255         elif [[ -f "$TEST_DEV" ]]; then
256                 loop_opt="-o loop"
257         else
258                 _fail "Invalid \$TEST_DEV: $TEST_DEV"
259         fi
260
261         [[ -d "$TEST_MNT" ]] || {
262                 _fail "Invalid \$TEST_MNT: $TEST_MNT"
263         }
264
265         run_check $SUDO_HELPER mount $loop_opt "$@" "$TEST_DEV" "$TEST_MNT"
266 }
267
268 run_check_umount_test_dev()
269 {
270         setup_root_helper
271         run_check $SUDO_HELPER umount "$@" "$TEST_DEV"
272 }
273
274 init_env()
275 {
276         TEST_MNT="${TEST_MNT:-$TOP/tests/mnt}"
277         export TEST_MNT
278         mkdir -p "$TEST_MNT" || { echo "Failed mkdir -p $TEST_MNT"; exit 1; }
279 }
280 init_env