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