2012-04-16 08:33:37 +04:00
# Hey Emacs, this is a -*- shell-script -*- !!! :-)
. " ${ TEST_SCRIPTS_DIR } /common.sh "
# Common variables and functions for CTDB unit tests.
2013-11-22 03:35:35 +04:00
trap -- '' PIPE
2012-04-18 04:37:45 +04:00
# Set the required result for a test.
# - Argument 1 is exit code.
# - Argument 2, if present is the required test output but "--"
# indicates empty output.
# If argument 2 is not present or null then read required test output
# from stdin.
2012-04-16 08:33:37 +04:00
required_result ( )
{
required_rc = " ${ 1 :- 0 } "
2012-04-18 04:37:45 +04:00
if [ -n " $2 " ] ; then
if [ " $2 " = "--" ] ; then
required_output = ""
else
required_output = " $2 "
fi
else
if ! tty -s ; then
required_output = $( cat)
else
required_output = ""
fi
fi
}
ok ( )
{
required_result 0 " $@ "
}
ok_null ( )
{
ok --
}
result_print ( )
{
_passed = " $1 "
_out = " $2 "
_rc = " $3 "
_extra_header = " $4 "
if " $TEST_VERBOSE " || ! $_passed ; then
if [ -n " $_extra_header " ] ; then
cat <<EOF
2012-09-03 09:37:01 +04:00
##################################################
2012-04-18 04:37:45 +04:00
$_extra_header
EOF
fi
cat <<EOF
--------------------------------------------------
Output ( Exit status: ${ _rc } ) :
--------------------------------------------------
EOF
echo " $_out " | cat $TEST_CAT_RESULTS_OPTS
fi
if ! $_passed ; then
cat <<EOF
--------------------------------------------------
Required output ( Exit status: ${ required_rc } ) :
--------------------------------------------------
EOF
echo " $required_output " | cat $TEST_CAT_RESULTS_OPTS
if $TEST_DIFF_RESULTS ; then
_outr = $( mktemp)
echo " $required_output " >" $_outr "
_outf = $( mktemp)
2013-05-03 09:53:13 +04:00
echo " $_fout " >" $_outf "
2012-04-18 04:37:45 +04:00
cat <<EOF
--------------------------------------------------
Diff:
--------------------------------------------------
EOF
diff -u " $_outr " " $_outf " | cat -A
rm " $_outr " " $_outf "
fi
fi
}
result_footer ( )
{
_passed = " $1 "
_extra_footer = " $2 "
if " $TEST_VERBOSE " || ! $_passed ; then
if [ -n " $_extra_footer " ] ; then
cat <<EOF
--------------------------------------------------
$_extra_footer
--------------------------------------------------
EOF
fi
fi
if $_passed ; then
echo "PASSED"
return 0
else
echo
echo "FAILED"
return 1
fi
}
2013-11-15 05:22:05 +04:00
# Result filtering is (usually) used to replace the date/time/PID
# prefix on some CTDB tool/client log messages with the literal string
# "DATE TIME [PID]". This allows tests to loosely match this output,
# since it can't otherwise be matched.
result_filter_default ( )
{
_date_time_pid = '[0-9/][0-9/]*\ [0-9:\.][0-9:\.]*\ \[[\ 0-9][\ 0-9]*\]'
sed -e " s@^ ${ _date_time_pid } :@DATE\ TIME\ \[PID\]:@ "
}
2014-09-24 11:07:12 +04:00
TEST_DATE_STAMP = "DATE TIME [PID]: "
2013-11-15 05:22:05 +04:00
# Override this function to customise output filtering.
result_filter ( )
{
result_filter_default
}
2012-04-18 04:37:45 +04:00
result_check ( )
{
_rc = $?
_extra_header = " $1 "
2013-11-15 05:22:05 +04:00
_fout = $( echo " $_out " | result_filter)
2012-04-18 04:37:45 +04:00
if [ " $_fout " = " $required_output " -a $_rc = $required_rc ] ; then
_passed = true
else
_passed = false
fi
result_print " $_passed " " $_out " " $_rc " " $_extra_header "
result_footer " $_passed "
2012-04-16 08:33:37 +04:00
}
local = " ${ TEST_SUBDIR } /scripts/local.sh "
if [ -r " $local " ] ; then
. " $local "
fi