2013-06-10 13:28:47 +04:00
#!/usr/bin/env bash
2012-03-20 14:51:57 +04:00
# Copyright (C) 2011-2012 Red Hat, Inc. All rights reserved.
2011-01-05 03:16:18 +03:00
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions
# of the GNU General Public License v.2.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
set -e
2011-01-05 03:26:19 +03:00
MAX_TRIES = 4
2012-03-20 14:51:57 +04:00
IFS_NL = '
'
2011-01-05 03:26:19 +03:00
2012-03-16 16:59:43 +04:00
die( ) {
2013-09-16 13:05:03 +04:00
rm -f debug.log
echo -e " $@ " >& 2
2012-03-16 16:59:43 +04:00
return 1
}
rand_bytes( ) {
n = $1
2011-01-05 03:26:19 +03:00
2012-03-16 16:59:43 +04:00
chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
2011-01-05 03:26:19 +03:00
2012-03-16 16:59:43 +04:00
dev_rand = "/dev/urandom"
if test -r " $dev_rand " ; then
# Note: 256-length($chars) == 194; 3 copies of $chars is 186 + 8 = 194.
head -c" $n " " $dev_rand " | tr -c " $chars " " 01234567 $chars $chars $chars "
return
fi
2011-01-05 03:26:19 +03:00
2012-03-16 16:59:43 +04:00
cmds = 'date; date +%N; free; who -a; w; ps auxww; ps ef; netstat -n'
data = $( ( eval " $cmds " ) 2>& 1 | gzip )
2011-01-05 03:26:19 +03:00
2012-03-16 16:59:43 +04:00
n_plus_50 = $( expr $n + 50)
2011-01-05 03:26:19 +03:00
2012-03-16 16:59:43 +04:00
# Ensure that $data has length at least 50+$n
while :; do
len = $( echo " $data " | wc -c)
test $n_plus_50 -le $len && break;
data = $( ( echo " $data " ; eval " $cmds " ) 2>& 1 | gzip )
done
2011-01-05 03:26:19 +03:00
2012-03-16 16:59:43 +04:00
echo " $data " | dd bs = 1 skip = 50 count = $n 2>/dev/null \
| tr -c " $chars " " 01234567 $chars $chars $chars "
2011-01-05 03:26:19 +03:00
}
2012-03-16 16:59:43 +04:00
mkdtemp( ) {
case $# in
2) ; ;
*) die "Usage: mkdtemp DIR TEMPLATE" ; ;
esac
destdir = $1
template = $2
case " $template " in
*XXXX) ; ;
*) die " Invalid template: $template (must have a suffix of at least 4 X's) " ; ;
esac
fail = 0
# First, try to use mktemp.
2012-03-20 14:51:57 +04:00
d = $( env -u TMPDIR mktemp -d -t -p " $destdir " " $template " 2>/dev/null) || fail = 1
2012-03-16 16:59:43 +04:00
# The resulting name must be in the specified directory.
case " $d " in " ${ destdir } " *) ; ; *) fail = 1; ; esac
# It must have created the directory.
test -d " $d " || fail = 1
# It must have 0700 permissions.
perms = $( ls -dgo " $d " 2>/dev/null) || fail = 1
case " $perms " in drwx------*) ; ; *) fail = 1; ; esac
test $fail = 0 && { echo " $d " ; return ; }
# If we reach this point, we'll have to create a directory manually.
# Get a copy of the template without its suffix of X's.
base_template = $( echo " $template " | sed 's/XX*$//' )
# Calculate how many X's we've just removed.
nx = $( expr length " $template " - length " $base_template " )
err =
i = 1
while :; do
X = $( rand_bytes " $nx " )
candidate_dir = " $destdir / $base_template $X "
err = $( mkdir -m 0700 " $candidate_dir " 2>& 1) && \
{ echo " $candidate_dir " ; return ; }
test $MAX_TRIES -le $i && break;
i = $( expr $i + 1)
done
die " $err "
2011-01-05 03:26:19 +03:00
}
2011-01-05 03:16:18 +03:00
STACKTRACE( ) {
2012-03-16 16:59:43 +04:00
trap - ERR
local i = 0
echo " ## - $0 : ${ BASH_LINENO [0] } "
while FUNC = ${ FUNCNAME [ $i ] } ; test " $FUNC " != "main" ; do
echo " ## $i ${ FUNC } () called from ${ BASH_SOURCE [ $i ] } : ${ BASH_LINENO [ $i ] } "
i = $(( $i + 1 ))
2011-01-05 03:16:18 +03:00
done
2014-03-17 19:32:29 +04:00
test " ${ LVM_TEST_PARALLEL :- 0 } " -eq 1 -o -n " $RUNNING_DMEVENTD " -o -f LOCAL_DMEVENTD || {
2012-03-16 16:59:43 +04:00
pgrep dmeventd & >/dev/null && \
die "** During test dmeventd has been started!"
}
# Get backtraces from coredumps
if which gdb & >/dev/null; then
echo bt full > gdb_commands.txt
echo l >> gdb_commands.txt
echo quit >> gdb_commands.txt
for core in $( ls core* 2>/dev/null) ; do
bin = $( gdb -batch -c " $core " 2>& 1 | grep "generated by" | \
sed -e "s,.*generated by \`\([^ ']*\).*,\1," )
gdb -batch -c " $core " -x gdb_commands.txt $( which " $bin " )
done
fi
test -z " $LVM_TEST_NODEBUG " -a -f debug.log && {
sed -e " s,^,## DEBUG: ,;s, $top_srcdir /\?,, " < debug.log
}
2011-01-05 03:16:18 +03:00
2012-03-16 16:59:43 +04:00
test -f SKIP_THIS_TEST && exit 200
2011-01-05 03:16:18 +03:00
}
init_udev_transaction( ) {
2012-03-16 16:59:43 +04:00
if test " $DM_UDEV_SYNCHRONISATION " = 1; then
local cookie = $( dmsetup udevcreatecookie)
# Cookie is not generated if udev is not running!
test -z " $cookie " || export DM_UDEV_COOKIE = $cookie
2011-01-05 03:16:18 +03:00
fi
}
finish_udev_transaction( ) {
2012-03-16 16:59:43 +04:00
if test " $DM_UDEV_SYNCHRONISATION " = 1 -a -n " $DM_UDEV_COOKIE " ; then
dmsetup udevreleasecookie
unset DM_UDEV_COOKIE
fi
2011-01-05 03:16:18 +03:00
}
2011-06-29 22:18:18 +04:00
teardown_udev_cookies( ) {
2012-03-16 16:59:43 +04:00
if test " $DM_UDEV_SYNCHRONISATION " = 1; then
# Delete any cookies created more than 10 minutes ago
# and not used in the last 10 minutes.
# Log only non-zero semaphores count
( dmsetup udevcomplete_all -y 10 | grep -v "^0 " ) || true
fi
2011-06-29 22:18:18 +04:00
}
2012-03-28 15:10:08 +04:00
dm_info( ) {
2014-03-04 02:01:40 +04:00
should dmsetup info --mangle none --noheadings -c -o " $@ "
2012-03-28 15:10:08 +04:00
}
dm_table( ) {
should dmsetup table " $@ "
}
2011-01-07 18:04:52 +03:00
skip( ) {
2012-03-16 16:59:43 +04:00
touch SKIP_THIS_TEST
exit 200
2011-01-07 18:04:52 +03:00
}
2011-06-09 22:58:00 +04:00
kernel_at_least( ) {
2012-03-16 16:59:43 +04:00
local major = $( uname -r | cut -d. -f1)
local minor = $( uname -r | cut -d. -f2 | cut -d- -f1)
2012-03-20 14:51:57 +04:00
test " $major " -gt " $1 " && return 0
test " $major " -eq " $1 " || return 1
test " $minor " -gt " $2 " && return 0
test " $minor " -eq " $2 " || return 1
2012-03-16 16:59:43 +04:00
test -z " $3 " && return 0
local minor2 = $( uname -r | cut -d. -f3 | cut -d- -f1)
2012-03-20 14:51:57 +04:00
test -z " $minor2 " -a " $3 " -ne 0 && return 1
test " $minor2 " -ge " $3 " 2>/dev/null || return 1
2012-03-16 16:59:43 +04:00
}
2011-06-09 22:58:00 +04:00
2014-06-04 15:55:13 +04:00
get_devs( ) {
local IFS = $IFS_NL
DEVICES = ( $( <DEVICES) )
export DEVICES
# local DEVS=( $(<DEVICES) )
# eval "$1"'=("${DEVS[@]}")'
}
2012-03-16 16:59:43 +04:00
prepare_test_vars( ) {
vg = " ${ PREFIX } vg "
lv = LV
2011-06-09 22:58:00 +04:00
2012-03-16 16:59:43 +04:00
for i in $( seq 1 16) ; do
name = " ${ PREFIX } pv $i "
dev = " $DM_DEV_DIR /mapper/ $name "
eval " dev $i =\" $dev \" "
eval " lv $i =LV $i "
eval " vg $i = ${ PREFIX } vg $i "
done
2011-06-09 22:58:00 +04:00
}
2012-03-20 14:51:57 +04:00
# check if $abs_top_builddir was already set via 'lib/paths'
test -n " ${ abs_top_builddir +varset } " || . lib/paths || die "you must run make first"
case " $PATH " in
*" $abs_top_builddir /test/lib " *) ; ;
*)
2012-10-27 00:30:21 +04:00
PATH = " $abs_top_builddir /test/lib " :" $abs_top_builddir /test/api " :$PATH
for i in ` find $abs_top_builddir -name \* .so` ; do
p = ` dirname $i `
LD_LIBRARY_PATH = " $p " :$LD_LIBRARY_PATH
done
2012-03-20 14:51:57 +04:00
export PATH LD_LIBRARY_PATH ; ;
esac
2012-03-16 16:59:43 +04:00
test -z " $PREFIX " || prepare_test_vars