]> git.ipfire.org Git - thirdparty/util-linux.git/blob - tests/functions.sh
Merge branch 'tests-check-prog' of https://github.com/rudimeier/util-linux
[thirdparty/util-linux.git] / tests / functions.sh
1 #
2 # Copyright (C) 2007 Karel Zak <kzak@redhat.com>
3 #
4 # This file is part of util-linux.
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 #
16
17
18 function ts_abspath {
19 cd $1
20 pwd
21 }
22
23 function 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
34 function ts_report {
35 if [ "$TS_PARALLEL" == "yes" ]; then
36 echo "$TS_TITLE $1"
37 else
38 echo "$1"
39 fi
40 }
41
42 function ts_check_test_command {
43 if [ ! -x "$1" ]; then
44 ts_skip "${1##*/} not found"
45 fi
46 }
47
48 function ts_check_prog {
49 local cmd=$1
50 type "$cmd" >/dev/null 2>&1 || ts_skip "missing in PATH: $cmd"
51 }
52
53 function ts_check_losetup {
54 local tmp
55 ts_check_test_command "$TS_CMD_LOSETUP"
56
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
60 return 0
61 fi
62 ts_skip "no loop device support"
63 }
64
65 function ts_skip_subtest {
66 ts_report " IGNORE ($1)"
67 }
68
69 function ts_skip {
70 ts_skip_subtest "$1"
71 if [ -n "$2" -a -b "$2" ]; then
72 ts_device_deinit "$2"
73 fi
74 exit 0
75 }
76
77 function ts_skip_nonroot {
78 if [ $UID -ne 0 ]; then
79 ts_skip "not root permissions"
80 fi
81 }
82
83 function ts_failed_subtest {
84 local msg="FAILED"
85 local ret=1
86 if [ "$TS_KNOWN_FAIL" = "yes" ]; then
87 msg="KNOWN FAILED"
88 ret=0
89 fi
90
91 if [ x"$1" == x"" ]; then
92 ts_report " $msg ($TS_NS)"
93 else
94 ts_report " $msg ($1)"
95 fi
96
97 return $ret
98 }
99
100 function ts_failed {
101 ts_failed_subtest "$1"
102 exit $?
103 }
104
105 function ts_ok_subtest {
106 if [ x"$1" == x"" ]; then
107 ts_report " OK"
108 else
109 ts_report " OK ($1)"
110 fi
111 }
112
113 function ts_ok {
114 ts_ok_subtest "$1"
115 exit 0
116 }
117
118 function ts_log {
119 echo "$1" >> $TS_OUTPUT
120 [ "$TS_VERBOSE" == "yes" ] && echo "$1"
121 }
122
123 function ts_has_option {
124 NAME="$1"
125 ALL="$2"
126
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
136 echo "yes"
137 return
138 elif [ "$env_opt_test" = "no" \
139 -o "$env_opt_comp" = "no" -a "$env_opt_test" != "yes" ]; then
140 return
141 fi
142
143 # or just check the global command line options
144 echo -n $ALL | sed 's/ //g' | awk 'BEGIN { FS="="; RS="--" } /('$NAME'$|'$NAME'=)/ { print "yes" }'
145 }
146
147 function ts_option_argument {
148 NAME="$1"
149 ALL="$2"
150 echo -n $ALL | sed 's/ //g' | awk 'BEGIN { FS="="; RS="--" } /'$NAME'=/ { print $2 }'
151 }
152
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"
160 }
161
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"
169
170 rm -f $TS_OUTPUT $TS_VGDUMP
171 [ -d "$TS_OUTDIR" ] || mkdir -p "$TS_OUTDIR"
172
173 touch $TS_OUTPUT
174 [ -n "$TS_VALGRIND_CMD" ] && touch $TS_VGDUMP
175 }
176
177 function ts_init_env {
178 local mydir=$(ts_abspath ${0%/*})
179 local tmp
180
181 LANG="POSIX"
182 LANGUAGE="POSIX"
183 LC_ALL="POSIX"
184 CHARSET="UTF-8"
185
186 export LANG LANGUAGE LC_ALL CHARSET
187
188 mydir=$(ts_canonicalize "$mydir")
189
190 # automake directories
191 top_srcdir=$(ts_option_argument "srcdir" "$*")
192 top_builddir=$(ts_option_argument "builddir" "$*")
193
194 # where is this script
195 TS_TOPDIR=$(ts_abspath $mydir/../../)
196
197 # default
198 if [ -z "$top_srcdir" ]; then
199 top_srcdir="$TS_TOPDIR/.."
200 fi
201 if [ -z "$top_builddir" ]; then
202 top_builddir="$TS_TOPDIR/.."
203 fi
204
205 top_srcdir=$(ts_abspath $top_srcdir)
206 top_builddir=$(ts_abspath $top_builddir)
207
208 TS_SCRIPT="$mydir/$(basename $0)"
209 TS_SUBDIR=$(dirname $TS_SCRIPT)
210 TS_TESTNAME=$(basename $TS_SCRIPT)
211 TS_COMPONENT=$(basename $TS_SUBDIR)
212
213 TS_NSUBTESTS=0
214 TS_NSUBFAILED=0
215
216 TS_SELF="$TS_SUBDIR"
217
218 TS_OUTDIR="$top_builddir/tests/output/$TS_COMPONENT"
219 TS_DIFFDIR="$top_builddir/tests/diff/$TS_COMPONENT"
220
221 ts_init_core_env
222
223 TS_VERBOSE=$(ts_has_option "verbose" "$*")
224 TS_PARALLEL=$(ts_has_option "parallel" "$*")
225 TS_KNOWN_FAIL=$(ts_has_option "known-fail" "$*")
226
227 tmp=$( ts_has_option "memcheck" "$*")
228 if [ "$tmp" == "yes" -a -f /usr/bin/valgrind ]; then
229 TS_VALGRIND_CMD="/usr/bin/valgrind"
230 fi
231
232 BLKID_FILE="$TS_OUTDIR/${TS_TESTNAME}.blkidtab"
233
234 declare -a TS_SUID_PROGS
235 declare -a TS_SUID_USER
236 declare -a TS_SUID_GROUP
237
238 if [ -f $TS_TOPDIR/commands.sh ]; then
239 . $TS_TOPDIR/commands.sh
240 fi
241
242 export BLKID_FILE
243
244 rm -f $TS_OUTPUT $TS_VGDUMP
245 [ -d "$TS_OUTDIR" ] || mkdir -p "$TS_OUTDIR"
246
247 touch $TS_OUTPUT
248 [ -n "$TS_VALGRIND_CMD" ] && touch $TS_VGDUMP
249
250 if [ "$TS_VERBOSE" == "yes" ]; then
251 echo
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"
265 echo
266 fi
267 }
268
269 function ts_init_subtest {
270
271 TS_SUBNAME="$1"
272
273 ts_init_core_subtest_env
274
275 [ $TS_NSUBTESTS -eq 0 ] && echo
276 TS_NSUBTESTS=$(( $TS_NSUBTESTS + 1 ))
277
278 if [ "$TS_PARALLEL" == "yes" ]; then
279 TS_TITLE=$(printf "%13s: %-30s ...\n%16s: %-27s ..." "$TS_COMPONENT" "$TS_DESC" "" "$TS_SUBNAME")
280 else
281 TS_TITLE=$(printf "%16s: %-27s ..." "" "$TS_SUBNAME")
282 echo -n "$TS_TITLE"
283 fi
284 }
285
286 function ts_init {
287 ts_init_env "$*"
288
289 local is_fake=$( ts_has_option "fake" "$*")
290 local is_force=$( ts_has_option "force" "$*")
291
292 if [ "$TS_PARALLEL" == "yes" ]; then
293 TS_TITLE=$(printf "%13s: %-30s ..." "$TS_COMPONENT" "$TS_DESC")
294 else
295 TS_TITLE=$(printf "%13s: %-30s ..." "$TS_COMPONENT" "$TS_DESC")
296 echo -n "$TS_TITLE"
297 fi
298
299 [ "$is_fake" == "yes" ] && ts_skip "fake mode"
300 [ "$TS_OPTIONAL" == "yes" -a "$is_force" != "yes" ] && ts_skip "optional"
301 }
302
303 function ts_init_suid {
304 PROG="$1"
305 ct=${#TS_SUID_PROGS[*]}
306
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)
311
312 chown root.root $PROG &> /dev/null
313 chmod u+s $PROG &> /dev/null
314 }
315
316 function ts_init_py {
317 LIBNAME="$1"
318
319 [ -f "$TS_TOPDIR/../py${LIBNAME}.la" ] || ts_skip "py${LIBNAME} not compiled"
320
321 export LD_LIBRARY_PATH="$TS_TOPDIR/../.libs"
322 export PYTHONPATH="$TS_TOPDIR/../$LIBNAME/python:$TS_TOPDIR/../.libs"
323
324 export PYTHON_VERSION=$(awk '/^PYTHON_VERSION/ { print $3 }' $top_builddir/Makefile)
325 export PYTHON_MAJOR_VERSION=$(echo $PYTHON_VERSION | sed 's/\..*//')
326
327 export PYTHON="python${PYTHON_MAJOR_VERSION}"
328 }
329
330 function ts_valgrind {
331 if [ -z "$TS_VALGRIND_CMD" ]; then
332 $*
333 else
334 $TS_VALGRIND_CMD --tool=memcheck --leak-check=full \
335 --leak-resolution=high --num-callers=20 \
336 --log-file="$TS_VGDUMP" $*
337 fi
338 }
339
340 function ts_gen_diff {
341 local res=0
342
343 if [ -s "$TS_OUTPUT" ]; then
344
345 # remove libtool lt- prefixes
346 sed --in-place 's/^lt\-\(.*\: \)/\1/g' $TS_OUTPUT
347
348 [ -d "$TS_DIFFDIR" ] || mkdir -p "$TS_DIFFDIR"
349 diff -u $TS_EXPECTED $TS_OUTPUT > $TS_DIFF
350
351 if [ -s $TS_DIFF ]; then
352 res=1
353 else
354 rm -f $TS_DIFF;
355 fi
356 else
357 res=1
358 fi
359 return $res
360 }
361
362 function tt_gen_mem_report {
363 [ -z "$TS_VALGRIND_CMD" ] && echo "$1"
364
365 grep -q -E 'ERROR SUMMARY: [1-9]' $TS_VGDUMP &> /dev/null
366 if [ $? -eq 0 ]; then
367 echo "mem-error detected!"
368 fi
369 }
370
371 function ts_finalize_subtest {
372 local res=0
373
374 if [ -s "$TS_EXPECTED" ]; then
375 ts_gen_diff
376 if [ $? -eq 1 ]; then
377 ts_failed_subtest "$1"
378 res=1
379 else
380 ts_ok_subtest "$(tt_gen_mem_report "$1")"
381 fi
382 else
383 ts_skip_subtest "output undefined"
384 fi
385
386 [ $res -ne 0 ] && TS_NSUBFAILED=$(( $TS_NSUBFAILED + 1 ))
387
388 # reset environment back to parental test
389 ts_init_core_env
390
391 return $res
392 }
393
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
399 done
400
401 if [ $TS_NSUBTESTS -ne 0 ]; then
402 printf "%11s..."
403 if [ $TS_NSUBFAILED -ne 0 ]; then
404 ts_failed "$TS_NSUBFAILED from $TS_NSUBTESTS sub-tests"
405 else
406 ts_ok "all $TS_NSUBTESTS sub-tests PASSED"
407 fi
408 fi
409
410 if [ -s $TS_EXPECTED ]; then
411 ts_gen_diff
412 if [ $? -eq 1 ]; then
413 ts_failed "$1"
414 fi
415 ts_ok "$1"
416 fi
417
418 ts_skip "output undefined"
419 }
420
421 function ts_die {
422 ts_log "$1"
423 if [ -n "$2" ] && [ -b "$2" ]; then
424 ts_device_deinit "$2"
425 ts_fstab_clean # for sure...
426 fi
427 ts_finalize
428 }
429
430 function ts_image_md5sum {
431 local img=${1:-"$TS_OUTDIR/${TS_TESTNAME}.img"}
432 echo $(md5sum "$img" | awk '{printf $1}') $(basename "$img")
433 }
434
435 function ts_image_init {
436 local mib=${1:-"5"} # size in MiBs
437 local img=${2:-"$TS_OUTDIR/${TS_TESTNAME}.img"}
438
439 dd if=/dev/zero of="$img" bs=1M count=$mib &> /dev/null
440 echo "$img"
441 return 0
442 }
443
444 function ts_device_init {
445 local img
446 local dev
447
448 img=$(ts_image_init $1 $2)
449 dev=$($TS_CMD_LOSETUP --show -f "$img")
450
451 echo $dev
452 }
453
454 function ts_device_deinit {
455 local DEV="$1"
456
457 if [ -b "$DEV" ]; then
458 $TS_CMD_UMOUNT "$DEV" &> /dev/null
459 $TS_CMD_LOSETUP -d "$DEV" &> /dev/null
460 fi
461 }
462
463 function ts_uuid_by_devname {
464 echo $($TS_CMD_BLKID -p -s UUID -o value $1)
465 }
466
467 function ts_label_by_devname {
468 echo $($TS_CMD_BLKID -p -s LABEL -o value $1)
469 }
470
471 function ts_fstype_by_devname {
472 echo $($TS_CMD_BLKID -p -s TYPE -o value $1)
473 }
474
475 function ts_device_has {
476 local TAG="$1"
477 local VAL="$2"
478 local DEV="$3"
479 local vl=""
480
481 case $TAG in
482 "TYPE") vl=$(ts_fstype_by_devname $DEV);;
483 "LABEL") vl=$(ts_label_by_devname $DEV);;
484 "UUID") vl=$(ts_uuid_by_devname $DEV);;
485 *) return 1;;
486 esac
487
488 if [ "$vl" == "$VAL" ]; then
489 return 0
490 fi
491 return 1
492 }
493
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}$'
496 return $?
497 }
498
499 function ts_is_mounted {
500 local DEV=$(ts_canonicalize "$1")
501
502 grep -q $DEV /proc/mounts && return 0
503
504 if [ "${DEV#/dev/loop/}" != "$DEV" ]; then
505 return grep -q "/dev/loop${DEV#/dev/loop/}" /proc/mounts
506 fi
507 return 1
508 }
509
510 function ts_fstab_open {
511 echo "# <!-- util-linux test entry" >> /etc/fstab
512 }
513
514 function ts_fstab_close {
515 echo "# -->" >> /etc/fstab
516 }
517
518 function ts_fstab_addline {
519 local SPEC="$1"
520 local MNT=${2:-"$TS_MOUNTPOINT"}
521 local FS=${3:-"auto"}
522 local OPT=${4:-"defaults"}
523
524 echo "$SPEC $MNT $FS $OPT 0 0" >> /etc/fstab
525 }
526
527 function ts_fstab_add {
528 ts_fstab_open
529 ts_fstab_addline $*
530 ts_fstab_close
531 }
532
533 function ts_fstab_clean {
534 sed --in-place "
535 /# <!-- util-linux/!b
536 :a
537 /# -->/!{
538 N
539 ba
540 }
541 s/# <!-- util-linux.*-->//;
542 /^$/d" /etc/fstab
543 }
544
545 function ts_fdisk_clean {
546 local DEVNAME=$1
547
548 # remove non comparable parts of fdisk output
549 if [ x"${DEVNAME}" != x"" ]; then
550 sed -i -e "s:${DEVNAME}:<removed>:g" $TS_OUTPUT
551 fi
552
553 sed -i -e 's/Disk identifier:.*/Disk identifier: <removed>/g' \
554 -e 's/Created a new.*/Created a new <removed>./g' \
555 -e 's/^Device[[:blank:]]*Start/Device Start/g' \
556 -e 's/^Device[[:blank:]]*Boot/Device Boot/g' \
557 -e 's/^Device[[:blank:]]*Flag/Device Flag/g' \
558 -e 's/Welcome to fdisk.*/Welcome to fdisk <removed>./g' \
559 $TS_OUTPUT
560 }
561
562 function ts_scsi_debug_init {
563 local devname
564 TS_DEVICE="none"
565
566 modprobe --dry-run --quiet scsi_debug &>/dev/null
567 [ "$?" == 0 ] || ts_skip "missing scsi_debug module"
568
569 rmmod scsi_debug &> /dev/null
570 modprobe scsi_debug $*
571 [ "$?" == 0 ] || ts_die "Cannot init device"
572
573 devname=$(grep --with-filename scsi_debug /sys/block/*/device/model | awk -F '/' '{print $4}')
574 [ "x${devname}" == "x" ] && ts_die "Cannot find device"
575
576 sleep 1
577 udevadm settle
578
579 TS_DEVICE="/dev/${devname}"
580 }