ctdb-server: Use find_public_ip_vnn() in a couple of extra places
[samba4-gss.git] / ctdb / tests / scripts / unit.sh
blob403ee07c47456ab2f8393e90409a2b16165dac99
1 # Hey Emacs, this is a -*- shell-script -*- !!! :-)
3 . "${TEST_SCRIPTS_DIR}/common.sh"
5 # Common variables and functions for CTDB unit tests.
7 trap -- '' PIPE
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
14 # from stdin.
15 required_result()
17 required_rc="${1:-0}"
18 if [ -n "$2" ]; then
19 if [ "$2" = "--" ]; then
20 required_output=""
21 else
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
25 # trailing newlines.
26 # shellcheck disable=SC2116
27 required_output=$(echo "$2")
29 else
30 if ! tty -s; then
31 required_output=$(cat)
32 else
33 required_output=""
38 required_error()
40 rc=$(errcode "$1")
41 shift
42 required_result "$rc" "$@"
45 ok()
47 required_result 0 "$@"
50 ok_null()
52 ok --
55 reset_extra_header()
57 # Re-define this function to output extra header information
58 extra_header()
64 reset_extra_footer()
66 # Re-define this function to output extra footer information
67 extra_footer()
73 reset_extra_header
74 reset_extra_footer
76 result_print()
78 _passed="$1"
79 _out="$2"
80 _rc="$3"
82 if "$CTDB_TEST_VERBOSE" || ! $_passed; then
83 extra_header
85 cat <<EOF
86 --------------------------------------------------
87 Output (Exit status: ${_rc}):
88 --------------------------------------------------
89 EOF
90 # Avoid echo, which might expand unintentional escapes
91 printf '%s\n' "$_out" |
92 result_filter |
93 cat "${CTDB_TEST_CAT_RESULTS_OPTS:--}"
96 if ! $_passed; then
97 cat <<EOF
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
107 _outr=$(mktemp)
108 # Avoid echo, which might expand unintentional escapes
109 printf '%s\n' "$required_output" >"$_outr"
111 _outf=$(mktemp)
112 # Avoid echo, which might expand unintentional escapes
113 printf '%s\n' "$_fout" >"$_outf"
115 cat <<EOF
116 --------------------------------------------------
117 Diff:
118 --------------------------------------------------
120 diff -u "$_outr" "$_outf" | cat -A
121 rm "$_outr" "$_outf"
126 result_footer()
128 _passed="$1"
130 if "$CTDB_TEST_VERBOSE" || ! $_passed; then
131 extra_footer
134 if $_passed; then
135 echo "PASSED"
136 return 0
137 else
138 echo
139 echo "FAILED"
140 return 1
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\]:@"
153 # Used in testcases
154 # shellcheck disable=SC2034
155 TEST_DATE_STAMP=""
157 # Override this function to customise output filtering.
158 result_filter()
160 result_filter_default
163 result_check()
165 _rc=$?
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
172 _passed=true
173 else
174 _passed=false
177 result_print "$_passed" "$_out" "$_rc"
178 result_footer "$_passed"
181 test_fail()
183 _passed=false
184 return 1
187 test_case_string=""
188 test_case()
190 test_case_string="$*"
193 test_header_default()
195 echo "=================================================="
196 if [ -n "$test_case_string" ]; then
197 echo "Summary: ${test_case_string}"
198 test_case_string=""
200 echo "Running: $*"
203 reset_test_header()
205 # Re-define this function to get different header
206 test_header()
208 test_header_default "$@"
212 reset_test_header
214 # Simple test harness for running binary unit tests
215 unit_test()
217 test_header "$@"
219 _wrapper="$VALGRIND"
220 if $CTDB_TEST_COMMAND_TRACE; then
221 _wrapper="strace"
223 _out=$($_wrapper "$@" 2>&1)
225 result_check || exit $?
228 # Simple test harness for running shell script unit tests
229 script_test()
231 test_header "$@"
233 _shell=""
234 if ${CTDB_TEST_COMMAND_TRACE}; then
235 _shell="sh -x"
236 else
237 _shell="sh"
240 _out=$($_shell "$@" 2>&1)
242 result_check || exit $?
245 # Simple test harness for running tests without tracing
246 unit_test_notrace()
248 test_header "$@"
250 _out=$("$@" 2>&1)
252 result_check || exit $?
255 test_cleanup_hooks=""
257 test_cleanup()
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
266 . "$local"