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