264424d302fb3bb170814329cf54637257450e37
[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 extract_image()
132 {
133         local image
134         local cleanme
135
136         image="$1"
137         case "$image" in
138         *.img)
139                 rm -f $image.restored
140                 : ;;
141         *.img.xz)
142                 xz --decompress --keep "$image" || \
143                         _fail "failed to decompress image $image" >&2
144                 image=${image%%.xz}
145                 rm -f $image.restored
146                 cleanme=$image
147                 ;;
148         *.raw)
149                 cp --sparse=auto $image $image.restored
150                 ;;
151         *.raw.xz)
152                 xz --decompress --keep "$image" || \
153                         _fail "failed to decompress image $image" >&2
154                 image=${image%%.xz}
155                 mv "$image" "$image".restored
156                 ;;
157         esac
158
159         if ! [ -f $image.restored ]; then
160                 echo "restoring image $(basename $image)" >> $RESULTS
161                 $TOP/btrfs-image -r $image $image.restored \
162                         &>> $RESULTS \
163                         || _fail "failed to restore image $image" >&2
164         fi
165
166         [ -f "$cleanme" ] && rm -f "$cleanme"
167
168         echo "$image.restored"
169 }
170
171 # Process all image dumps in a given directory
172 check_all_images()
173 {
174         local dir
175         local extracted
176
177         dir="$1"
178         for image in $(find $dir \( -iname '*.img' -o   \
179                                 -iname '*.img.xz' -o    \
180                                 -iname '*.raw' -o       \
181                                 -iname '*.raw.xz' \) | sort)
182         do
183                 extracted=$(extract_image "$image")
184                 check_image "$extracted"
185                 rm -f "$extracted"
186         done
187 }
188
189 # some tests need to mount the recovered image and do verifications call
190 # 'setup_root_helper' and then check for have_root_helper == 1 if the test
191 # needs to fail otherwise; using sudo by default for now
192 SUDO_HELPER=
193 NEED_SUDO_VALIDATE=unknown
194 export SUDO_HELPER
195 export NEED_SUDO_VALIDATE
196 root_helper()
197 {
198         if [ $UID -eq 0 ]; then
199                 "$@"
200         else
201                 if [ "$NEED_SUDO_VALIDATE" = 'yes' ]; then
202                         sudo -v -n &>/dev/null || \
203                                 _not_run "Need to validate sudo credentials"
204                         sudo -n "$@"
205                 elif [ "$NEED_SUDO_VALIDATE" = 'no' ]; then
206                         sudo -n /bin/true &> /dev/null || \
207                                 _not_run "Need to validate sudo user settings"
208                         sudo -n "$@"
209                 else
210                         # should not happen
211                         _not_run "Need to validate root privileges"
212                 fi
213         fi
214 }
215
216 setup_root_helper()
217 {
218         if [ $UID -eq 0 -o -n "$SUDO_HELPER" ]; then
219                 return
220         fi
221
222         # Test for old sudo or special settings, which make sudo -v fail even
223         # if user setting is NOPASSWD
224         sudo -n /bin/true &>/dev/null && NEED_SUDO_VALIDATE=no
225
226         # Newer sudo or default sudo setting
227         sudo -v -n &>/dev/null && NEED_SUDO_VALIDATE=yes
228
229         if [ "$NEED_SUDO_VALIDATE" = 'unknown' ]; then
230                 _not_run "Need to validate root privileges"
231         fi
232         SUDO_HELPER=root_helper
233 }
234
235 prepare_test_dev()
236 {
237         # num[K/M/G/T...]
238         local size="$1"
239
240         [[ "$TEST_DEV" ]] && return
241         [[ "$size" ]] || size='2G'
242
243         echo "\$TEST_DEV not given, use $TOP/test/test.img as fallback" >> \
244                 $RESULTS
245         TEST_DEV="$TOP/tests/test.img"
246
247         truncate -s "$size" "$TEST_DEV" || _not_run "create file for loop device failed"
248 }
249
250 run_check_mount_test_dev()
251 {
252         setup_root_helper
253
254         local loop_opt
255         if [[ -b "$TEST_DEV" ]]; then
256                 loop_opt=""
257         elif [[ -f "$TEST_DEV" ]]; then
258                 loop_opt="-o loop"
259         else
260                 _fail "Invalid \$TEST_DEV: $TEST_DEV"
261         fi
262
263         [[ -d "$TEST_MNT" ]] || {
264                 _fail "Invalid \$TEST_MNT: $TEST_MNT"
265         }
266
267         run_check $SUDO_HELPER mount $loop_opt "$@" "$TEST_DEV" "$TEST_MNT"
268 }
269
270 run_check_umount_test_dev()
271 {
272         setup_root_helper
273         run_check $SUDO_HELPER umount "$@" "$TEST_DEV"
274 }
275
276 init_env()
277 {
278         TEST_MNT="${TEST_MNT:-$TOP/tests/mnt}"
279         export TEST_MNT
280         mkdir -p "$TEST_MNT" || { echo "Failed mkdir -p $TEST_MNT"; exit 1; }
281 }
282 init_env