1
0
mirror of https://github.com/samba-team/samba.git synced 2025-03-20 22:50:26 +03:00

tests: More unit test factoring/rationalisation and bug fixes

Move some options from eventscripts/run_tests.sh to scripts/run_tests.
Remove the former.

Move some functions from eventscripts/scripts/local.sh to
scripts/unit.sh.

Both of these are modified during move so they are no longer
eventscript-specific.

Tweak */local.sh so that the new functions in unit.sh are used.

Signed-off-by: Martin Schwenke <martin@meltin.net>

(This used to be ctdb commit 7ff485687891732074c9fc9998502ca197663d02)
This commit is contained in:
Martin Schwenke 2012-04-18 10:37:45 +10:00
parent 6e05811c28
commit 116f19b808
8 changed files with 175 additions and 268 deletions

View File

@ -1 +0,0 @@
../../../config/interface_modify.sh

View File

@ -1,53 +0,0 @@
#!/bin/sh
# Eventscript unit test harness.
cd $(dirname "$0")
export EVENTSCRIPTS_TESTS_DIR=$(pwd)
test_dir=$(dirname "$EVENTSCRIPTS_TESTS_DIR")
export EVENTSCRIPT_TESTS_CAT_RESULTS_OPTS=""
export EVENTSCRIPT_TESTS_DIFF_RESULTS=false
opts="-d"
for i ; do
case "$i" in
-v)
export EVENTSCRIPT_TESTS_VERBOSE="yes"
shift
;;
-T)
# This will cause tests to fail but is good for debugging
# individual tests when they fail.
export EVENTSCRIPTS_TESTS_TRACE="sh -x"
shift
;;
-A)
# Useful for detecting whitespace differences in results
export EVENTSCRIPT_TESTS_CAT_RESULTS_OPTS="-A"
shift
;;
-D)
# Useful for detecting whitespace differences in results
export EVENTSCRIPT_TESTS_DIFF_RESULTS=true
shift
;;
-*)
opts="$opts $i"
shift
;;
*)
break
esac
done
tests=""
if [ -z "$*" ] ; then
tests=$(ls simple/[0-9][0-9].*.*.[0-9][0-9][0-9].sh simple/[0-9][0-9].*.*.[0-9][0-9][0-9]/run_test.sh 2>/dev/null)
fi
"$test_dir/scripts/run_tests" $opts "$@" $tests || exit 1
exit 0

View File

