blob: 403ee07c47456ab2f8393e90409a2b16165dac99 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
# Hey Emacs, this is a -*- shell-script -*- !!! :-)
. "${TEST_SCRIPTS_DIR}/common.sh"
# Common variables and functions for CTDB unit tests.
trap -- '' PIPE
# 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}"
if [ -n "$2" ]; then
if [ "$2" = "--" ]; then
required_output=""
else
# Use a sub-shell to strip trailing newlines.
# They can't be matched anyway because the
# test is run in a sub-shell, which strips
# trailing newlines.
# shellcheck disable=SC2116
required_output=$(echo "$2")
fi
else
if ! tty -s; then
required_output=$(cat)
else
required_output=""
fi
fi
}
required_error()
{
rc=$(errcode "$1")
shift
required_result "$rc" "$@"
}
ok()
{
required_result 0 "$@"
}
ok_null()
{
ok --
}
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
result_print()
{
_passed="$1"
_out="$2"
_rc="$3"
if "$CTDB_TEST_VERBOSE" || ! $_passed; then
extra_header
cat <<EOF
--------------------------------------------------
Output (Exit status: ${_rc}):
--------------------------------------------------
EOF
# Avoid echo, which might expand unintentional escapes
printf '%s\n' "$_out" |
result_filter |
cat "${CTDB_TEST_CAT_RESULTS_OPTS:--}"
fi
if ! $_passed; then
cat <<EOF
--------------------------------------------------
Required output (Exit status: ${required_rc}):
--------------------------------------------------
EOF
# Avoid echo, which might expand unintentional escapes
printf '%s\n' "$required_output" |
cat "${CTDB_TEST_CAT_RESULTS_OPTS:--}"
if $CTDB_TEST_DIFF_RESULTS; then
_outr=$(mktemp)
# Avoid echo, which might expand unintentional escapes
printf '%s\n' "$required_output" >"$_outr"
_outf=$(mktemp)
# Avoid echo, which might expand unintentional escapes
printf '%s\n' "$_fout" >"$_outf"
cat <<EOF
--------------------------------------------------
Diff:
--------------------------------------------------
EOF
diff -u "$_outr" "$_outf" | cat -A
rm "$_outr" "$_outf"
fi
fi
}
result_footer()
{
_passed="$1"
if "$CTDB_TEST_VERBOSE" || ! $_passed; then
extra_footer
fi
if $_passed; then
echo "PASSED"
return 0
else
echo
echo "FAILED"
return 1
fi
}
# 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\]:@"
}
# Used in testcases
# shellcheck disable=SC2034
TEST_DATE_STAMP=""
# Override this function to customise output filtering.
result_filter()
{
result_filter_default
}
result_check()
{
_rc=$?
# Avoid echo, which might expand unintentional escapes
_fout=$(printf '%s\n' "$_out" | result_filter)
if [ "$_fout" = "$required_output" ] &&
[ "$_rc" = "$required_rc" ]; then
_passed=true
else
_passed=false
fi
result_print "$_passed" "$_out" "$_rc"
result_footer "$_passed"
}
test_fail()
{
_passed=false
return 1
}
test_case_string=""
test_case()
{
test_case_string="$*"
}
test_header_default()
{
echo "=================================================="
if [ -n "$test_case_string" ]; then
echo "Summary: ${test_case_string}"
test_case_string=""
fi
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 $CTDB_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 ${CTDB_TEST_COMMAND_TRACE}; then
_shell="sh -x"
else
_shell="sh"
fi
_out=$($_shell "$@" 2>&1)
result_check || exit $?
}
# Simple test harness for running tests without tracing
unit_test_notrace()
{
test_header "$@"
_out=$("$@" 2>&1)
result_check || exit $?
}
test_cleanup_hooks=""
test_cleanup()
{
test_cleanup_hooks="${test_cleanup_hooks}${test_cleanup_hooks:+ ; }$*"
}
trap 'eval $test_cleanup_hooks' 0
local="${CTDB_TEST_SUITE_DIR}/scripts/local.sh"
if [ -r "$local" ]; then
. "$local"
fi
|