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
{
35 if [ "$TS_PARALLEL" == "yes" ]; then
42 function ts_check_test_command
{
43 if [ ! -x "$1" ]; then
44 ts_skip
"${1##*/} not found"
48 function ts_check_prog
{
50 type "$cmd" >/dev
/null
2>&1 || ts_skip
"missing in PATH: $cmd"
53 function ts_check_losetup
{
55 ts_check_test_command
"$TS_CMD_LOSETUP"
57 # assuming that losetup -f works ... to be checked somewhere else
58 tmp
=$
($TS_CMD_LOSETUP -f 2>/dev
/null
)
59 if test -b "$tmp"; then
62 ts_skip
"no loop device support"
65 function ts_skip_subtest
{
66 ts_report
" IGNORE ($1)"
71 if [ -n "$2" -a -b "$2" ]; then
77 function ts_skip_nonroot
{
78 if [ $UID -ne 0 ]; then
79 ts_skip
"not root permissions"
83 function ts_failed_subtest
{
86 if [ "$TS_KNOWN_FAIL" = "yes" ]; then
91 if [ x
"$1" == x
"" ]; then
92 ts_report
" $msg ($TS_NS)"
94 ts_report
" $msg ($1)"
101 ts_failed_subtest
"$1"
105 function ts_ok_subtest
{
106 if [ x
"$1" == x
"" ]; then
119 echo "$1" >> $TS_OUTPUT
120 [ "$TS_VERBOSE" == "yes" ] && echo "$1"
123 function ts_has_option
{
127 # user may set options by env for a single test or whole component
128 # e.g. TS_OPT_ipcs_limits2_fake="yes" or TS_OPT_ipcs_fake="yes"
129 local v_test
=${TS_TESTNAME//[-.]/_}
130 local v_comp
=${TS_COMPONENT//[-.]/_}
131 local v_name
=${NAME//[-.]/_}
132 eval local env_opt_test
=\
$TS_OPT_${v_comp}_${v_test}_${v_name}
133 eval local env_opt_comp
=\
$TS_OPT_${v_comp}_
${v_name}
134 if [ "$env_opt_test" = "yes" \
135 -o "$env_opt_comp" = "yes" -a "$env_opt_test" != "no" ]; then
138 elif [ "$env_opt_test" = "no" \
139 -o "$env_opt_comp" = "no" -a "$env_opt_test" != "yes" ]; then
143 # or just check the global command line options
144 echo -n $ALL |
sed 's/ //g' |
awk 'BEGIN { FS="="; RS="--" } /('$NAME'$|'$NAME'=)/ { print "yes" }'
147 function ts_option_argument
{
150 echo -n $ALL |
sed 's/ //g' |
awk 'BEGIN { FS="="; RS="--" } /'$NAME'=/ { print $2 }'
153 function ts_init_core_env
{
154 TS_NS
="$TS_COMPONENT/$TS_TESTNAME"
155 TS_OUTPUT
="$TS_OUTDIR/$TS_TESTNAME"
156 TS_VGDUMP
="$TS_OUTDIR/$TS_TESTNAME.vgdump"
157 TS_DIFF
="$TS_DIFFDIR/$TS_TESTNAME"
158 TS_EXPECTED
="$TS_TOPDIR/expected/$TS_NS"
159 TS_MOUNTPOINT
="$TS_OUTDIR/${TS_TESTNAME}-mnt"
162 function ts_init_core_subtest_env
{
163 TS_NS
="$TS_COMPONENT/$TS_TESTNAME-$TS_SUBNAME"
164 TS_OUTPUT
="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME"
165 TS_VGDUMP
="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME.vgdump"
166 TS_DIFF
="$TS_DIFFDIR/$TS_TESTNAME-$TS_SUBNAME"
167 TS_EXPECTED
="$TS_TOPDIR/expected/$TS_NS"
168 TS_MOUNTPOINT
="$TS_OUTDIR/${TS_TESTNAME}-${TS_SUBNAME}-mnt"
170 rm -f $TS_OUTPUT $TS_VGDUMP
171 [ -d "$TS_OUTDIR" ] || mkdir
-p "$TS_OUTDIR"
174 [ -n "$TS_VALGRIND_CMD" ] && touch $TS_VGDUMP
177 function ts_init_env
{
178 local mydir
=$
(ts_abspath
${0%/*})
186 export LANG LANGUAGE LC_ALL CHARSET
188 mydir
=$
(ts_canonicalize
"$mydir")
190 # automake directories
191 top_srcdir
=$
(ts_option_argument
"srcdir" "$*")
192 top_builddir
=$
(ts_option_argument
"builddir" "$*")
194 # where is this script
195 TS_TOPDIR
=$
(ts_abspath
$mydir/..
/..
/)
198 if [ -z "$top_srcdir" ]; then
199 top_srcdir
="$TS_TOPDIR/.."
201 if [ -z "$top_builddir" ]; then
202 top_builddir
="$TS_TOPDIR/.."
205 top_srcdir
=$
(ts_abspath
$top_srcdir)
206 top_builddir
=$
(ts_abspath
$top_builddir)
208 TS_SCRIPT
="$mydir/$(basename $0)"
209 TS_SUBDIR
=$
(dirname $TS_SCRIPT)
210 TS_TESTNAME
=$
(basename $TS_SCRIPT)
211 TS_COMPONENT
=$
(basename $TS_SUBDIR)
218 TS_OUTDIR
="$top_builddir/tests/output/$TS_COMPONENT"
219 TS_DIFFDIR
="$top_builddir/tests/diff/$TS_COMPONENT"
223 TS_VERBOSE
=$
(ts_has_option
"verbose" "$*")
224 TS_PARALLEL
=$
(ts_has_option
"parallel" "$*")
225 TS_KNOWN_FAIL
=$
(ts_has_option
"known-fail" "$*")
227 tmp
=$
( ts_has_option
"memcheck" "$*")
228 if [ "$tmp" == "yes" -a -f /usr
/bin
/valgrind
]; then
229 TS_VALGRIND_CMD
="/usr/bin/valgrind"
232 BLKID_FILE
="$TS_OUTDIR/${TS_TESTNAME}.blkidtab"
234 declare -a TS_SUID_PROGS
235 declare -a TS_SUID_USER
236 declare -a TS_SUID_GROUP
238 if [ -f $TS_TOPDIR/commands.sh
]; then
239 .
$TS_TOPDIR/commands.sh
244 rm -f $TS_OUTPUT $TS_VGDUMP
245 [ -d "$TS_OUTDIR" ] || mkdir
-p "$TS_OUTDIR"
248 [ -n "$TS_VALGRIND_CMD" ] && touch $TS_VGDUMP
250 if [ "$TS_VERBOSE" == "yes" ]; then
252 echo " script: $TS_SCRIPT"
253 echo " sub dir: $TS_SUBDIR"
254 echo " top dir: $TS_TOPDIR"
255 echo " self: $TS_SELF"
256 echo " test name: $TS_TESTNAME"
257 echo " test desc: $TS_DESC"
258 echo " component: $TS_COMPONENT"
259 echo " namespace: $TS_NS"
260 echo " verbose: $TS_VERBOSE"
261 echo " output: $TS_OUTPUT"
262 echo " valgrind: $TS_VGDUMP"
263 echo " expected: $TS_EXPECTED"
264 echo " mountpoint: $TS_MOUNTPOINT"
269 function ts_init_subtest
{
273 ts_init_core_subtest_env
275 [ $TS_NSUBTESTS -eq 0 ] && echo
276 TS_NSUBTESTS
=$
(( $TS_NSUBTESTS + 1 ))
278 if [ "$TS_PARALLEL" == "yes" ]; then
279 TS_TITLE
=$
(printf "%13s: %-30s ...\n%16s: %-27s ..." "$TS_COMPONENT" "$TS_DESC" "" "$TS_SUBNAME")
281 TS_TITLE
=$
(printf "%16s: %-27s ..." "" "$TS_SUBNAME")
289 local is_fake
=$
( ts_has_option
"fake" "$*")
290 local is_force
=$
( ts_has_option
"force" "$*")
292 if [ "$TS_PARALLEL" == "yes" ]; then
293 TS_TITLE
=$
(printf "%13s: %-30s ..." "$TS_COMPONENT" "$TS_DESC")
295 TS_TITLE
=$
(printf "%13s: %-30s ..." "$TS_COMPONENT" "$TS_DESC")
299 [ "$is_fake" == "yes" ] && ts_skip
"fake mode"
300 [ "$TS_OPTIONAL" == "yes" -a "$is_force" != "yes" ] && ts_skip
"optional"
303 function ts_init_suid
{
305 ct
=${#TS_SUID_PROGS[*]}
307 # Save info about original setting
308 TS_SUID_PROGS
[$ct]=$PROG
309 TS_SUID_USER
[$ct]=$
(stat
--printf="%U" $PROG)
310 TS_SUID_GROUP
[$ct]=$
(stat
--printf="%G" $PROG)
312 chown root.root
$PROG &> /dev
/null
313 chmod u
+s
$PROG &> /dev
/null
316 function ts_init_py
{
319 [ -f "$TS_TOPDIR/../py${LIBNAME}.la" ] || ts_skip
"py${LIBNAME} not compiled"
321 export LD_LIBRARY_PATH
="$TS_TOPDIR/../.libs"
322 export PYTHONPATH
="$TS_TOPDIR/../$LIBNAME/python:$TS_TOPDIR/../.libs"
324 export PYTHON_VERSION
=$
(awk '/^PYTHON_VERSION/ { print $3 }' $top_builddir/Makefile
)
325 export PYTHON_MAJOR_VERSION
=$
(echo $PYTHON_VERSION |
sed 's/\..*//')
327 export PYTHON
="python${PYTHON_MAJOR_VERSION}"
330 function ts_valgrind
{
331 if [ -z "$TS_VALGRIND_CMD" ]; then
334 $TS_VALGRIND_CMD --tool=memcheck
--leak-check=full \
335 --leak-resolution=high
--num-callers=20 \
336 --log-file="$TS_VGDUMP" $
*
340 function ts_gen_diff
{
343 if [ -s "$TS_OUTPUT" ]; then
345 # remove libtool lt- prefixes
346 sed --in-place 's/^lt\-\(.*\: \)/\1/g' $TS_OUTPUT
348 [ -d "$TS_DIFFDIR" ] || mkdir
-p "$TS_DIFFDIR"
349 diff -u $TS_EXPECTED $TS_OUTPUT > $TS_DIFF
351 if [ -s $TS_DIFF ]; then
362 function tt_gen_mem_report
{
363 [ -z "$TS_VALGRIND_CMD" ] && echo "$1"
365 grep -q -E 'ERROR SUMMARY: [1-9]' $TS_VGDUMP &> /dev
/null
366 if [ $?
-eq 0 ]; then
367 echo "mem-error detected!"
371 function ts_finalize_subtest
{
374 if [ -s "$TS_EXPECTED" ]; then
376 if [ $?
-eq 1 ]; then
377 ts_failed_subtest
"$1"
380 ts_ok_subtest
"$(tt_gen_mem_report "$1")"
383 ts_skip_subtest
"output undefined"
386 [ $res -ne 0 ] && TS_NSUBFAILED
=$
(( $TS_NSUBFAILED + 1 ))
388 # reset environment back to parental test
394 function ts_finalize
{
395 for idx
in $
(seq 0 $
((${#TS_SUID_PROGS[*]} - 1))); do
396 PROG
=${TS_SUID_PROGS[$idx]}
397 chmod a-s
$PROG &> /dev
/null
398 chown
${TS_SUID_USER[$idx]}.
${TS_SUID_GROUP[$idx]} $PROG &> /dev
/null
401 if [ $TS_NSUBTESTS -ne 0 ]; then
403 if [ $TS_NSUBFAILED -ne 0 ]; then
404 ts_failed
"$TS_NSUBFAILED from $TS_NSUBTESTS sub-tests"
406 ts_ok
"all $TS_NSUBTESTS sub-tests PASSED"
410 if [ -s $TS_EXPECTED ]; then
412 if [ $?
-eq 1 ]; then
418 ts_skip
"output undefined"
423 if [ -n "$2" ] && [ -b "$2" ]; then
424 ts_device_deinit
"$2"
425 ts_fstab_clean
# for sure...
430 function ts_image_md5sum
{
431 local img
=${1:-"$TS_OUTDIR/${TS_TESTNAME}.img"}
432 echo $
(md5sum "$img" |
awk '{printf $1}') $
(basename "$img")
435 function ts_image_init
{
436 local mib
=${1:-"5"} # size in MiBs
437 local img
=${2:-"$TS_OUTDIR/${TS_TESTNAME}.img"}
439 dd if=/dev
/zero of
="$img" bs
=1M count
=$mib &> /dev
/null
444 function ts_device_init
{
448 img
=$
(ts_image_init
$1 $2)
449 dev
=$
($TS_CMD_LOSETUP --show -f "$img")
454 function ts_device_deinit
{
457 if [ -b "$DEV" ]; then
458 $TS_CMD_UMOUNT "$DEV" &> /dev
/null
459 $TS_CMD_LOSETUP -d "$DEV" &> /dev
/null
463 function ts_uuid_by_devname
{
464 echo $
($TS_CMD_BLKID -p -s UUID
-o value
$1)
467 function ts_label_by_devname
{
468 echo $
($TS_CMD_BLKID -p -s LABEL
-o value
$1)
471 function ts_fstype_by_devname
{
472 echo $
($TS_CMD_BLKID -p -s TYPE
-o value
$1)
475 function ts_device_has
{
482 "TYPE") vl
=$
(ts_fstype_by_devname
$DEV);;
483 "LABEL") vl
=$
(ts_label_by_devname
$DEV);;
484 "UUID") vl
=$
(ts_uuid_by_devname
$DEV);;
488 if [ "$vl" == "$VAL" ]; then
494 function ts_device_has_uuid
{
495 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}$'
507 out
=$
($TS_CMD_MOUNT "$@" 2>&1)
509 echo -n "$out" >> $TS_OUTPUT
511 if [ $result != 0 ] \
512 && msg
=$
(echo "$out" |
grep -m1 "unknown filesystem type")
514 # skip only if reported fs correctly and if it's not available
515 fs
=$
(echo "$msg" |
sed -n "s/.*type '\(.*\)'$/\1/p")
516 [ "$fs" = "fs_exp" ] \
517 && grep -qe "[[:space:]]${fs}$" /proc
/filesystems
&>/dev
/null \
523 function ts_is_mounted
{
524 local DEV
=$
(ts_canonicalize
"$1")
526 grep -q $DEV /proc
/mounts
&& return 0
528 if [ "${DEV#/dev/loop/}" != "$DEV" ]; then
529 return grep -q "/dev/loop${DEV#/dev/loop/}" /proc
/mounts
534 function ts_fstab_open
{
535 echo "# <!-- util-linux test entry" >> /etc
/fstab
538 function ts_fstab_close
{
539 echo "# -->" >> /etc
/fstab
542 function ts_fstab_addline
{
544 local MNT
=${2:-"$TS_MOUNTPOINT"}
545 local FS
=${3:-"auto"}
546 local OPT
=${4:-"defaults"}
548 echo "$SPEC $MNT $FS $OPT 0 0" >> /etc
/fstab
551 function ts_fstab_add
{
557 function ts_fstab_clean
{
559 /# <!-- util-linux/!b
565 s/# <!-- util-linux.*-->//;
569 function ts_fdisk_clean
{
572 # remove non comparable parts of fdisk output
573 if [ x
"${DEVNAME}" != x
"" ]; then
574 sed -i -e "s:${DEVNAME}:<removed>:g" $TS_OUTPUT
577 sed -i -e 's/Disk identifier:.*/Disk identifier: <removed>/g' \
578 -e 's/Created a new.*/Created a new <removed>./g' \
579 -e 's/^Device[[:blank:]]*Start/Device Start/g' \
580 -e 's/^Device[[:blank:]]*Boot/Device Boot/g' \
581 -e 's/^Device[[:blank:]]*Flag/Device Flag/g' \
582 -e 's/Welcome to fdisk.*/Welcome to fdisk <removed>./g' \
586 function ts_scsi_debug_init
{
590 # dry run is not realy reliable, real modprobe may still fail
591 modprobe
--dry-run --quiet scsi_debug
&>/dev
/null \
592 || ts_skip
"missing scsi_debug module (dry-run)"
594 # skip if still in use or removal of modules not supported at all
595 modprobe
-r scsi_debug
&>/dev
/null \
596 || ts_skip
"cannot remove scsi_debug module (rmmod)"
598 modprobe
-b scsi_debug $
* &>/dev
/null \
599 || ts_skip
"cannot load scsi_debug module (modprobe)"
601 # it might be still not loaded, modprobe.conf or whatever
602 lsmod |
grep -q "^scsi_debug " \
603 || ts_skip
"scsi_debug module not loaded (lsmod)"
605 devname
=$
(grep --with-filename scsi_debug
/sys
/block
/*/device
/model |
awk -F '/' '{print $4}')
606 [ "x${devname}" == "x" ] && ts_die
"cannot find scsi_debug device"
611 TS_DEVICE
="/dev/${devname}"