]> git.ipfire.org Git - thirdparty/util-linux.git/blame - tests/functions.sh
build-sys: add non-widechar.conf
[thirdparty/util-linux.git] / tests / functions.sh
CommitLineData
92f2c23e
KZ
1#
2# Copyright (C) 2007 Karel Zak <kzak@redhat.com>
3#
601d12fb 4# This file is part of util-linux.
92f2c23e
KZ
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#
e83446da 16
e83446da 17
a02f320d
KZ
18function ts_abspath {
19 cd $1
20 pwd
21}
d42bbae5 22
6dfe6f0e
KZ
23function 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
723c7043
SK
34function 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
f0c60dff
KZ
48function 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
40e6f7a0 58function ts_report {
2979724f
RM
59 local desc=
60
855f7f06 61 if [ "$TS_PARSABLE" != "yes" ]; then
2979724f
RM
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")
40e6f7a0 71 else
2979724f 72 desc=$TS_DESC
40e6f7a0 73 fi
2979724f 74 printf "%13s: %-45s ...%s\n" "$TS_COMPONENT" "$desc" "$1"
2f791546 75}
40e6f7a0 76
2f791546
SK
77function ts_check_test_command {
78 if [ ! -x "$1" ]; then
79 ts_skip "${1##*/} not found"
80 fi
40e6f7a0
SK
81}
82
d1962aae
RM
83function ts_check_prog {
84 local cmd=$1
85 type "$cmd" >/dev/null 2>&1 || ts_skip "missing in PATH: $cmd"
86}
87
5ec15aef
RM
88function ts_check_losetup {
89 local tmp
90 ts_check_test_command "$TS_CMD_LOSETUP"
91
d995c2f0 92 if [ "$TS_SKIP_LOOPDEVS" = "yes" ]; then
5f708381
RM
93 ts_skip "loop-device tests disabled"
94 fi
95
5ec15aef
RM
96 # assuming that losetup -f works ... to be checked somewhere else
97 tmp=$($TS_CMD_LOSETUP -f 2>/dev/null)
98 if test -b "$tmp"; then
99 return 0
100 fi
0209a128 101 ts_skip "no loop-device support"
5ec15aef
RM
102}
103
94fa9b46 104function ts_report_skip {
0209a128 105 ts_report " SKIPPED ($1)"
09888efe
KZ
106}
107
108function ts_skip {
94fa9b46 109 ts_report_skip "$1"
cbae7931 110
caf31605 111 ts_cleanup_on_exit
b30cd7ee
KZ
112 exit 0
113}
114
09892fb6 115function ts_skip_nonroot {
f0b561b6 116 if [ $UID -ne 0 ]; then
0209a128 117 ts_skip "no root permissions"
09892fb6
KZ
118 fi
119}
120
09888efe 121function ts_failed_subtest {
733094a8
RM
122 local msg="FAILED"
123 local ret=1
124 if [ "$TS_KNOWN_FAIL" = "yes" ]; then
125 msg="KNOWN FAILED"
126 ret=0
127 fi
128
05de8126 129 if [ x"$1" == x"" ]; then
733094a8 130 ts_report " $msg ($TS_NS)"
05de8126 131 else
733094a8 132 ts_report " $msg ($1)"
05de8126 133 fi
733094a8
RM
134
135 return $ret
05de8126
KZ
136}
137
09888efe
KZ
138function ts_failed {
139 ts_failed_subtest "$1"
733094a8 140 exit $?
7641ccec
RM
141}
142
94fa9b46 143function ts_report_ok {
05de8126 144 if [ x"$1" == x"" ]; then
40e6f7a0 145 ts_report " OK"
05de8126 146 else
40e6f7a0 147 ts_report " OK ($1)"
05de8126 148 fi
09888efe
KZ
149}
150
151function ts_ok {
94fa9b46 152 ts_report_ok "$1"
05de8126
KZ
153 exit 0
154}
155
57a917d6
KZ
156function ts_log {
157 echo "$1" >> $TS_OUTPUT
158 [ "$TS_VERBOSE" == "yes" ] && echo "$1"
159}
160
1d9acab1
KZ
161function ts_has_option {
162 NAME="$1"
163 ALL="$2"
eac40eb0
RM
164
165 # user may set options by env for a single test or whole component
166 # e.g. TS_OPT_ipcs_limits2_fake="yes" or TS_OPT_ipcs_fake="yes"
c08863ff
RM
167 local v_test=${TS_TESTNAME//[-.]/_}
168 local v_comp=${TS_COMPONENT//[-.]/_}
169 local v_name=${NAME//[-.]/_}
170 eval local env_opt_test=\$TS_OPT_${v_comp}_${v_test}_${v_name}
171 eval local env_opt_comp=\$TS_OPT_${v_comp}_${v_name}
eac40eb0
RM
172 if [ "$env_opt_test" = "yes" \
173 -o "$env_opt_comp" = "yes" -a "$env_opt_test" != "no" ]; then
174 echo "yes"
175 return
176 elif [ "$env_opt_test" = "no" \
177 -o "$env_opt_comp" = "no" -a "$env_opt_test" != "yes" ]; then
178 return
179 fi
180
181 # or just check the global command line options
855f7f06
RM
182 if [[ $ALL =~ ([$' \t\n']|^)--$NAME([$'= \t\n']|$) ]]; then
183 echo yes
184 return
185 fi
186
187 # or the _global_ env, e.g TS_OPT_parsable="yes"
188 eval local env_opt=\$TS_OPT_${v_name}
189 if [ "$env_opt" = "yes" ]; then echo "yes"; fi
1d9acab1
KZ
190}
191
1b03e2cd
KZ
192function ts_option_argument {
193 NAME="$1"
194 ALL="$2"
4303124a
RM
195
196 # last option wins!
197 echo "$ALL" | sed -n "s/.*[ \t\n]--$NAME=\([^ \t\n]*\).*/\1/p" | tail -n 1
1b03e2cd
KZ
198}
199
db17c74b 200function ts_init_core_env {
2979724f 201 TS_SUBNAME=""
db17c74b
KZ
202 TS_NS="$TS_COMPONENT/$TS_TESTNAME"
203 TS_OUTPUT="$TS_OUTDIR/$TS_TESTNAME"
495674f8 204 TS_VGDUMP="$TS_OUTDIR/$TS_TESTNAME.vgdump"
db17c74b
KZ
205 TS_DIFF="$TS_DIFFDIR/$TS_TESTNAME"
206 TS_EXPECTED="$TS_TOPDIR/expected/$TS_NS"
207 TS_MOUNTPOINT="$TS_OUTDIR/${TS_TESTNAME}-mnt"
208}
209
210function ts_init_core_subtest_env {
211 TS_NS="$TS_COMPONENT/$TS_TESTNAME-$TS_SUBNAME"
212 TS_OUTPUT="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME"
495674f8 213 TS_VGDUMP="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME.vgdump"
db17c74b
KZ
214 TS_DIFF="$TS_DIFFDIR/$TS_TESTNAME-$TS_SUBNAME"
215 TS_EXPECTED="$TS_TOPDIR/expected/$TS_NS"
64a2331f 216 TS_MOUNTPOINT="$TS_OUTDIR/${TS_TESTNAME}-${TS_SUBNAME}-mnt"
495674f8
KZ
217
218 rm -f $TS_OUTPUT $TS_VGDUMP
4210c47a
KZ
219 [ -d "$TS_OUTDIR" ] || mkdir -p "$TS_OUTDIR"
220
495674f8
KZ
221 touch $TS_OUTPUT
222 [ -n "$TS_VALGRIND_CMD" ] && touch $TS_VGDUMP
db17c74b
KZ
223}
224
905d0b9b 225function ts_init_env {
e130ce53 226 local mydir=$(ts_abspath ${0%/*})
d9a9ff09 227 local tmp
d42bbae5 228
08b825db
YD
229 LANG="POSIX"
230 LANGUAGE="POSIX"
231 LC_ALL="POSIX"
232 CHARSET="UTF-8"
233
1b03e2cd
KZ
234 export LANG LANGUAGE LC_ALL CHARSET
235
6dfe6f0e
KZ
236 mydir=$(ts_canonicalize "$mydir")
237
1b03e2cd
KZ
238 # automake directories
239 top_srcdir=$(ts_option_argument "srcdir" "$*")
240 top_builddir=$(ts_option_argument "builddir" "$*")
d42bbae5 241
1b03e2cd 242 # where is this script
a02f320d 243 TS_TOPDIR=$(ts_abspath $mydir/../../)
1b03e2cd
KZ
244
245 # default
246 if [ -z "$top_srcdir" ]; then
247 top_srcdir="$TS_TOPDIR/.."
248 fi
249 if [ -z "$top_builddir" ]; then
250 top_builddir="$TS_TOPDIR/.."
251 fi
252
253 top_srcdir=$(ts_abspath $top_srcdir)
254 top_builddir=$(ts_abspath $top_builddir)
255
4ba0bfbb
RM
256 # some ul commands search other ul commands in $PATH
257 export PATH="$top_builddir:$PATH"
258
a02f320d 259 TS_SCRIPT="$mydir/$(basename $0)"
d42bbae5
KZ
260 TS_SUBDIR=$(dirname $TS_SCRIPT)
261 TS_TESTNAME=$(basename $TS_SCRIPT)
262 TS_COMPONENT=$(basename $TS_SUBDIR)
98604ef9 263 TS_DESC=${TS_DESC:-$TS_TESTNAME}
d42bbae5 264
09888efe
KZ
265 TS_NSUBTESTS=0
266 TS_NSUBFAILED=0
267
d42bbae5
KZ
268 TS_SELF="$TS_SUBDIR"
269
1b03e2cd
KZ
270 TS_OUTDIR="$top_builddir/tests/output/$TS_COMPONENT"
271 TS_DIFFDIR="$top_builddir/tests/diff/$TS_COMPONENT"
db17c74b
KZ
272
273 ts_init_core_env
d42bbae5 274
cbac71bd 275 TS_VERBOSE=$(ts_has_option "verbose" "$*")
40e6f7a0 276 TS_PARALLEL=$(ts_has_option "parallel" "$*")
7641ccec 277 TS_KNOWN_FAIL=$(ts_has_option "known-fail" "$*")
d995c2f0 278 TS_SKIP_LOOPDEVS=$(ts_has_option "skip-loopdevs" "$*")
855f7f06
RM
279 TS_PARSABLE=$(ts_has_option "parsable" "$*")
280 [ "$TS_PARSABLE" = "yes" ] || TS_PARSABLE="$TS_PARALLEL"
d42bbae5 281
d9a9ff09
RM
282 tmp=$( ts_has_option "memcheck" "$*")
283 if [ "$tmp" == "yes" -a -f /usr/bin/valgrind ]; then
284 TS_VALGRIND_CMD="/usr/bin/valgrind"
285 fi
286
632830cc 287 BLKID_FILE="$TS_OUTDIR/${TS_TESTNAME}.blkidtab"
d42bbae5 288
1b5417ac
KZ
289 declare -a TS_SUID_PROGS
290 declare -a TS_SUID_USER
291 declare -a TS_SUID_GROUP
cbae7931 292 declare -a TS_LOOP_DEVS
1b5417ac 293
adc8c80f
KZ
294 if [ -f $TS_TOPDIR/commands.sh ]; then
295 . $TS_TOPDIR/commands.sh
296 fi
a02f320d 297
b7ea07e0 298 export BLKID_FILE
66822df3 299
495674f8 300 rm -f $TS_OUTPUT $TS_VGDUMP
4210c47a
KZ
301 [ -d "$TS_OUTDIR" ] || mkdir -p "$TS_OUTDIR"
302
3dfa278e 303 touch $TS_OUTPUT
495674f8 304 [ -n "$TS_VALGRIND_CMD" ] && touch $TS_VGDUMP
97cdb3cb 305
d42bbae5
KZ
306 if [ "$TS_VERBOSE" == "yes" ]; then
307 echo
308 echo " script: $TS_SCRIPT"
d42bbae5 309 echo " sub dir: $TS_SUBDIR"
a02f320d 310 echo " top dir: $TS_TOPDIR"
d42bbae5
KZ
311 echo " self: $TS_SELF"
312 echo " test name: $TS_TESTNAME"
313 echo " test desc: $TS_DESC"
314 echo " component: $TS_COMPONENT"
315 echo " namespace: $TS_NS"
316 echo " verbose: $TS_VERBOSE"
317 echo " output: $TS_OUTPUT"
495674f8 318 echo " valgrind: $TS_VGDUMP"
d42bbae5
KZ
319 echo " expected: $TS_EXPECTED"
320 echo " mountpoint: $TS_MOUNTPOINT"
321 echo
322 fi
905d0b9b
KZ
323}
324
09888efe
KZ
325function ts_init_subtest {
326
327 TS_SUBNAME="$1"
db17c74b 328 ts_init_core_subtest_env
09888efe
KZ
329 TS_NSUBTESTS=$(( $TS_NSUBTESTS + 1 ))
330
855f7f06 331 if [ "$TS_PARSABLE" != "yes" ]; then
2979724f
RM
332 [ $TS_NSUBTESTS -eq 1 ] && echo
333 printf "%16s: %-27s ..." "" "$TS_SUBNAME"
40e6f7a0 334 fi
09888efe
KZ
335}
336
905d0b9b 337function ts_init {
d9a9ff09
RM
338 ts_init_env "$*"
339
905d0b9b 340 local is_fake=$( ts_has_option "fake" "$*")
949cf64b 341 local is_force=$( ts_has_option "force" "$*")
905d0b9b 342
855f7f06 343 if [ "$TS_PARSABLE" != "yes" ]; then
2979724f 344 printf "%13s: %-30s ..." "$TS_COMPONENT" "$TS_DESC"
40e6f7a0 345 fi
d42bbae5
KZ
346
347 [ "$is_fake" == "yes" ] && ts_skip "fake mode"
949cf64b 348 [ "$TS_OPTIONAL" == "yes" -a "$is_force" != "yes" ] && ts_skip "optional"
e83446da
KZ
349}
350
1b5417ac
KZ
351function ts_init_suid {
352 PROG="$1"
353 ct=${#TS_SUID_PROGS[*]}
354
355 # Save info about original setting
356 TS_SUID_PROGS[$ct]=$PROG
357 TS_SUID_USER[$ct]=$(stat --printf="%U" $PROG)
358 TS_SUID_GROUP[$ct]=$(stat --printf="%G" $PROG)
359
360 chown root.root $PROG &> /dev/null
361 chmod u+s $PROG &> /dev/null
362}
363
a731b541
KZ
364function ts_init_py {
365 LIBNAME="$1"
366
15b2606c 367 [ -f "$top_builddir/py${LIBNAME}.la" ] || ts_skip "py${LIBNAME} not compiled"
a731b541 368
fa8f8b74
RM
369 export LD_LIBRARY_PATH="$top_builddir/.libs:$LD_LIBRARY_PATH"
370 export PYTHONPATH="$top_builddir/$LIBNAME/python:$top_builddir/.libs:$PYTHONPATH"
46407453
OO
371
372 export PYTHON_VERSION=$(awk '/^PYTHON_VERSION/ { print $3 }' $top_builddir/Makefile)
373 export PYTHON_MAJOR_VERSION=$(echo $PYTHON_VERSION | sed 's/\..*//')
374
375 export PYTHON="python${PYTHON_MAJOR_VERSION}"
a731b541
KZ
376}
377
495674f8
KZ
378function ts_valgrind {
379 if [ -z "$TS_VALGRIND_CMD" ]; then
88a5f90e 380 "$@"
495674f8
KZ
381 else
382 $TS_VALGRIND_CMD --tool=memcheck --leak-check=full \
383 --leak-resolution=high --num-callers=20 \
88a5f90e 384 --log-file="$TS_VGDUMP" "$@"
495674f8
KZ
385 fi
386}
387
09888efe
KZ
388function ts_gen_diff {
389 local res=0
390
7e604f3c
RM
391 [ -f "$TS_OUTPUT" ] || return 1
392 [ -f "$TS_EXPECTED" ] || TS_EXPECTED=/dev/null
4210c47a 393
7e604f3c
RM
394 # remove libtool lt- prefixes
395 sed --in-place 's/^lt\-\(.*\: \)/\1/g' $TS_OUTPUT
46949388 396
7e604f3c
RM
397 [ -d "$TS_DIFFDIR" ] || mkdir -p "$TS_DIFFDIR"
398 diff -u $TS_EXPECTED $TS_OUTPUT > $TS_DIFF
4210c47a 399
7e604f3c 400 if [ $? -ne 0 ] || [ -s $TS_DIFF ]; then
09888efe 401 res=1
7e604f3c
RM
402 else
403 rm -f $TS_DIFF;
09888efe 404 fi
7e604f3c 405
09888efe
KZ
406 return $res
407}
408
495674f8
KZ
409function tt_gen_mem_report {
410 [ -z "$TS_VALGRIND_CMD" ] && echo "$1"
411
412 grep -q -E 'ERROR SUMMARY: [1-9]' $TS_VGDUMP &> /dev/null
413 if [ $? -eq 0 ]; then
414 echo "mem-error detected!"
415 fi
416}
417
09888efe 418function ts_finalize_subtest {
e83446da
KZ
419 local res=0
420
7e604f3c
RM
421 ts_gen_diff
422 if [ $? -eq 1 ]; then
423 ts_failed_subtest "$1"
424 res=1
09888efe 425 else
94fa9b46 426 ts_report_ok "$(tt_gen_mem_report "$1")"
09888efe
KZ
427 fi
428
429 [ $res -ne 0 ] && TS_NSUBFAILED=$(( $TS_NSUBFAILED + 1 ))
db17c74b
KZ
430
431 # reset environment back to parental test
432 ts_init_core_env
433
09888efe
KZ
434 return $res
435}
436
94fa9b46
KZ
437function ts_skip_subtest {
438 ts_report_skip "$1"
439 # reset environment back to parental test
440 ts_init_core_env
441
442}
443
09888efe 444function ts_finalize {
caf31605 445 ts_cleanup_on_exit
1b5417ac 446
09888efe 447 if [ $TS_NSUBTESTS -ne 0 ]; then
7e604f3c 448 if ! ts_gen_diff || [ $TS_NSUBFAILED -ne 0 ]; then
09888efe
KZ
449 ts_failed "$TS_NSUBFAILED from $TS_NSUBTESTS sub-tests"
450 else
451 ts_ok "all $TS_NSUBTESTS sub-tests PASSED"
452 fi
e83446da 453 fi
425ca40a 454
7e604f3c
RM
455 ts_gen_diff || ts_failed "$1"
456 ts_ok "$1"
e83446da
KZ
457}
458
3dfa278e 459function ts_die {
57a917d6 460 ts_log "$1"
3dfa278e
KZ
461 ts_finalize
462}
463
caf31605
RM
464function ts_cleanup_on_exit {
465
466 for idx in $(seq 0 $((${#TS_SUID_PROGS[*]} - 1))); do
467 PROG=${TS_SUID_PROGS[$idx]}
468 chmod a-s $PROG &> /dev/null
469 chown ${TS_SUID_USER[$idx]}.${TS_SUID_GROUP[$idx]} $PROG &> /dev/null
470 done
cbae7931
RM
471
472 for dev in "${TS_LOOP_DEVS[@]}"; do
473 ts_device_deinit "$dev"
474 done
475 unset TS_LOOP_DEVS
5c711ba9
RM
476
477 ts_scsi_debug_rmmod
caf31605
RM
478}
479
35c636e1
KZ
480function ts_image_md5sum {
481 local img=${1:-"$TS_OUTDIR/${TS_TESTNAME}.img"}
c3f323cb 482 echo $("$TS_HELPER_MD5" < "$img") $(basename "$img")
35c636e1 483}
05de8126 484
35c636e1
KZ
485function ts_image_init {
486 local mib=${1:-"5"} # size in MiBs
487 local img=${2:-"$TS_OUTDIR/${TS_TESTNAME}.img"}
2f791546 488
1cb10736
RM
489 rm -f $img
490 truncate -s "${mib}M" "$img"
35c636e1
KZ
491 echo "$img"
492 return 0
493}
05de8126 494
cbae7931
RM
495function ts_register_loop_device {
496 local ct=${#TS_LOOP_DEVS[*]}
497 TS_LOOP_DEVS[$ct]=$1
498}
499
35c636e1 500function ts_device_init {
b5eb5097
RM
501 local img
502 local dev
df7e52d7 503
b5eb5097
RM
504 img=$(ts_image_init $1 $2)
505 dev=$($TS_CMD_LOSETUP --show -f "$img")
cbae7931
RM
506 if [ "$?" != "0" -o "$dev" = "" ]; then
507 ts_die "Cannot init device"
508 fi
df7e52d7 509
cbae7931
RM
510 ts_register_loop_device "$dev"
511 TS_LODEV=$dev
df7e52d7
KZ
512}
513
cbae7931 514# call from ts_cleanup_on_exit() only because of TS_LOOP_DEVS maintenance
df7e52d7 515function ts_device_deinit {
3dfa278e
KZ
516 local DEV="$1"
517
518 if [ -b "$DEV" ]; then
519 $TS_CMD_UMOUNT "$DEV" &> /dev/null
520 $TS_CMD_LOSETUP -d "$DEV" &> /dev/null
df7e52d7
KZ
521 fi
522}
064b8c38 523
3f5bda01 524function ts_uuid_by_devname {
283a8c15 525 echo $($TS_CMD_BLKID -p -s UUID -o value $1)
3f5bda01
KZ
526}
527
528function ts_label_by_devname {
283a8c15 529 echo $($TS_CMD_BLKID -p -s LABEL -o value $1)
3f5bda01
KZ
530}
531
532function ts_fstype_by_devname {
283a8c15 533 echo $($TS_CMD_BLKID -p -s TYPE -o value $1)
3f5bda01
KZ
534}
535
536function ts_device_has {
537 local TAG="$1"
538 local VAL="$2"
539 local DEV="$3"
3dfa278e 540 local vl=""
3f5bda01
KZ
541
542 case $TAG in
543 "TYPE") vl=$(ts_fstype_by_devname $DEV);;
544 "LABEL") vl=$(ts_label_by_devname $DEV);;
545 "UUID") vl=$(ts_uuid_by_devname $DEV);;
546 *) return 1;;
547 esac
548
549 if [ "$vl" == "$VAL" ]; then
550 return 0
551 fi
552 return 1
553}
3dfa278e
KZ
554
555function ts_device_has_uuid {
556 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}$'
557 return $?
558}
559
2f954947
RM
560function ts_mount {
561 local out
562 local result
563 local msg
a23c435e 564 local fs
d2cc2ba7
RM
565 local fs_exp=$1
566 shift
2f954947
RM
567
568 out=$($TS_CMD_MOUNT "$@" 2>&1)
569 result=$?
570 echo -n "$out" >> $TS_OUTPUT
571
a23c435e
RM
572 if [ $result != 0 ] \
573 && msg=$(echo "$out" | grep -m1 "unknown filesystem type")
574 then
575 # skip only if reported fs correctly and if it's not available
576 fs=$(echo "$msg" | sed -n "s/.*type '\(.*\)'$/\1/p")
d2cc2ba7 577 [ "$fs" = "fs_exp" ] \
a23c435e
RM
578 && grep -qe "[[:space:]]${fs}$" /proc/filesystems &>/dev/null \
579 || ts_skip "$msg"
2f954947
RM
580 fi
581 return $result
582}
583
3ff2557c 584function ts_is_mounted {
6dfe6f0e 585 local DEV=$(ts_canonicalize "$1")
3ff2557c
KZ
586
587 grep -q $DEV /proc/mounts && return 0
588
589 if [ "${DEV#/dev/loop/}" != "$DEV" ]; then
866372c6 590 grep -q "/dev/loop${DEV#/dev/loop/}" /proc/mounts && return 0
3ff2557c
KZ
591 fi
592 return 1
593}
594
c98825ac 595function ts_fstab_open {
601d12fb 596 echo "# <!-- util-linux test entry" >> /etc/fstab
c98825ac
KZ
597}
598
599function ts_fstab_close {
600 echo "# -->" >> /etc/fstab
601}
602
603function ts_fstab_addline {
604 local SPEC="$1"
605 local MNT=${2:-"$TS_MOUNTPOINT"}
606 local FS=${3:-"auto"}
b002d021 607 local OPT=${4:-"defaults"}
c98825ac 608
b002d021 609 echo "$SPEC $MNT $FS $OPT 0 0" >> /etc/fstab
c98825ac
KZ
610}
611
612function ts_fstab_add {
613 ts_fstab_open
b002d021 614 ts_fstab_addline $*
c98825ac
KZ
615 ts_fstab_close
616}
617
618function ts_fstab_clean {
619 sed --in-place "
601d12fb 620/# <!-- util-linux/!b
c98825ac
KZ
621:a
622/# -->/!{
623 N
624 ba
625}
601d12fb 626s/# <!-- util-linux.*-->//;
c98825ac
KZ
627/^$/d" /etc/fstab
628}
629
d0bcd9b3 630function ts_fdisk_clean {
e1fe1815 631 local DEVNAME=$1
618ec053 632
d0bcd9b3 633 # remove non comparable parts of fdisk output
23d8c556
RM
634 if [ -n "${DEVNAME}" ]; then
635 sed -i -e "s@${DEVNAME}@<removed>@;" $TS_OUTPUT
636 fi
637
638 sed -i \
639 -e 's/Disk identifier:.*/Disk identifier: <removed>/' \
640 -e 's/Created a new.*/Created a new <removed>./' \
641 -e 's/^Device[[:blank:]]*Start/Device Start/' \
642 -e 's/^Device[[:blank:]]*Boot/Device Boot/' \
643 -e 's/Welcome to fdisk.*/Welcome to fdisk <removed>./' \
644 -e 's/typescript file.*/typescript file <removed>./' \
01e8c90c 645 -e 's@^\(I/O size (minimum/op.* bytes /\) [1-9][0-9]* @\1 <removed> @' \
23d8c556 646 $TS_OUTPUT
d0bcd9b3 647}
618ec053
KZ
648
649function ts_scsi_debug_init {
dd761f79 650 local devname
8d323f0d 651 local t
dd761f79 652 TS_DEVICE="none"
618ec053 653
9779f598 654 # dry run is not really reliable, real modprobe may still fail
f80c0d38
RM
655 modprobe --dry-run --quiet scsi_debug &>/dev/null \
656 || ts_skip "missing scsi_debug module (dry-run)"
618ec053 657
f80c0d38 658 # skip if still in use or removal of modules not supported at all
5c711ba9 659 # We don't want a slow timeout here so we don't use ts_scsi_debug_rmmod!
f80c0d38
RM
660 modprobe -r scsi_debug &>/dev/null \
661 || ts_skip "cannot remove scsi_debug module (rmmod)"
662
85fca7e5 663 modprobe -b scsi_debug "$@" &>/dev/null \
f80c0d38
RM
664 || ts_skip "cannot load scsi_debug module (modprobe)"
665
666 # it might be still not loaded, modprobe.conf or whatever
fc5fa903 667 lsmod 2>/dev/null | grep -q "^scsi_debug " \
f80c0d38 668 || ts_skip "scsi_debug module not loaded (lsmod)"
618ec053 669
618ec053
KZ
670 udevadm settle
671
8d323f0d
RM
672 # wait for device if udevadm settle does not work
673 for t in 0 0.02 0.05 0.1 1; do
674 sleep $t
675 devname=$(grep --with-filename scsi_debug /sys/block/*/device/model) && break
676 done
677 [ -n "${devname}" ] || ts_die "timeout waiting for scsi_debug device"
85fca7e5 678
8d323f0d 679 devname=$(echo $devname | awk -F '/' '{print $4}')
dd761f79 680 TS_DEVICE="/dev/${devname}"
8d323f0d
RM
681
682 # TODO validate that device is really up, for now just a warning on stderr
5c711ba9
RM
683 test -b $TS_DEVICE || echo "warning: scsi_debug device is still down" >&2
684}
685
686# automatically called once in ts_cleanup_on_exit()
687function ts_scsi_debug_rmmod {
688 local err=1
689 local t
690 local lastmsg
691
692 # Return early most importantly in case we are not root or the module does
693 # not exist at all.
694 [ $UID -eq 0 ] || return 0
695 [ -n "$TS_DEVICE" ] || return 0
fc5fa903 696 lsmod 2>/dev/null | grep -q "^scsi_debug " || return 0
5c711ba9
RM
697
698 udevadm settle
699
700 # wait for successful rmmod if udevadm settle does not work
701 for t in 0 0.02 0.05 0.1 1; do
702 sleep $t
703 lastmsg="$(modprobe -r scsi_debug 2>&1)" && err=0 && break
704 done
705
706 if [ "$err" = "1" ]; then
707 ts_log "rmmod failed: '$lastmsg'"
708 ts_log "timeout removing scsi_debug module (rmmod)"
709 return 1
710 fi
711 if lsmod | grep -q "^scsi_debug "; then
712 ts_log "BUG! scsi_debug still loaded"
713 return 1
714 fi
715
716 # TODO Do we need to validate that all devices are gone?
717 udevadm settle
718 test -b "$TS_DEVICE" && echo "warning: scsi_debug device is still up" >&2
719
720 # TODO unset TS_DEVICE, check that nobody uses it later, e.g. ts_fdisk_clean
721
722 return 0
618ec053 723}
a98de969
RM
724
725function ts_resolve_host {
726 local host="$1"
727 local tmp
728
729 # currently we just resolve default records (might be "A", ipv4 only)
730 if type "dig" >/dev/null 2>&1; then
731 tmp=$(dig "$host" +short 2>/dev/null) || return 1
732 elif type "nslookup" >/dev/null 2>&1; then
733 tmp=$(nslookup "$host" 2>/dev/null) || return 1
734 tmp=$(echo "$tmp"| grep -A1 "^Name:"| grep "^Address:"| cut -d" " -f2)
eee79f28
AH
735 elif type "host" >/dev/null 2>&1; then
736 tmp=$(host "$host" 2>/dev/null) || return 1
737 tmp=$(echo "$tmp" | grep " has address " | cut -d " " -f4)
738 elif type "getent" >/dev/null 2>&1; then
739 tmp=$(getent ahosts "$host" 2>/dev/null) || return 1
740 tmp=$(echo "$tmp" | cut -d " " -f 1 | sort -u)
a98de969
RM
741 fi
742
743 # we return 1 if tmp is empty
744 test -n "$tmp" || return 1
745 echo "$tmp" | sort -R | head -n 1
746}
c9813f2c
RM
747
748# listen to unix socket (background socat)
749function ts_init_socket_to_file {
750 local socket=$1
751 local outfile=$2
752 local pid="0"
753
754 ts_check_prog "socat"
755 rm -f "$socket" "$outfile"
756
e4866229 757 # if socat is too old for these options we'll skip it below
c9813f2c 758 socat -u UNIX-LISTEN:$socket,fork,max-children=1,backlog=128 \
e4866229 759 STDOUT > "$outfile" 2>/dev/null &
c9813f2c
RM
760 pid=$!
761
762 # check for running background process
e4866229 763 if [ "$pid" -le "0" ] || ! kill -s 0 "$pid" &>/dev/null; then
c9813f2c
RM
764 ts_skip "unable to run socat"
765 fi
766 # wait for the socket listener
e4866229
RM
767 if ! socat -u /dev/null UNIX-CONNECT:$socket,retry=30,interval=0.1 &>/dev/null; then
768 kill -9 "$pid" &>/dev/null
769 ts_skip "timeout waiting for socat socket"
c9813f2c
RM
770 fi
771 # check socket again
e4866229
RM
772 if ! socat -u /dev/null UNIX-CONNECT:$socket &>/dev/null; then
773 kill -9 "$pid" &>/dev/null
774 ts_skip "socat socket stopped listening"
c9813f2c
RM
775 fi
776}
edeb6223
KZ
777
778function ts_has_mtab_support {
779 grep -q '#define USE_LIBMOUNT_SUPPORT_MTAB' ${top_builddir}/config.h
780 if [ $? == 0 ]; then
781 echo "yes"
782 else
783 echo "no"
784 fi
785}
786
4a62215c
KZ
787function ts_has_ncurses_support {
788 grep -q '#define HAVE_LIBNCURSES' ${top_builddir}/config.h
789 if [ $? == 0 ]; then
790 echo "yes"
791 else
792 echo "no"
793 fi
794}
795