source: vendor/current/ctdb/tests/scripts/unit.sh

Last change on this file was 988, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.3

File size: 3.9 KB
Line 
1# Hey Emacs, this is a -*- shell-script -*- !!! :-)
2
3. "${TEST_SCRIPTS_DIR}/common.sh"
4
5# Common variables and functions for CTDB unit tests.
6
7trap -- '' PIPE
8
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.
15required_result ()
16{
17 required_rc="${1:-0}"
18 if [ -n "$2" ] ; then
19 if [ "$2" = "--" ] ; then
20 required_output=""
21 else
22 required_output="$2"
23 fi
24 else
25 if ! tty -s ; then
26 required_output=$(cat)
27 else
28 required_output=""
29 fi
30 fi
31}
32
33ok ()
34{
35 required_result 0 "$@"
36}
37
38ok_null ()
39{
40 ok --
41}
42
43reset_extra_header ()
44{
45 # Re-define this function to output extra header information
46 extra_header ()
47 {
48 :
49 }
50}
51
52reset_extra_footer ()
53{
54 # Re-define this function to output extra footer information
55 extra_footer ()
56 {
57 :
58 }
59}
60
61reset_extra_header
62reset_extra_footer
63
64result_print ()
65{
66 _passed="$1"
67 _out="$2"
68 _rc="$3"
69
70 if "$TEST_VERBOSE" || ! $_passed ; then
71 extra_header
72
73cat <<EOF
74--------------------------------------------------
75Output (Exit status: ${_rc}):
76--------------------------------------------------
77EOF
78 echo "$_out" | cat $TEST_CAT_RESULTS_OPTS
79 fi
80
81 if ! $_passed ; then
82 cat <<EOF
83--------------------------------------------------
84Required output (Exit status: ${required_rc}):
85--------------------------------------------------
86EOF
87 echo "$required_output" | cat $TEST_CAT_RESULTS_OPTS
88
89 if $TEST_DIFF_RESULTS ; then
90 _outr=$(mktemp)
91 echo "$required_output" >"$_outr"
92
93 _outf=$(mktemp)
94 echo "$_fout" >"$_outf"
95
96 cat <<EOF
97--------------------------------------------------
98Diff:
99--------------------------------------------------
100EOF
101 diff -u "$_outr" "$_outf" | cat -A
102 rm "$_outr" "$_outf"
103 fi
104 fi
105}
106
107result_footer ()
108{
109 _passed="$1"
110
111 if "$TEST_VERBOSE" || ! $_passed ; then
112 extra_footer
113 fi
114
115 if $_passed ; then
116 echo "PASSED"
117 return 0
118 else
119 echo
120 echo "FAILED"
121 return 1
122 fi
123}
124
125# Result filtering is (usually) used to replace the date/time/PID
126# prefix on some CTDB tool/client log messages with the literal string
127# "DATE TIME [PID]". This allows tests to loosely match this output,
128# since it can't otherwise be matched.
129result_filter_default ()
130{
131 _date_time_pid='[0-9/][0-9/]*\ [0-9:\.][0-9:\.]*\ \[[\ 0-9][\ 0-9]*\]'
132 sed -e "s@^${_date_time_pid}:@DATE\ TIME\ \[PID\]:@"
133}
134TEST_DATE_STAMP=""
135
136# Override this function to customise output filtering.
137result_filter ()
138{
139 result_filter_default
140}
141
142result_check ()
143{
144 _rc=$?
145
146 _fout=$(echo "$_out" | result_filter)
147
148 if [ "$_fout" = "$required_output" -a $_rc = $required_rc ] ; then
149 _passed=true
150 else
151 _passed=false
152 fi
153
154 result_print "$_passed" "$_out" "$_rc"
155 result_footer "$_passed"
156}
157
158test_fail ()
159{
160 _passed=false
161 return 1
162}
163
164test_header_default ()
165{
166 echo "=================================================="
167 echo "Running \"$*\""
168}
169
170reset_test_header ()
171{
172 # Re-define this function to get different header
173 test_header ()
174 {
175 test_header_default "$@"
176 }
177}
178
179reset_test_header
180
181# Simple test harness for running binary unit tests
182unit_test ()
183{
184 test_header "$@"
185
186 _wrapper="$VALGRIND"
187 if $TEST_COMMAND_TRACE ; then
188 _wrapper="strace"
189 fi
190 _out=$($_wrapper "$@" 2>&1)
191
192 result_check || exit $?
193}
194
195# Simple test harness for running shell script unit tests
196script_test ()
197{
198 test_header "$@"
199
200 _shell=""
201 if ${TEST_COMMAND_TRACE} ; then
202 _shell="sh -x"
203 else
204 _shell="sh"
205 fi
206
207 _out=$($_shell "$@" 2>&1)
208
209 result_check || exit $?
210}
211
212test_cleanup_hooks=""
213
214test_cleanup ()
215{
216 test_cleanup_hooks="${test_cleanup_hooks}${test_cleanup_hooks:+ ; }$*"
217}
218
219trap 'eval $test_cleanup_hooks' 0
220
221local="${TEST_SUBDIR}/scripts/local.sh"
222if [ -r "$local" ] ; then
223 . "$local"
224fi
Note: See TracBrowser for help on using the repository browser.