2018-07-01 06:30:06 +03:00
#!/usr/bin/env bash
2007-04-28 12:57:58 +04:00
2013-12-02 08:39:48 +04:00
usage( ) {
cat <<EOF
Usage: $0 [ OPTIONS] [ TESTS]
Options:
-A Use "cat -A" to print test output ( only some tests)
-c Run integration tests on a cluster
2019-05-01 08:10:28 +03:00
-C Remove TEST_VAR_DIR when done
2013-12-02 08:39:48 +04:00
-d Print descriptions of tests instead of filenames ( dodgy!)
-D Show diff between failed/expected test output ( some tests only)
-e Exit on the first test failure
-H No headers - for running single test with other wrapper
-N Don' t print summary of tests results after running all tests
-q Quiet - don' t show tests being run ( hint: use with -s)
2018-01-22 11:14:48 +03:00
-S <lib> Use socket wrapper library <lib> for local integration tests
2013-12-02 08:39:48 +04:00
-v Verbose - print test output for non-failures ( only some tests)
-V <dir> Use <dir> as TEST_VAR_DIR
-x Trace this script with the -x option
-X Trace certain scripts run by tests using -x ( only some tests)
EOF
exit 1
}
# Print a message and exit.
die ( )
{
echo " $1 " >& 2 ; exit ${ 2 :- 1 }
}
######################################################################
with_summary = true
with_desc = false
quiet = false
exit_on_fail = false
no_header = false
export TEST_VERBOSE = false
export TEST_COMMAND_TRACE = false
export TEST_CAT_RESULTS_OPTS = ""
export TEST_DIFF_RESULTS = false
export TEST_LOCAL_DAEMONS
[ -n " $TEST_LOCAL_DAEMONS " ] || TEST_LOCAL_DAEMONS = 3
export TEST_VAR_DIR = ""
export TEST_CLEANUP = false
2018-02-05 07:45:09 +03:00
export TEST_TIMEOUT = 3600
2018-01-22 11:14:48 +03:00
export TEST_SOCKET_WRAPPER_SO_PATH = ""
2013-12-02 08:39:48 +04:00
2018-07-08 14:54:40 +03:00
while getopts "AcCdDehHNqS:T:vV:xX?" opt ; do
case " $opt " in
A) TEST_CAT_RESULTS_OPTS = "-A" ; ;
c) TEST_LOCAL_DAEMONS = "" ; ;
C) TEST_CLEANUP = true ; ;
d) with_desc = true ; ; # 4th line of output is description
D) TEST_DIFF_RESULTS = true ; ;
e) exit_on_fail = true ; ;
H) no_header = true ; ;
N) with_summary = false ; ;
q) quiet = true ; ;
S) TEST_SOCKET_WRAPPER_SO_PATH = " $OPTARG " ; ;
T) TEST_TIMEOUT = " $OPTARG " ; ;
v) TEST_VERBOSE = true ; ;
V) TEST_VAR_DIR = " $OPTARG " ; ;
x) set -x ; ;
X) TEST_COMMAND_TRACE = true ; ;
\? | h) usage ; ;
esac
2013-12-02 08:39:48 +04:00
done
2018-07-08 14:54:40 +03:00
shift $(( OPTIND - 1 ))
2012-04-20 08:10:34 +04:00
2012-05-14 05:57:20 +04:00
case $( basename " $0 " ) in
*run_cluster_tests*)
2013-12-02 08:39:48 +04:00
# Running on a cluster... same as -c
TEST_LOCAL_DAEMONS = ""
2012-05-14 05:57:20 +04:00
; ;
esac
2013-12-02 08:39:48 +04:00
if $quiet ; then
show_progress( ) { cat >/dev/null ; }
else
show_progress( ) { cat ; }
fi
######################################################################
ctdb_test_begin ( )
{
local name = " $1 "
teststarttime = $( date '+%s' )
testduration = 0
echo "--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--"
echo " Running test $name ( $( date '+%T' ) ) "
echo "--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--"
}
ctdb_test_end ( )
{
local name = " $1 " ; shift
local status = " $1 " ; shift
# "$@" is command-line
local interp = "SKIPPED"
local statstr = " (reason $* ) "
if [ -n " $status " ] ; then
if [ $status -eq 0 ] ; then
interp = "PASSED"
statstr = ""
echo " ALL OK: $* "
2018-01-20 09:05:37 +03:00
elif [ $status -eq 124 ] ; then
interp = "TIMEOUT"
statstr = " (status $status ) "
2013-12-02 08:39:48 +04:00
else
interp = "FAILED"
statstr = " (status $status ) "
fi
fi
testduration = $(( $( date +%s) - $teststarttime ))
echo "=========================================================================="
echo " TEST ${ interp } : ${ name } ${ statstr } (duration: ${ testduration } s) "
echo "=========================================================================="
}
ctdb_test_run ( )
{
local name = " $1 " ; shift
[ -n " $1 " ] || set -- " $name "
$no_header || ctdb_test_begin " $name "
local status = 0
2018-05-18 04:44:11 +03:00
if [ -x " $1 " ] ; then
timeout $TEST_TIMEOUT " $@ " || status = $?
else
echo "TEST IS NOT EXECUTABLE"
status = 1
fi
2013-12-02 08:39:48 +04:00
$no_header || ctdb_test_end " $name " " $status " " $* "
return $status
}
######################################################################
tests_total = 0
tests_passed = 0
tests_failed = 0
summary = ""
if ! which mktemp >/dev/null 2>& 1 ; then
# Not perfect, but it will do...
mktemp ( )
{
2016-10-11 05:32:31 +03:00
local dir = false
2013-12-02 08:39:48 +04:00
if [ " $1 " = "-d" ] ; then
2016-10-11 05:32:31 +03:00
dir = true
2013-12-02 08:39:48 +04:00
fi
2016-10-11 05:32:31 +03:00
local t = " ${ TMPDIR :- /tmp } /tmp. $$ . $RANDOM "
2013-12-02 08:39:48 +04:00
(
umask 077
2016-10-11 05:32:31 +03:00
if $dir ; then
mkdir " $t "
2013-12-02 08:39:48 +04:00
else
2016-10-11 05:32:31 +03:00
>" $t "
2013-12-02 08:39:48 +04:00
fi
)
2016-10-11 05:32:31 +03:00
echo " $t "
2013-12-02 08:39:48 +04:00
}
fi
2017-03-18 12:38:32 +03:00
tf = $( mktemp) || die "mktemp failed for tf - is TMPDIR missing?"
sf = $( mktemp) || die "mktemp failed for sf - is TMPDIR missing?"
2013-12-02 08:39:48 +04:00
set -o pipefail
run_one_test ( )
{
2016-10-11 05:32:31 +03:00
local f = " $1 "
2013-12-02 08:39:48 +04:00
tests_total = $(( $tests_total + 1 ))
2018-05-18 04:39:49 +03:00
ctdb_test_run " $f " | tee " $tf " | show_progress
2013-12-02 08:39:48 +04:00
status = $?
if [ $status -eq 0 ] ; then
tests_passed = $(( $tests_passed + 1 ))
else
tests_failed = $(( $tests_failed + 1 ))
fi
if $with_summary ; then
2016-10-11 05:32:31 +03:00
local t
2013-12-02 08:39:48 +04:00
if [ $status -eq 0 ] ; then
2016-10-11 05:32:31 +03:00
t = " PASSED "
2013-12-02 08:39:48 +04:00
else
2016-10-11 05:32:31 +03:00
t = "*FAILED*"
2013-12-02 08:39:48 +04:00
fi
if $with_desc ; then
desc = $( tail -n +4 $tf | head -n 1)
2016-10-11 05:32:31 +03:00
f = " $desc "
2013-12-02 08:39:48 +04:00
fi
2016-10-11 05:32:31 +03:00
echo " $t $f " >>" $sf "
2013-12-02 08:39:48 +04:00
fi
}
find_and_run_one_test ( )
{
2016-10-11 05:32:31 +03:00
local t = " $1 "
local dir = " $2 "
2013-12-02 08:39:48 +04:00
2016-10-11 05:32:31 +03:00
local f = " ${ dir } ${ dir : +/ } ${ t } "
2013-12-02 08:39:48 +04:00
2016-10-11 05:32:31 +03:00
if [ -d " $f " ] ; then
local i
for i in $( ls " ${ f %/ } / " *".sh" 2>/dev/null) ; do
run_one_test " $i "
2013-12-02 08:39:48 +04:00
if $exit_on_fail && [ $status -ne 0 ] ; then
break
fi
done
# No tests found? Not a tests directory! Not found...
[ -n " $status " ] || status = 127
2016-10-11 05:32:31 +03:00
elif [ -f " $f " ] ; then
run_one_test " $f "
2013-12-02 08:39:48 +04:00
else
status = 127
fi
}
2018-04-05 08:37:42 +03:00
export CTDB_TEST_MODE = "yes"
2013-12-02 08:39:48 +04:00
# Following 2 lines may be modified by installation script
export CTDB_TESTS_ARE_INSTALLED = false
2017-08-03 13:36:57 +03:00
export CTDB_TEST_DIR = $( dirname " $0 " )
2013-12-02 08:39:48 +04:00
if [ -z " $TEST_VAR_DIR " ] ; then
if $CTDB_TESTS_ARE_INSTALLED ; then
TEST_VAR_DIR = $( mktemp -d)
else
2017-08-03 13:36:57 +03:00
TEST_VAR_DIR = " ${ CTDB_TEST_DIR } /var "
2013-12-02 08:39:48 +04:00
fi
fi
mkdir -p " $TEST_VAR_DIR "
2017-08-03 13:36:57 +03:00
export TEST_SCRIPTS_DIR = " ${ CTDB_TEST_DIR } /scripts "
2013-12-02 08:39:48 +04:00
2018-01-22 11:48:02 +03:00
unit_tests = "
cunit
2018-05-04 11:08:08 +03:00
eventd
2018-01-22 11:48:02 +03:00
eventscripts
onnode
shellcheck
takeover
takeover_helper
tool
"
2013-12-02 08:39:48 +04:00
# If no tests specified then run some defaults
if [ -z " $1 " ] ; then
2018-01-22 11:48:02 +03:00
if [ -n " $TEST_LOCAL_DAEMONS " ] ; then
set -- UNIT simple
else
set -- simple complex
2013-12-02 08:39:48 +04:00
fi
fi
2014-07-04 11:04:10 +04:00
do_cleanup ( )
{
if $TEST_CLEANUP ; then
echo " Removing TEST_VAR_DIR= $TEST_VAR_DIR "
rm -rf " $TEST_VAR_DIR "
else
echo " Not cleaning up TEST_VAR_DIR= $TEST_VAR_DIR "
fi
}
2018-10-08 05:39:30 +03:00
trap "do_cleanup ; exit 130" SIGINT
trap "do_cleanup ; exit 143" SIGTERM
2014-07-04 11:04:10 +04:00
2018-01-22 11:48:02 +03:00
declare -a tests
i = 0
2013-12-02 08:39:48 +04:00
for f ; do
2018-01-22 11:48:02 +03:00
if [ " $f " = "UNIT" ] ; then
for t in $unit_tests ; do
tests[ i++] = " $t "
done
else
tests[ i++] = " $f "
fi
done
for f in " ${ tests [@] } " ; do
2013-12-02 08:39:48 +04:00
find_and_run_one_test " $f "
if [ $status -eq 127 ] ; then
# Find the the top-level tests directory
tests_dir = $( dirname $( cd $TEST_SCRIPTS_DIR ; echo $PWD ) )
# Strip off current directory from beginning, if there, just
# to make paths more friendly.
tests_dir = ${ tests_dir # $PWD / }
find_and_run_one_test " $f " " $tests_dir "
fi
if [ $status -eq 127 ] ; then
die " test \" $f \" is not recognised "
fi
if $exit_on_fail && [ $status -ne 0 ] ; then
break
fi
done
rm -f " $tf "
if $with_summary ; then
echo
cat " $sf "
echo
echo " ${ tests_passed } / ${ tests_total } tests passed "
fi
rm -f " $sf "
echo
2014-07-04 11:04:10 +04:00
do_cleanup
2013-12-02 08:39:48 +04:00
if $no_header || $exit_on_fail ; then
exit $status
elif [ $tests_failed -gt 0 ] ; then
exit 1
else
exit 0
fi