Commit | Line | Data |
---|---|---|
644e0364 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 | ||
9c5dd55a 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. | |
644e0364 | 27 | scriptdir="$(dirname "${BASH_SOURCE[0]}")" |
9c5dd55a MJ |
28 | if readlink -f "." >/dev/null 2>&1; then |
29 | testsdir=$(readlink -f "$scriptdir/..") | |
30 | else | |
31 | testsdir="$scriptdir/.." | |
32 | fi | |
644e0364 | 33 | |
5058d31b 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 | ;; | |
df9db467 JR |
50 | CYGWIN*) |
51 | BT_OS_TYPE="cygwin" | |
52 | ;; | |
5058d31b JR |
53 | *) |
54 | BT_OS_TYPE="unsupported" | |
55 | ;; | |
56 | esac | |
57 | fi | |
58 | export BT_OS_TYPE | |
59 | ||
644e0364 MJ |
60 | # Allow overriding the source and build directories |
61 | if [ "x${BT_TESTS_SRCDIR:-}" = "x" ]; then | |
9c5dd55a | 62 | BT_TESTS_SRCDIR="$testsdir" |
644e0364 MJ |
63 | fi |
64 | export BT_TESTS_SRCDIR | |
65 | ||
66 | if [ "x${BT_TESTS_BUILDDIR:-}" = "x" ]; then | |
9c5dd55a | 67 | BT_TESTS_BUILDDIR="$testsdir" |
644e0364 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 | |
1d970e04 | 75 | # shellcheck source=./tap/tap.sh |
644e0364 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 | |
4881a20e | 81 | BT_TESTS_BT2_BIN="$BT_TESTS_BUILDDIR/../src/cli/babeltrace2" |
5058d31b | 82 | if [ "$BT_OS_TYPE" = "mingw" ]; then |
27e2c58d JR |
83 | BT_TESTS_BT2_BIN="${BT_TESTS_BT2_BIN}.exe" |
84 | fi | |
644e0364 MJ |
85 | fi |
86 | export BT_TESTS_BT2_BIN | |
87 | ||
644e0364 MJ |
88 | # TODO: Remove when bindings/python/bt2/test_plugin.py is fixed |
89 | BT_PLUGINS_PATH="${BT_TESTS_BUILDDIR}/../src/plugins" | |
90 | ||
91 | # Allow overriding the babeltrace2 plugin path | |
92 | if [ "x${BT_TESTS_BABELTRACE_PLUGIN_PATH:-}" = "x" ]; then | |
93 | BT_TESTS_BABELTRACE_PLUGIN_PATH="${BT_PLUGINS_PATH}/ctf:${BT_PLUGINS_PATH}/utils:${BT_PLUGINS_PATH}/text" | |
94 | fi | |
95 | ||
b14c7bf1 MJ |
96 | if [ "x${BT_TESTS_PROVIDER_DIR:-}" = "x" ]; then |
97 | BT_TESTS_PROVIDER_DIR="${BT_TESTS_BUILDDIR}/../src/python-plugin-provider/.libs" | |
98 | fi | |
99 | ||
644e0364 MJ |
100 | # Allow overriding the babeltrace2 executables |
101 | if [ "x${BT_TESTS_PYTHONPATH:-}" = "x" ]; then | |
102 | BT_TESTS_PYTHONPATH="${BT_TESTS_BUILDDIR}/../src/bindings/python/bt2/build/build_lib" | |
103 | fi | |
104 | ||
105 | ||
106 | ### External Tools ### | |
107 | if [ "x${BT_TESTS_AWK_BIN:-}" = "x" ]; then | |
108 | BT_TESTS_AWK_BIN="awk" | |
109 | fi | |
110 | export BT_TESTS_AWK_BIN | |
111 | ||
112 | if [ "x${BT_TESTS_GREP_BIN:-}" = "x" ]; then | |
113 | BT_TESTS_GREP_BIN="grep" | |
114 | fi | |
115 | export BT_TESTS_GREP_BIN | |
116 | ||
117 | if [ "x${BT_TESTS_PYTHON_BIN:-}" = "x" ]; then | |
118 | BT_TESTS_PYTHON_BIN="python3" | |
119 | fi | |
120 | export BT_TESTS_PYTHON_BIN | |
121 | ||
e23e08c4 MJ |
122 | if [ "x${BT_TESTS_PYTHON_CONFIG_BIN:-}" = "x" ]; then |
123 | BT_TESTS_PYTHON_CONFIG_BIN="python3-config" | |
124 | fi | |
242ddcb7 | 125 | export BT_TESTS_PYTHON_CONFIG_BIN |
e23e08c4 | 126 | |
644e0364 MJ |
127 | if [ "x${BT_TESTS_SED_BIN:-}" = "x" ]; then |
128 | BT_TESTS_SED_BIN="sed" | |
129 | fi | |
130 | export BT_TESTS_SED_BIN | |
131 | ||
132 | ||
133 | # Data files path | |
134 | BT_TESTS_DATADIR="${BT_TESTS_SRCDIR}/data" | |
135 | BT_CTF_TRACES_PATH="${BT_TESTS_DATADIR}/ctf-traces" | |
644e0364 | 136 | |
22703f66 SM |
137 | # Run the Babeltrace CLI, redirecting stdout and stderr to specified files. |
138 | # | |
139 | # $1: file to redirect stdout to | |
140 | # $2: file to redirect stderr to | |
141 | # remaining args: arguments to pass to the CLI | |
142 | # | |
143 | # Return the exit code of the CLI. | |
144 | ||
145 | bt_cli() { | |
146 | local stdout_file="$1" | |
147 | local stderr_file="$2" | |
148 | shift 2 | |
149 | local args=("$@") | |
150 | ||
ea458b95 | 151 | echo "Running: $BT_TESTS_BT2_BIN ${args[*]}" >&2 |
22703f66 SM |
152 | run_python_bt2 "$BT_TESTS_BT2_BIN" "${args[@]}" 1>"$stdout_file" 2>"$stderr_file" |
153 | } | |
644e0364 MJ |
154 | |
155 | ### Diff Functions ### | |
156 | ||
a70b6702 SM |
157 | # Check the differences between two files (typically some expected output vs |
158 | # some actual output). If there are differences, print the diff to stderr. | |
ff89ed28 | 159 | # |
a70b6702 SM |
160 | # $1: file 1 (expected) |
161 | # $2: file 2 (actual) | |
ff89ed28 | 162 | # |
ea458b95 | 163 | # Return 0 if there's no difference, and non-zero if there are. |
ff89ed28 | 164 | # |
a70b6702 SM |
165 | # Note that this function modifies the actual output file ($2) _in-place_ to |
166 | # remove any \r character. | |
167 | ||
ff89ed28 | 168 | bt_diff() { |
a70b6702 SM |
169 | local expected_file="$1" |
170 | local actual_file="$2" | |
ff89ed28 | 171 | local ret=0 |
ff89ed28 MJ |
172 | |
173 | # Strip any \r present due to Windows (\n -> \r\n). | |
174 | # "diff --string-trailing-cr" is not used since it is not present on | |
175 | # Solaris. | |
a70b6702 | 176 | "$BT_TESTS_SED_BIN" -i 's/\r//g' "$actual_file" |
ff89ed28 | 177 | |
ea458b95 | 178 | diff -u "$expected_file" "$actual_file" 1>&2 |
ff89ed28 | 179 | |
ea458b95 | 180 | return $? |
ff89ed28 MJ |
181 | } |
182 | ||
58db335e FD |
183 | # Checks the difference between: |
184 | # | |
185 | # 1. What the CLI outputs on its standard output when given the arguments | |
186 | # "$@" (excluding the first two arguments). | |
187 | # 2. The file with path "$1". | |
188 | # | |
189 | # And the difference between: | |
190 | # | |
191 | # 1. What the CLI outputs on its standard error when given the arguments | |
192 | # "$@" (excluding the first two arguments). | |
193 | # 2. The file with path "$2". | |
644e0364 MJ |
194 | # |
195 | # Returns 0 if there's no difference, and 1 if there is, also printing | |
196 | # said difference to the standard error. | |
197 | bt_diff_cli() { | |
58db335e FD |
198 | local expected_stdout_file="$1" |
199 | local expected_stderr_file="$2" | |
200 | shift 2 | |
53cc240b MJ |
201 | local args=("$@") |
202 | ||
58db335e FD |
203 | local temp_stdout_output_file |
204 | local temp_stderr_output_file | |
644e0364 | 205 | local ret=0 |
a70b6702 SM |
206 | local ret_stdout |
207 | local ret_stderr | |
644e0364 | 208 | |
ff89ed28 MJ |
209 | temp_stdout_output_file="$(mktemp -t actual_stdout.XXXXXX)" |
210 | temp_stderr_output_file="$(mktemp -t actual_stderr.XXXXXX)" | |
644e0364 | 211 | |
58db335e | 212 | # Run the CLI to get a detailed file. |
22703f66 | 213 | bt_cli "$temp_stdout_output_file" "$temp_stderr_output_file" "${args[@]}" |
58db335e | 214 | |
a70b6702 SM |
215 | bt_diff "$expected_stdout_file" "$temp_stdout_output_file" "${args[@]}" |
216 | ret_stdout=$? | |
217 | bt_diff "$expected_stderr_file" "$temp_stderr_output_file" "${args[@]}" | |
218 | ret_stderr=$? | |
219 | ||
220 | if ((ret_stdout != 0 || ret_stderr != 0)); then | |
221 | ret=1 | |
222 | fi | |
58db335e | 223 | |
ff89ed28 | 224 | rm -f "$temp_stdout_output_file" "$temp_stderr_output_file" |
644e0364 | 225 | |
ff89ed28 MJ |
226 | return $ret |
227 | } | |
228 | ||
229 | # Checks the difference between: | |
230 | # | |
231 | # 1. What the CLI outputs on its standard output when given the arguments | |
232 | # "$@" (excluding the first two arguments), sorted with the default "sort". | |
233 | # 2. The file with path "$1". | |
234 | # | |
235 | # And the difference between: | |
236 | # | |
237 | # 1. What the CLI outputs on its standard error when given the arguments | |
238 | # "$@" (excluding the first two arguments). | |
239 | # 2. The file with path "$2". | |
240 | # | |
241 | # Returns 0 if there's no difference, and 1 if there is, also printing | |
242 | # said difference to the standard error. | |
243 | bt_diff_cli_sorted() { | |
244 | local expected_stdout_file="$1" | |
245 | local expected_stderr_file="$2" | |
246 | shift 2 | |
247 | local args=("$@") | |
248 | ||
249 | local temp_stdout_output_file | |
250 | local temp_stderr_output_file | |
251 | local ret=0 | |
a70b6702 SM |
252 | local ret_stdout |
253 | local ret_stderr | |
ff89ed28 MJ |
254 | |
255 | temp_stdout_output_file="$(mktemp -t actual_stdout.XXXXXX)" | |
256 | temp_stderr_output_file="$(mktemp -t actual_stderr.XXXXXX)" | |
257 | ||
258 | # Run the CLI to get a detailed file. | |
22703f66 | 259 | bt_cli "$temp_stdout_output_file" "$temp_stderr_output_file" "${args[@]}" |
ff89ed28 MJ |
260 | |
261 | # Sort the stdout file, use a subshell to do it in-place | |
262 | # shellcheck disable=SC2005 | |
263 | echo "$(LC_ALL=C sort "$temp_stdout_output_file")" > "$temp_stdout_output_file" | |
264 | ||
ea458b95 | 265 | bt_diff "$expected_stdout_file" "$temp_stdout_output_file" |
a70b6702 | 266 | ret_stdout=$? |
ea458b95 | 267 | bt_diff "$expected_stderr_file" "$temp_stderr_output_file" |
a70b6702 SM |
268 | ret_stderr=$? |
269 | ||
270 | if ((ret_stdout != 0 || ret_stderr != 0)); then | |
271 | ret=1 | |
272 | fi | |
644e0364 | 273 | |
ff89ed28 | 274 | rm -f "$temp_stdout_output_file" "$temp_stderr_output_file" |
644e0364 MJ |
275 | |
276 | return $ret | |
277 | } | |
278 | ||
53cc240b MJ |
279 | # Checks the difference between the content of the file with path "$1" |
280 | # and the output of the CLI when called on the directory path "$2" with | |
281 | # the arguments '-c sink.text.details' and the rest of the arguments to | |
282 | # this function. | |
644e0364 MJ |
283 | # |
284 | # Returns 0 if there's no difference, and 1 if there is, also printing | |
285 | # said difference to the standard error. | |
286 | bt_diff_details_ctf_single() { | |
58db335e | 287 | local expected_stdout_file="$1" |
53cc240b MJ |
288 | local trace_dir="$2" |
289 | shift 2 | |
290 | local extra_details_args=("$@") | |
58db335e | 291 | expected_stderr_file="/dev/null" |
644e0364 MJ |
292 | |
293 | # Compare using the CLI with `sink.text.details` | |
58db335e | 294 | bt_diff_cli "$expected_stdout_file" "$expected_stderr_file" "$trace_dir" "-c" "sink.text.details" "${extra_details_args[@]}" |
644e0364 MJ |
295 | } |
296 | ||
297 | # Calls bt_diff_details_ctf_single(), except that "$1" is the path to a | |
298 | # program which generates the CTF trace to compare to. The program "$1" | |
299 | # receives the path to a temporary, empty directory where to write the | |
300 | # CTF trace as its first argument. | |
301 | bt_diff_details_ctf_gen_single() { | |
302 | local ctf_gen_prog_path="$1" | |
58db335e | 303 | local expected_stdout_file="$2" |
53cc240b MJ |
304 | shift 2 |
305 | local extra_details_args=("$@") | |
644e0364 MJ |
306 | |
307 | local temp_trace_dir | |
308 | local ret | |
309 | ||
310 | temp_trace_dir="$(mktemp -d)" | |
311 | ||
312 | # Run the CTF trace generator program to get a CTF trace | |
313 | if ! "$ctf_gen_prog_path" "$temp_trace_dir" 2>/dev/null; then | |
314 | echo "ERROR: \"$ctf_gen_prog_path\" \"$temp_trace_dir\" failed" >&2 | |
315 | rm -rf "$temp_trace_dir" | |
316 | return 1 | |
317 | fi | |
318 | ||
319 | # Compare using the CLI with `sink.text.details` | |
58db335e | 320 | bt_diff_details_ctf_single "$expected_stdout_file" "$temp_trace_dir" "${extra_details_args[@]}" |
644e0364 MJ |
321 | ret=$? |
322 | rm -rf "$temp_trace_dir" | |
323 | return $ret | |
324 | } | |
325 | ||
326 | ||
327 | ### Functions ### | |
328 | ||
329 | check_coverage() { | |
330 | coverage run "$@" | |
331 | } | |
332 | ||
333 | # Execute a shell command in the appropriate environment to have access to the | |
334 | # bt2 Python bindings. | |
335 | run_python_bt2() { | |
e23e08c4 MJ |
336 | local env_args |
337 | ||
338 | env_args=( | |
339 | "BABELTRACE_PYTHON_BT2_NO_TRACEBACK=1" \ | |
340 | "BABELTRACE_PLUGIN_PATH=${BT_TESTS_BABELTRACE_PLUGIN_PATH}" \ | |
341 | "LIBBABELTRACE2_PLUGIN_PROVIDER_DIR=${BT_TESTS_PROVIDER_DIR}" \ | |
f3c9a159 | 342 | "BT_TESTS_DATADIR=${BT_TESTS_DATADIR}" \ |
e23e08c4 MJ |
343 | "BT_CTF_TRACES_PATH=${BT_CTF_TRACES_PATH}" \ |
344 | "BT_PLUGINS_PATH=${BT_PLUGINS_PATH}" \ | |
345 | "PYTHONPATH=${BT_TESTS_PYTHONPATH}:${BT_TESTS_SRCDIR}/utils/python" | |
346 | ) | |
644e0364 | 347 | |
0408934a SM |
348 | local main_lib_path="${BT_TESTS_BUILDDIR}/../src/lib/.libs" |
349 | ||
644e0364 | 350 | # Set the library search path so the python interpreter can load libbabeltrace2 |
df9db467 | 351 | if [ "$BT_OS_TYPE" = "mingw" ] || [ "$BT_OS_TYPE" = "cygwin" ]; then |
e23e08c4 | 352 | env_args+=("PATH=${main_lib_path}:${PATH:-}") |
5058d31b | 353 | elif [ "$BT_OS_TYPE" = "darwin" ]; then |
e23e08c4 | 354 | env_args+=("DYLD_LIBRARY_PATH=${main_lib_path}:${DYLD_LIBRARY_PATH:-}") |
644e0364 | 355 | else |
e23e08c4 MJ |
356 | env_args+=("LD_LIBRARY_PATH=${main_lib_path}:${LD_LIBRARY_PATH:-}") |
357 | fi | |
358 | ||
359 | # On Windows, an embedded Python interpreter needs a way to locate the path | |
360 | # to it's internal modules, set the prefix from python-config to the | |
361 | # PYTHONHOME variable. | |
362 | if [ "$BT_OS_TYPE" = "mingw" ]; then | |
363 | env_args+=("PYTHONHOME=$($BT_TESTS_PYTHON_CONFIG_BIN --prefix)") | |
644e0364 MJ |
364 | fi |
365 | ||
e23e08c4 | 366 | env "${env_args[@]}" "$@" |
644e0364 MJ |
367 | } |
368 | ||
369 | # Set the environment and run python tests in the directory. | |
370 | # | |
371 | # $1 : The directory containing the python test scripts | |
372 | # $2 : The pattern to match python test script names (optional) | |
644e0364 MJ |
373 | run_python_bt2_test() { |
374 | local test_dir="$1" | |
a818a617 | 375 | local test_pattern="${2:-'*'}" # optional, if none default to "*" |
644e0364 MJ |
376 | |
377 | local ret | |
378 | local test_runner_args=() | |
379 | ||
380 | test_runner_args+=("$test_dir") | |
381 | if [ "x${test_pattern}" != "x" ]; then | |
382 | test_runner_args+=("${test_pattern}") | |
383 | fi | |
384 | ||
385 | if test "x${BT_TESTS_COVERAGE:-}" = "x1"; then | |
386 | python_exec="check_coverage" | |
387 | else | |
388 | python_exec="${BT_TESTS_PYTHON_BIN}" | |
389 | fi | |
390 | ||
391 | run_python_bt2 \ | |
392 | "${python_exec}" \ | |
393 | "${BT_TESTS_SRCDIR}/utils/python/testrunner.py" \ | |
a818a617 FD |
394 | --pattern "$test_pattern" \ |
395 | "$test_dir" \ | |
396 | ||
644e0364 MJ |
397 | ret=$? |
398 | ||
399 | if test "x${BT_TESTS_COVERAGE_REPORT:-}" = "x1"; then | |
400 | coverage report -m | |
401 | fi | |
402 | ||
403 | if test "x${BT_TESTS_COVERAGE_HTML:-}" = "x1"; then | |
404 | coverage html | |
405 | fi | |
406 | ||
407 | return $ret | |
408 | } |