2 # Copyright (C) 2007 Karel Zak <kzak@redhat.com>
4 # This file is part of util-linux-ng.
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_skip_subtest {
29 if [ -n "$2" -a -b "$2" ]; then
35 function ts_skip_nonroot {
36 if [ $UID -ne 0 ]; then
37 ts_skip "not root permissions"
41 function ts_failed_subtest {
42 if [ x"$1" == x"" ]; then
43 echo " FAILED ($TS_NS)"
50 ts_failed_subtest "$1"
54 function ts_ok_subtest {
55 if [ x"$1" == x"" ]; then
68 echo "$1" >> $TS_OUTPUT
69 [ "$TS_VERBOSE" == "yes" ] && echo "$1"
72 function ts_has_option {
75 echo -n $ALL | sed 's/ //g' | awk 'BEGIN { FS="="; RS="--" } /('$NAME'$|'$NAME'=)/ { print "yes" }'
78 function ts_init_env {
79 local mydir=$(ts_abspath $(dirname $0))
81 export LANG="en_US.UTF-8"
83 TS_TOPDIR=$(ts_abspath $mydir/../../)
84 TS_SCRIPT="$mydir/$(basename $0)"
85 TS_SUBDIR=$(dirname $TS_SCRIPT)
86 TS_TESTNAME=$(basename $TS_SCRIPT)
87 TS_COMPONENT=$(basename $TS_SUBDIR)
92 TS_NS="$TS_COMPONENT/$TS_TESTNAME"
95 TS_OUTDIR="$TS_TOPDIR/output/$TS_COMPONENT"
96 TS_OUTPUT="$TS_OUTDIR/$TS_TESTNAME"
97 TS_DIFFDIR="$TS_TOPDIR/diff/$TS_COMPONENT"
98 TS_DIFF="$TS_DIFFDIR/$TS_TESTNAME"
99 TS_EXPECTED="$TS_TOPDIR/expected/$TS_NS"
100 TS_MOUNTPOINT="$TS_OUTDIR/${TS_TESTNAME}-mnt"
102 TS_VERBOSE=$(ts_has_option "verbose" "$*")
105 BLKID_FILE="$TS_OUTDIR/${TS_TESTNAME}.blkidtab"
107 [ -d "$TS_OUTDIR" ] || mkdir -p "$TS_OUTDIR"
108 [ -d "$TS_DIFFDIR" ] || mkdir -p "$TS_DIFFDIR"
110 declare -a TS_SUID_PROGS
111 declare -a TS_SUID_USER
112 declare -a TS_SUID_GROUP
114 . $TS_TOPDIR/commands.sh
118 if [ -x $TS_CMD_MOUNT ]; then
119 ldd $TS_CMD_MOUNT | grep -q 'libvolume_id' &> /dev/null
120 [ "$?" == "0" ] && TS_HAS_VOLUMEID="yes"
126 if [ "$TS_VERBOSE" == "yes" ]; then
128 echo " script: $TS_SCRIPT"
129 echo " sub dir: $TS_SUBDIR"
130 echo " top dir: $TS_TOPDIR"
131 echo " self: $TS_SELF"
132 echo " test name: $TS_TESTNAME"
133 echo " test desc: $TS_DESC"
134 echo " component: $TS_COMPONENT"
135 echo " namespace: $TS_NS"
136 echo " verbose: $TS_VERBOSE"
137 echo " output: $TS_OUTPUT"
138 echo " expected: $TS_EXPECTED"
139 echo " mountpoint: $TS_MOUNTPOINT"
144 function ts_init_subtest {
148 TS_OUTPUT="$TS_OUTDIR/$TS_TESTNAME-$TS_SUBNAME"
149 TS_DIFF="$TS_DIFFDIR/$TS_TESTNAME-$TS_SUBNAME"
150 TS_EXPECTED="$TS_TOPDIR/expected/$TS_NS-$TS_SUBNAME"
151 TS_MOUNTPOINT="$TS_OUTDIR/${TS_TESTNAME-$TS_SUBNAME}-mnt"
153 [ $TS_NSUBTESTS -eq 0 ] && echo
154 TS_NSUBTESTS=$(( $TS_NSUBTESTS + 1 ))
156 printf "%16s: %-27s ..." "" "$TS_SUBNAME"
160 local is_fake=$( ts_has_option "fake" "$*")
164 printf "%13s: %-30s ..." "$TS_COMPONENT" "$TS_DESC"
166 [ "$is_fake" == "yes" ] && ts_skip "fake mode"
169 function ts_init_suid {
171 ct=${#TS_SUID_PROGS[*]}
173 # Save info about original setting
174 TS_SUID_PROGS[$ct]=$PROG
175 TS_SUID_USER[$ct]=$(stat --printf="%U" $PROG)
176 TS_SUID_GROUP[$ct]=$(stat --printf="%G" $PROG)
178 chown root.root $PROG &> /dev/null
179 chmod u+s $PROG &> /dev/null
182 function ts_gen_diff {
185 if [ -s $TS_OUTPUT ]; then
186 diff -u $TS_EXPECTED $TS_OUTPUT > $TS_DIFF
187 [ -s $TS_DIFF ] && res=1
194 function ts_finalize_subtest {
197 if [ -s $TS_EXPECTED ]; then
199 if [ $? -eq 1 ]; then
200 ts_failed_subtest "$1"
206 ts_skip_subtest "output undefined"
209 [ $res -ne 0 ] && TS_NSUBFAILED=$(( $TS_NSUBFAILED + 1 ))
213 function ts_finalize {
214 for idx in $(seq 0 $((${#TS_SUID_PROGS[*]} - 1))); do
215 PROG=${TS_SUID_PROGS[$idx]}
216 chmod a-s $PROG &> /dev/null
217 chown ${TS_SUID_USER[$idx]}.${TS_SUID_GROUP[$idx]} $PROG &> /dev/null
220 if [ $TS_NSUBTESTS -ne 0 ]; then
222 if [ $TS_NSUBFAILED -ne 0 ]; then
223 ts_failed "$TS_NSUBFAILED from $TS_NSUBTESTS sub-tests"
225 ts_ok "all $TS_NSUBTESTS sub-tests PASSED"
229 if [ -s $TS_EXPECTED ]; then
231 if [ $? -eq 1 ]; then
237 ts_skip "output undefined"
242 if [ -n "$2" ] && [ -b "$2" ]; then
243 ts_device_deinit "$2"
244 ts_fstab_clean # for sure...
249 function ts_image_md5sum {
250 local img=${1:-"$TS_OUTDIR/${TS_TESTNAME}.img"}
251 echo $(md5sum "$img" | awk '{printf $1}') $(basename "$img")
254 function ts_image_init {
255 local mib=${1:-"5"} # size in MiBs
256 local img=${2:-"$TS_OUTDIR/${TS_TESTNAME}.img"}
258 dd if=/dev/zero of="$img" bs=1M count=$mib &> /dev/null
263 function ts_device_init {
264 local img=$(ts_image_init)
265 local dev=$($TS_CMD_LOSETUP -s -f "$img")
267 if [ -z "$dev" ]; then
268 ts_device_deinit $dev
276 function ts_device_deinit {
279 if [ -b "$DEV" ]; then
280 $TS_CMD_UMOUNT "$DEV" &> /dev/null
281 $TS_CMD_LOSETUP -d "$DEV" &> /dev/null
285 function ts_udev_dev_support {
286 if [ "$TS_HAS_VOLUMEID" == "yes" ] && [ ! -L "/dev/disk/$1/$2" ]; then
292 function ts_uuid_by_devname {
295 if [ -x "$TS_ECMD_BLKID" ]; then
296 UUID=$($TS_ECMD_BLKID -c /dev/null -w /dev/null -s "UUID" $DEV | sed 's/.*UUID="//g; s/"//g')
297 elif [ -x "$TS_ECMD_VOLID" ]; then
298 UUID=$($TS_ECMD_VOLID -u $DEV)
303 function ts_label_by_devname {
306 if [ -x "$TS_ECMD_BLKID" ]; then
307 LABEL=$($TS_ECMD_BLKID -c /dev/null -w /dev/null -s "LABEL" $DEV | sed 's/.*LABEL="//g; s/"//g')
308 elif [ -x "$TS_ECMD_VOLID" ]; then
309 LABEL=$($TS_ECMD_VOLID -l $DEV)
314 function ts_fstype_by_devname {
317 if [ -x "$TS_ECMD_BLKID" ]; then
318 TYPE=$($TS_ECMD_BLKID -c /dev/null -w /dev/null -s "TYPE" $DEV | sed 's/.*TYPE="//g; s/"//g')
319 elif [ -x "$TS_ECMD_VOLID" ]; then
320 TYPE=$($TS_ECMD_VOLID -t $DEV)
325 function ts_device_has {
332 "TYPE") vl=$(ts_fstype_by_devname $DEV);;
333 "LABEL") vl=$(ts_label_by_devname $DEV);;
334 "UUID") vl=$(ts_uuid_by_devname $DEV);;
338 if [ "$vl" == "$VAL" ]; then
344 function ts_device_has_uuid {
345 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}$'
349 function ts_is_mounted {
352 grep -q $DEV /proc/mounts && return 0
354 if [ "${DEV#/dev/loop/}" != "$DEV" ]; then
355 return grep -q "/dev/loop${DEV#/dev/loop/}" /proc/mounts
360 function ts_swapoff {
363 # swapoff doesn't exist in build tree
364 if [ ! -x "$TS_CMD_SWAPOFF" ]; then
365 ln -sf $TS_CMD_SWAPON $TS_CMD_SWAPOFF
368 $TS_CMD_SWAPOFF $DEV 2>&1 >> $TS_OUTPUT
369 if [ -n "$REMSWAPOFF" ]; then
370 rm -f $TS_CMD_SWAPOFF
374 function ts_fstab_open {
375 echo "# <!-- util-linux-ng test entry" >> /etc/fstab
378 function ts_fstab_close {
379 echo "# -->" >> /etc/fstab
382 function ts_fstab_addline {
384 local MNT=${2:-"$TS_MOUNTPOINT"}
385 local FS=${3:-"auto"}
386 local OPT=${4:-"defaults"}
388 echo "$SPEC $MNT $FS $OPT 0 0" >> /etc/fstab
391 function ts_fstab_add {
397 function ts_fstab_clean {
399 /# <!-- util-linux-ng/!b
405 s/# <!-- util-linux-ng.*-->//;