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)"
89 if [ -n "$2" -a -b "$2" ]; then
95 function ts_skip_nonroot
{
96 if [ $UID -ne 0 ]; then
97 ts_skip
"no root permissions"
101 function ts_failed_subtest
{
104 if [ "$TS_KNOWN_FAIL" = "yes" ]; then
109 if [ x
"$1" == x
"" ]; then
110 ts_report
" $msg ($TS_NS)"
112 ts_report
" $msg ($1)"
119 ts_failed_subtest
"$1"
123 function ts_ok_subtest
{
124 if [ x
"$1" == x
"" ]; then
137 echo "$1" >> $TS_OUTPUT
138 [ "$TS_VERBOSE" == "yes" ] && echo "$1"
141 function ts_has_option
{
145 # user may set options by env for a single test or whole component
146 # e.g. TS_OPT_ipcs_limits2_fake="yes" or TS_OPT_ipcs_fake="yes"
147 local v_test
=${TS_TESTNAME//[-.]/_}
148 local v_comp
=${TS_COMPONENT//[-.]/_}
149 local v_name
=${NAME//[-.]/_}
150 eval local env_opt_test
=\
$TS_OPT_${v_comp}_${v_test}_${v_name}
151 eval local env_opt_comp
=\
$TS_OPT_${v_comp}_
${v_name}
152 if [ "$env_opt_test" = "yes" \
153 -o "$env_opt_comp" = "yes" -a "$env_opt_test" != "no" ]; then
156 elif [ "$env_opt_test" = "no" \
157 -o "$env_opt_comp" = "no" -a "$env_opt_test" != "yes" ]; then
161 # or just check the global command line options
162 echo -n $ALL |
sed 's/ //g' |
awk 'BEGIN { FS="="; RS="--" } /('$NAME'$|'$NAME'=)/ { print "yes" }'
165 function ts_option_argument
{
168 echo -n $ALL |
sed 's/ //g' |
awk 'BEGIN { FS="="; RS="--" } /'$NAME'=/ { print $2 }'
171 function ts_init_core_env
{
172 TS_NS
="$TS_COMPONENT/$TS_TESTNAME"
173 TS_OUTPUT
="$TS_OUTDIR/$TS_TESTNAME"
174 TS_VGDUMP
="$TS_OUTDIR/$TS_TESTNAME.vgdump"
175 TS_DIFF
="$TS_DIFFDIR/$TS_TESTNAME"
176 TS_EXPECTED
="$TS_TOPDIR/expected/$TS_NS"
177 TS_MOUNTPOINT
="$TS_OUTDIR/${TS_TESTNAME}-mnt"
180 function ts_init_core_subtest_env
{
181 TS_NS
="$TS_COMPONENT/$TS_TESTNAME-$TS_SUBNAME"
182 TS_OUTPUT
="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME"
183 TS_VGDUMP
="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME.vgdump"
184 TS_DIFF
="$TS_DIFFDIR/$TS_TESTNAME-$TS_SUBNAME"
185 TS_EXPECTED
="$TS_TOPDIR/expected/$TS_NS"
186 TS_MOUNTPOINT
="$TS_OUTDIR/${TS_TESTNAME}-${TS_SUBNAME}-mnt"
188 rm -f $TS_OUTPUT $TS_VGDUMP
189 [ -d "$TS_OUTDIR" ] || mkdir
-p "$TS_OUTDIR"
192 [ -n "$TS_VALGRIND_CMD" ] && touch $TS_VGDUMP
195 function ts_init_env
{
196 local mydir
=$
(ts_abspath
${0%/*})
204 export LANG LANGUAGE LC_ALL CHARSET
206 mydir
=$
(ts_canonicalize
"$mydir")
208 # automake directories
209 top_srcdir
=$
(ts_option_argument
"srcdir" "$*")
210 top_builddir
=$
(ts_option_argument
"builddir" "$*")
212 # where is this script
213 TS_TOPDIR
=$
(ts_abspath
$mydir/..
/..
/)
216 if [ -z "$top_srcdir" ]; then
217 top_srcdir
="$TS_TOPDIR/.."
219 if [ -z "$top_builddir" ]; then
220 top_builddir
="$TS_TOPDIR/.."
223 top_srcdir
=$
(ts_abspath
$top_srcdir)
224 top_builddir
=$
(ts_abspath
$top_builddir)
226 TS_SCRIPT
="$mydir/$(basename $0)"
227 TS_SUBDIR
=$
(dirname $TS_SCRIPT)
228 TS_TESTNAME
=$
(basename $TS_SCRIPT)
229 TS_COMPONENT
=$
(basename $TS_SUBDIR)
236 TS_OUTDIR
="$top_builddir/tests/output/$TS_COMPONENT"
237 TS_DIFFDIR
="$top_builddir/tests/diff/$TS_COMPONENT"
241 TS_VERBOSE
=$
(ts_has_option
"verbose" "$*")
242 TS_PARALLEL
=$
(ts_has_option
"parallel" "$*")
243 TS_KNOWN_FAIL
=$
(ts_has_option
"known-fail" "$*")
244 TS_SKIP_LOOPDEVS
=$
(ts_has_option
"skip-loopdevs" "$*")
246 tmp
=$
( ts_has_option
"memcheck" "$*")
247 if [ "$tmp" == "yes" -a -f /usr
/bin
/valgrind
]; then
248 TS_VALGRIND_CMD
="/usr/bin/valgrind"
251 BLKID_FILE
="$TS_OUTDIR/${TS_TESTNAME}.blkidtab"
253 declare -a TS_SUID_PROGS
254 declare -a TS_SUID_USER
255 declare -a TS_SUID_GROUP
257 if [ -f $TS_TOPDIR/commands.sh
]; then
258 .
$TS_TOPDIR/commands.sh
263 rm -f $TS_OUTPUT $TS_VGDUMP
264 [ -d "$TS_OUTDIR" ] || mkdir
-p "$TS_OUTDIR"
267 [ -n "$TS_VALGRIND_CMD" ] && touch $TS_VGDUMP
269 if [ "$TS_VERBOSE" == "yes" ]; then
271 echo " script: $TS_SCRIPT"
272 echo " sub dir: $TS_SUBDIR"
273 echo " top dir: $TS_TOPDIR"
274 echo " self: $TS_SELF"
275 echo " test name: $TS_TESTNAME"
276 echo " test desc: $TS_DESC"
277 echo " component: $TS_COMPONENT"
278 echo " namespace: $TS_NS"
279 echo " verbose: $TS_VERBOSE"
280 echo " output: $TS_OUTPUT"
281 echo " valgrind: $TS_VGDUMP"
282 echo " expected: $TS_EXPECTED"
283 echo " mountpoint: $TS_MOUNTPOINT"
288 function ts_init_subtest
{
292 ts_init_core_subtest_env
294 [ $TS_NSUBTESTS -eq 0 ] && echo
295 TS_NSUBTESTS
=$
(( $TS_NSUBTESTS + 1 ))
297 if [ "$TS_PARALLEL" == "yes" ]; then
298 TS_TITLE
=$
(printf "%13s: %-30s ...\n%16s: %-27s ..." "$TS_COMPONENT" "$TS_DESC" "" "$TS_SUBNAME")
300 TS_TITLE
=$
(printf "%16s: %-27s ..." "" "$TS_SUBNAME")
308 local is_fake
=$
( ts_has_option
"fake" "$*")
309 local is_force
=$
( ts_has_option
"force" "$*")
311 if [ "$TS_PARALLEL" == "yes" ]; then
312 TS_TITLE
=$
(printf "%13s: %-30s ..." "$TS_COMPONENT" "$TS_DESC")
314 TS_TITLE
=$
(printf "%13s: %-30s ..." "$TS_COMPONENT" "$TS_DESC")
318 [ "$is_fake" == "yes" ] && ts_skip
"fake mode"
319 [ "$TS_OPTIONAL" == "yes" -a "$is_force" != "yes" ] && ts_skip
"optional"
322 function ts_init_suid
{
324 ct
=${#TS_SUID_PROGS[*]}
326 # Save info about original setting
327 TS_SUID_PROGS
[$ct]=$PROG
328 TS_SUID_USER
[$ct]=$
(stat
--printf="%U" $PROG)
329 TS_SUID_GROUP
[$ct]=$
(stat
--printf="%G" $PROG)
331 chown root.root
$PROG &> /dev
/null
332 chmod u
+s
$PROG &> /dev
/null
335 function ts_init_py
{
338 [ -f "$top_builddir/py${LIBNAME}.la" ] || ts_skip
"py${LIBNAME} not compiled"
340 export LD_LIBRARY_PATH
="$top_builddir/.libs:$LD_LIBRARY_PATH"
341 export PYTHONPATH
="$top_builddir/$LIBNAME/python:$top_builddir/.libs:$PYTHONPATH"
343 export PYTHON_VERSION
=$
(awk '/^PYTHON_VERSION/ { print $3 }' $top_builddir/Makefile
)
344 export PYTHON_MAJOR_VERSION
=$
(echo $PYTHON_VERSION |
sed 's/\..*//')
346 export PYTHON
="python${PYTHON_MAJOR_VERSION}"
349 function ts_valgrind
{
350 if [ -z "$TS_VALGRIND_CMD" ]; then
353 $TS_VALGRIND_CMD --tool=memcheck
--leak-check=full \
354 --leak-resolution=high
--num-callers=20 \
355 --log-file="$TS_VGDUMP" $
*
359 function ts_gen_diff
{
362 if [ -s "$TS_OUTPUT" ]; then
364 # remove libtool lt- prefixes
365 sed --in-place 's/^lt\-\(.*\: \)/\1/g' $TS_OUTPUT
367 [ -d "$TS_DIFFDIR" ] || mkdir
-p "$TS_DIFFDIR"
368 diff -u $TS_EXPECTED $TS_OUTPUT > $TS_DIFF
370 if [ -s $TS_DIFF ]; then
381 function tt_gen_mem_report
{
382 [ -z "$TS_VALGRIND_CMD" ] && echo "$1"
384 grep -q -E 'ERROR SUMMARY: [1-9]' $TS_VGDUMP &> /dev
/null
385 if [ $?
-eq 0 ]; then
386 echo "mem-error detected!"
390 function ts_finalize_subtest
{
393 if [ -s "$TS_EXPECTED" ]; then
395 if [ $?
-eq 1 ]; then
396 ts_failed_subtest
"$1"
399 ts_ok_subtest
"$(tt_gen_mem_report "$1")"
402 ts_skip_subtest
"output undefined"
405 [ $res -ne 0 ] && TS_NSUBFAILED
=$
(( $TS_NSUBFAILED + 1 ))
407 # reset environment back to parental test
413 function ts_finalize
{
414 for idx
in $
(seq 0 $
((${#TS_SUID_PROGS[*]} - 1))); do
415 PROG
=${TS_SUID_PROGS[$idx]}
416 chmod a-s
$PROG &> /dev
/null
417 chown
${TS_SUID_USER[$idx]}.
${TS_SUID_GROUP[$idx]} $PROG &> /dev
/null
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"
442 if [ -n "$2" ] && [ -b "$2" ]; then
443 ts_device_deinit
"$2"
444 ts_fstab_clean
# for sure...
449 function ts_image_md5sum
{
450 local img
=${1:-"$TS_OUTDIR/${TS_TESTNAME}.img"}
451 echo $
(md5sum "$img" |
awk '{printf $1}') $
(basename "$img")
454 function ts_image_init
{
455 local mib
=${1:-"5"} # size in MiBs
456 local img
=${2:-"$TS_OUTDIR/${TS_TESTNAME}.img"}
458 dd if=/dev
/zero of
="$img" bs
=1M count
=$mib &> /dev
/null
463 function ts_device_init
{
467 img
=$
(ts_image_init
$1 $2)
468 dev
=$
($TS_CMD_LOSETUP --show -f "$img")
473 function ts_device_deinit
{
476 if [ -b "$DEV" ]; then
477 $TS_CMD_UMOUNT "$DEV" &> /dev
/null
478 $TS_CMD_LOSETUP -d "$DEV" &> /dev
/null
482 function ts_uuid_by_devname
{
483 echo $
($TS_CMD_BLKID -p -s UUID
-o value
$1)
486 function ts_label_by_devname
{
487 echo $
($TS_CMD_BLKID -p -s LABEL
-o value
$1)
490 function ts_fstype_by_devname
{
491 echo $
($TS_CMD_BLKID -p -s TYPE
-o value
$1)
494 function ts_device_has
{
501 "TYPE") vl
=$
(ts_fstype_by_devname
$DEV);;
502 "LABEL") vl
=$
(ts_label_by_devname
$DEV);;
503 "UUID") vl
=$
(ts_uuid_by_devname
$DEV);;
507 if [ "$vl" == "$VAL" ]; then
513 function ts_device_has_uuid
{
514 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}$'
526 out
=$
($TS_CMD_MOUNT "$@" 2>&1)
528 echo -n "$out" >> $TS_OUTPUT
530 if [ $result != 0 ] \
531 && msg
=$
(echo "$out" |
grep -m1 "unknown filesystem type")
533 # skip only if reported fs correctly and if it's not available
534 fs
=$
(echo "$msg" |
sed -n "s/.*type '\(.*\)'$/\1/p")
535 [ "$fs" = "fs_exp" ] \
536 && grep -qe "[[:space:]]${fs}$" /proc
/filesystems
&>/dev
/null \
542 function ts_is_mounted
{
543 local DEV
=$
(ts_canonicalize
"$1")
545 grep -q $DEV /proc
/mounts
&& return 0
547 if [ "${DEV#/dev/loop/}" != "$DEV" ]; then
548 return grep -q "/dev/loop${DEV#/dev/loop/}" /proc
/mounts
553 function ts_fstab_open
{
554 echo "# <!-- util-linux test entry" >> /etc
/fstab
557 function ts_fstab_close
{
558 echo "# -->" >> /etc
/fstab
561 function ts_fstab_addline
{
563 local MNT
=${2:-"$TS_MOUNTPOINT"}
564 local FS
=${3:-"auto"}
565 local OPT
=${4:-"defaults"}
567 echo "$SPEC $MNT $FS $OPT 0 0" >> /etc
/fstab
570 function ts_fstab_add
{
576 function ts_fstab_clean
{
578 /# <!-- util-linux/!b
584 s/# <!-- util-linux.*-->//;
588 function ts_fdisk_clean
{
591 # remove non comparable parts of fdisk output
592 if [ x
"${DEVNAME}" != x
"" ]; then
593 sed -i -e "s:${DEVNAME}:<removed>:g" $TS_OUTPUT
596 sed -i -e 's/Disk identifier:.*/Disk identifier: <removed>/g' \
597 -e 's/Created a new.*/Created a new <removed>./g' \
598 -e 's/^Device[[:blank:]]*Start/Device Start/g' \
599 -e 's/^Device[[:blank:]]*Boot/Device Boot/g' \
600 -e 's/^Device[[:blank:]]*Flag/Device Flag/g' \
601 -e 's/Welcome to fdisk.*/Welcome to fdisk <removed>./g' \
605 function ts_scsi_debug_init
{
609 # dry run is not really reliable, real modprobe may still fail
610 modprobe
--dry-run --quiet scsi_debug
&>/dev
/null \
611 || ts_skip
"missing scsi_debug module (dry-run)"
613 # skip if still in use or removal of modules not supported at all
614 modprobe
-r scsi_debug
&>/dev
/null \
615 || ts_skip
"cannot remove scsi_debug module (rmmod)"
617 modprobe
-b scsi_debug
"$@" &>/dev
/null \
618 || ts_skip
"cannot load scsi_debug module (modprobe)"
620 # it might be still not loaded, modprobe.conf or whatever
621 lsmod |
grep -q "^scsi_debug " \
622 || ts_skip
"scsi_debug module not loaded (lsmod)"
627 devname
=$
(grep --with-filename scsi_debug
/sys
/block
/*/device
/model |
awk -F '/' '{print $4}')
628 [ "x${devname}" == "x" ] && ts_die
"cannot find scsi_debug device"
630 TS_DEVICE
="/dev/${devname}"
633 function ts_resolve_host
{
637 # currently we just resolve default records (might be "A", ipv4 only)
638 if type "dig" >/dev
/null
2>&1; then
639 tmp
=$
(dig "$host" +short
2>/dev
/null
) ||
return 1
640 elif type "nslookup" >/dev
/null
2>&1; then
641 tmp
=$
(nslookup "$host" 2>/dev
/null
) ||
return 1
642 tmp
=$
(echo "$tmp"|
grep -A1 "^Name:"|
grep "^Address:"| cut
-d" " -f2)
643 elif type "host" >/dev
/null
2>&1; then
644 tmp
=$
(host "$host" 2>/dev
/null
) ||
return 1
645 tmp
=$
(echo "$tmp" |
grep " has address " | cut
-d " " -f4)
646 elif type "getent" >/dev
/null
2>&1; then
647 tmp
=$
(getent ahosts
"$host" 2>/dev
/null
) ||
return 1
648 tmp
=$
(echo "$tmp" | cut
-d " " -f 1 |
sort -u)
651 # we return 1 if tmp is empty
652 test -n "$tmp" ||
return 1
653 echo "$tmp" |
sort -R |
head -n 1
656 # listen to unix socket (background socat)
657 function ts_init_socket_to_file
{
662 ts_check_prog
"socat"
663 rm -f "$socket" "$outfile"
665 socat
-u UNIX-LISTEN
:$socket,fork
,max-children
=1,backlog
=128 \
666 STDOUT
> "$outfile" &
669 # check for running background process
670 if [ "$pid" -le "0" ] ||
! kill -s 0 "$pid"; then
671 ts_skip
"unable to run socat"
673 # wait for the socket listener
674 if ! socat
-u /dev
/null UNIX-CONNECT
:$socket,retry
=30,interval
=0.1; then
676 ts_skip
"timeout waiting for socket"
679 if ! socat
-u /dev
/null UNIX-CONNECT
:$socket; then
681 ts_skip
"socket stopped listening"