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
}
2018-07-10 11:47:27 +03:00
required_error ( )
{
rc = $( errcode $1 )
shift
required_result $rc " $@ "
}
2012-04-18 04:37:45 +04:00
ok ( )
{
required_result 0 " $@ "
}
ok_null ( )
{
ok --
}
2015-07-06 10:35:18 +03:00
reset_extra_header ( )
{
# Re-define this function to output extra header information
extra_header ( )
{
:
}
}
reset_extra_footer ( )
{
# Re-define this function to output extra footer information
extra_footer ( )
{
:
}
}
reset_extra_header
reset_extra_footer
2012-04-18 04:37:45 +04:00
result_print ( )
{
_passed = " $1 "
_out = " $2 "
_rc = " $3 "
if " $TEST_VERBOSE " || ! $_passed ; then
2015-07-06 10:35:18 +03:00
extra_header
2012-04-18 04:37:45 +04:00
cat <<EOF
--------------------------------------------------
Output ( Exit status: ${ _rc } ) :
--------------------------------------------------
EOF
2018-03-26 10:32:57 +03:00
# Avoid echo, which might expand unintentional escapes
printf '%s\n' " $_out " | result_filter | cat $TEST_CAT_RESULTS_OPTS
2012-04-18 04:37:45 +04:00
fi
if ! $_passed ; then
cat <<EOF
--------------------------------------------------
Required output ( Exit status: ${ required_rc } ) :
--------------------------------------------------
EOF
2018-03-26 10:32:57 +03:00
# Avoid echo, which might expand unintentional escapes
printf '%s\n' " $required_output " | cat $TEST_CAT_RESULTS_OPTS
2012-04-18 04:37:45 +04:00
if $TEST_DIFF_RESULTS ; then
_outr = $( mktemp)
2018-03-26 10:32:57 +03:00
# Avoid echo, which might expand unintentional escapes
printf '%s\n' " $required_output " >" $_outr "
2012-04-18 04:37:45 +04:00
_outf = $( mktemp)
2018-03-26 10:32:57 +03:00
# Avoid echo, which might expand unintentional escapes
printf '%s\n' " $_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 "
if " $TEST_VERBOSE " || ! $_passed ; then
2015-07-06 10:35:18 +03:00
extra_footer
2012-04-18 04:37:45 +04:00
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:12:56 +04:00
TEST_DATE_STAMP = ""
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 = $?
2018-03-26 10:32:57 +03:00
# Avoid echo, which might expand unintentional escapes
_fout = $( printf '%s\n' " $_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
2015-07-06 10:35:18 +03:00
result_print " $_passed " " $_out " " $_rc "
2012-04-18 04:37:45 +04:00
result_footer " $_passed "
2012-04-16 08:33:37 +04:00
}
2015-03-05 05:11:46 +03:00
test_fail ( )
{
_passed = false
return 1
}
2015-07-06 07:45:23 +03:00
test_header_default ( )
{
echo "=================================================="
echo " Running \" $* \" "
}
reset_test_header ( )
{
# Re-define this function to get different header
test_header ( )
{
test_header_default " $@ "
}
}
reset_test_header
# Simple test harness for running binary unit tests
unit_test ( )
{
test_header " $@ "
_wrapper = " $VALGRIND "
if $TEST_COMMAND_TRACE ; then
_wrapper = "strace"
fi
_out = $( $_wrapper " $@ " 2>& 1)
result_check || exit $?
}
# Simple test harness for running shell script unit tests
script_test ( )
{
test_header " $@ "
_shell = ""
if ${ TEST_COMMAND_TRACE } ; then
_shell = "sh -x"
else
_shell = "sh"
fi
_out = $( $_shell " $@ " 2>& 1)
result_check || exit $?
}
2016-12-13 23:18:57 +03:00
# Simple test harness for running tests without tracing
unit_test_notrace ( )
{
test_header " $@ "
_out = $( " $@ " 2>& 1)
result_check || exit $?
}
2015-07-06 08:14:53 +03:00
test_cleanup_hooks = ""
test_cleanup ( )
{
test_cleanup_hooks = " ${ test_cleanup_hooks } ${ test_cleanup_hooks : + ; } $* "
}
trap 'eval $test_cleanup_hooks' 0
2012-04-16 08:33:37 +04:00
local = " ${ TEST_SUBDIR } /scripts/local.sh "
if [ -r " $local " ] ; then
. " $local "
fi