2 # Copyright (C) 2007 Karel Zak <kzak@redhat.com>
4 # This file is part of util-linux.
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.
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.
23 function ts_canonicalize
{
36 ts_failed
"ul_cd: not enough arguments"
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"
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"
51 if [ "$TS_PARSABLE" != "yes" ]; then
52 if [ $TS_NSUBTESTS -ne 0 ] && [ -z "$TS_SUBNAME" ]; then
53 desc
=$
(printf "%11s...")
59 if [ -n "$TS_SUBNAME" ]; then
60 desc
=$
(printf "%s: [%02d] %s" "$TS_DESC" "$TS_NSUBTESTS" "$TS_SUBNAME")
64 printf "%13s: %-45s ...%s\n" "$TS_COMPONENT" "$desc" "$1"
67 function ts_check_test_command
{
68 if [ ! -x "$1" ]; then
69 ts_skip
"${1##*/} not found"
73 function ts_check_prog
{
75 type "$cmd" >/dev
/null
2>&1 || ts_skip
"missing in PATH: $cmd"
78 function ts_check_losetup
{
80 ts_check_test_command
"$TS_CMD_LOSETUP"
82 if [ "$TS_SKIP_LOOPDEVS" = "yes" ]; then
83 ts_skip
"loop-device tests disabled"
86 # assuming that losetup -f works ... to be checked somewhere else
87 tmp
=$
($TS_CMD_LOSETUP -f 2>/dev
/null
)
88 if test -b "$tmp"; then
91 ts_skip
"no loop-device support"
94 function ts_skip_subtest
{
95 ts_report
" SKIPPED ($1)"
105 function ts_skip_nonroot
{
106 if [ $UID -ne 0 ]; then
107 ts_skip
"no root permissions"
111 function ts_failed_subtest
{
114 if [ "$TS_KNOWN_FAIL" = "yes" ]; then
119 if [ x
"$1" == x
"" ]; then
120 ts_report
" $msg ($TS_NS)"
122 ts_report
" $msg ($1)"
129 ts_failed_subtest
"$1"
133 function ts_ok_subtest
{
134 if [ x
"$1" == x
"" ]; then
147 echo "$1" >> $TS_OUTPUT
148 [ "$TS_VERBOSE" == "yes" ] && echo "$1"
151 function ts_has_option
{
155 # user may set options by env for a single test or whole component
156 # e.g. TS_OPT_ipcs_limits2_fake="yes" or TS_OPT_ipcs_fake="yes"
157 local v_test
=${TS_TESTNAME//[-.]/_}
158 local v_comp
=${TS_COMPONENT//[-.]/_}
159 local v_name
=${NAME//[-.]/_}
160 eval local env_opt_test
=\
$TS_OPT_${v_comp}_${v_test}_${v_name}
161 eval local env_opt_comp
=\
$TS_OPT_${v_comp}_
${v_name}
162 if [ "$env_opt_test" = "yes" \
163 -o "$env_opt_comp" = "yes" -a "$env_opt_test" != "no" ]; then
166 elif [ "$env_opt_test" = "no" \
167 -o "$env_opt_comp" = "no" -a "$env_opt_test" != "yes" ]; then
171 # or just check the global command line options
172 if [[ $ALL =~
([$
' \t\n']|^
)--$NAME([$
'= \t\n']|$
) ]]; then
177 # or the _global_ env, e.g TS_OPT_parsable="yes"
178 eval local env_opt
=\
$TS_OPT_${v_name}
179 if [ "$env_opt" = "yes" ]; then echo "yes"; fi
182 function ts_option_argument
{
187 echo "$ALL" |
sed -n "s/.*[ \t\n]--$NAME=\([^ \t\n]*\).*/\1/p" |
tail -n 1
190 function ts_init_core_env
{
192 TS_NS
="$TS_COMPONENT/$TS_TESTNAME"
193 TS_OUTPUT
="$TS_OUTDIR/$TS_TESTNAME"
194 TS_VGDUMP
="$TS_OUTDIR/$TS_TESTNAME.vgdump"
195 TS_DIFF
="$TS_DIFFDIR/$TS_TESTNAME"
196 TS_EXPECTED
="$TS_TOPDIR/expected/$TS_NS"
197 TS_MOUNTPOINT
="$TS_OUTDIR/${TS_TESTNAME}-mnt"
200 function ts_init_core_subtest_env
{
201 TS_NS
="$TS_COMPONENT/$TS_TESTNAME-$TS_SUBNAME"
202 TS_OUTPUT
="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME"
203 TS_VGDUMP
="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME.vgdump"
204 TS_DIFF
="$TS_DIFFDIR/$TS_TESTNAME-$TS_SUBNAME"
205 TS_EXPECTED
="$TS_TOPDIR/expected/$TS_NS"
206 TS_MOUNTPOINT
="$TS_OUTDIR/${TS_TESTNAME}-${TS_SUBNAME}-mnt"
208 rm -f $TS_OUTPUT $TS_VGDUMP
209 [ -d "$TS_OUTDIR" ] || mkdir
-p "$TS_OUTDIR"
212 [ -n "$TS_VALGRIND_CMD" ] && touch $TS_VGDUMP
215 function ts_init_env
{
216 local mydir
=$
(ts_abspath
${0%/*})
224 export LANG LANGUAGE LC_ALL CHARSET
226 mydir
=$
(ts_canonicalize
"$mydir")
228 # automake directories
229 top_srcdir
=$
(ts_option_argument
"srcdir" "$*")
230 top_builddir
=$
(ts_option_argument
"builddir" "$*")
232 # where is this script
233 TS_TOPDIR
=$
(ts_abspath
$mydir/..
/..
/)
236 if [ -z "$top_srcdir" ]; then
237 top_srcdir
="$TS_TOPDIR/.."
239 if [ -z "$top_builddir" ]; then
240 top_builddir
="$TS_TOPDIR/.."
243 top_srcdir
=$
(ts_abspath
$top_srcdir)
244 top_builddir
=$
(ts_abspath
$top_builddir)
246 # some ul commands search other ul commands in $PATH
247 export PATH
="$top_builddir:$PATH"
249 TS_SCRIPT
="$mydir/$(basename $0)"
250 TS_SUBDIR
=$
(dirname $TS_SCRIPT)
251 TS_TESTNAME
=$
(basename $TS_SCRIPT)
252 TS_COMPONENT
=$
(basename $TS_SUBDIR)
253 TS_DESC
=${TS_DESC:-$TS_TESTNAME}
260 TS_OUTDIR
="$top_builddir/tests/output/$TS_COMPONENT"
261 TS_DIFFDIR
="$top_builddir/tests/diff/$TS_COMPONENT"
265 TS_VERBOSE
=$
(ts_has_option
"verbose" "$*")
266 TS_PARALLEL
=$
(ts_has_option
"parallel" "$*")
267 TS_KNOWN_FAIL
=$
(ts_has_option
"known-fail" "$*")
268 TS_SKIP_LOOPDEVS
=$
(ts_has_option
"skip-loopdevs" "$*")
269 TS_PARSABLE
=$
(ts_has_option
"parsable" "$*")
270 [ "$TS_PARSABLE" = "yes" ] || TS_PARSABLE
="$TS_PARALLEL"
272 tmp
=$
( ts_has_option
"memcheck" "$*")
273 if [ "$tmp" == "yes" -a -f /usr
/bin
/valgrind
]; then
274 TS_VALGRIND_CMD
="/usr/bin/valgrind"
277 BLKID_FILE
="$TS_OUTDIR/${TS_TESTNAME}.blkidtab"
279 declare -a TS_SUID_PROGS
280 declare -a TS_SUID_USER
281 declare -a TS_SUID_GROUP
282 declare -a TS_LOOP_DEVS
284 if [ -f $TS_TOPDIR/commands.sh
]; then
285 .
$TS_TOPDIR/commands.sh
290 rm -f $TS_OUTPUT $TS_VGDUMP
291 [ -d "$TS_OUTDIR" ] || mkdir
-p "$TS_OUTDIR"
294 [ -n "$TS_VALGRIND_CMD" ] && touch $TS_VGDUMP
296 if [ "$TS_VERBOSE" == "yes" ]; then
298 echo " script: $TS_SCRIPT"
299 echo " sub dir: $TS_SUBDIR"
300 echo " top dir: $TS_TOPDIR"
301 echo " self: $TS_SELF"
302 echo " test name: $TS_TESTNAME"
303 echo " test desc: $TS_DESC"
304 echo " component: $TS_COMPONENT"
305 echo " namespace: $TS_NS"
306 echo " verbose: $TS_VERBOSE"
307 echo " output: $TS_OUTPUT"
308 echo " valgrind: $TS_VGDUMP"
309 echo " expected: $TS_EXPECTED"
310 echo " mountpoint: $TS_MOUNTPOINT"
315 function ts_init_subtest
{
318 ts_init_core_subtest_env
319 TS_NSUBTESTS
=$
(( $TS_NSUBTESTS + 1 ))
321 if [ "$TS_PARSABLE" != "yes" ]; then
322 [ $TS_NSUBTESTS -eq 1 ] && echo
323 printf "%16s: %-27s ..." "" "$TS_SUBNAME"
330 local is_fake
=$
( ts_has_option
"fake" "$*")
331 local is_force
=$
( ts_has_option
"force" "$*")
333 if [ "$TS_PARSABLE" != "yes" ]; then
334 printf "%13s: %-30s ..." "$TS_COMPONENT" "$TS_DESC"
337 [ "$is_fake" == "yes" ] && ts_skip
"fake mode"
338 [ "$TS_OPTIONAL" == "yes" -a "$is_force" != "yes" ] && ts_skip
"optional"
341 function ts_init_suid
{
343 ct
=${#TS_SUID_PROGS[*]}
345 # Save info about original setting
346 TS_SUID_PROGS
[$ct]=$PROG
347 TS_SUID_USER
[$ct]=$
(stat
--printf="%U" $PROG)
348 TS_SUID_GROUP
[$ct]=$
(stat
--printf="%G" $PROG)
350 chown root.root
$PROG &> /dev
/null
351 chmod u
+s
$PROG &> /dev
/null
354 function ts_init_py
{
357 [ -f "$top_builddir/py${LIBNAME}.la" ] || ts_skip
"py${LIBNAME} not compiled"
359 export LD_LIBRARY_PATH
="$top_builddir/.libs:$LD_LIBRARY_PATH"
360 export PYTHONPATH
="$top_builddir/$LIBNAME/python:$top_builddir/.libs:$PYTHONPATH"
362 export PYTHON_VERSION
=$
(awk '/^PYTHON_VERSION/ { print $3 }' $top_builddir/Makefile
)
363 export PYTHON_MAJOR_VERSION
=$
(echo $PYTHON_VERSION |
sed 's/\..*//')
365 export PYTHON
="python${PYTHON_MAJOR_VERSION}"
368 function ts_valgrind
{
369 if [ -z "$TS_VALGRIND_CMD" ]; then
372 $TS_VALGRIND_CMD --tool=memcheck
--leak-check=full \
373 --leak-resolution=high
--num-callers=20 \
374 --log-file="$TS_VGDUMP" $
*
378 function ts_gen_diff
{
381 [ -f "$TS_OUTPUT" ] ||
return 1
382 [ -f "$TS_EXPECTED" ] || TS_EXPECTED
=/dev
/null
384 # remove libtool lt- prefixes
385 sed --in-place 's/^lt\-\(.*\: \)/\1/g' $TS_OUTPUT
387 [ -d "$TS_DIFFDIR" ] || mkdir
-p "$TS_DIFFDIR"
388 diff -u $TS_EXPECTED $TS_OUTPUT > $TS_DIFF
390 if [ $?
-ne 0 ] ||
[ -s $TS_DIFF ]; then
399 function tt_gen_mem_report
{
400 [ -z "$TS_VALGRIND_CMD" ] && echo "$1"
402 grep -q -E 'ERROR SUMMARY: [1-9]' $TS_VGDUMP &> /dev
/null
403 if [ $?
-eq 0 ]; then
404 echo "mem-error detected!"
408 function ts_finalize_subtest
{
412 if [ $?
-eq 1 ]; then
413 ts_failed_subtest
"$1"
416 ts_ok_subtest
"$(tt_gen_mem_report "$1")"
419 [ $res -ne 0 ] && TS_NSUBFAILED
=$
(( $TS_NSUBFAILED + 1 ))
421 # reset environment back to parental test
427 function ts_finalize
{
430 if [ $TS_NSUBTESTS -ne 0 ]; then
431 if ! ts_gen_diff ||
[ $TS_NSUBFAILED -ne 0 ]; then
432 ts_failed
"$TS_NSUBFAILED from $TS_NSUBTESTS sub-tests"
434 ts_ok
"all $TS_NSUBTESTS sub-tests PASSED"
438 ts_gen_diff || ts_failed
"$1"
447 function ts_cleanup_on_exit
{
449 for idx
in $
(seq 0 $
((${#TS_SUID_PROGS[*]} - 1))); do
450 PROG
=${TS_SUID_PROGS[$idx]}
451 chmod a-s
$PROG &> /dev
/null
452 chown
${TS_SUID_USER[$idx]}.
${TS_SUID_GROUP[$idx]} $PROG &> /dev
/null
455 for dev
in "${TS_LOOP_DEVS[@]}"; do
456 ts_device_deinit
"$dev"
461 function ts_image_md5sum
{
462 local img
=${1:-"$TS_OUTDIR/${TS_TESTNAME}.img"}
463 echo $
(md5sum "$img" |
awk '{printf $1}') $
(basename "$img")
466 function ts_image_init
{
467 local mib
=${1:-"5"} # size in MiBs
468 local img
=${2:-"$TS_OUTDIR/${TS_TESTNAME}.img"}
471 truncate
-s "${mib}M" "$img"
476 function ts_register_loop_device
{
477 local ct
=${#TS_LOOP_DEVS[*]}
481 function ts_device_init
{
485 img
=$
(ts_image_init
$1 $2)
486 dev
=$
($TS_CMD_LOSETUP --show -f "$img")
487 if [ "$?" != "0" -o "$dev" = "" ]; then
488 ts_die
"Cannot init device"
491 ts_register_loop_device
"$dev"
495 # call from ts_cleanup_on_exit() only because of TS_LOOP_DEVS maintenance
496 function ts_device_deinit
{
499 if [ -b "$DEV" ]; then
500 $TS_CMD_UMOUNT "$DEV" &> /dev
/null
501 $TS_CMD_LOSETUP -d "$DEV" &> /dev
/null
505 function ts_uuid_by_devname
{
506 echo $
($TS_CMD_BLKID -p -s UUID
-o value
$1)
509 function ts_label_by_devname
{
510 echo $
($TS_CMD_BLKID -p -s LABEL
-o value
$1)
513 function ts_fstype_by_devname
{
514 echo $
($TS_CMD_BLKID -p -s TYPE
-o value
$1)
517 function ts_device_has
{
524 "TYPE") vl
=$
(ts_fstype_by_devname
$DEV);;
525 "LABEL") vl
=$
(ts_label_by_devname
$DEV);;
526 "UUID") vl
=$
(ts_uuid_by_devname
$DEV);;
530 if [ "$vl" == "$VAL" ]; then
536 function ts_device_has_uuid
{
537 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}$'
549 out
=$
($TS_CMD_MOUNT "$@" 2>&1)
551 echo -n "$out" >> $TS_OUTPUT
553 if [ $result != 0 ] \
554 && msg
=$
(echo "$out" |
grep -m1 "unknown filesystem type")
556 # skip only if reported fs correctly and if it's not available
557 fs
=$
(echo "$msg" |
sed -n "s/.*type '\(.*\)'$/\1/p")
558 [ "$fs" = "fs_exp" ] \
559 && grep -qe "[[:space:]]${fs}$" /proc
/filesystems
&>/dev
/null \
565 function ts_is_mounted
{
566 local DEV
=$
(ts_canonicalize
"$1")
568 grep -q $DEV /proc
/mounts
&& return 0
570 if [ "${DEV#/dev/loop/}" != "$DEV" ]; then
571 grep -q "/dev/loop${DEV#/dev/loop/}" /proc
/mounts
&& return 0
576 function ts_fstab_open
{
577 echo "# <!-- util-linux test entry" >> /etc
/fstab
580 function ts_fstab_close
{
581 echo "# -->" >> /etc
/fstab
584 function ts_fstab_addline
{
586 local MNT
=${2:-"$TS_MOUNTPOINT"}
587 local FS
=${3:-"auto"}
588 local OPT
=${4:-"defaults"}
590 echo "$SPEC $MNT $FS $OPT 0 0" >> /etc
/fstab
593 function ts_fstab_add
{
599 function ts_fstab_clean
{
601 /# <!-- util-linux/!b
607 s/# <!-- util-linux.*-->//;
611 function ts_fdisk_clean
{
614 # remove non comparable parts of fdisk output
615 if [ -n "${DEVNAME}" ]; then
616 sed -i -e "s@${DEVNAME}@<removed>@;" $TS_OUTPUT
620 -e 's/Disk identifier:.*/Disk identifier: <removed>/' \
621 -e 's/Created a new.*/Created a new <removed>./' \
622 -e 's/^Device[[:blank:]]*Start/Device Start/' \
623 -e 's/^Device[[:blank:]]*Boot/Device Boot/' \
624 -e 's/Welcome to fdisk.*/Welcome to fdisk <removed>./' \
625 -e 's/typescript file.*/typescript file <removed>./' \
626 -e 's@^\(I/O size (minimum/op.* bytes /\) [1-9][0-9]* @\1 <removed> @' \
630 function ts_scsi_debug_init
{
635 # dry run is not really reliable, real modprobe may still fail
636 modprobe
--dry-run --quiet scsi_debug
&>/dev
/null \
637 || ts_skip
"missing scsi_debug module (dry-run)"
639 # skip if still in use or removal of modules not supported at all
640 modprobe
-r scsi_debug
&>/dev
/null \
641 || ts_skip
"cannot remove scsi_debug module (rmmod)"
643 # TODO validate that all devices are gone, add function ts_scsi_debug_rmmod
644 # to be used by the tests too. Tests which produce non-removable scsi
645 # devices should fail!
647 modprobe
-b scsi_debug
"$@" &>/dev
/null \
648 || ts_skip
"cannot load scsi_debug module (modprobe)"
650 # it might be still not loaded, modprobe.conf or whatever
651 lsmod |
grep -q "^scsi_debug " \
652 || ts_skip
"scsi_debug module not loaded (lsmod)"
656 # wait for device if udevadm settle does not work
657 for t
in 0 0.02 0.05 0.1 1; do
659 devname
=$
(grep --with-filename scsi_debug
/sys
/block
/*/device
/model
) && break
661 [ -n "${devname}" ] || ts_die
"timeout waiting for scsi_debug device"
663 devname
=$
(echo $devname |
awk -F '/' '{print $4}')
664 TS_DEVICE
="/dev/${devname}"
666 # TODO validate that device is really up, for now just a warning on stderr
667 test -b $TS_DEVICE ||
echo "warning: scsi_debug device is still down"
670 function ts_resolve_host
{
674 # currently we just resolve default records (might be "A", ipv4 only)
675 if type "dig" >/dev
/null
2>&1; then
676 tmp
=$
(dig "$host" +short
2>/dev
/null
) ||
return 1
677 elif type "nslookup" >/dev
/null
2>&1; then
678 tmp
=$
(nslookup "$host" 2>/dev
/null
) ||
return 1
679 tmp
=$
(echo "$tmp"|
grep -A1 "^Name:"|
grep "^Address:"| cut
-d" " -f2)
680 elif type "host" >/dev
/null
2>&1; then
681 tmp
=$
(host "$host" 2>/dev
/null
) ||
return 1
682 tmp
=$
(echo "$tmp" |
grep " has address " | cut
-d " " -f4)
683 elif type "getent" >/dev
/null
2>&1; then
684 tmp
=$
(getent ahosts
"$host" 2>/dev
/null
) ||
return 1
685 tmp
=$
(echo "$tmp" | cut
-d " " -f 1 |
sort -u)
688 # we return 1 if tmp is empty
689 test -n "$tmp" ||
return 1
690 echo "$tmp" |
sort -R |
head -n 1
693 # listen to unix socket (background socat)
694 function ts_init_socket_to_file
{
699 ts_check_prog
"socat"
700 rm -f "$socket" "$outfile"
702 # if socat is too old for these options we'll skip it below
703 socat
-u UNIX-LISTEN
:$socket,fork
,max-children
=1,backlog
=128 \
704 STDOUT
> "$outfile" 2>/dev
/null
&
707 # check for running background process
708 if [ "$pid" -le "0" ] ||
! kill -s 0 "$pid" &>/dev
/null
; then
709 ts_skip
"unable to run socat"
711 # wait for the socket listener
712 if ! socat
-u /dev
/null UNIX-CONNECT
:$socket,retry
=30,interval
=0.1 &>/dev
/null
; then
713 kill -9 "$pid" &>/dev
/null
714 ts_skip
"timeout waiting for socat socket"
717 if ! socat
-u /dev
/null UNIX-CONNECT
:$socket &>/dev
/null
; then
718 kill -9 "$pid" &>/dev
/null
719 ts_skip
"socat socket stopped listening"