2012-04-16 08:32:55 +04:00
# Hey Emacs, this is a -*- shell-script -*- !!! :-)
# Common variables and functions for all CTDB tests.
2017-08-03 13:57:47 +03:00
# Print a message and exit.
die ( )
{
echo " $1 " >& 2 ; exit ${ 2 :- 1 }
}
2012-04-19 05:23:07 +04:00
# This expands the most probable problem cases like "." and "..".
TEST_SUBDIR = $( dirname " $0 " )
if [ $( dirname " $TEST_SUBDIR " ) = "." ] ; then
2017-08-03 14:01:59 +03:00
TEST_SUBDIR = $( cd " $TEST_SUBDIR " ; pwd )
2012-04-19 05:23:07 +04:00
fi
2012-04-18 09:04:50 +04:00
2012-04-24 05:50:36 +04:00
# If we are running from within the source tree then, depending on the
# tests that we're running, we may need to add the top level bin/ and
2017-03-15 07:50:46 +03:00
# tools/ subdirectories to $PATH. In this case, sanity check that
2018-10-09 08:30:15 +03:00
# run_tests.sh is in the expected place.
if ! $CTDB_TESTS_ARE_INSTALLED ; then
2017-03-15 07:50:46 +03:00
if [ ! -f " ${ CTDB_TEST_DIR } /run_tests.sh " ] ; then
die "Tests not installed but can't find run_tests.sh"
fi
ctdb_dir = $( dirname " $CTDB_TEST_DIR " )
_tools_dir = " ${ ctdb_dir } /tools "
if [ -d " $_tools_dir " ] ; then
PATH = " ${ _tools_dir } : $PATH "
fi
2018-08-30 02:35:58 +03:00
top_dir = $( cd -P " $ctdb_dir " && echo " $PWD " ) # real path
2018-10-09 08:30:15 +03:00
if [ ! -d " ${ top_dir } /bin " ] ; then
2018-08-30 02:35:58 +03:00
top_dir = $( dirname " $top_dir " )
fi
2012-04-24 05:50:36 +04:00
fi
2018-02-07 05:56:34 +03:00
. " ${ TEST_SCRIPTS_DIR } /script_install_paths.sh "
2018-10-09 08:30:15 +03:00
if [ -d " $CTDB_SCRIPTS_TESTS_BINDIR " ] ; then
PATH = " ${ CTDB_SCRIPTS_TESTS_BINDIR } : ${ PATH } "
fi
2017-10-25 04:04:49 +03:00
# Wait until either timeout expires or command succeeds. The command
# will be tried once per second, unless timeout has format T/I, where
# I is the recheck interval.
wait_until ( )
{
local timeout = " $1 " ; shift # "$@" is the command...
local interval = 1
case " $timeout " in
*/*)
interval = " ${ timeout #*/ } "
timeout = " ${ timeout %/* } "
esac
local negate = false
if [ " $1 " = "!" ] ; then
negate = true
shift
fi
echo -n " < ${ timeout } | "
local t = $timeout
while [ $t -gt 0 ] ; do
local rc = 0
" $@ " || rc = $?
if { ! $negate && [ $rc -eq 0 ] ; } || \
{ $negate && [ $rc -ne 0 ] ; } ; then
echo " | $(( $timeout - $t )) | "
echo "OK"
return 0
fi
local i
for i in $( seq 1 $interval ) ; do
echo -n .
done
t = $(( $t - $interval ))
sleep $interval
done
echo "*TIMEOUT*"
return 1
}
2018-02-07 05:56:34 +03:00
# setup_ctdb_base <parent> <subdir> [item-to-copy]...
setup_ctdb_base ( )
{
[ $# -ge 2 ] || die "usage: setup_ctdb_base <parent> <subdir> [item]..."
# If empty arguments are passed then we attempt to remove /
# (i.e. the root directory) below
[ -n " $1 " -a -n " $2 " ] || \
die "usage: setup_ctdb_base <parent> <subdir> [item]..."
_parent = " $1 "
_subdir = " $2 "
# Other arguments are files/directories to copy
shift 2
export CTDB_BASE = " ${ _parent } / ${ _subdir } "
if [ -d " $CTDB_BASE " ] ; then
rm -r " $CTDB_BASE "
fi
2018-05-08 11:09:46 +03:00
mkdir -p " $CTDB_BASE " || die " Failed to create CTDB_BASE= $CTDB_BASE "
mkdir -p " ${ CTDB_BASE } /run " || die " Failed to create ${ CTDB_BASE } /run "
mkdir -p " ${ CTDB_BASE } /var " || die " Failed to create ${ CTDB_BASE } /var "
2018-02-07 05:56:34 +03:00
for _i ; do
cp -pr " ${ CTDB_SCRIPTS_BASE } / ${ _i } " " ${ CTDB_BASE } / "
done
for _i in " ${ TEST_SUBDIR } /etc-ctdb/ " * ; do
# No/empty etc-ctdb directory
[ -e " $_i " ] || break
cp -pr " $_i " " ${ CTDB_BASE } / "
done
}