cli: remove babeltrace2-log
[babeltrace.git] / tests / utils / utils.sh
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
25 # If "readlink -f" is available, get a resolved absolute path to the
26 # tests source dir, otherwise make do with a relative path.
27 scriptdir="$(dirname "${BASH_SOURCE[0]}")"
28 if readlink -f "." >/dev/null 2>&1; then
29 testsdir=$(readlink -f "$scriptdir/..")
30 else
31 testsdir="$scriptdir/.."
32 fi
33
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 ;;
50 CYGWIN*)
51 BT_OS_TYPE="cygwin"
52 ;;
53 *)
54 BT_OS_TYPE="unsupported"
55 ;;
56 esac
57 fi
58 export BT_OS_TYPE
59
60 # Allow overriding the source and build directories
61 if [ "x${BT_TESTS_SRCDIR:-}" = "x" ]; then
62 BT_TESTS_SRCDIR="$testsdir"
63 fi
64 export BT_TESTS_SRCDIR
65
66 if [ "x${BT_TESTS_BUILDDIR:-}" = "x" ]; then
67 BT_TESTS_BUILDDIR="$testsdir"
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
75 # shellcheck source=./tap/tap.sh
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
81 BT_TESTS_BT2_BIN="$BT_TESTS_BUILDDIR/../src/cli/babeltrace2"
82 if [ "$BT_OS_TYPE" = "mingw" ]; then
83 BT_TESTS_BT2_BIN="${BT_TESTS_BT2_BIN}.exe"
84 fi
85 fi
86 export BT_TESTS_BT2_BIN
87
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
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
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
122 if [ "x${BT_TESTS_PYTHON_CONFIG_BIN:-}" = "x" ]; then
123 BT_TESTS_PYTHON_CONFIG_BIN="python3-config"
124 fi
125 export BT_TESTS_PYTHON_CONFIG_BIN
126
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"
136
137
138 ### Diff Functions ###
139
140 # Checks the difference between stdout:
141 #
142 # The file with path "$1", and the file with path "$2"
143 #
144 # And the difference between stderr:
145 #
146 # The file with path "$3", and the file with path "$4"
147 #
148 # Returns 0 if there's no difference, and 1 if there is, also printing
149 # said difference to the standard error, and an error message with the
150 # args starting at "$5".
151 bt_diff() {
152 local expected_stdout_file="$1"
153 local actual_stdout_file="$2"
154 local expected_stderr_file="$3"
155 local actual_stderr_file="$4"
156 shift 4
157 local args=("$@")
158
159 local ret=0
160 local temp_diff
161
162 temp_diff="$(mktemp -t diff.XXXXXX)"
163
164 # Strip any \r present due to Windows (\n -> \r\n).
165 # "diff --string-trailing-cr" is not used since it is not present on
166 # Solaris.
167 "$BT_TESTS_SED_BIN" -i 's/\r//g' "$actual_stdout_file"
168 "$BT_TESTS_SED_BIN" -i 's/\r//g' "$actual_stderr_file"
169
170 # Compare stdout output with expected stdout output
171 if ! diff -u "$actual_stdout_file" "$expected_stdout_file" 2>/dev/null >"$temp_diff"; then
172 echo "ERROR: for '${args[*]}': actual standard output and expected output differ:" >&2
173 cat "$temp_diff" >&2
174 ret=1
175 fi
176
177 # Compare stderr output with expected stderr output
178 if ! diff -u "$actual_stderr_file" "$expected_stderr_file" 2>/dev/null >"$temp_diff"; then
179 echo "ERROR: for '${args[*]}': actual standard error and expected error differ:" >&2
180 cat "$temp_diff" >&2
181 ret=1
182 fi
183
184 rm -f "$temp_diff"
185
186 return $ret
187 }
188
189 # Checks the difference between:
190 #
191 # 1. What the CLI outputs on its standard output when given the arguments
192 # "$@" (excluding the first two arguments).
193 # 2. The file with path "$1".
194 #
195 # And the difference between:
196 #
197 # 1. What the CLI outputs on its standard error when given the arguments
198 # "$@" (excluding the first two arguments).
199 # 2. The file with path "$2".
200 #
201 # Returns 0 if there's no difference, and 1 if there is, also printing
202 # said difference to the standard error.
203 bt_diff_cli() {
204 local expected_stdout_file="$1"
205 local expected_stderr_file="$2"
206 shift 2
207 local args=("$@")
208
209 local temp_stdout_output_file
210 local temp_stderr_output_file
211 local ret=0
212
213 temp_stdout_output_file="$(mktemp -t actual_stdout.XXXXXX)"
214 temp_stderr_output_file="$(mktemp -t actual_stderr.XXXXXX)"
215
216 # Run the CLI to get a detailed file.
217 run_python_bt2 "$BT_TESTS_BT2_BIN" "${args[@]}" 1>"$temp_stdout_output_file" 2>"$temp_stderr_output_file"
218
219 bt_diff "$expected_stdout_file" "$temp_stdout_output_file" "$expected_stderr_file" "$temp_stderr_output_file" "${args[@]}"
220 ret=$?
221
222 rm -f "$temp_stdout_output_file" "$temp_stderr_output_file"
223
224 return $ret
225 }
226
227 # Checks the difference between:
228 #
229 # 1. What the CLI outputs on its standard output when given the arguments
230 # "$@" (excluding the first two arguments), sorted with the default "sort".
231 # 2. The file with path "$1".
232 #
233 # And the difference between:
234 #
235 # 1. What the CLI outputs on its standard error when given the arguments
236 # "$@" (excluding the first two arguments).
237 # 2. The file with path "$2".
238 #
239 # Returns 0 if there's no difference, and 1 if there is, also printing
240 # said difference to the standard error.
241 bt_diff_cli_sorted() {
242 local expected_stdout_file="$1"
243 local expected_stderr_file="$2"
244 shift 2
245 local args=("$@")
246
247 local temp_stdout_output_file
248 local temp_stderr_output_file
249 local ret=0
250
251 temp_stdout_output_file="$(mktemp -t actual_stdout.XXXXXX)"
252 temp_stderr_output_file="$(mktemp -t actual_stderr.XXXXXX)"
253
254 # Run the CLI to get a detailed file.
255 run_python_bt2 "$BT_TESTS_BT2_BIN" "${args[@]}" 1>"$temp_stdout_output_file" 2>"$temp_stderr_output_file"
256
257 # Sort the stdout file, use a subshell to do it in-place
258 # shellcheck disable=SC2005
259 echo "$(LC_ALL=C sort "$temp_stdout_output_file")" > "$temp_stdout_output_file"
260
261 bt_diff "$expected_stdout_file" "$temp_stdout_output_file" "$expected_stderr_file" "$temp_stderr_output_file" "${args[@]}"
262 ret=$?
263
264 rm -f "$temp_stdout_output_file" "$temp_stderr_output_file"
265
266 return $ret
267 }
268
269 # Checks the difference between the content of the file with path "$1"
270 # and the output of the CLI when called on the directory path "$2" with
271 # the arguments '-c sink.text.details' and the rest of the arguments to
272 # this function.
273 #
274 # Returns 0 if there's no difference, and 1 if there is, also printing
275 # said difference to the standard error.
276 bt_diff_details_ctf_single() {
277 local expected_stdout_file="$1"
278 local trace_dir="$2"
279 shift 2
280 local extra_details_args=("$@")
281 expected_stderr_file="/dev/null"
282
283 # Compare using the CLI with `sink.text.details`
284 bt_diff_cli "$expected_stdout_file" "$expected_stderr_file" "$trace_dir" "-c" "sink.text.details" "${extra_details_args[@]}"
285 }
286
287 # Calls bt_diff_details_ctf_single(), except that "$1" is the path to a
288 # program which generates the CTF trace to compare to. The program "$1"
289 # receives the path to a temporary, empty directory where to write the
290 # CTF trace as its first argument.
291 bt_diff_details_ctf_gen_single() {
292 local ctf_gen_prog_path="$1"
293 local expected_stdout_file="$2"
294 shift 2
295 local extra_details_args=("$@")
296
297 local temp_trace_dir
298 local ret
299
300 temp_trace_dir="$(mktemp -d)"
301
302 # Run the CTF trace generator program to get a CTF trace
303 if ! "$ctf_gen_prog_path" "$temp_trace_dir" 2>/dev/null; then
304 echo "ERROR: \"$ctf_gen_prog_path\" \"$temp_trace_dir\" failed" >&2
305 rm -rf "$temp_trace_dir"
306 return 1
307 fi
308
309 # Compare using the CLI with `sink.text.details`
310 bt_diff_details_ctf_single "$expected_stdout_file" "$temp_trace_dir" "${extra_details_args[@]}"
311 ret=$?
312 rm -rf "$temp_trace_dir"
313 return $ret
314 }
315
316
317 ### Functions ###
318
319 check_coverage() {
320 coverage run "$@"
321 }
322
323 # Execute a shell command in the appropriate environment to have access to the
324 # bt2 Python bindings.
325 run_python_bt2() {
326 local env_args
327
328 env_args=(
329 "BABELTRACE_PYTHON_BT2_NO_TRACEBACK=1" \
330 "BABELTRACE_PLUGIN_PATH=${BT_TESTS_BABELTRACE_PLUGIN_PATH}" \
331 "LIBBABELTRACE2_PLUGIN_PROVIDER_DIR=${BT_TESTS_PROVIDER_DIR}" \
332 "BT_CTF_TRACES_PATH=${BT_CTF_TRACES_PATH}" \
333 "BT_PLUGINS_PATH=${BT_PLUGINS_PATH}" \
334 "PYTHONPATH=${BT_TESTS_PYTHONPATH}:${BT_TESTS_SRCDIR}/utils/python"
335 )
336
337 local main_lib_path="${BT_TESTS_BUILDDIR}/../src/lib/.libs"
338
339 # Set the library search path so the python interpreter can load libbabeltrace2
340 if [ "$BT_OS_TYPE" = "mingw" ] || [ "$BT_OS_TYPE" = "cygwin" ]; then
341 env_args+=("PATH=${main_lib_path}:${PATH:-}")
342 elif [ "$BT_OS_TYPE" = "darwin" ]; then
343 env_args+=("DYLD_LIBRARY_PATH=${main_lib_path}:${DYLD_LIBRARY_PATH:-}")
344 else
345 env_args+=("LD_LIBRARY_PATH=${main_lib_path}:${LD_LIBRARY_PATH:-}")
346 fi
347
348 # On Windows, an embedded Python interpreter needs a way to locate the path
349 # to it's internal modules, set the prefix from python-config to the
350 # PYTHONHOME variable.
351 if [ "$BT_OS_TYPE" = "mingw" ]; then
352 env_args+=("PYTHONHOME=$($BT_TESTS_PYTHON_CONFIG_BIN --prefix)")
353 fi
354
355 env "${env_args[@]}" "$@"
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)
362 run_python_bt2_test() {
363 local test_dir="$1"
364 local test_pattern="${2:-'*'}" # optional, if none default to "*"
365
366 local ret
367 local test_runner_args=()
368
369 test_runner_args+=("$test_dir")
370 if [ "x${test_pattern}" != "x" ]; then
371 test_runner_args+=("${test_pattern}")
372 fi
373
374 if test "x${BT_TESTS_COVERAGE:-}" = "x1"; then
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" \
383 --pattern "$test_pattern" \
384 "$test_dir" \
385
386 ret=$?
387
388 if test "x${BT_TESTS_COVERAGE_REPORT:-}" = "x1"; then
389 coverage report -m
390 fi
391
392 if test "x${BT_TESTS_COVERAGE_HTML:-}" = "x1"; then
393 coverage html
394 fi
395
396 return $ret
397 }
This page took 0.050461 seconds and 5 git commands to generate.