@ -38,7 +38,7 @@ export CTDB_VARDIR="$EVENTSCRIPTS_TESTS_VAR_DIR/ctdb"
######################################################################
if [ "$TEST_VERBOSE" = "yes" ] ; then
if "$TEST_VERBOSE" ; then
debug () { echo "$@" ; }
else
debug () { : ; }
@ -672,133 +672,13 @@ define_test ()
printf "%-17s %-10s %-4s - %s\n\n" "$script" "$event" "$_num" "$desc"
}
# 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.
required_result ()
_extra_header ()
{
required_rc="${1:-0}"
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"
_iteration="$4"
if [ "$EVENTSCRIPT_TESTS_VERBOSE" = "yes" ] || ! $_passed ; then
if [ -n "$_iteration" ] ; then
cat <<EOF
==================================================
Iteration $_iteration
EOF
fi
cat <<EOF
--------------------------------------------------
Output (Exit status: ${_rc}):
--------------------------------------------------
EOF
echo "$_out" | cat $EVENTSCRIPT_TESTS_CAT_RESULTS_OPTS
fi
if ! $_passed ; then
cat <<EOF
--------------------------------------------------
Required output (Exit status: ${required_rc}):
--------------------------------------------------
EOF
echo "$required_output" | cat $EVENTSCRIPT_TESTS_CAT_RESULTS_OPTS
if $EVENTSCRIPT_TESTS_DIFF_RESULTS ; then
_outr=$(mktemp)
echo "$required_output" >"$_outr"
_outf=$(mktemp)
echo "$_out" >"$_outf"
cat <<EOF
--------------------------------------------------
Diff:
--------------------------------------------------
EOF
diff -u "$_outr" "$_outf" | cat -A
rm "$_outr" "$_outf"
fi
fi
}
result_footer ()
{
_passed="$1"
if [ "$TEST_VERBOSE" = "yes" ] || ! $_passed ; then
cat <<EOF
--------------------------------------------------
cat <<EOF
CTDB_BASE="$CTDB_BASE"
CTDB_ETCDIR="$CTDB_ETCDIR"
ctdb client is "$(which ctdb)"
--------------------------------------------------
EOF
fi
if $_passed ; then
echo "PASSED"
return 0
else
echo
echo "FAILED"
return 1
fi
}
result_check ()
{
_rc=$?
if [ -n "$OUT_FILTER" ] ; then
_fout=$(echo "$_out" | eval sed -r $OUT_FILTER)
else
_fout="$_out"
fi
if [ "$_fout" = "$required_output" -a $_rc = $required_rc ] ; then
_passed=true
else
_passed=false
fi
result_print "$_passed" "$_out" "$_rc"
result_footer "$_passed"
}
# Run an eventscript once. The test passes if the return code and
@ -815,10 +695,12 @@ simple_test ()
{
[ -n "$event" ] || die 'simple_test: $event not set'
echo "Running eventscript \"$script $event${1:+ }$*\""
_out=$($EVENTSCRIPTS_TESTS_TRACE "${CTDB_BASE}/events.d/$script" "$event" "$@" 2>&1)
_extra_header=$(_extra_header)
result_check
echo "Running eventscript \"$script $event${1:+ }$*\""
_out=$($TEST_COMMAND_TRACE "${CTDB_BASE}/events.d/$script" "$event" "$@" 2>&1)
result_check "$_extra_header"
}
simple_test_event ()
@ -912,8 +794,8 @@ iterate_test ()
_result=false
fi
result_print "$_passed" "$_out" "$_rc" "$iteration"
result_print "$_passed" "$_out" "$_rc" "Iteration $iteration"
done
result_footer "$_result"
result_footer "$_result" "$(_extra_header)"
}

View File

@ -49,10 +49,14 @@ ctdb_set_output ()
trap "rm -f $_out $_rc" 0
}
required_result ()
_extra_header ()
{
required_rc="${1:-0}"
required_output=$(cat)
cat <<EOF
CTDB_NODES_FILE="${CTDB_NODES_FILE}"
CTDB_BASE="$CTDB_BASE"
$(which ctdb)
EOF
}
simple_test ()
@ -62,27 +66,16 @@ simple_test ()
shift
_sort="sort"
fi
_out=$("$@" 2>&1)
_rc=$?
_out=$(echo "$_out" | $_sort )
if [ "$_out" = "$required_output" -a $_rc = $required_rc ] ; then
echo "PASSED"
else
cat <<EOF
CTDB_NODES_FILE="${CTDB_NODES_FILE}"
CTDB_BASE="$CTDB_BASE"
$(which ctdb)
# Can't do this inline or it affects return code
_extra_header="$(_extra_header)"
##################################################
Required output (Exit status: ${required_rc}):
##################################################
$required_output
##################################################
Actual output (Exit status: ${_rc}):
##################################################
$_out
EOF
return 1
fi
# Get the return code back into $?
(exit $_rc)
result_check "$_extra_header"
}

View File

@ -11,6 +11,15 @@ usage() {
cat <<EOF
Usage: run_tests [OPTIONS] [TESTS]
Options:
-s Print a summary of tests results after running all tests
-v Verbose - print test output for non-failures (only some tests)
-A Use "cat -A" to print test output (only some tests)
-D Show diff between failed/expected test output (some tests only)
-X Trace certain scripts run by tests using -x (only some tests)
-d Print descriptions of tests instead of filenames (dodgy!)
-q Quiet - don't show tests being run (hint: use with -s)
-x Trace this script with the -x option
EOF
exit 1
}
@ -22,8 +31,11 @@ with_desc=false
quiet=false
export TEST_VERBOSE=false
export TEST_COMMAND_TRACE=""
export TEST_CAT_RESULTS_OPTS=""
export TEST_DIFF_RESULTS=false
temp=$(getopt -n "$prog" -o "xdhqsv" -l help -- "$@")
temp=$(getopt -n "$prog" -o "xdhqsvXAD" -l help -- "$@")
[ $? != 0 ] && usage
@ -36,6 +48,9 @@ while true ; do
-q) quiet=true ; shift ;;
-s) with_summary=true ; shift ;;
-v) TEST_VERBOSE=true ; shift ;;
-X) TEST_COMMAND_TRACE="sh -x" ; shift ;;
-A) TEST_CAT_RESULTS_OPTS="-A" ; shift ;;
-D) TEST_DIFF_RESULTS=true ; shift ;;
--) shift ; break ;;
*) usage ;;
esac

