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