Commit | Line | Data |
---|---|---|
644e0364 | 1 | #!/bin/bash |
644e0364 | 2 | # |
0235b0db | 3 | # SPDX-License-Identifier: GPL-2.0-only |
644e0364 | 4 | # |
0235b0db MJ |
5 | # Copyright (c) 2019 Michael Jeanson <mjeanson@efficios.com> |
6 | # Copyright (C) 2019 Philippe Proulx <pproulx@efficios.com> | |
644e0364 | 7 | # |
644e0364 MJ |
8 | |
9 | # This file is meant to be sourced at the start of shell script-based tests. | |
10 | ||
11 | ||
12 | # Error out when encountering an undefined variable | |
13 | set -u | |
14 | ||
9c5dd55a MJ |
15 | # If "readlink -f" is available, get a resolved absolute path to the |
16 | # tests source dir, otherwise make do with a relative path. | |
644e0364 | 17 | scriptdir="$(dirname "${BASH_SOURCE[0]}")" |
9c5dd55a MJ |
18 | if readlink -f "." >/dev/null 2>&1; then |
19 | testsdir=$(readlink -f "$scriptdir/..") | |
20 | else | |
21 | testsdir="$scriptdir/.." | |
22 | fi | |
644e0364 | 23 | |
5058d31b JR |
24 | # The OS on which we are running. See [1] for possible values of 'uname -s'. |
25 | # We do a bit of translation to ease our life down the road for comparison. | |
26 | # Export it so that called executables can use it. | |
27 | # [1] https://en.wikipedia.org/wiki/Uname#Examples | |
75e396f6 | 28 | if [ -z "${BT_TESTS_OS_TYPE:-}" ]; then |
a0baab4a SM |
29 | BT_TESTS_OS_TYPE="$(uname -s)" |
30 | case "$BT_TESTS_OS_TYPE" in | |
5058d31b | 31 | MINGW*) |
a0baab4a | 32 | BT_TESTS_OS_TYPE="mingw" |
5058d31b JR |
33 | ;; |
34 | Darwin) | |
a0baab4a | 35 | BT_TESTS_OS_TYPE="darwin" |
5058d31b JR |
36 | ;; |
37 | Linux) | |
a0baab4a | 38 | BT_TESTS_OS_TYPE="linux" |
5058d31b | 39 | ;; |
df9db467 | 40 | CYGWIN*) |
a0baab4a | 41 | BT_TESTS_OS_TYPE="cygwin" |
df9db467 | 42 | ;; |
5058d31b | 43 | *) |
a0baab4a | 44 | BT_TESTS_OS_TYPE="unsupported" |
5058d31b JR |
45 | ;; |
46 | esac | |
47 | fi | |
a0baab4a | 48 | export BT_TESTS_OS_TYPE |
5058d31b | 49 | |
644e0364 | 50 | # Allow overriding the source and build directories |
75e396f6 | 51 | if [ -z "${BT_TESTS_SRCDIR:-}" ]; then |
9c5dd55a | 52 | BT_TESTS_SRCDIR="$testsdir" |
644e0364 MJ |
53 | fi |
54 | export BT_TESTS_SRCDIR | |
55 | ||
75e396f6 | 56 | if [ -z "${BT_TESTS_BUILDDIR:-}" ]; then |
9c5dd55a | 57 | BT_TESTS_BUILDDIR="$testsdir" |
644e0364 MJ |
58 | fi |
59 | export BT_TESTS_BUILDDIR | |
60 | ||
e46cbefe MJ |
61 | |
62 | # Source the generated environment file if it's present. | |
63 | if [ -f "${BT_TESTS_BUILDDIR}/utils/env.sh" ]; then | |
64 | # shellcheck source=./env.sh | |
65 | . "${BT_TESTS_BUILDDIR}/utils/env.sh" | |
644e0364 MJ |
66 | fi |
67 | ||
68 | # Allow overriding the babeltrace2 executables | |
75e396f6 | 69 | if [ -z "${BT_TESTS_BT2_BIN:-}" ]; then |
4881a20e | 70 | BT_TESTS_BT2_BIN="$BT_TESTS_BUILDDIR/../src/cli/babeltrace2" |
a0baab4a | 71 | if [ "$BT_TESTS_OS_TYPE" = "mingw" ]; then |
27e2c58d JR |
72 | BT_TESTS_BT2_BIN="${BT_TESTS_BT2_BIN}.exe" |
73 | fi | |
644e0364 MJ |
74 | fi |
75 | export BT_TESTS_BT2_BIN | |
76 | ||
644e0364 MJ |
77 | # TODO: Remove when bindings/python/bt2/test_plugin.py is fixed |
78 | BT_PLUGINS_PATH="${BT_TESTS_BUILDDIR}/../src/plugins" | |
79 | ||
80 | # Allow overriding the babeltrace2 plugin path | |
75e396f6 | 81 | if [ -z "${BT_TESTS_BABELTRACE_PLUGIN_PATH:-}" ]; then |
e8cafc6e | 82 | BT_TESTS_BABELTRACE_PLUGIN_PATH="${BT_PLUGINS_PATH}/ctf:${BT_PLUGINS_PATH}/utils:${BT_PLUGINS_PATH}/text:${BT_PLUGINS_PATH}/lttng-utils" |
644e0364 | 83 | fi |
e46cbefe | 84 | export BT_TESTS_BABELTRACE_PLUGIN_PATH |
644e0364 | 85 | |
75e396f6 | 86 | if [ -z "${BT_TESTS_PROVIDER_DIR:-}" ]; then |
b14c7bf1 MJ |
87 | BT_TESTS_PROVIDER_DIR="${BT_TESTS_BUILDDIR}/../src/python-plugin-provider/.libs" |
88 | fi | |
e46cbefe | 89 | export BT_TESTS_PROVIDER_DIR |
b14c7bf1 | 90 | |
644e0364 | 91 | # Allow overriding the babeltrace2 executables |
75e396f6 | 92 | if [ -z "${BT_TESTS_PYTHONPATH:-}" ]; then |
644e0364 MJ |
93 | BT_TESTS_PYTHONPATH="${BT_TESTS_BUILDDIR}/../src/bindings/python/bt2/build/build_lib" |
94 | fi | |
e46cbefe | 95 | export BT_TESTS_PYTHONPATH |
644e0364 MJ |
96 | |
97 | ||
98 | ### External Tools ### | |
75e396f6 | 99 | if [ -z "${BT_TESTS_AWK_BIN:-}" ]; then |
644e0364 MJ |
100 | BT_TESTS_AWK_BIN="awk" |
101 | fi | |
102 | export BT_TESTS_AWK_BIN | |
103 | ||
75e396f6 | 104 | if [ -z "${BT_TESTS_GREP_BIN:-}" ]; then |
644e0364 MJ |
105 | BT_TESTS_GREP_BIN="grep" |
106 | fi | |
107 | export BT_TESTS_GREP_BIN | |
108 | ||
75e396f6 | 109 | if [ -z "${BT_TESTS_PYTHON_BIN:-}" ]; then |
644e0364 MJ |
110 | BT_TESTS_PYTHON_BIN="python3" |
111 | fi | |
112 | export BT_TESTS_PYTHON_BIN | |
113 | ||
75e396f6 | 114 | if [ -z "${BT_TESTS_PYTHON_CONFIG_BIN:-}" ]; then |
e23e08c4 MJ |
115 | BT_TESTS_PYTHON_CONFIG_BIN="python3-config" |
116 | fi | |
242ddcb7 | 117 | export BT_TESTS_PYTHON_CONFIG_BIN |
e23e08c4 | 118 | |
75e396f6 | 119 | if [ -z "${BT_TESTS_SED_BIN:-}" ]; then |
644e0364 MJ |
120 | BT_TESTS_SED_BIN="sed" |
121 | fi | |
122 | export BT_TESTS_SED_BIN | |
123 | ||
75e396f6 | 124 | if [ -z "${BT_TESTS_CC_BIN:-}" ]; then |
0b0893d4 SM |
125 | BT_TESTS_CC_BIN="cc" |
126 | fi | |
127 | export BT_TESTS_CC_BIN | |
128 | ||
129 | ||
130 | ### Optional features ### | |
131 | ||
75e396f6 | 132 | if [ -z "${BT_TESTS_ENABLE_ASAN:-}" ]; then |
0b0893d4 SM |
133 | BT_TESTS_ENABLE_ASAN="0" |
134 | fi | |
135 | export BT_TESTS_ENABLE_ASAN | |
136 | ||
644e0364 MJ |
137 | |
138 | # Data files path | |
139 | BT_TESTS_DATADIR="${BT_TESTS_SRCDIR}/data" | |
140 | BT_CTF_TRACES_PATH="${BT_TESTS_DATADIR}/ctf-traces" | |
644e0364 | 141 | |
e46cbefe MJ |
142 | # By default, it will not source tap.sh. If you want to output tap directly |
143 | # from the test script, define the 'SH_TAP' variable to '1' before sourcing | |
144 | # this script. | |
75e396f6 | 145 | if [ "${SH_TAP:-}" = 1 ]; then |
e46cbefe MJ |
146 | # shellcheck source=./tap/tap.sh |
147 | . "${BT_TESTS_SRCDIR}/utils/tap/tap.sh" | |
148 | fi | |
149 | ||
150 | ||
90a8a0f2 SM |
151 | # Remove CR characters in file "$1". |
152 | ||
153 | bt_remove_cr() { | |
154 | "$BT_TESTS_SED_BIN" -i 's/\r//g' "$1" | |
155 | } | |
156 | ||
22703f66 SM |
157 | # Run the Babeltrace CLI, redirecting stdout and stderr to specified files. |
158 | # | |
159 | # $1: file to redirect stdout to | |
160 | # $2: file to redirect stderr to | |
161 | # remaining args: arguments to pass to the CLI | |
162 | # | |
163 | # Return the exit code of the CLI. | |
164 | ||
165 | bt_cli() { | |
166 | local stdout_file="$1" | |
167 | local stderr_file="$2" | |
168 | shift 2 | |
169 | local args=("$@") | |
170 | ||
ea458b95 | 171 | echo "Running: $BT_TESTS_BT2_BIN ${args[*]}" >&2 |
22703f66 SM |
172 | run_python_bt2 "$BT_TESTS_BT2_BIN" "${args[@]}" 1>"$stdout_file" 2>"$stderr_file" |
173 | } | |
644e0364 MJ |
174 | |
175 | ### Diff Functions ### | |
176 | ||
a70b6702 SM |
177 | # Check the differences between two files (typically some expected output vs |
178 | # some actual output). If there are differences, print the diff to stderr. | |
ff89ed28 | 179 | # |
a70b6702 SM |
180 | # $1: file 1 (expected) |
181 | # $2: file 2 (actual) | |
ff89ed28 | 182 | # |
ea458b95 | 183 | # Return 0 if there's no difference, and non-zero if there are. |
ff89ed28 | 184 | # |
a70b6702 SM |
185 | # Note that this function modifies the actual output file ($2) _in-place_ to |
186 | # remove any \r character. | |
187 | ||
ff89ed28 | 188 | bt_diff() { |
a70b6702 SM |
189 | local expected_file="$1" |
190 | local actual_file="$2" | |
ff89ed28 | 191 | local ret=0 |
ff89ed28 MJ |
192 | |
193 | # Strip any \r present due to Windows (\n -> \r\n). | |
194 | # "diff --string-trailing-cr" is not used since it is not present on | |
195 | # Solaris. | |
90a8a0f2 | 196 | bt_remove_cr "$actual_file" |
ff89ed28 | 197 | |
ea458b95 | 198 | diff -u "$expected_file" "$actual_file" 1>&2 |
ff89ed28 | 199 | |
ea458b95 | 200 | return $? |
ff89ed28 MJ |
201 | } |
202 | ||
58db335e FD |
203 | # Checks the difference between: |
204 | # | |
205 | # 1. What the CLI outputs on its standard output when given the arguments | |
206 | # "$@" (excluding the first two arguments). | |
207 | # 2. The file with path "$1". | |
208 | # | |
209 | # And the difference between: | |
210 | # | |
211 | # 1. What the CLI outputs on its standard error when given the arguments | |
212 | # "$@" (excluding the first two arguments). | |
213 | # 2. The file with path "$2". | |
644e0364 MJ |
214 | # |
215 | # Returns 0 if there's no difference, and 1 if there is, also printing | |
216 | # said difference to the standard error. | |
217 | bt_diff_cli() { | |
58db335e FD |
218 | local expected_stdout_file="$1" |
219 | local expected_stderr_file="$2" | |
220 | shift 2 | |
53cc240b MJ |
221 | local args=("$@") |
222 | ||
58db335e FD |
223 | local temp_stdout_output_file |
224 | local temp_stderr_output_file | |
644e0364 | 225 | local ret=0 |
a70b6702 SM |
226 | local ret_stdout |
227 | local ret_stderr | |
644e0364 | 228 | |
ff89ed28 MJ |
229 | temp_stdout_output_file="$(mktemp -t actual_stdout.XXXXXX)" |
230 | temp_stderr_output_file="$(mktemp -t actual_stderr.XXXXXX)" | |
644e0364 | 231 | |
58db335e | 232 | # Run the CLI to get a detailed file. |
22703f66 | 233 | bt_cli "$temp_stdout_output_file" "$temp_stderr_output_file" "${args[@]}" |
58db335e | 234 | |
a70b6702 SM |
235 | bt_diff "$expected_stdout_file" "$temp_stdout_output_file" "${args[@]}" |
236 | ret_stdout=$? | |
237 | bt_diff "$expected_stderr_file" "$temp_stderr_output_file" "${args[@]}" | |
238 | ret_stderr=$? | |
239 | ||
240 | if ((ret_stdout != 0 || ret_stderr != 0)); then | |
241 | ret=1 | |
242 | fi | |
58db335e | 243 | |
ff89ed28 | 244 | rm -f "$temp_stdout_output_file" "$temp_stderr_output_file" |
644e0364 | 245 | |
ff89ed28 MJ |
246 | return $ret |
247 | } | |
248 | ||
53cc240b MJ |
249 | # Checks the difference between the content of the file with path "$1" |
250 | # and the output of the CLI when called on the directory path "$2" with | |
251 | # the arguments '-c sink.text.details' and the rest of the arguments to | |
252 | # this function. | |
644e0364 MJ |
253 | # |
254 | # Returns 0 if there's no difference, and 1 if there is, also printing | |
255 | # said difference to the standard error. | |
256 | bt_diff_details_ctf_single() { | |
58db335e | 257 | local expected_stdout_file="$1" |
53cc240b MJ |
258 | local trace_dir="$2" |
259 | shift 2 | |
260 | local extra_details_args=("$@") | |
58db335e | 261 | expected_stderr_file="/dev/null" |
644e0364 MJ |
262 | |
263 | # Compare using the CLI with `sink.text.details` | |
8b729209 PP |
264 | bt_diff_cli "$expected_stdout_file" "$expected_stderr_file" "$trace_dir" \ |
265 | "-c" "sink.text.details" "${extra_details_args[@]+${extra_details_args[@]}}" | |
644e0364 MJ |
266 | } |
267 | ||
268 | # Calls bt_diff_details_ctf_single(), except that "$1" is the path to a | |
269 | # program which generates the CTF trace to compare to. The program "$1" | |
270 | # receives the path to a temporary, empty directory where to write the | |
271 | # CTF trace as its first argument. | |
272 | bt_diff_details_ctf_gen_single() { | |
273 | local ctf_gen_prog_path="$1" | |
58db335e | 274 | local expected_stdout_file="$2" |
53cc240b MJ |
275 | shift 2 |
276 | local extra_details_args=("$@") | |
644e0364 MJ |
277 | |
278 | local temp_trace_dir | |
279 | local ret | |
280 | ||
281 | temp_trace_dir="$(mktemp -d)" | |
282 | ||
283 | # Run the CTF trace generator program to get a CTF trace | |
284 | if ! "$ctf_gen_prog_path" "$temp_trace_dir" 2>/dev/null; then | |
285 | echo "ERROR: \"$ctf_gen_prog_path\" \"$temp_trace_dir\" failed" >&2 | |
286 | rm -rf "$temp_trace_dir" | |
287 | return 1 | |
288 | fi | |
289 | ||
290 | # Compare using the CLI with `sink.text.details` | |
8b729209 PP |
291 | bt_diff_details_ctf_single "$expected_stdout_file" "$temp_trace_dir" \ |
292 | "${extra_details_args[@]+${extra_details_args[@]}}" | |
644e0364 MJ |
293 | ret=$? |
294 | rm -rf "$temp_trace_dir" | |
295 | return $ret | |
296 | } | |
297 | ||
298 | ||
299 | ### Functions ### | |
300 | ||
301 | check_coverage() { | |
302 | coverage run "$@" | |
303 | } | |
304 | ||
305 | # Execute a shell command in the appropriate environment to have access to the | |
306 | # bt2 Python bindings. | |
307 | run_python_bt2() { | |
e23e08c4 | 308 | local env_args |
0b0893d4 | 309 | local lib_asan |
e23e08c4 MJ |
310 | |
311 | env_args=( | |
312 | "BABELTRACE_PYTHON_BT2_NO_TRACEBACK=1" \ | |
313 | "BABELTRACE_PLUGIN_PATH=${BT_TESTS_BABELTRACE_PLUGIN_PATH}" \ | |
314 | "LIBBABELTRACE2_PLUGIN_PROVIDER_DIR=${BT_TESTS_PROVIDER_DIR}" \ | |
f3c9a159 | 315 | "BT_TESTS_DATADIR=${BT_TESTS_DATADIR}" \ |
e23e08c4 MJ |
316 | "BT_CTF_TRACES_PATH=${BT_CTF_TRACES_PATH}" \ |
317 | "BT_PLUGINS_PATH=${BT_PLUGINS_PATH}" \ | |
318 | "PYTHONPATH=${BT_TESTS_PYTHONPATH}:${BT_TESTS_SRCDIR}/utils/python" | |
319 | ) | |
644e0364 | 320 | |
0408934a SM |
321 | local main_lib_path="${BT_TESTS_BUILDDIR}/../src/lib/.libs" |
322 | ||
644e0364 | 323 | # Set the library search path so the python interpreter can load libbabeltrace2 |
a0baab4a | 324 | if [ "$BT_TESTS_OS_TYPE" = "mingw" ] || [ "$BT_TESTS_OS_TYPE" = "cygwin" ]; then |
e23e08c4 | 325 | env_args+=("PATH=${main_lib_path}:${PATH:-}") |
a0baab4a | 326 | elif [ "$BT_TESTS_OS_TYPE" = "darwin" ]; then |
e23e08c4 | 327 | env_args+=("DYLD_LIBRARY_PATH=${main_lib_path}:${DYLD_LIBRARY_PATH:-}") |
644e0364 | 328 | else |
e23e08c4 MJ |
329 | env_args+=("LD_LIBRARY_PATH=${main_lib_path}:${LD_LIBRARY_PATH:-}") |
330 | fi | |
331 | ||
332 | # On Windows, an embedded Python interpreter needs a way to locate the path | |
333 | # to it's internal modules, set the prefix from python-config to the | |
334 | # PYTHONHOME variable. | |
a0baab4a | 335 | if [ "$BT_TESTS_OS_TYPE" = "mingw" ]; then |
e23e08c4 | 336 | env_args+=("PYTHONHOME=$($BT_TESTS_PYTHON_CONFIG_BIN --prefix)") |
644e0364 MJ |
337 | fi |
338 | ||
0b0893d4 SM |
339 | # If AddressSanitizer is used, we must preload libasan.so so that |
340 | # libasan doesn't complain about not being the first loaded library. | |
341 | # | |
342 | # Python and sed (executed as part of the libtool wrapper) produce some | |
343 | # leaks, so we must unfortunately disable leak detection. Append it to | |
344 | # existing ASAN_OPTIONS, such that we override the user's value if it | |
345 | # contains detect_leaks=1. | |
75e396f6 | 346 | if [ "${BT_TESTS_ENABLE_ASAN:-}" = "1" ]; then |
2d12d310 SM |
347 | if ${BT_TESTS_CC_BIN} --version | head -n 1 | grep -q '^gcc'; then |
348 | lib_asan=$(${BT_TESTS_CC_BIN} -print-file-name=libasan.so) | |
349 | env_args+=("LD_PRELOAD=${lib_asan}:${LD_PRELOAD:-}") | |
350 | fi | |
0b0893d4 | 351 | |
0b0893d4 SM |
352 | env_args+=("ASAN_OPTIONS=${ASAN_OPTIONS:-},detect_leaks=0") |
353 | fi | |
354 | ||
e23e08c4 | 355 | env "${env_args[@]}" "$@" |
644e0364 MJ |
356 | } |
357 | ||
358 | # Set the environment and run python tests in the directory. | |
359 | # | |
360 | # $1 : The directory containing the python test scripts | |
361 | # $2 : The pattern to match python test script names (optional) | |
644e0364 MJ |
362 | run_python_bt2_test() { |
363 | local test_dir="$1" | |
a818a617 | 364 | local test_pattern="${2:-'*'}" # optional, if none default to "*" |
644e0364 MJ |
365 | |
366 | local ret | |
367 | local test_runner_args=() | |
368 | ||
369 | test_runner_args+=("$test_dir") | |
75e396f6 | 370 | if [ -n "${test_pattern}" ]; then |
644e0364 MJ |
371 | test_runner_args+=("${test_pattern}") |
372 | fi | |
373 | ||
75e396f6 | 374 | if test "${BT_TESTS_COVERAGE:-}" = "1"; then |
644e0364 MJ |
375 | python_exec="check_coverage" |
376 | else | |
377 | python_exec="${BT_TESTS_PYTHON_BIN}" | |
378 | fi | |
379 | ||
380 | run_python_bt2 \ | |
381 | "${python_exec}" \ | |
382 | "${BT_TESTS_SRCDIR}/utils/python/testrunner.py" \ | |
a818a617 FD |
383 | --pattern "$test_pattern" \ |
384 | "$test_dir" \ | |
385 | ||
644e0364 MJ |
386 | ret=$? |
387 | ||
75e396f6 | 388 | if test "${BT_TESTS_COVERAGE_REPORT:-}" = "1"; then |
644e0364 MJ |
389 | coverage report -m |
390 | fi | |
391 | ||
75e396f6 | 392 | if test "${BT_TESTS_COVERAGE_HTML:-}" = "1"; then |
644e0364 MJ |
393 | coverage html |
394 | fi | |
395 | ||
396 | return $ret | |
397 | } |