View File

@ -4,10 +4,135 @@
# Common variables and functions for CTDB unit tests.
# 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.
required_result ()
{
required_rc="${1:-0}"
required_output=$(cat)
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
==================================================
$_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)
echo "$_out" >"$_outf"
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
}
result_check ()
{
_rc=$?
_extra_header="$1"
if [ -n "$OUT_FILTER" ] ; then
_fout=$(echo "$_out" | eval sed -r $OUT_FILTER)
else
_fout="$_out"
fi
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"
}
local="${TEST_SUBDIR}/scripts/local.sh"

View File

@ -24,35 +24,13 @@ define_test ()
simple_test ()
{
# Do some filtering of the output to replace date/time.
if [ "$algorithm" = "lcp2" -a -n "$CTDB_TEST_LOGLEVEL" ] ; then
OUT_FILTER='s@^.*:@DATE\ TIME\ \[PID\]:@'
fi
_states="$1"
_out=$($test_prog $_states 2>&1)
_rc=$?
if [ "$algorithm" = "lcp2" -a -n "$CTDB_TEST_LOGLEVEL" ] ; then
OUT_FILTER='s@^.*:@DATE TIME \[PID\]:@'
fi
if [ -n "$OUT_FILTER" ] ; then
_fout=$(echo "$_out" | sed -r "$OUT_FILTER")
else
_fout="$_out"
fi
if [ "$_fout" = "$required_output" -a $_rc = $required_rc ] ; then
echo "PASSED"
else
cat <<EOF
Algorithm: $algorithm
##################################################
Required output (Exit status: ${required_rc}):
##################################################
$required_output
##################################################
Actual output (Exit status: ${_rc}):
##################################################
$_out
EOF
return 1
fi
result_check "Algorithm: $algorithm"
}

View File

@ -1,8 +1,5 @@
# Hey Emacs, this is a -*- shell-script -*- !!! :-)
# Print a message and exit.
die () { echo "$@" >&2 ; exit 1 ; }
test_bin="$(dirname ${TEST_SUBDIR})/bin"
define_test ()
@ -29,42 +26,13 @@ define_test ()
simple_test ()
{
_out=$($test_prog "$@" 2>&1)
_rc=$?
# Most of the tests when the tool fails will have a date/time/pid
# prefix. Strip that because it isn't possible to match it.
if [ $required_rc -ne 0 ] ; then
OUT_FILTER='s@^[0-9/]+ [0-9:\.]+ \[[ 0-9]+\]:@DATE TIME \[PID\]:@'
OUT_FILTER='s@^[0-9/]+\ [0-9:\.]+\ \[[\ 0-9]+\]:@DATE\ TIME\ \[PID\]:@'
fi
if [ -n "$OUT_FILTER" ] ; then
_fout=$(echo "$_out" | sed -r "$OUT_FILTER")
else
_fout="$_out"
fi
_out=$($test_prog "$@" 2>&1)
if [ "$_fout" = "$required_output" -a $_rc = $required_rc ] ; then
if [ "$TEST_VERBOSE" = "yes" ] ; then
cat <<EOF
##################################################
Output (Exit status: ${_rc}):
##################################################
$_fout
EOF
fi
echo "PASSED"
else
cat -A <<EOF
##################################################
Required output (Exit status: ${required_rc}):
##################################################
$required_output
##################################################
Actual output (Exit status: ${_rc}):
##################################################
$_fout
EOF
return 1
fi
result_check
}