Commit | Line | Data |
---|---|---|
abdabab5 MJ |
1 | #!/bin/bash |
2 | ||
3 | # Copyright (c) 2019 Michael Jeanson <mjeanson@efficios.com> | |
4 | # Copyright (C) 2019 Philippe Proulx <pproulx@efficios.com> | |
5 | # | |
6 | # This program is free software; you can redistribute it and/or modify | |
7 | # it under the terms of the GNU General Public License as published by | |
8 | # the Free Software Foundation; under version 2 of the License. | |
9 | # | |
10 | # This program is distributed in the hope that it will be useful, | |
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | # GNU General Public License for more details. | |
14 | # | |
15 | # You should have received a copy of the GNU General Public License along | |
16 | # with this program; if not, write to the Free Software Foundation, Inc., | |
17 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 | ||
19 | # This file is meant to be sourced at the start of shell script-based tests. | |
20 | ||
21 | ||
22 | # Error out when encountering an undefined variable | |
23 | set -u | |
24 | ||
d3f71134 MJ |
25 | # If "readlink -f" is available, get a resolved absolute path to the |
26 | # tests source dir, otherwise make do with a relative path. | |
abdabab5 | 27 | scriptdir="$(dirname "${BASH_SOURCE[0]}")" |
d3f71134 MJ |
28 | if readlink -f "." >/dev/null 2>&1; then |
29 | testsdir=$(readlink -f "$scriptdir/..") | |
30 | else | |
31 | testsdir="$scriptdir/.." | |
32 | fi | |
abdabab5 | 33 | |
d660133b JR |
34 | # The OS on which we are running. See [1] for possible values of 'uname -s'. |
35 | # We do a bit of translation to ease our life down the road for comparison. | |
36 | # Export it so that called executables can use it. | |
37 | # [1] https://en.wikipedia.org/wiki/Uname#Examples | |
38 | if [ "x${BT_OS_TYPE:-}" = "x" ]; then | |
39 | BT_OS_TYPE="$(uname -s)" | |
40 | case "$BT_OS_TYPE" in | |
41 | MINGW*) | |
42 | BT_OS_TYPE="mingw" | |
43 | ;; | |
44 | Darwin) | |
45 | BT_OS_TYPE="darwin" | |
46 | ;; | |
47 | Linux) | |
48 | BT_OS_TYPE="linux" | |
49 | ;; | |
5200c954 JR |
50 | CYGWIN*) |
51 | BT_OS_TYPE="cygwin" | |
52 | ;; | |
d660133b JR |
53 | *) |
54 | BT_OS_TYPE="unsupported" | |
55 | ;; | |
56 | esac | |
57 | fi | |
58 | export BT_OS_TYPE | |
59 | ||
abdabab5 MJ |
60 | # Allow overriding the source and build directories |
61 | if [ "x${BT_TESTS_SRCDIR:-}" = "x" ]; then | |
d3f71134 | 62 | BT_TESTS_SRCDIR="$testsdir" |
abdabab5 MJ |
63 | fi |
64 | export BT_TESTS_SRCDIR | |
65 | ||
66 | if [ "x${BT_TESTS_BUILDDIR:-}" = "x" ]; then | |
d3f71134 | 67 | BT_TESTS_BUILDDIR="$testsdir" |
abdabab5 MJ |
68 | fi |
69 | export BT_TESTS_BUILDDIR | |
70 | ||
71 | # By default, it will not source tap.sh. If you to tap output directly from | |
72 | # the test script, define the 'SH_TAP' variable to '1' before sourcing this | |
73 | # script. | |
74 | if [ "x${SH_TAP:-}" = x1 ]; then | |
8c083b3e | 75 | # shellcheck source=./tap/tap.sh |
abdabab5 MJ |
76 | . "${BT_TESTS_SRCDIR}/utils/tap/tap.sh" |
77 | fi | |
78 | ||
79 | # Allow overriding the babeltrace2 executables | |
80 | if [ "x${BT_TESTS_BT2_BIN:-}" = "x" ]; then | |
f090979c | 81 | BT_TESTS_BT2_BIN="$BT_TESTS_BUILDDIR/../src/cli/babeltrace2" |
d660133b | 82 | if [ "$BT_OS_TYPE" = "mingw" ]; then |
a8a11adb JR |
83 | BT_TESTS_BT2_BIN="${BT_TESTS_BT2_BIN}.exe" |
84 | fi | |
abdabab5 MJ |
85 | fi |
86 | export BT_TESTS_BT2_BIN | |
87 | ||
88 | if [ "x${BT_TESTS_BT2LOG_BIN:-}" = "x" ]; then | |
f090979c | 89 | BT_TESTS_BT2LOG_BIN="$BT_TESTS_BUILDDIR/../src/cli/babeltrace2-log" |
d660133b | 90 | if [ "$BT_OS_TYPE" = "mingw" ]; then |
a8a11adb JR |
91 | BT_TESTS_BT2LOG_BIN="${BT_TESTS_BT2LOG_BIN}.exe" |
92 | fi | |
abdabab5 MJ |
93 | fi |
94 | export BT_TESTS_BT2LOG_BIN | |
95 | ||
96 | # TODO: Remove when bindings/python/bt2/test_plugin.py is fixed | |
97 | BT_PLUGINS_PATH="${BT_TESTS_BUILDDIR}/../src/plugins" | |
98 | ||
99 | # Allow overriding the babeltrace2 plugin path | |
100 | if [ "x${BT_TESTS_BABELTRACE_PLUGIN_PATH:-}" = "x" ]; then | |
101 | BT_TESTS_BABELTRACE_PLUGIN_PATH="${BT_PLUGINS_PATH}/ctf:${BT_PLUGINS_PATH}/utils:${BT_PLUGINS_PATH}/text" | |
102 | fi | |
103 | ||
7b783015 MJ |
104 | if [ "x${BT_TESTS_PROVIDER_DIR:-}" = "x" ]; then |
105 | BT_TESTS_PROVIDER_DIR="${BT_TESTS_BUILDDIR}/../src/python-plugin-provider/.libs" | |
106 | fi | |
107 | ||
abdabab5 MJ |
108 | # Allow overriding the babeltrace2 executables |
109 | if [ "x${BT_TESTS_PYTHONPATH:-}" = "x" ]; then | |
110 | BT_TESTS_PYTHONPATH="${BT_TESTS_BUILDDIR}/../src/bindings/python/bt2/build/build_lib" | |
111 | fi | |
112 | ||
113 | ||
114 | ### External Tools ### | |
115 | if [ "x${BT_TESTS_AWK_BIN:-}" = "x" ]; then | |
116 | BT_TESTS_AWK_BIN="awk" | |
117 | fi | |
118 | export BT_TESTS_AWK_BIN | |
119 | ||
120 | if [ "x${BT_TESTS_GREP_BIN:-}" = "x" ]; then | |
121 | BT_TESTS_GREP_BIN="grep" | |
122 | fi | |
123 | export BT_TESTS_GREP_BIN | |
124 | ||
125 | if [ "x${BT_TESTS_PYTHON_BIN:-}" = "x" ]; then | |
126 | BT_TESTS_PYTHON_BIN="python3" | |
127 | fi | |
128 | export BT_TESTS_PYTHON_BIN | |
129 | ||
4b2d0d1f MJ |
130 | if [ "x${BT_TESTS_PYTHON_CONFIG_BIN:-}" = "x" ]; then |
131 | BT_TESTS_PYTHON_CONFIG_BIN="python3-config" | |
132 | fi | |
133 | export BT_TESTS_PYTHON_BIN | |
134 | ||
abdabab5 MJ |
135 | if [ "x${BT_TESTS_SED_BIN:-}" = "x" ]; then |
136 | BT_TESTS_SED_BIN="sed" | |
137 | fi | |
138 | export BT_TESTS_SED_BIN | |
139 | ||
140 | ||
141 | # Data files path | |
142 | BT_TESTS_DATADIR="${BT_TESTS_SRCDIR}/data" | |
143 | BT_CTF_TRACES_PATH="${BT_TESTS_DATADIR}/ctf-traces" | |
abdabab5 MJ |
144 | |
145 | ||
146 | ### Diff Functions ### | |
147 | ||
d8f6e7fb MJ |
148 | # Checks the difference between stdout: |
149 | # | |
150 | # The file with path "$1", and the file with path "$2" | |
151 | # | |
152 | # And the difference between stderr: | |
153 | # | |
154 | # The file with path "$3", and the file with path "$4" | |
155 | # | |
156 | # Returns 0 if there's no difference, and 1 if there is, also printing | |
157 | # said difference to the standard error, and an error message with the | |
158 | # args starting at "$5". | |
159 | bt_diff() { | |
160 | local expected_stdout_file="$1" | |
161 | local actual_stdout_file="$2" | |
162 | local expected_stderr_file="$3" | |
163 | local actual_stderr_file="$4" | |
164 | shift 4 | |
165 | local args=("$@") | |
166 | ||
167 | local ret=0 | |
168 | local temp_diff | |
169 | ||
170 | temp_diff="$(mktemp -t diff.XXXXXX)" | |
171 | ||
172 | # Strip any \r present due to Windows (\n -> \r\n). | |
173 | # "diff --string-trailing-cr" is not used since it is not present on | |
174 | # Solaris. | |
175 | "$BT_TESTS_SED_BIN" -i 's/\r//g' "$actual_stdout_file" | |
176 | "$BT_TESTS_SED_BIN" -i 's/\r//g' "$actual_stderr_file" | |
177 | ||
178 | # Compare stdout output with expected stdout output | |
179 | if ! diff -u "$actual_stdout_file" "$expected_stdout_file" 2>/dev/null >"$temp_diff"; then | |
180 | echo "ERROR: for '${args[*]}': actual standard output and expected output differ:" >&2 | |
181 | cat "$temp_diff" >&2 | |
182 | ret=1 | |
183 | fi | |
184 | ||
185 | # Compare stderr output with expected stderr output | |
186 | if ! diff -u "$actual_stderr_file" "$expected_stderr_file" 2>/dev/null >"$temp_diff"; then | |
187 | echo "ERROR: for '${args[*]}': actual standard error and expected error differ:" >&2 | |
188 | cat "$temp_diff" >&2 | |
189 | ret=1 | |
190 | fi | |
191 | ||
192 | rm -f "$temp_diff" | |
193 | ||
194 | return $ret | |
195 | } | |
196 | ||
ca5f1044 FD |
197 | # Checks the difference between: |
198 | # | |
199 | # 1. What the CLI outputs on its standard output when given the arguments | |
200 | # "$@" (excluding the first two arguments). | |
201 | # 2. The file with path "$1". | |
202 | # | |
203 | # And the difference between: | |
204 | # | |
205 | # 1. What the CLI outputs on its standard error when given the arguments | |
206 | # "$@" (excluding the first two arguments). | |
207 | # 2. The file with path "$2". | |
abdabab5 MJ |
208 | # |
209 | # Returns 0 if there's no difference, and 1 if there is, also printing | |
210 | # said difference to the standard error. | |
211 | bt_diff_cli() { | |
ca5f1044 FD |
212 | local expected_stdout_file="$1" |
213 | local expected_stderr_file="$2" | |
214 | shift 2 | |
b241db8f MJ |
215 | local args=("$@") |
216 | ||
ca5f1044 FD |
217 | local temp_stdout_output_file |
218 | local temp_stderr_output_file | |
abdabab5 MJ |
219 | local ret=0 |
220 | ||
d8f6e7fb MJ |
221 | temp_stdout_output_file="$(mktemp -t actual_stdout.XXXXXX)" |
222 | temp_stderr_output_file="$(mktemp -t actual_stderr.XXXXXX)" | |
abdabab5 | 223 | |
ca5f1044 FD |
224 | # Run the CLI to get a detailed file. |
225 | run_python_bt2 "$BT_TESTS_BT2_BIN" "${args[@]}" 1>"$temp_stdout_output_file" 2>"$temp_stderr_output_file" | |
226 | ||
d8f6e7fb MJ |
227 | bt_diff "$expected_stdout_file" "$temp_stdout_output_file" "$expected_stderr_file" "$temp_stderr_output_file" "${args[@]}" |
228 | ret=$? | |
ca5f1044 | 229 | |
d8f6e7fb | 230 | rm -f "$temp_stdout_output_file" "$temp_stderr_output_file" |
abdabab5 | 231 | |
d8f6e7fb MJ |
232 | return $ret |
233 | } | |
234 | ||
235 | # Checks the difference between: | |
236 | # | |
237 | # 1. What the CLI outputs on its standard output when given the arguments | |
238 | # "$@" (excluding the first two arguments), sorted with the default "sort". | |
239 | # 2. The file with path "$1". | |
240 | # | |
241 | # And the difference between: | |
242 | # | |
243 | # 1. What the CLI outputs on its standard error when given the arguments | |
244 | # "$@" (excluding the first two arguments). | |
245 | # 2. The file with path "$2". | |
246 | # | |
247 | # Returns 0 if there's no difference, and 1 if there is, also printing | |
248 | # said difference to the standard error. | |
249 | bt_diff_cli_sorted() { | |
250 | local expected_stdout_file="$1" | |
251 | local expected_stderr_file="$2" | |
252 | shift 2 | |
253 | local args=("$@") | |
254 | ||
255 | local temp_stdout_output_file | |
256 | local temp_stderr_output_file | |
257 | local ret=0 | |
258 | ||
259 | temp_stdout_output_file="$(mktemp -t actual_stdout.XXXXXX)" | |
260 | temp_stderr_output_file="$(mktemp -t actual_stderr.XXXXXX)" | |
261 | ||
262 | # Run the CLI to get a detailed file. | |
263 | run_python_bt2 "$BT_TESTS_BT2_BIN" "${args[@]}" 1>"$temp_stdout_output_file" 2>"$temp_stderr_output_file" | |
264 | ||
265 | # Sort the stdout file, use a subshell to do it in-place | |
266 | # shellcheck disable=SC2005 | |
267 | echo "$(LC_ALL=C sort "$temp_stdout_output_file")" > "$temp_stdout_output_file" | |
268 | ||
269 | bt_diff "$expected_stdout_file" "$temp_stdout_output_file" "$expected_stderr_file" "$temp_stderr_output_file" "${args[@]}" | |
270 | ret=$? | |
abdabab5 | 271 | |
d8f6e7fb | 272 | rm -f "$temp_stdout_output_file" "$temp_stderr_output_file" |
abdabab5 MJ |
273 | |
274 | return $ret | |
275 | } | |
276 | ||
b241db8f MJ |
277 | # Checks the difference between the content of the file with path "$1" |
278 | # and the output of the CLI when called on the directory path "$2" with | |
279 | # the arguments '-c sink.text.details' and the rest of the arguments to | |
280 | # this function. | |
abdabab5 MJ |
281 | # |
282 | # Returns 0 if there's no difference, and 1 if there is, also printing | |
283 | # said difference to the standard error. | |
284 | bt_diff_details_ctf_single() { | |
ca5f1044 | 285 | local expected_stdout_file="$1" |
b241db8f MJ |
286 | local trace_dir="$2" |
287 | shift 2 | |
288 | local extra_details_args=("$@") | |
ca5f1044 | 289 | expected_stderr_file="/dev/null" |
abdabab5 MJ |
290 | |
291 | # Compare using the CLI with `sink.text.details` | |
ca5f1044 | 292 | bt_diff_cli "$expected_stdout_file" "$expected_stderr_file" "$trace_dir" "-c" "sink.text.details" "${extra_details_args[@]}" |
abdabab5 MJ |
293 | } |
294 | ||
295 | # Calls bt_diff_details_ctf_single(), except that "$1" is the path to a | |
296 | # program which generates the CTF trace to compare to. The program "$1" | |
297 | # receives the path to a temporary, empty directory where to write the | |
298 | # CTF trace as its first argument. | |
299 | bt_diff_details_ctf_gen_single() { | |
300 | local ctf_gen_prog_path="$1" | |
ca5f1044 | 301 | local expected_stdout_file="$2" |
b241db8f MJ |
302 | shift 2 |
303 | local extra_details_args=("$@") | |
abdabab5 MJ |
304 | |
305 | local temp_trace_dir | |
306 | local ret | |
307 | ||
308 | temp_trace_dir="$(mktemp -d)" | |
309 | ||
310 | # Run the CTF trace generator program to get a CTF trace | |
311 | if ! "$ctf_gen_prog_path" "$temp_trace_dir" 2>/dev/null; then | |
312 | echo "ERROR: \"$ctf_gen_prog_path\" \"$temp_trace_dir\" failed" >&2 | |
313 | rm -rf "$temp_trace_dir" | |
314 | return 1 | |
315 | fi | |
316 | ||
317 | # Compare using the CLI with `sink.text.details` | |
ca5f1044 | 318 | bt_diff_details_ctf_single "$expected_stdout_file" "$temp_trace_dir" "${extra_details_args[@]}" |
abdabab5 MJ |
319 | ret=$? |
320 | rm -rf "$temp_trace_dir" | |
321 | return $ret | |
322 | } | |
323 | ||
324 | ||
325 | ### Functions ### | |
326 | ||
327 | check_coverage() { | |
328 | coverage run "$@" | |
329 | } | |
330 | ||
331 | # Execute a shell command in the appropriate environment to have access to the | |
332 | # bt2 Python bindings. | |
333 | run_python_bt2() { | |
4b2d0d1f MJ |
334 | local env_args |
335 | ||
336 | env_args=( | |
337 | "BABELTRACE_PYTHON_BT2_NO_TRACEBACK=1" \ | |
338 | "BABELTRACE_PLUGIN_PATH=${BT_TESTS_BABELTRACE_PLUGIN_PATH}" \ | |
339 | "LIBBABELTRACE2_PLUGIN_PROVIDER_DIR=${BT_TESTS_PROVIDER_DIR}" \ | |
340 | "BT_CTF_TRACES_PATH=${BT_CTF_TRACES_PATH}" \ | |
341 | "BT_PLUGINS_PATH=${BT_PLUGINS_PATH}" \ | |
342 | "PYTHONPATH=${BT_TESTS_PYTHONPATH}:${BT_TESTS_SRCDIR}/utils/python" | |
343 | ) | |
abdabab5 | 344 | |
2e3c8ba5 SM |
345 | local main_lib_path="${BT_TESTS_BUILDDIR}/../src/lib/.libs" |
346 | ||
abdabab5 | 347 | # Set the library search path so the python interpreter can load libbabeltrace2 |
5200c954 | 348 | if [ "$BT_OS_TYPE" = "mingw" ] || [ "$BT_OS_TYPE" = "cygwin" ]; then |
4b2d0d1f | 349 | env_args+=("PATH=${main_lib_path}:${PATH:-}") |
d660133b | 350 | elif [ "$BT_OS_TYPE" = "darwin" ]; then |
4b2d0d1f | 351 | env_args+=("DYLD_LIBRARY_PATH=${main_lib_path}:${DYLD_LIBRARY_PATH:-}") |
abdabab5 | 352 | else |
4b2d0d1f MJ |
353 | env_args+=("LD_LIBRARY_PATH=${main_lib_path}:${LD_LIBRARY_PATH:-}") |
354 | fi | |
355 | ||
356 | # On Windows, an embedded Python interpreter needs a way to locate the path | |
357 | # to it's internal modules, set the prefix from python-config to the | |
358 | # PYTHONHOME variable. | |
359 | if [ "$BT_OS_TYPE" = "mingw" ]; then | |
360 | env_args+=("PYTHONHOME=$($BT_TESTS_PYTHON_CONFIG_BIN --prefix)") | |
abdabab5 MJ |
361 | fi |
362 | ||
4b2d0d1f | 363 | env "${env_args[@]}" "$@" |
abdabab5 MJ |
364 | } |
365 | ||
366 | # Set the environment and run python tests in the directory. | |
367 | # | |
368 | # $1 : The directory containing the python test scripts | |
369 | # $2 : The pattern to match python test script names (optional) | |
abdabab5 MJ |
370 | run_python_bt2_test() { |
371 | local test_dir="$1" | |
541308b8 | 372 | local test_pattern="${2:-'*'}" # optional, if none default to "*" |
abdabab5 MJ |
373 | |
374 | local ret | |
375 | local test_runner_args=() | |
376 | ||
377 | test_runner_args+=("$test_dir") | |
378 | if [ "x${test_pattern}" != "x" ]; then | |
379 | test_runner_args+=("${test_pattern}") | |
380 | fi | |
381 | ||
382 | if test "x${BT_TESTS_COVERAGE:-}" = "x1"; then | |
383 | python_exec="check_coverage" | |
384 | else | |
385 | python_exec="${BT_TESTS_PYTHON_BIN}" | |
386 | fi | |
387 | ||
388 | run_python_bt2 \ | |
389 | "${python_exec}" \ | |
390 | "${BT_TESTS_SRCDIR}/utils/python/testrunner.py" \ | |
541308b8 FD |
391 | --pattern "$test_pattern" \ |
392 | "$test_dir" \ | |
393 | ||
abdabab5 MJ |
394 | ret=$? |
395 | ||
396 | if test "x${BT_TESTS_COVERAGE_REPORT:-}" = "x1"; then | |
397 | coverage report -m | |
398 | fi | |
399 | ||
400 | if test "x${BT_TESTS_COVERAGE_HTML:-}" = "x1"; then | |
401 | coverage html | |
402 | fi | |
403 | ||
404 | return $ret | |
405 | } |