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