]>
Commit | Line | Data |
---|---|---|
92f2c23e KZ |
1 | # |
2 | # Copyright (C) 2007 Karel Zak <kzak@redhat.com> | |
3 | # | |
601d12fb | 4 | # This file is part of util-linux. |
92f2c23e KZ |
5 | # |
6 | # This file is free software; you can redistribute it and/or modify | |
7 | # it under the terms of the GNU General Public License as published by | |
8 | # the Free Software Foundation; either version 2 of the License, or | |
9 | # (at your option) any later version. | |
10 | # | |
11 | # This file is distributed in the hope that it will be useful, | |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | # GNU General Public License for more details. | |
15 | # | |
e83446da | 16 | |
e83446da | 17 | |
613a337e RM |
18 | # Global array to remember all issued lock FDs. It does not seem possible to |
19 | # declare this within a function (ts_init_env). | |
20 | declare -A TS_LOCKFILE_FD | |
21 | ||
22 | ||
a02f320d KZ |
23 | function ts_abspath { |
24 | cd $1 | |
25 | pwd | |
26 | } | |
d42bbae5 | 27 | |
6dfe6f0e KZ |
28 | function ts_canonicalize { |
29 | P="$1" | |
30 | C=$(readlink -f $P) | |
31 | ||
32 | if [ -n "$C" ]; then | |
33 | echo "$C" | |
34 | else | |
35 | echo "$P" | |
36 | fi | |
37 | } | |
38 | ||
723c7043 SK |
39 | function ts_cd { |
40 | if [ $# -eq 0 ]; then | |
41 | ts_failed "ul_cd: not enough arguments" | |
42 | fi | |
43 | DEST=$(readlink -f "$1" 2>/dev/null) | |
44 | if [ "x$DEST" = "x" ] || [ ! -d "$DEST" ]; then | |
45 | ts_failed "ul_cd: $1: no such directory" | |
46 | fi | |
47 | cd "$DEST" 2>/dev/null || ts_failed "ul_cd: $1: cannot change directory" | |
48 | if [ "$PWD" != "$DEST" ]; then | |
49 | ts_failed "ul_cd: $PWD is not $DEST" | |
50 | fi | |
51 | } | |
52 | ||
f0c60dff KZ |
53 | function ts_separator { |
54 | local header="$1" | |
55 | echo >> $TS_OUTPUT | |
56 | if [ -z "$header" ]; then | |
57 | echo "============================================" >> $TS_OUTPUT | |
58 | else | |
59 | echo "=====$header================================" >> $TS_OUTPUT | |
60 | fi | |
61 | } | |
62 | ||
40e6f7a0 | 63 | function ts_report { |
2979724f RM |
64 | local desc= |
65 | ||
855f7f06 | 66 | if [ "$TS_PARSABLE" != "yes" ]; then |
2979724f RM |
67 | if [ $TS_NSUBTESTS -ne 0 ] && [ -z "$TS_SUBNAME" ]; then |
68 | desc=$(printf "%11s...") | |
69 | fi | |
70 | echo "$desc$1" | |
71 | return | |
72 | fi | |
73 | ||
74 | if [ -n "$TS_SUBNAME" ]; then | |
75 | desc=$(printf "%s: [%02d] %s" "$TS_DESC" "$TS_NSUBTESTS" "$TS_SUBNAME") | |
40e6f7a0 | 76 | else |
2979724f | 77 | desc=$TS_DESC |
40e6f7a0 | 78 | fi |
2979724f | 79 | printf "%13s: %-45s ...%s\n" "$TS_COMPONENT" "$desc" "$1" |
2f791546 | 80 | } |
40e6f7a0 | 81 | |
2f791546 SK |
82 | function ts_check_test_command { |
83 | if [ ! -x "$1" ]; then | |
84 | ts_skip "${1##*/} not found" | |
85 | fi | |
40e6f7a0 SK |
86 | } |
87 | ||
d1962aae RM |
88 | function ts_check_prog { |
89 | local cmd=$1 | |
90 | type "$cmd" >/dev/null 2>&1 || ts_skip "missing in PATH: $cmd" | |
91 | } | |
92 | ||
5ec15aef RM |
93 | function ts_check_losetup { |
94 | local tmp | |
95 | ts_check_test_command "$TS_CMD_LOSETUP" | |
96 | ||
d995c2f0 | 97 | if [ "$TS_SKIP_LOOPDEVS" = "yes" ]; then |
5f708381 RM |
98 | ts_skip "loop-device tests disabled" |
99 | fi | |
100 | ||
5ec15aef RM |
101 | # assuming that losetup -f works ... to be checked somewhere else |
102 | tmp=$($TS_CMD_LOSETUP -f 2>/dev/null) | |
103 | if test -b "$tmp"; then | |
104 | return 0 | |
105 | fi | |
0209a128 | 106 | ts_skip "no loop-device support" |
5ec15aef RM |
107 | } |
108 | ||
94fa9b46 | 109 | function ts_report_skip { |
0209a128 | 110 | ts_report " SKIPPED ($1)" |
09888efe KZ |
111 | } |
112 | ||
113 | function ts_skip { | |
94fa9b46 | 114 | ts_report_skip "$1" |
cbae7931 | 115 | |
caf31605 | 116 | ts_cleanup_on_exit |
b30cd7ee KZ |
117 | exit 0 |
118 | } | |
119 | ||
09892fb6 | 120 | function ts_skip_nonroot { |
f0b561b6 | 121 | if [ $UID -ne 0 ]; then |
0209a128 | 122 | ts_skip "no root permissions" |
09892fb6 KZ |
123 | fi |
124 | } | |
125 | ||
09888efe | 126 | function ts_failed_subtest { |
733094a8 RM |
127 | local msg="FAILED" |
128 | local ret=1 | |
129 | if [ "$TS_KNOWN_FAIL" = "yes" ]; then | |
130 | msg="KNOWN FAILED" | |
131 | ret=0 | |
132 | fi | |
133 | ||
05de8126 | 134 | if [ x"$1" == x"" ]; then |
733094a8 | 135 | ts_report " $msg ($TS_NS)" |
05de8126 | 136 | else |
733094a8 | 137 | ts_report " $msg ($1)" |
05de8126 | 138 | fi |
733094a8 RM |
139 | |
140 | return $ret | |
05de8126 KZ |
141 | } |
142 | ||
09888efe KZ |
143 | function ts_failed { |
144 | ts_failed_subtest "$1" | |
733094a8 | 145 | exit $? |
7641ccec RM |
146 | } |
147 | ||
94fa9b46 | 148 | function ts_report_ok { |
05de8126 | 149 | if [ x"$1" == x"" ]; then |
40e6f7a0 | 150 | ts_report " OK" |
05de8126 | 151 | else |
40e6f7a0 | 152 | ts_report " OK ($1)" |
05de8126 | 153 | fi |
09888efe KZ |
154 | } |
155 | ||
156 | function ts_ok { | |
94fa9b46 | 157 | ts_report_ok "$1" |
05de8126 KZ |
158 | exit 0 |
159 | } | |
160 | ||
57a917d6 KZ |
161 | function ts_log { |
162 | echo "$1" >> $TS_OUTPUT | |
163 | [ "$TS_VERBOSE" == "yes" ] && echo "$1" | |
164 | } | |
165 | ||
1d9acab1 KZ |
166 | function ts_has_option { |
167 | NAME="$1" | |
168 | ALL="$2" | |
eac40eb0 RM |
169 | |
170 | # user may set options by env for a single test or whole component | |
171 | # e.g. TS_OPT_ipcs_limits2_fake="yes" or TS_OPT_ipcs_fake="yes" | |
c08863ff RM |
172 | local v_test=${TS_TESTNAME//[-.]/_} |
173 | local v_comp=${TS_COMPONENT//[-.]/_} | |
174 | local v_name=${NAME//[-.]/_} | |
175 | eval local env_opt_test=\$TS_OPT_${v_comp}_${v_test}_${v_name} | |
176 | eval local env_opt_comp=\$TS_OPT_${v_comp}_${v_name} | |
eac40eb0 RM |
177 | if [ "$env_opt_test" = "yes" \ |
178 | -o "$env_opt_comp" = "yes" -a "$env_opt_test" != "no" ]; then | |
179 | echo "yes" | |
180 | return | |
181 | elif [ "$env_opt_test" = "no" \ | |
182 | -o "$env_opt_comp" = "no" -a "$env_opt_test" != "yes" ]; then | |
183 | return | |
184 | fi | |
185 | ||
186 | # or just check the global command line options | |
855f7f06 RM |
187 | if [[ $ALL =~ ([$' \t\n']|^)--$NAME([$'= \t\n']|$) ]]; then |
188 | echo yes | |
189 | return | |
190 | fi | |
191 | ||
192 | # or the _global_ env, e.g TS_OPT_parsable="yes" | |
193 | eval local env_opt=\$TS_OPT_${v_name} | |
194 | if [ "$env_opt" = "yes" ]; then echo "yes"; fi | |
1d9acab1 KZ |
195 | } |
196 | ||
1b03e2cd KZ |
197 | function ts_option_argument { |
198 | NAME="$1" | |
199 | ALL="$2" | |
4303124a RM |
200 | |
201 | # last option wins! | |
202 | echo "$ALL" | sed -n "s/.*[ \t\n]--$NAME=\([^ \t\n]*\).*/\1/p" | tail -n 1 | |
1b03e2cd KZ |
203 | } |
204 | ||
db17c74b | 205 | function ts_init_core_env { |
2979724f | 206 | TS_SUBNAME="" |
db17c74b KZ |
207 | TS_NS="$TS_COMPONENT/$TS_TESTNAME" |
208 | TS_OUTPUT="$TS_OUTDIR/$TS_TESTNAME" | |
495674f8 | 209 | TS_VGDUMP="$TS_OUTDIR/$TS_TESTNAME.vgdump" |
db17c74b KZ |
210 | TS_DIFF="$TS_DIFFDIR/$TS_TESTNAME" |
211 | TS_EXPECTED="$TS_TOPDIR/expected/$TS_NS" | |
212 | TS_MOUNTPOINT="$TS_OUTDIR/${TS_TESTNAME}-mnt" | |
213 | } | |
214 | ||
215 | function ts_init_core_subtest_env { | |
216 | TS_NS="$TS_COMPONENT/$TS_TESTNAME-$TS_SUBNAME" | |
217 | TS_OUTPUT="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME" | |
495674f8 | 218 | TS_VGDUMP="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME.vgdump" |
db17c74b KZ |
219 | TS_DIFF="$TS_DIFFDIR/$TS_TESTNAME-$TS_SUBNAME" |
220 | TS_EXPECTED="$TS_TOPDIR/expected/$TS_NS" | |
64a2331f | 221 | TS_MOUNTPOINT="$TS_OUTDIR/${TS_TESTNAME}-${TS_SUBNAME}-mnt" |
495674f8 KZ |
222 | |
223 | rm -f $TS_OUTPUT $TS_VGDUMP | |
4210c47a KZ |
224 | [ -d "$TS_OUTDIR" ] || mkdir -p "$TS_OUTDIR" |
225 | ||
495674f8 KZ |
226 | touch $TS_OUTPUT |
227 | [ -n "$TS_VALGRIND_CMD" ] && touch $TS_VGDUMP | |
db17c74b KZ |
228 | } |
229 | ||
905d0b9b | 230 | function ts_init_env { |
e130ce53 | 231 | local mydir=$(ts_abspath ${0%/*}) |
d9a9ff09 | 232 | local tmp |
d42bbae5 | 233 | |
08b825db YD |
234 | LANG="POSIX" |
235 | LANGUAGE="POSIX" | |
236 | LC_ALL="POSIX" | |
237 | CHARSET="UTF-8" | |
b97cc9a8 | 238 | ASAN_OPTIONS="detect_leaks=0" |
08b825db | 239 | |
b97cc9a8 | 240 | export LANG LANGUAGE LC_ALL CHARSET ASAN_OPTIONS |
1b03e2cd | 241 | |
6dfe6f0e KZ |
242 | mydir=$(ts_canonicalize "$mydir") |
243 | ||
1b03e2cd KZ |
244 | # automake directories |
245 | top_srcdir=$(ts_option_argument "srcdir" "$*") | |
246 | top_builddir=$(ts_option_argument "builddir" "$*") | |
d42bbae5 | 247 | |
1b03e2cd | 248 | # where is this script |
a02f320d | 249 | TS_TOPDIR=$(ts_abspath $mydir/../../) |
1b03e2cd KZ |
250 | |
251 | # default | |
252 | if [ -z "$top_srcdir" ]; then | |
253 | top_srcdir="$TS_TOPDIR/.." | |
254 | fi | |
255 | if [ -z "$top_builddir" ]; then | |
256 | top_builddir="$TS_TOPDIR/.." | |
257 | fi | |
258 | ||
259 | top_srcdir=$(ts_abspath $top_srcdir) | |
260 | top_builddir=$(ts_abspath $top_builddir) | |
261 | ||
4ba0bfbb RM |
262 | # some ul commands search other ul commands in $PATH |
263 | export PATH="$top_builddir:$PATH" | |
264 | ||
a02f320d | 265 | TS_SCRIPT="$mydir/$(basename $0)" |
d42bbae5 KZ |
266 | TS_SUBDIR=$(dirname $TS_SCRIPT) |
267 | TS_TESTNAME=$(basename $TS_SCRIPT) | |
268 | TS_COMPONENT=$(basename $TS_SUBDIR) | |
98604ef9 | 269 | TS_DESC=${TS_DESC:-$TS_TESTNAME} |
d42bbae5 | 270 | |
09888efe KZ |
271 | TS_NSUBTESTS=0 |
272 | TS_NSUBFAILED=0 | |
273 | ||
d42bbae5 KZ |
274 | TS_SELF="$TS_SUBDIR" |
275 | ||
1b03e2cd KZ |
276 | TS_OUTDIR="$top_builddir/tests/output/$TS_COMPONENT" |
277 | TS_DIFFDIR="$top_builddir/tests/diff/$TS_COMPONENT" | |
db17c74b | 278 | |
9deb8c7d KZ |
279 | TS_NOLOCKS=$(ts_has_option "nolocks" "$*") |
280 | TS_LOCKDIR="$top_builddir/tests/output" | |
281 | ||
7acd4b46 KZ |
282 | # Don't lock if flock(1) is missing |
283 | type "flock" >/dev/null 2>&1 || TS_NOLOCKS="yes" | |
284 | ||
db17c74b | 285 | ts_init_core_env |
d42bbae5 | 286 | |
cbac71bd | 287 | TS_VERBOSE=$(ts_has_option "verbose" "$*") |
74fe554a | 288 | TS_SHOWDIFF=$(ts_has_option "show-diff" "$*") |
40e6f7a0 | 289 | TS_PARALLEL=$(ts_has_option "parallel" "$*") |
7641ccec | 290 | TS_KNOWN_FAIL=$(ts_has_option "known-fail" "$*") |
d995c2f0 | 291 | TS_SKIP_LOOPDEVS=$(ts_has_option "skip-loopdevs" "$*") |
855f7f06 RM |
292 | TS_PARSABLE=$(ts_has_option "parsable" "$*") |
293 | [ "$TS_PARSABLE" = "yes" ] || TS_PARSABLE="$TS_PARALLEL" | |
d42bbae5 | 294 | |
d3f58f8a | 295 | tmp=$( ts_has_option "memcheck-valgrind" "$*") |
d9a9ff09 RM |
296 | if [ "$tmp" == "yes" -a -f /usr/bin/valgrind ]; then |
297 | TS_VALGRIND_CMD="/usr/bin/valgrind" | |
298 | fi | |
b97cc9a8 KZ |
299 | tmp=$( ts_has_option "memcheck-asan" "$*") |
300 | if [ "$tmp" == "yes" ]; then | |
301 | TS_ENABLE_ASAN="yes" | |
302 | fi | |
d9a9ff09 | 303 | |
632830cc | 304 | BLKID_FILE="$TS_OUTDIR/${TS_TESTNAME}.blkidtab" |
d42bbae5 | 305 | |
1b5417ac KZ |
306 | declare -a TS_SUID_PROGS |
307 | declare -a TS_SUID_USER | |
308 | declare -a TS_SUID_GROUP | |
cbae7931 | 309 | declare -a TS_LOOP_DEVS |
1b5417ac | 310 | |
adc8c80f KZ |
311 | if [ -f $TS_TOPDIR/commands.sh ]; then |
312 | . $TS_TOPDIR/commands.sh | |
313 | fi | |
a02f320d | 314 | |
b7ea07e0 | 315 | export BLKID_FILE |
66822df3 | 316 | |
495674f8 | 317 | rm -f $TS_OUTPUT $TS_VGDUMP |
4210c47a KZ |
318 | [ -d "$TS_OUTDIR" ] || mkdir -p "$TS_OUTDIR" |
319 | ||
3dfa278e | 320 | touch $TS_OUTPUT |
495674f8 | 321 | [ -n "$TS_VALGRIND_CMD" ] && touch $TS_VGDUMP |
97cdb3cb | 322 | |
d42bbae5 KZ |
323 | if [ "$TS_VERBOSE" == "yes" ]; then |
324 | echo | |
325 | echo " script: $TS_SCRIPT" | |
d42bbae5 | 326 | echo " sub dir: $TS_SUBDIR" |
a02f320d | 327 | echo " top dir: $TS_TOPDIR" |
d42bbae5 KZ |
328 | echo " self: $TS_SELF" |
329 | echo " test name: $TS_TESTNAME" | |
330 | echo " test desc: $TS_DESC" | |
331 | echo " component: $TS_COMPONENT" | |
332 | echo " namespace: $TS_NS" | |
333 | echo " verbose: $TS_VERBOSE" | |
334 | echo " output: $TS_OUTPUT" | |
495674f8 | 335 | echo " valgrind: $TS_VGDUMP" |
d42bbae5 KZ |
336 | echo " expected: $TS_EXPECTED" |
337 | echo " mountpoint: $TS_MOUNTPOINT" | |
338 | echo | |
339 | fi | |
905d0b9b KZ |
340 | } |
341 | ||
09888efe KZ |
342 | function ts_init_subtest { |
343 | ||
344 | TS_SUBNAME="$1" | |
db17c74b | 345 | ts_init_core_subtest_env |
09888efe KZ |
346 | TS_NSUBTESTS=$(( $TS_NSUBTESTS + 1 )) |
347 | ||
855f7f06 | 348 | if [ "$TS_PARSABLE" != "yes" ]; then |
2979724f RM |
349 | [ $TS_NSUBTESTS -eq 1 ] && echo |
350 | printf "%16s: %-27s ..." "" "$TS_SUBNAME" | |
40e6f7a0 | 351 | fi |
09888efe KZ |
352 | } |
353 | ||
905d0b9b | 354 | function ts_init { |
d9a9ff09 RM |
355 | ts_init_env "$*" |
356 | ||
905d0b9b | 357 | local is_fake=$( ts_has_option "fake" "$*") |
949cf64b | 358 | local is_force=$( ts_has_option "force" "$*") |
905d0b9b | 359 | |
855f7f06 | 360 | if [ "$TS_PARSABLE" != "yes" ]; then |
2979724f | 361 | printf "%13s: %-30s ..." "$TS_COMPONENT" "$TS_DESC" |
40e6f7a0 | 362 | fi |
d42bbae5 KZ |
363 | |
364 | [ "$is_fake" == "yes" ] && ts_skip "fake mode" | |
949cf64b | 365 | [ "$TS_OPTIONAL" == "yes" -a "$is_force" != "yes" ] && ts_skip "optional" |
e83446da KZ |
366 | } |
367 | ||
1b5417ac KZ |
368 | function ts_init_suid { |
369 | PROG="$1" | |
370 | ct=${#TS_SUID_PROGS[*]} | |
371 | ||
372 | # Save info about original setting | |
373 | TS_SUID_PROGS[$ct]=$PROG | |
374 | TS_SUID_USER[$ct]=$(stat --printf="%U" $PROG) | |
375 | TS_SUID_GROUP[$ct]=$(stat --printf="%G" $PROG) | |
376 | ||
377 | chown root.root $PROG &> /dev/null | |
378 | chmod u+s $PROG &> /dev/null | |
379 | } | |
380 | ||
a731b541 KZ |
381 | function ts_init_py { |
382 | LIBNAME="$1" | |
383 | ||
15b2606c | 384 | [ -f "$top_builddir/py${LIBNAME}.la" ] || ts_skip "py${LIBNAME} not compiled" |
a731b541 | 385 | |
fa8f8b74 RM |
386 | export LD_LIBRARY_PATH="$top_builddir/.libs:$LD_LIBRARY_PATH" |
387 | export PYTHONPATH="$top_builddir/$LIBNAME/python:$top_builddir/.libs:$PYTHONPATH" | |
46407453 OO |
388 | |
389 | export PYTHON_VERSION=$(awk '/^PYTHON_VERSION/ { print $3 }' $top_builddir/Makefile) | |
390 | export PYTHON_MAJOR_VERSION=$(echo $PYTHON_VERSION | sed 's/\..*//') | |
391 | ||
392 | export PYTHON="python${PYTHON_MAJOR_VERSION}" | |
a731b541 KZ |
393 | } |
394 | ||
02ae505f | 395 | function ts_run { |
d3f58f8a KZ |
396 | # |
397 | # valgrind mode | |
398 | # | |
399 | if [ -n "$TS_VALGRIND_CMD" ]; then | |
495674f8 KZ |
400 | $TS_VALGRIND_CMD --tool=memcheck --leak-check=full \ |
401 | --leak-resolution=high --num-callers=20 \ | |
88a5f90e | 402 | --log-file="$TS_VGDUMP" "$@" |
b97cc9a8 KZ |
403 | # |
404 | # ASAN mode | |
405 | # | |
406 | elif [ "$TS_ENABLE_ASAN" == "yes" ]; then | |
407 | ASAN_OPTIONS='detect_leaks=1' "$@" | |
d3f58f8a KZ |
408 | |
409 | # | |
410 | # Default mode | |
411 | # | |
412 | else | |
413 | "$@" | |
495674f8 KZ |
414 | fi |
415 | } | |
416 | ||
09888efe KZ |
417 | function ts_gen_diff { |
418 | local res=0 | |
419 | ||
7e604f3c RM |
420 | [ -f "$TS_OUTPUT" ] || return 1 |
421 | [ -f "$TS_EXPECTED" ] || TS_EXPECTED=/dev/null | |
4210c47a | 422 | |
7e604f3c RM |
423 | # remove libtool lt- prefixes |
424 | sed --in-place 's/^lt\-\(.*\: \)/\1/g' $TS_OUTPUT | |
46949388 | 425 | |
7e604f3c RM |
426 | [ -d "$TS_DIFFDIR" ] || mkdir -p "$TS_DIFFDIR" |
427 | diff -u $TS_EXPECTED $TS_OUTPUT > $TS_DIFF | |
4210c47a | 428 | |
7e604f3c | 429 | if [ $? -ne 0 ] || [ -s $TS_DIFF ]; then |
09888efe | 430 | res=1 |
74fe554a KZ |
431 | if [ "$TS_SHOWDIFF" == "yes" ]; then |
432 | echo | |
433 | echo "diff-{{{" | |
434 | cat $TS_DIFF | |
435 | echo "}}}-diff" | |
436 | echo | |
437 | fi | |
7e604f3c RM |
438 | else |
439 | rm -f $TS_DIFF; | |
09888efe | 440 | fi |
7e604f3c | 441 | |
09888efe KZ |
442 | return $res |
443 | } | |
444 | ||
495674f8 | 445 | function tt_gen_mem_report { |
d3f58f8a KZ |
446 | if [ -n "$TS_VALGRIND_CMD" ]; then |
447 | grep -q -E 'ERROR SUMMARY: [1-9]' $TS_VGDUMP &> /dev/null | |
448 | if [ $? -eq 0 ]; then | |
449 | echo "mem-error detected!" | |
450 | fi | |
451 | else | |
452 | echo "$1" | |
495674f8 KZ |
453 | fi |
454 | } | |
455 | ||
09888efe | 456 | function ts_finalize_subtest { |
e83446da KZ |
457 | local res=0 |
458 | ||
7e604f3c RM |
459 | ts_gen_diff |
460 | if [ $? -eq 1 ]; then | |
461 | ts_failed_subtest "$1" | |
462 | res=1 | |
09888efe | 463 | else |
94fa9b46 | 464 | ts_report_ok "$(tt_gen_mem_report "$1")" |
09888efe KZ |
465 | fi |
466 | ||
467 | [ $res -ne 0 ] && TS_NSUBFAILED=$(( $TS_NSUBFAILED + 1 )) | |
db17c74b KZ |
468 | |
469 | # reset environment back to parental test | |
470 | ts_init_core_env | |
471 | ||
09888efe KZ |
472 | return $res |
473 | } | |
474 | ||
94fa9b46 KZ |
475 | function ts_skip_subtest { |
476 | ts_report_skip "$1" | |
477 | # reset environment back to parental test | |
478 | ts_init_core_env | |
479 | ||
480 | } | |
481 | ||
09888efe | 482 | function ts_finalize { |
caf31605 | 483 | ts_cleanup_on_exit |
1b5417ac | 484 | |
09888efe | 485 | if [ $TS_NSUBTESTS -ne 0 ]; then |
7e604f3c | 486 | if ! ts_gen_diff || [ $TS_NSUBFAILED -ne 0 ]; then |
09888efe KZ |
487 | ts_failed "$TS_NSUBFAILED from $TS_NSUBTESTS sub-tests" |
488 | else | |
489 | ts_ok "all $TS_NSUBTESTS sub-tests PASSED" | |
490 | fi | |
e83446da | 491 | fi |
425ca40a | 492 | |
7e604f3c RM |
493 | ts_gen_diff || ts_failed "$1" |
494 | ts_ok "$1" | |
e83446da KZ |
495 | } |
496 | ||
3dfa278e | 497 | function ts_die { |
57a917d6 | 498 | ts_log "$1" |
3dfa278e KZ |
499 | ts_finalize |
500 | } | |
501 | ||
caf31605 RM |
502 | function ts_cleanup_on_exit { |
503 | ||
504 | for idx in $(seq 0 $((${#TS_SUID_PROGS[*]} - 1))); do | |
505 | PROG=${TS_SUID_PROGS[$idx]} | |
506 | chmod a-s $PROG &> /dev/null | |
507 | chown ${TS_SUID_USER[$idx]}.${TS_SUID_GROUP[$idx]} $PROG &> /dev/null | |
508 | done | |
cbae7931 RM |
509 | |
510 | for dev in "${TS_LOOP_DEVS[@]}"; do | |
511 | ts_device_deinit "$dev" | |
512 | done | |
513 | unset TS_LOOP_DEVS | |
5c711ba9 RM |
514 | |
515 | ts_scsi_debug_rmmod | |
caf31605 RM |
516 | } |
517 | ||
35c636e1 KZ |
518 | function ts_image_md5sum { |
519 | local img=${1:-"$TS_OUTDIR/${TS_TESTNAME}.img"} | |
c3f323cb | 520 | echo $("$TS_HELPER_MD5" < "$img") $(basename "$img") |
35c636e1 | 521 | } |
05de8126 | 522 | |
35c636e1 KZ |
523 | function ts_image_init { |
524 | local mib=${1:-"5"} # size in MiBs | |
525 | local img=${2:-"$TS_OUTDIR/${TS_TESTNAME}.img"} | |
2f791546 | 526 | |
1cb10736 RM |
527 | rm -f $img |
528 | truncate -s "${mib}M" "$img" | |
35c636e1 KZ |
529 | echo "$img" |
530 | return 0 | |
531 | } | |
05de8126 | 532 | |
cbae7931 RM |
533 | function ts_register_loop_device { |
534 | local ct=${#TS_LOOP_DEVS[*]} | |
535 | TS_LOOP_DEVS[$ct]=$1 | |
536 | } | |
537 | ||
35c636e1 | 538 | function ts_device_init { |
b5eb5097 RM |
539 | local img |
540 | local dev | |
df7e52d7 | 541 | |
b5eb5097 RM |
542 | img=$(ts_image_init $1 $2) |
543 | dev=$($TS_CMD_LOSETUP --show -f "$img") | |
cbae7931 RM |
544 | if [ "$?" != "0" -o "$dev" = "" ]; then |
545 | ts_die "Cannot init device" | |
546 | fi | |
df7e52d7 | 547 | |
cbae7931 RM |
548 | ts_register_loop_device "$dev" |
549 | TS_LODEV=$dev | |
df7e52d7 KZ |
550 | } |
551 | ||
cbae7931 | 552 | # call from ts_cleanup_on_exit() only because of TS_LOOP_DEVS maintenance |
df7e52d7 | 553 | function ts_device_deinit { |
3dfa278e KZ |
554 | local DEV="$1" |
555 | ||
556 | if [ -b "$DEV" ]; then | |
557 | $TS_CMD_UMOUNT "$DEV" &> /dev/null | |
558 | $TS_CMD_LOSETUP -d "$DEV" &> /dev/null | |
df7e52d7 KZ |
559 | fi |
560 | } | |
064b8c38 | 561 | |
3f5bda01 | 562 | function ts_uuid_by_devname { |
283a8c15 | 563 | echo $($TS_CMD_BLKID -p -s UUID -o value $1) |
3f5bda01 KZ |
564 | } |
565 | ||
566 | function ts_label_by_devname { | |
283a8c15 | 567 | echo $($TS_CMD_BLKID -p -s LABEL -o value $1) |
3f5bda01 KZ |
568 | } |
569 | ||
570 | function ts_fstype_by_devname { | |
283a8c15 | 571 | echo $($TS_CMD_BLKID -p -s TYPE -o value $1) |
3f5bda01 KZ |
572 | } |
573 | ||
574 | function ts_device_has { | |
575 | local TAG="$1" | |
576 | local VAL="$2" | |
577 | local DEV="$3" | |
3dfa278e | 578 | local vl="" |
3f5bda01 KZ |
579 | |
580 | case $TAG in | |
581 | "TYPE") vl=$(ts_fstype_by_devname $DEV);; | |
582 | "LABEL") vl=$(ts_label_by_devname $DEV);; | |
583 | "UUID") vl=$(ts_uuid_by_devname $DEV);; | |
584 | *) return 1;; | |
585 | esac | |
586 | ||
587 | if [ "$vl" == "$VAL" ]; then | |
588 | return 0 | |
589 | fi | |
590 | return 1 | |
591 | } | |
3dfa278e KZ |
592 | |
593 | function ts_device_has_uuid { | |
594 | ts_uuid_by_devname "$1" | egrep -q '^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$' | |
595 | return $? | |
596 | } | |
597 | ||
2f954947 RM |
598 | function ts_mount { |
599 | local out | |
600 | local result | |
601 | local msg | |
a23c435e | 602 | local fs |
d2cc2ba7 RM |
603 | local fs_exp=$1 |
604 | shift | |
2f954947 RM |
605 | |
606 | out=$($TS_CMD_MOUNT "$@" 2>&1) | |
607 | result=$? | |
608 | echo -n "$out" >> $TS_OUTPUT | |
609 | ||
a23c435e RM |
610 | if [ $result != 0 ] \ |
611 | && msg=$(echo "$out" | grep -m1 "unknown filesystem type") | |
612 | then | |
613 | # skip only if reported fs correctly and if it's not available | |
614 | fs=$(echo "$msg" | sed -n "s/.*type '\(.*\)'$/\1/p") | |
d2cc2ba7 | 615 | [ "$fs" = "fs_exp" ] \ |
a23c435e RM |
616 | && grep -qe "[[:space:]]${fs}$" /proc/filesystems &>/dev/null \ |
617 | || ts_skip "$msg" | |
2f954947 RM |
618 | fi |
619 | return $result | |
620 | } | |
621 | ||
3ff2557c | 622 | function ts_is_mounted { |
6dfe6f0e | 623 | local DEV=$(ts_canonicalize "$1") |
3ff2557c KZ |
624 | |
625 | grep -q $DEV /proc/mounts && return 0 | |
626 | ||
627 | if [ "${DEV#/dev/loop/}" != "$DEV" ]; then | |
866372c6 | 628 | grep -q "/dev/loop${DEV#/dev/loop/}" /proc/mounts && return 0 |
3ff2557c KZ |
629 | fi |
630 | return 1 | |
631 | } | |
632 | ||
c98825ac | 633 | function ts_fstab_open { |
601d12fb | 634 | echo "# <!-- util-linux test entry" >> /etc/fstab |
c98825ac KZ |
635 | } |
636 | ||
637 | function ts_fstab_close { | |
638 | echo "# -->" >> /etc/fstab | |
20432749 | 639 | sync /etc/fstab 2>/dev/null |
c98825ac KZ |
640 | } |
641 | ||
642 | function ts_fstab_addline { | |
643 | local SPEC="$1" | |
644 | local MNT=${2:-"$TS_MOUNTPOINT"} | |
645 | local FS=${3:-"auto"} | |
b002d021 | 646 | local OPT=${4:-"defaults"} |
c98825ac | 647 | |
b002d021 | 648 | echo "$SPEC $MNT $FS $OPT 0 0" >> /etc/fstab |
c98825ac KZ |
649 | } |
650 | ||
e140506a KZ |
651 | function ts_fstab_lock { |
652 | ts_lock "fstab" | |
653 | } | |
654 | ||
c98825ac | 655 | function ts_fstab_add { |
e140506a | 656 | ts_fstab_lock |
c98825ac | 657 | ts_fstab_open |
b002d021 | 658 | ts_fstab_addline $* |
c98825ac KZ |
659 | ts_fstab_close |
660 | } | |
661 | ||
662 | function ts_fstab_clean { | |
663 | sed --in-place " | |
601d12fb | 664 | /# <!-- util-linux/!b |
c98825ac KZ |
665 | :a |
666 | /# -->/!{ | |
667 | N | |
668 | ba | |
669 | } | |
601d12fb | 670 | s/# <!-- util-linux.*-->//; |
c98825ac | 671 | /^$/d" /etc/fstab |
e140506a | 672 | |
20432749 | 673 | sync /etc/fstab 2>/dev/null |
e140506a | 674 | ts_unlock "fstab" |
c98825ac KZ |
675 | } |
676 | ||
d0bcd9b3 | 677 | function ts_fdisk_clean { |
e1fe1815 | 678 | local DEVNAME=$1 |
618ec053 | 679 | |
d0bcd9b3 | 680 | # remove non comparable parts of fdisk output |
23d8c556 RM |
681 | if [ -n "${DEVNAME}" ]; then |
682 | sed -i -e "s@${DEVNAME}@<removed>@;" $TS_OUTPUT | |
683 | fi | |
684 | ||
685 | sed -i \ | |
686 | -e 's/Disk identifier:.*/Disk identifier: <removed>/' \ | |
687 | -e 's/Created a new.*/Created a new <removed>./' \ | |
688 | -e 's/^Device[[:blank:]]*Start/Device Start/' \ | |
689 | -e 's/^Device[[:blank:]]*Boot/Device Boot/' \ | |
690 | -e 's/Welcome to fdisk.*/Welcome to fdisk <removed>./' \ | |
691 | -e 's/typescript file.*/typescript file <removed>./' \ | |
01e8c90c | 692 | -e 's@^\(I/O size (minimum/op.* bytes /\) [1-9][0-9]* @\1 <removed> @' \ |
23d8c556 | 693 | $TS_OUTPUT |
d0bcd9b3 | 694 | } |
618ec053 | 695 | |
9deb8c7d | 696 | |
613a337e RM |
697 | # https://stackoverflow.com/questions/41603787/how-to-find-next-available-file-descriptor-in-bash |
698 | function ts_find_free_fd() | |
699 | { | |
700 | local rco | |
701 | local rci | |
702 | for fd in {3..200}; do | |
703 | rco="$(true 2>/dev/null >&${fd}; echo $?)" | |
704 | rci="$(true 2>/dev/null <&${fd}; echo $?)" | |
705 | if [[ "${rco}${rci}" = "11" ]]; then | |
706 | echo "$fd" | |
707 | return 0 | |
708 | fi | |
709 | done | |
710 | return 1 | |
7acd4b46 KZ |
711 | } |
712 | ||
9deb8c7d KZ |
713 | function ts_lock { |
714 | local resource="$1" | |
715 | local lockfile="${TS_LOCKDIR}/${resource}.lock" | |
7acd4b46 | 716 | local fd |
9deb8c7d KZ |
717 | |
718 | if [ "$TS_NOLOCKS" == "yes" ]; then | |
719 | return 0 | |
720 | fi | |
721 | ||
7acd4b46 | 722 | # Don't lock again |
613a337e | 723 | fd=${TS_LOCKFILE_FD["$resource"]} |
7acd4b46 | 724 | if [ -n "$fd" ]; then |
44d75340 | 725 | echo "[$$ $TS_TESTNAME] ${resource} already locked!" |
7acd4b46 KZ |
726 | return 0 |
727 | fi | |
9deb8c7d | 728 | |
613a337e | 729 | fd=$(ts_find_free_fd) || ts_skip "failed to find lock fd" |
7acd4b46 KZ |
730 | |
731 | eval "exec $fd>$lockfile" | |
732 | flock --exclusive --timeout 30 $fd || ts_skip "failed to lock $resource" | |
613a337e | 733 | TS_LOCKFILE_FD["$resource"]="$fd" |
7acd4b46 | 734 | |
44d75340 | 735 | ###echo "[$$ $TS_TESTNAME] Locked $resource" |
9deb8c7d KZ |
736 | } |
737 | ||
9deb8c7d | 738 | function ts_unlock { |
7acd4b46 KZ |
739 | local resource="$1" |
740 | local lockfile="${TS_LOCKDIR}/${resource}.lock" | |
741 | local fd | |
742 | ||
743 | if [ "$TS_NOLOCKS" == "yes" ]; then | |
744 | return 0 | |
745 | fi | |
746 | ||
613a337e | 747 | fd=${TS_LOCKFILE_FD["$resource"]} |
7acd4b46 | 748 | if [ -n "$fd" ]; then |
7acd4b46 | 749 | eval "exec $fd<&-" |
613a337e RM |
750 | TS_LOCKFILE_FD["$resource"]="" |
751 | ###echo "[$$ $TS_TESTNAME] Unlocked $resource" | |
7acd4b46 | 752 | fi |
9deb8c7d KZ |
753 | } |
754 | ||
618ec053 | 755 | function ts_scsi_debug_init { |
dd761f79 | 756 | local devname |
8d323f0d | 757 | local t |
dd761f79 | 758 | TS_DEVICE="none" |
618ec053 | 759 | |
9deb8c7d KZ |
760 | ts_lock "scsi_debug" |
761 | ||
9779f598 | 762 | # dry run is not really reliable, real modprobe may still fail |
f80c0d38 RM |
763 | modprobe --dry-run --quiet scsi_debug &>/dev/null \ |
764 | || ts_skip "missing scsi_debug module (dry-run)" | |
618ec053 | 765 | |
f80c0d38 | 766 | # skip if still in use or removal of modules not supported at all |
5c711ba9 | 767 | # We don't want a slow timeout here so we don't use ts_scsi_debug_rmmod! |
44d75340 KZ |
768 | modprobe -r scsi_debug &>/dev/null |
769 | if [ "$?" -eq 1 ]; then | |
770 | ts_unlock "scsi_debug" | |
771 | ts_skip "cannot remove scsi_debug module (rmmod)" | |
772 | fi | |
f80c0d38 | 773 | |
85fca7e5 | 774 | modprobe -b scsi_debug "$@" &>/dev/null \ |
f80c0d38 RM |
775 | || ts_skip "cannot load scsi_debug module (modprobe)" |
776 | ||
777 | # it might be still not loaded, modprobe.conf or whatever | |
fc5fa903 | 778 | lsmod 2>/dev/null | grep -q "^scsi_debug " \ |
f80c0d38 | 779 | || ts_skip "scsi_debug module not loaded (lsmod)" |
618ec053 | 780 | |
618ec053 KZ |
781 | udevadm settle |
782 | ||
8d323f0d RM |
783 | # wait for device if udevadm settle does not work |
784 | for t in 0 0.02 0.05 0.1 1; do | |
785 | sleep $t | |
786 | devname=$(grep --with-filename scsi_debug /sys/block/*/device/model) && break | |
787 | done | |
788 | [ -n "${devname}" ] || ts_die "timeout waiting for scsi_debug device" | |
85fca7e5 | 789 | |
8d323f0d | 790 | devname=$(echo $devname | awk -F '/' '{print $4}') |
dd761f79 | 791 | TS_DEVICE="/dev/${devname}" |
8d323f0d RM |
792 | |
793 | # TODO validate that device is really up, for now just a warning on stderr | |
5c711ba9 RM |
794 | test -b $TS_DEVICE || echo "warning: scsi_debug device is still down" >&2 |
795 | } | |
796 | ||
797 | # automatically called once in ts_cleanup_on_exit() | |
798 | function ts_scsi_debug_rmmod { | |
799 | local err=1 | |
800 | local t | |
801 | local lastmsg | |
802 | ||
803 | # Return early most importantly in case we are not root or the module does | |
804 | # not exist at all. | |
805 | [ $UID -eq 0 ] || return 0 | |
806 | [ -n "$TS_DEVICE" ] || return 0 | |
fc5fa903 | 807 | lsmod 2>/dev/null | grep -q "^scsi_debug " || return 0 |
5c711ba9 RM |
808 | |
809 | udevadm settle | |
810 | ||
811 | # wait for successful rmmod if udevadm settle does not work | |
812 | for t in 0 0.02 0.05 0.1 1; do | |
813 | sleep $t | |
814 | lastmsg="$(modprobe -r scsi_debug 2>&1)" && err=0 && break | |
815 | done | |
816 | ||
817 | if [ "$err" = "1" ]; then | |
818 | ts_log "rmmod failed: '$lastmsg'" | |
819 | ts_log "timeout removing scsi_debug module (rmmod)" | |
820 | return 1 | |
821 | fi | |
822 | if lsmod | grep -q "^scsi_debug "; then | |
823 | ts_log "BUG! scsi_debug still loaded" | |
824 | return 1 | |
825 | fi | |
826 | ||
827 | # TODO Do we need to validate that all devices are gone? | |
828 | udevadm settle | |
829 | test -b "$TS_DEVICE" && echo "warning: scsi_debug device is still up" >&2 | |
830 | ||
831 | # TODO unset TS_DEVICE, check that nobody uses it later, e.g. ts_fdisk_clean | |
832 | ||
9deb8c7d | 833 | ts_unlock "scsi_debug" |
5c711ba9 | 834 | return 0 |
618ec053 | 835 | } |
a98de969 RM |
836 | |
837 | function ts_resolve_host { | |
838 | local host="$1" | |
839 | local tmp | |
840 | ||
841 | # currently we just resolve default records (might be "A", ipv4 only) | |
842 | if type "dig" >/dev/null 2>&1; then | |
843 | tmp=$(dig "$host" +short 2>/dev/null) || return 1 | |
844 | elif type "nslookup" >/dev/null 2>&1; then | |
845 | tmp=$(nslookup "$host" 2>/dev/null) || return 1 | |
846 | tmp=$(echo "$tmp"| grep -A1 "^Name:"| grep "^Address:"| cut -d" " -f2) | |
eee79f28 AH |
847 | elif type "host" >/dev/null 2>&1; then |
848 | tmp=$(host "$host" 2>/dev/null) || return 1 | |
849 | tmp=$(echo "$tmp" | grep " has address " | cut -d " " -f4) | |
850 | elif type "getent" >/dev/null 2>&1; then | |
851 | tmp=$(getent ahosts "$host" 2>/dev/null) || return 1 | |
852 | tmp=$(echo "$tmp" | cut -d " " -f 1 | sort -u) | |
a98de969 RM |
853 | fi |
854 | ||
855 | # we return 1 if tmp is empty | |
856 | test -n "$tmp" || return 1 | |
857 | echo "$tmp" | sort -R | head -n 1 | |
858 | } | |
c9813f2c RM |
859 | |
860 | # listen to unix socket (background socat) | |
861 | function ts_init_socket_to_file { | |
862 | local socket=$1 | |
863 | local outfile=$2 | |
864 | local pid="0" | |
865 | ||
866 | ts_check_prog "socat" | |
867 | rm -f "$socket" "$outfile" | |
868 | ||
e4866229 | 869 | # if socat is too old for these options we'll skip it below |
c9813f2c | 870 | socat -u UNIX-LISTEN:$socket,fork,max-children=1,backlog=128 \ |
e4866229 | 871 | STDOUT > "$outfile" 2>/dev/null & |
c9813f2c RM |
872 | pid=$! |
873 | ||
874 | # check for running background process | |
e4866229 | 875 | if [ "$pid" -le "0" ] || ! kill -s 0 "$pid" &>/dev/null; then |
c9813f2c RM |
876 | ts_skip "unable to run socat" |
877 | fi | |
878 | # wait for the socket listener | |
e4866229 RM |
879 | if ! socat -u /dev/null UNIX-CONNECT:$socket,retry=30,interval=0.1 &>/dev/null; then |
880 | kill -9 "$pid" &>/dev/null | |
881 | ts_skip "timeout waiting for socat socket" | |
c9813f2c RM |
882 | fi |
883 | # check socket again | |
e4866229 RM |
884 | if ! socat -u /dev/null UNIX-CONNECT:$socket &>/dev/null; then |
885 | kill -9 "$pid" &>/dev/null | |
886 | ts_skip "socat socket stopped listening" | |
c9813f2c RM |
887 | fi |
888 | } | |
edeb6223 KZ |
889 | |
890 | function ts_has_mtab_support { | |
891 | grep -q '#define USE_LIBMOUNT_SUPPORT_MTAB' ${top_builddir}/config.h | |
892 | if [ $? == 0 ]; then | |
893 | echo "yes" | |
894 | else | |
895 | echo "no" | |
896 | fi | |
897 | } | |
898 | ||
4a62215c KZ |
899 | function ts_has_ncurses_support { |
900 | grep -q '#define HAVE_LIBNCURSES' ${top_builddir}/config.h | |
901 | if [ $? == 0 ]; then | |
902 | echo "yes" | |
903 | else | |
904 | echo "no" | |
905 | fi | |
906 | } | |
907 |