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