1 # Hey Emacs, this is a -*- shell-script -*- !!! :-)
3 .
"${TEST_SCRIPTS_DIR}/common.sh"
5 # Common variables and functions for CTDB unit tests.
9 # Set the required result for a test.
10 # - Argument 1 is exit code.
11 # - Argument 2, if present is the required test output but "--"
12 # indicates empty output.
13 # If argument 2 is not present or null then read required test output
19 if [ "$2" = "--" ]; then
22 # Use a sub-shell to strip trailing newlines.
23 # They can't be matched anyway because the
24 # test is run in a sub-shell, which strips
26 # shellcheck disable=SC2116
27 required_output
=$
(echo "$2")
31 required_output
=$
(cat)
42 required_result
"$rc" "$@"
47 required_result
0 "$@"
57 # Re-define this function to output extra header information
66 # Re-define this function to output extra footer information
82 if "$CTDB_TEST_VERBOSE" ||
! $_passed; then
86 --------------------------------------------------
87 Output (Exit status: ${_rc}):
88 --------------------------------------------------
90 # Avoid echo, which might expand unintentional escapes
91 printf '%s\n' "$_out" |
93 cat "${CTDB_TEST_CAT_RESULTS_OPTS:--}"
98 --------------------------------------------------
99 Required output (Exit status: ${required_rc}):
100 --------------------------------------------------
102 # Avoid echo, which might expand unintentional escapes
103 printf '%s\n' "$required_output" |
104 cat "${CTDB_TEST_CAT_RESULTS_OPTS:--}"
106 if $CTDB_TEST_DIFF_RESULTS; then
108 # Avoid echo, which might expand unintentional escapes
109 printf '%s\n' "$required_output" >"$_outr"
112 # Avoid echo, which might expand unintentional escapes
113 printf '%s\n' "$_fout" >"$_outf"
116 --------------------------------------------------
118 --------------------------------------------------
120 diff -u "$_outr" "$_outf" |
cat -A
130 if "$CTDB_TEST_VERBOSE" ||
! $_passed; then
144 # Result filtering is (usually) used to replace the date/time/PID
145 # prefix on some CTDB tool/client log messages with the literal string
146 # "DATE TIME [PID]". This allows tests to loosely match this output,
147 # since it can't otherwise be matched.
148 result_filter_default
()
150 _date_time_pid
='[0-9/][0-9/]*\ [0-9:\.][0-9:\.]*\ \[[\ 0-9][\ 0-9]*\]'
151 sed -e "s@^${_date_time_pid}:@DATE\ TIME\ \[PID\]:@"
154 # shellcheck disable=SC2034
157 # Override this function to customise output filtering.
160 result_filter_default
167 # Avoid echo, which might expand unintentional escapes
168 _fout
=$
(printf '%s\n' "$_out" | result_filter
)
170 if [ "$_fout" = "$required_output" ] &&
171 [ "$_rc" = "$required_rc" ]; then
177 result_print
"$_passed" "$_out" "$_rc"
178 result_footer
"$_passed"
190 test_case_string
="$*"
193 test_header_default
()
195 echo "=================================================="
196 if [ -n "$test_case_string" ]; then
197 echo "Summary: ${test_case_string}"
205 # Re-define this function to get different header
208 test_header_default
"$@"
214 # Simple test harness for running binary unit tests
220 if $CTDB_TEST_COMMAND_TRACE; then
223 _out
=$
($_wrapper "$@" 2>&1)
225 result_check ||
exit $?
228 # Simple test harness for running shell script unit tests
234 if ${CTDB_TEST_COMMAND_TRACE}; then
240 _out
=$
($_shell "$@" 2>&1)
242 result_check ||
exit $?
245 # Simple test harness for running tests without tracing
252 result_check ||
exit $?
255 test_cleanup_hooks
=""
259 test_cleanup_hooks
="${test_cleanup_hooks}${test_cleanup_hooks:+ ; }$*"
262 trap 'eval $test_cleanup_hooks' 0
264 local="${CTDB_TEST_SUITE_DIR}/scripts/local.sh"
265 if [ -r "$local" ]; then