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"
49 if [ "$TS_PARALLEL" == "yes" ]; then
56 function ts_check_test_command
{
57 if [ ! -x "$1" ]; then
58 ts_skip
"${1##*/} not found"
62 function ts_check_prog
{
64 type "$cmd" >/dev
/null
2>&1 || ts_skip
"missing in PATH: $cmd"
67 function ts_check_losetup
{
69 ts_check_test_command
"$TS_CMD_LOSETUP"
71 if [ "$TS_SKIP_LOOPDEVS" = "yes" ]; then
72 ts_skip
"loop-device tests disabled"
75 # assuming that losetup -f works ... to be checked somewhere else
76 tmp
=$
($TS_CMD_LOSETUP -f 2>/dev
/null
)
77 if test -b "$tmp"; then
80 ts_skip
"no loop-device support"
83 function ts_skip_subtest
{
84 ts_report
" SKIPPED ($1)"
94 function ts_skip_nonroot
{
95 if [ $UID -ne 0 ]; then
96 ts_skip
"no root permissions"
100 function ts_failed_subtest
{
103 if [ "$TS_KNOWN_FAIL" = "yes" ]; then
108 if [ x
"$1" == x
"" ]; then
109 ts_report
" $msg ($TS_NS)"
111 ts_report
" $msg ($1)"
118 ts_failed_subtest
"$1"
122 function ts_ok_subtest
{
123 if [ x
"$1" == x
"" ]; then
136 echo "$1" >> $TS_OUTPUT
137 [ "$TS_VERBOSE" == "yes" ] && echo "$1"
140 function ts_has_option
{
144 # user may set options by env for a single test or whole component
145 # e.g. TS_OPT_ipcs_limits2_fake="yes" or TS_OPT_ipcs_fake="yes"
146 local v_test
=${TS_TESTNAME//[-.]/_}
147 local v_comp
=${TS_COMPONENT//[-.]/_}
148 local v_name
=${NAME//[-.]/_}
149 eval local env_opt_test
=\
$TS_OPT_${v_comp}_${v_test}_${v_name}
150 eval local env_opt_comp
=\
$TS_OPT_${v_comp}_
${v_name}
151 if [ "$env_opt_test" = "yes" \
152 -o "$env_opt_comp" = "yes" -a "$env_opt_test" != "no" ]; then
155 elif [ "$env_opt_test" = "no" \
156 -o "$env_opt_comp" = "no" -a "$env_opt_test" != "yes" ]; then
160 # or just check the global command line options
161 echo -n $ALL |
sed 's/ //g' |
awk 'BEGIN { FS="="; RS="--" } /('$NAME'$|'$NAME'=)/ { print "yes" }'
164 function ts_option_argument
{
167 echo -n $ALL |
sed 's/ //g' |
awk 'BEGIN { FS="="; RS="--" } /'$NAME'=/ { print $2 }'
170 function ts_init_core_env
{
171 TS_NS
="$TS_COMPONENT/$TS_TESTNAME"
172 TS_OUTPUT
="$TS_OUTDIR/$TS_TESTNAME"
173 TS_VGDUMP
="$TS_OUTDIR/$TS_TESTNAME.vgdump"
174 TS_DIFF
="$TS_DIFFDIR/$TS_TESTNAME"
175 TS_EXPECTED
="$TS_TOPDIR/expected/$TS_NS"
176 TS_MOUNTPOINT
="$TS_OUTDIR/${TS_TESTNAME}-mnt"
179 function ts_init_core_subtest_env
{
180 TS_NS
="$TS_COMPONENT/$TS_TESTNAME-$TS_SUBNAME"
181 TS_OUTPUT
="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME"
182 TS_VGDUMP
="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME.vgdump"
183 TS_DIFF
="$TS_DIFFDIR/$TS_TESTNAME-$TS_SUBNAME"
184 TS_EXPECTED
="$TS_TOPDIR/expected/$TS_NS"
185 TS_MOUNTPOINT
="$TS_OUTDIR/${TS_TESTNAME}-${TS_SUBNAME}-mnt"
187 rm -f $TS_OUTPUT $TS_VGDUMP
188 [ -d "$TS_OUTDIR" ] || mkdir
-p "$TS_OUTDIR"
191 [ -n "$TS_VALGRIND_CMD" ] && touch $TS_VGDUMP
194 function ts_init_env
{
195 local mydir
=$
(ts_abspath
${0%/*})
203 export LANG LANGUAGE LC_ALL CHARSET
205 mydir
=$
(ts_canonicalize
"$mydir")
207 # automake directories
208 top_srcdir
=$
(ts_option_argument
"srcdir" "$*")
209 top_builddir
=$
(ts_option_argument
"builddir" "$*")
211 # where is this script
212 TS_TOPDIR
=$
(ts_abspath
$mydir/..
/..
/)
215 if [ -z "$top_srcdir" ]; then
216 top_srcdir
="$TS_TOPDIR/.."
218 if [ -z "$top_builddir" ]; then
219 top_builddir
="$TS_TOPDIR/.."
222 top_srcdir
=$
(ts_abspath
$top_srcdir)
223 top_builddir
=$
(ts_abspath
$top_builddir)
225 # some ul commands search other ul commands in $PATH
226 export PATH
="$top_builddir:$PATH"
228 TS_SCRIPT
="$mydir/$(basename $0)"
229 TS_SUBDIR
=$
(dirname $TS_SCRIPT)
230 TS_TESTNAME
=$
(basename $TS_SCRIPT)
231 TS_COMPONENT
=$
(basename $TS_SUBDIR)
232 TS_DESC
=${TS_DESC:-$TS_TESTNAME}
239 TS_OUTDIR
="$top_builddir/tests/output/$TS_COMPONENT"
240 TS_DIFFDIR
="$top_builddir/tests/diff/$TS_COMPONENT"
244 TS_VERBOSE
=$
(ts_has_option
"verbose" "$*")
245 TS_PARALLEL
=$
(ts_has_option
"parallel" "$*")
246 TS_KNOWN_FAIL
=$
(ts_has_option
"known-fail" "$*")
247 TS_SKIP_LOOPDEVS
=$
(ts_has_option
"skip-loopdevs" "$*")
249 tmp
=$
( ts_has_option
"memcheck" "$*")
250 if [ "$tmp" == "yes" -a -f /usr
/bin
/valgrind
]; then
251 TS_VALGRIND_CMD
="/usr/bin/valgrind"
254 BLKID_FILE
="$TS_OUTDIR/${TS_TESTNAME}.blkidtab"
256 declare -a TS_SUID_PROGS
257 declare -a TS_SUID_USER
258 declare -a TS_SUID_GROUP
259 declare -a TS_LOOP_DEVS
261 if [ -f $TS_TOPDIR/commands.sh
]; then
262 .
$TS_TOPDIR/commands.sh
267 rm -f $TS_OUTPUT $TS_VGDUMP
268 [ -d "$TS_OUTDIR" ] || mkdir
-p "$TS_OUTDIR"
271 [ -n "$TS_VALGRIND_CMD" ] && touch $TS_VGDUMP
273 if [ "$TS_VERBOSE" == "yes" ]; then
275 echo " script: $TS_SCRIPT"
276 echo " sub dir: $TS_SUBDIR"
277 echo " top dir: $TS_TOPDIR"
278 echo " self: $TS_SELF"
279 echo " test name: $TS_TESTNAME"
280 echo " test desc: $TS_DESC"
281 echo " component: $TS_COMPONENT"
282 echo " namespace: $TS_NS"
283 echo " verbose: $TS_VERBOSE"
284 echo " output: $TS_OUTPUT"
285 echo " valgrind: $TS_VGDUMP"
286 echo " expected: $TS_EXPECTED"
287 echo " mountpoint: $TS_MOUNTPOINT"
292 function ts_init_subtest
{
296 ts_init_core_subtest_env
298 [ $TS_NSUBTESTS -eq 0 ] && echo
299 TS_NSUBTESTS
=$
(( $TS_NSUBTESTS + 1 ))
301 if [ "$TS_PARALLEL" == "yes" ]; then
302 TS_TITLE
=$
(printf "%13s: %-30s ...\n%16s: %-27s ..." "$TS_COMPONENT" "$TS_DESC" "" "$TS_SUBNAME")
304 TS_TITLE
=$
(printf "%16s: %-27s ..." "" "$TS_SUBNAME")
312 local is_fake
=$
( ts_has_option
"fake" "$*")
313 local is_force
=$
( ts_has_option
"force" "$*")
315 if [ "$TS_PARALLEL" == "yes" ]; then
316 TS_TITLE
=$
(printf "%13s: %-30s ..." "$TS_COMPONENT" "$TS_DESC")
318 TS_TITLE
=$
(printf "%13s: %-30s ..." "$TS_COMPONENT" "$TS_DESC")
322 [ "$is_fake" == "yes" ] && ts_skip
"fake mode"
323 [ "$TS_OPTIONAL" == "yes" -a "$is_force" != "yes" ] && ts_skip
"optional"
326 function ts_init_suid
{
328 ct
=${#TS_SUID_PROGS[*]}
330 # Save info about original setting
331 TS_SUID_PROGS
[$ct]=$PROG
332 TS_SUID_USER
[$ct]=$
(stat
--printf="%U" $PROG)
333 TS_SUID_GROUP
[$ct]=$
(stat
--printf="%G" $PROG)
335 chown root.root
$PROG &> /dev
/null
336 chmod u
+s
$PROG &> /dev
/null
339 function ts_init_py
{
342 [ -f "$top_builddir/py${LIBNAME}.la" ] || ts_skip
"py${LIBNAME} not compiled"
344 export LD_LIBRARY_PATH
="$top_builddir/.libs:$LD_LIBRARY_PATH"
345 export PYTHONPATH
="$top_builddir/$LIBNAME/python:$top_builddir/.libs:$PYTHONPATH"
347 export PYTHON_VERSION
=$
(awk '/^PYTHON_VERSION/ { print $3 }' $top_builddir/Makefile
)
348 export PYTHON_MAJOR_VERSION
=$
(echo $PYTHON_VERSION |
sed 's/\..*//')
350 export PYTHON
="python${PYTHON_MAJOR_VERSION}"
353 function ts_valgrind
{
354 if [ -z "$TS_VALGRIND_CMD" ]; then
357 $TS_VALGRIND_CMD --tool=memcheck
--leak-check=full \
358 --leak-resolution=high
--num-callers=20 \
359 --log-file="$TS_VGDUMP" $
*
363 function ts_gen_diff
{
366 if [ -s "$TS_OUTPUT" ]; then
368 # remove libtool lt- prefixes
369 sed --in-place 's/^lt\-\(.*\: \)/\1/g' $TS_OUTPUT
371 [ -d "$TS_DIFFDIR" ] || mkdir
-p "$TS_DIFFDIR"
372 diff -u $TS_EXPECTED $TS_OUTPUT > $TS_DIFF
374 if [ -s $TS_DIFF ]; then
385 function tt_gen_mem_report
{
386 [ -z "$TS_VALGRIND_CMD" ] && echo "$1"
388 grep -q -E 'ERROR SUMMARY: [1-9]' $TS_VGDUMP &> /dev
/null
389 if [ $?
-eq 0 ]; then
390 echo "mem-error detected!"
394 function ts_finalize_subtest
{
397 if [ -s "$TS_EXPECTED" ]; then
399 if [ $?
-eq 1 ]; then
400 ts_failed_subtest
"$1"
403 ts_ok_subtest
"$(tt_gen_mem_report "$1")"
406 ts_skip_subtest
"output undefined"
409 [ $res -ne 0 ] && TS_NSUBFAILED
=$
(( $TS_NSUBFAILED + 1 ))
411 # reset environment back to parental test
417 function ts_finalize
{
420 if [ $TS_NSUBTESTS -ne 0 ]; then
422 if [ $TS_NSUBFAILED -ne 0 ]; then
423 ts_failed
"$TS_NSUBFAILED from $TS_NSUBTESTS sub-tests"
425 ts_ok
"all $TS_NSUBTESTS sub-tests PASSED"
429 if [ -s $TS_EXPECTED ]; then
431 if [ $?
-eq 1 ]; then
437 ts_skip
"output undefined"
445 function ts_cleanup_on_exit
{
447 for idx
in $
(seq 0 $
((${#TS_SUID_PROGS[*]} - 1))); do
448 PROG
=${TS_SUID_PROGS[$idx]}
449 chmod a-s
$PROG &> /dev
/null
450 chown
${TS_SUID_USER[$idx]}.
${TS_SUID_GROUP[$idx]} $PROG &> /dev
/null
453 for dev
in "${TS_LOOP_DEVS[@]}"; do
454 ts_device_deinit
"$dev"
459 function ts_image_md5sum
{
460 local img
=${1:-"$TS_OUTDIR/${TS_TESTNAME}.img"}
461 echo $
(md5sum "$img" |
awk '{printf $1}') $
(basename "$img")
464 function ts_image_init
{
465 local mib
=${1:-"5"} # size in MiBs
466 local img
=${2:-"$TS_OUTDIR/${TS_TESTNAME}.img"}
469 truncate
-s "${mib}M" "$img"
474 function ts_register_loop_device
{
475 local ct
=${#TS_LOOP_DEVS[*]}
479 function ts_device_init
{
483 img
=$
(ts_image_init
$1 $2)
484 dev
=$
($TS_CMD_LOSETUP --show -f "$img")
485 if [ "$?" != "0" -o "$dev" = "" ]; then
486 ts_die
"Cannot init device"
489 ts_register_loop_device
"$dev"
493 # call from ts_cleanup_on_exit() only because of TS_LOOP_DEVS maintenance
494 function ts_device_deinit
{
497 if [ -b "$DEV" ]; then
498 $TS_CMD_UMOUNT "$DEV" &> /dev
/null
499 $TS_CMD_LOSETUP -d "$DEV" &> /dev
/null
503 function ts_uuid_by_devname
{
504 echo $
($TS_CMD_BLKID -p -s UUID
-o value
$1)
507 function ts_label_by_devname
{
508 echo $
($TS_CMD_BLKID -p -s LABEL
-o value
$1)
511 function ts_fstype_by_devname
{
512 echo $
($TS_CMD_BLKID -p -s TYPE
-o value
$1)
515 function ts_device_has
{
522 "TYPE") vl
=$
(ts_fstype_by_devname
$DEV);;
523 "LABEL") vl
=$
(ts_label_by_devname
$DEV);;
524 "UUID") vl
=$
(ts_uuid_by_devname
$DEV);;
528 if [ "$vl" == "$VAL" ]; then
534 function ts_device_has_uuid
{
535 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}$'
547 out
=$
($TS_CMD_MOUNT "$@" 2>&1)
549 echo -n "$out" >> $TS_OUTPUT
551 if [ $result != 0 ] \
552 && msg
=$
(echo "$out" |
grep -m1 "unknown filesystem type")
554 # skip only if reported fs correctly and if it's not available
555 fs
=$
(echo "$msg" |
sed -n "s/.*type '\(.*\)'$/\1/p")
556 [ "$fs" = "fs_exp" ] \
557 && grep -qe "[[:space:]]${fs}$" /proc
/filesystems
&>/dev
/null \
563 function ts_is_mounted
{
564 local DEV
=$
(ts_canonicalize
"$1")
566 grep -q $DEV /proc
/mounts
&& return 0
568 if [ "${DEV#/dev/loop/}" != "$DEV" ]; then
569 grep -q "/dev/loop${DEV#/dev/loop/}" /proc
/mounts
&& return 0
574 function ts_fstab_open
{
575 echo "# <!-- util-linux test entry" >> /etc
/fstab
578 function ts_fstab_close
{
579 echo "# -->" >> /etc
/fstab
582 function ts_fstab_addline
{
584 local MNT
=${2:-"$TS_MOUNTPOINT"}
585 local FS
=${3:-"auto"}
586 local OPT
=${4:-"defaults"}
588 echo "$SPEC $MNT $FS $OPT 0 0" >> /etc
/fstab
591 function ts_fstab_add
{
597 function ts_fstab_clean
{
599 /# <!-- util-linux/!b
605 s/# <!-- util-linux.*-->//;
609 function ts_fdisk_clean
{
612 # remove non comparable parts of fdisk output
613 if [ -n "${DEVNAME}" ]; then
614 sed -i -e "s@${DEVNAME}@<removed>@;" $TS_OUTPUT
618 -e 's/Disk identifier:.*/Disk identifier: <removed>/' \
619 -e 's/Created a new.*/Created a new <removed>./' \
620 -e 's/^Device[[:blank:]]*Start/Device Start/' \
621 -e 's/^Device[[:blank:]]*Boot/Device Boot/' \
622 -e 's/Welcome to fdisk.*/Welcome to fdisk <removed>./' \
623 -e 's/typescript file.*/typescript file <removed>./' \
624 -e 's@^\(I/O size (minimum/op.* bytes /\) [1-9][0-9]* @\1 <removed> @' \
628 function ts_scsi_debug_init
{
632 # dry run is not really reliable, real modprobe may still fail
633 modprobe
--dry-run --quiet scsi_debug
&>/dev
/null \
634 || ts_skip
"missing scsi_debug module (dry-run)"
636 # skip if still in use or removal of modules not supported at all
637 modprobe
-r scsi_debug
&>/dev
/null \
638 || ts_skip
"cannot remove scsi_debug module (rmmod)"
640 modprobe
-b scsi_debug
"$@" &>/dev
/null \
641 || ts_skip
"cannot load scsi_debug module (modprobe)"
643 # it might be still not loaded, modprobe.conf or whatever
644 lsmod |
grep -q "^scsi_debug " \
645 || ts_skip
"scsi_debug module not loaded (lsmod)"
649 devname
=$
(grep --with-filename scsi_debug
/sys
/block
/*/device
/model |
awk -F '/' '{print $4}')
650 [ "x${devname}" == "x" ] && ts_die
"cannot find scsi_debug device"
652 TS_DEVICE
="/dev/${devname}"
655 function ts_resolve_host
{
659 # currently we just resolve default records (might be "A", ipv4 only)
660 if type "dig" >/dev
/null
2>&1; then
661 tmp
=$
(dig "$host" +short
2>/dev
/null
) ||
return 1
662 elif type "nslookup" >/dev
/null
2>&1; then
663 tmp
=$
(nslookup "$host" 2>/dev
/null
) ||
return 1
664 tmp
=$
(echo "$tmp"|
grep -A1 "^Name:"|
grep "^Address:"| cut
-d" " -f2)
665 elif type "host" >/dev
/null
2>&1; then
666 tmp
=$
(host "$host" 2>/dev
/null
) ||
return 1
667 tmp
=$
(echo "$tmp" |
grep " has address " | cut
-d " " -f4)
668 elif type "getent" >/dev
/null
2>&1; then
669 tmp
=$
(getent ahosts
"$host" 2>/dev
/null
) ||
return 1
670 tmp
=$
(echo "$tmp" | cut
-d " " -f 1 |
sort -u)
673 # we return 1 if tmp is empty
674 test -n "$tmp" ||
return 1
675 echo "$tmp" |
sort -R |
head -n 1
678 # listen to unix socket (background socat)
679 function ts_init_socket_to_file
{
684 ts_check_prog
"socat"
685 rm -f "$socket" "$outfile"
687 socat
-u UNIX-LISTEN
:$socket,fork
,max-children
=1,backlog
=128 \
688 STDOUT
> "$outfile" &
691 # check for running background process
692 if [ "$pid" -le "0" ] ||
! kill -s 0 "$pid"; then
693 ts_skip
"unable to run socat"
695 # wait for the socket listener
696 if ! socat
-u /dev
/null UNIX-CONNECT
:$socket,retry
=30,interval
=0.1; then
698 ts_skip
"timeout waiting for socket"
701 if ! socat
-u /dev
/null UNIX-CONNECT
:$socket; then
703 ts_skip
"socket stopped listening"