Commit | Line | Data |
---|---|---|
abdabab5 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 | ||
25 | scriptdir="$(dirname "${BASH_SOURCE[0]}")" | |
26 | ||
d660133b JR |
27 | # The OS on which we are running. See [1] for possible values of 'uname -s'. |
28 | # We do a bit of translation to ease our life down the road for comparison. | |
29 | # Export it so that called executables can use it. | |
30 | # [1] https://en.wikipedia.org/wiki/Uname#Examples | |
31 | if [ "x${BT_OS_TYPE:-}" = "x" ]; then | |
32 | BT_OS_TYPE="$(uname -s)" | |
33 | case "$BT_OS_TYPE" in | |
34 | MINGW*) | |
35 | BT_OS_TYPE="mingw" | |
36 | ;; | |
37 | Darwin) | |
38 | BT_OS_TYPE="darwin" | |
39 | ;; | |
40 | Linux) | |
41 | BT_OS_TYPE="linux" | |
42 | ;; | |
5200c954 JR |
43 | CYGWIN*) |
44 | BT_OS_TYPE="cygwin" | |
45 | ;; | |
d660133b JR |
46 | *) |
47 | BT_OS_TYPE="unsupported" | |
48 | ;; | |
49 | esac | |
50 | fi | |
51 | export BT_OS_TYPE | |
52 | ||
abdabab5 MJ |
53 | # Allow overriding the source and build directories |
54 | if [ "x${BT_TESTS_SRCDIR:-}" = "x" ]; then | |
f090979c | 55 | BT_TESTS_SRCDIR="$scriptdir/.." |
abdabab5 MJ |
56 | fi |
57 | export BT_TESTS_SRCDIR | |
58 | ||
59 | if [ "x${BT_TESTS_BUILDDIR:-}" = "x" ]; then | |
f090979c | 60 | BT_TESTS_BUILDDIR="$scriptdir/.." |
abdabab5 MJ |
61 | fi |
62 | export BT_TESTS_BUILDDIR | |
63 | ||
64 | # By default, it will not source tap.sh. If you to tap output directly from | |
65 | # the test script, define the 'SH_TAP' variable to '1' before sourcing this | |
66 | # script. | |
67 | if [ "x${SH_TAP:-}" = x1 ]; then | |
8c083b3e | 68 | # shellcheck source=./tap/tap.sh |
abdabab5 MJ |
69 | . "${BT_TESTS_SRCDIR}/utils/tap/tap.sh" |
70 | fi | |
71 | ||
72 | # Allow overriding the babeltrace2 executables | |
73 | if [ "x${BT_TESTS_BT2_BIN:-}" = "x" ]; then | |
f090979c | 74 | BT_TESTS_BT2_BIN="$BT_TESTS_BUILDDIR/../src/cli/babeltrace2" |
d660133b | 75 | if [ "$BT_OS_TYPE" = "mingw" ]; then |
a8a11adb JR |
76 | BT_TESTS_BT2_BIN="${BT_TESTS_BT2_BIN}.exe" |
77 | fi | |
abdabab5 MJ |
78 | fi |
79 | export BT_TESTS_BT2_BIN | |
80 | ||
81 | if [ "x${BT_TESTS_BT2LOG_BIN:-}" = "x" ]; then | |
f090979c | 82 | BT_TESTS_BT2LOG_BIN="$BT_TESTS_BUILDDIR/../src/cli/babeltrace2-log" |
d660133b | 83 | if [ "$BT_OS_TYPE" = "mingw" ]; then |
a8a11adb JR |
84 | BT_TESTS_BT2LOG_BIN="${BT_TESTS_BT2LOG_BIN}.exe" |
85 | fi | |
abdabab5 MJ |
86 | fi |
87 | export BT_TESTS_BT2LOG_BIN | |
88 | ||
89 | # TODO: Remove when bindings/python/bt2/test_plugin.py is fixed | |
90 | BT_PLUGINS_PATH="${BT_TESTS_BUILDDIR}/../src/plugins" | |
91 | ||
92 | # Allow overriding the babeltrace2 plugin path | |
93 | if [ "x${BT_TESTS_BABELTRACE_PLUGIN_PATH:-}" = "x" ]; then | |
94 | BT_TESTS_BABELTRACE_PLUGIN_PATH="${BT_PLUGINS_PATH}/ctf:${BT_PLUGINS_PATH}/utils:${BT_PLUGINS_PATH}/text" | |
95 | fi | |
96 | ||
7b783015 MJ |
97 | if [ "x${BT_TESTS_PROVIDER_DIR:-}" = "x" ]; then |
98 | BT_TESTS_PROVIDER_DIR="${BT_TESTS_BUILDDIR}/../src/python-plugin-provider/.libs" | |
99 | fi | |
100 | ||
abdabab5 MJ |
101 | # Allow overriding the babeltrace2 executables |
102 | if [ "x${BT_TESTS_PYTHONPATH:-}" = "x" ]; then | |
103 | BT_TESTS_PYTHONPATH="${BT_TESTS_BUILDDIR}/../src/bindings/python/bt2/build/build_lib" | |
104 | fi | |
105 | ||
106 | ||
107 | ### External Tools ### | |
108 | if [ "x${BT_TESTS_AWK_BIN:-}" = "x" ]; then | |
109 | BT_TESTS_AWK_BIN="awk" | |
110 | fi | |
111 | export BT_TESTS_AWK_BIN | |
112 | ||
113 | if [ "x${BT_TESTS_GREP_BIN:-}" = "x" ]; then | |
114 | BT_TESTS_GREP_BIN="grep" | |
115 | fi | |
116 | export BT_TESTS_GREP_BIN | |
117 | ||
118 | if [ "x${BT_TESTS_PYTHON_BIN:-}" = "x" ]; then | |
119 | BT_TESTS_PYTHON_BIN="python3" | |
120 | fi | |
121 | export BT_TESTS_PYTHON_BIN | |
122 | ||
4b2d0d1f MJ |
123 | if [ "x${BT_TESTS_PYTHON_CONFIG_BIN:-}" = "x" ]; then |
124 | BT_TESTS_PYTHON_CONFIG_BIN="python3-config" | |
125 | fi | |
126 | export BT_TESTS_PYTHON_BIN | |
127 | ||
abdabab5 MJ |
128 | if [ "x${BT_TESTS_SED_BIN:-}" = "x" ]; then |
129 | BT_TESTS_SED_BIN="sed" | |
130 | fi | |
131 | export BT_TESTS_SED_BIN | |
132 | ||
133 | ||
134 | # Data files path | |
135 | BT_TESTS_DATADIR="${BT_TESTS_SRCDIR}/data" | |
136 | BT_CTF_TRACES_PATH="${BT_TESTS_DATADIR}/ctf-traces" | |
abdabab5 MJ |
137 | |
138 | ||
139 | ### Diff Functions ### | |
140 | ||
ca5f1044 FD |
141 | # Checks the difference between: |
142 | # | |
143 | # 1. What the CLI outputs on its standard output when given the arguments | |
144 | # "$@" (excluding the first two arguments). | |
145 | # 2. The file with path "$1". | |
146 | # | |
147 | # And the difference between: | |
148 | # | |
149 | # 1. What the CLI outputs on its standard error when given the arguments | |
150 | # "$@" (excluding the first two arguments). | |
151 | # 2. The file with path "$2". | |
abdabab5 MJ |
152 | # |
153 | # Returns 0 if there's no difference, and 1 if there is, also printing | |
154 | # said difference to the standard error. | |
155 | bt_diff_cli() { | |
ca5f1044 FD |
156 | local expected_stdout_file="$1" |
157 | local expected_stderr_file="$2" | |
158 | shift 2 | |
b241db8f MJ |
159 | local args=("$@") |
160 | ||
ca5f1044 FD |
161 | local temp_stdout_output_file |
162 | local temp_stderr_output_file | |
abdabab5 MJ |
163 | local temp_diff |
164 | local ret=0 | |
165 | ||
ca5f1044 FD |
166 | temp_stdout_output_file="$(mktemp)" |
167 | temp_stderr_output_file="$(mktemp)" | |
abdabab5 MJ |
168 | temp_diff="$(mktemp)" |
169 | ||
ca5f1044 FD |
170 | # Run the CLI to get a detailed file. |
171 | run_python_bt2 "$BT_TESTS_BT2_BIN" "${args[@]}" 1>"$temp_stdout_output_file" 2>"$temp_stderr_output_file" | |
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. | |
176 | "$BT_TESTS_SED_BIN" -i 's/\r//g' "$temp_stdout_output_file" | |
177 | "$BT_TESTS_SED_BIN" -i 's/\r//g' "$temp_stderr_output_file" | |
178 | ||
179 | # Compare stdout output with expected stdout output | |
180 | if ! diff -u "$temp_stdout_output_file" "$expected_stdout_file" 2>/dev/null >"$temp_diff"; then | |
181 | echo "ERROR: for '${args[*]}': actual standard output and expected output differ:" >&2 | |
182 | cat "$temp_diff" >&2 | |
183 | ret=1 | |
184 | fi | |
abdabab5 | 185 | |
ca5f1044 FD |
186 | # Compare stderr output with expected stderr output |
187 | if ! diff -u "$temp_stderr_output_file" "$expected_stderr_file" 2>/dev/null >"$temp_diff"; then | |
188 | echo "ERROR: for '${args[*]}': actual standard error and expected error differ:" >&2 | |
abdabab5 MJ |
189 | cat "$temp_diff" >&2 |
190 | ret=1 | |
191 | fi | |
192 | ||
ca5f1044 | 193 | rm -f "$temp_stdout_output_file" "$temp_stderr_output_file" "$temp_diff" |
abdabab5 MJ |
194 | |
195 | return $ret | |
196 | } | |
197 | ||
b241db8f MJ |
198 | # Checks the difference between the content of the file with path "$1" |
199 | # and the output of the CLI when called on the directory path "$2" with | |
200 | # the arguments '-c sink.text.details' and the rest of the arguments to | |
201 | # this function. | |
abdabab5 MJ |
202 | # |
203 | # Returns 0 if there's no difference, and 1 if there is, also printing | |
204 | # said difference to the standard error. | |
205 | bt_diff_details_ctf_single() { | |
ca5f1044 | 206 | local expected_stdout_file="$1" |
b241db8f MJ |
207 | local trace_dir="$2" |
208 | shift 2 | |
209 | local extra_details_args=("$@") | |
ca5f1044 | 210 | expected_stderr_file="/dev/null" |
abdabab5 MJ |
211 | |
212 | # Compare using the CLI with `sink.text.details` | |
ca5f1044 | 213 | bt_diff_cli "$expected_stdout_file" "$expected_stderr_file" "$trace_dir" "-c" "sink.text.details" "${extra_details_args[@]}" |
abdabab5 MJ |
214 | } |
215 | ||
216 | # Calls bt_diff_details_ctf_single(), except that "$1" is the path to a | |
217 | # program which generates the CTF trace to compare to. The program "$1" | |
218 | # receives the path to a temporary, empty directory where to write the | |
219 | # CTF trace as its first argument. | |
220 | bt_diff_details_ctf_gen_single() { | |
221 | local ctf_gen_prog_path="$1" | |
ca5f1044 | 222 | local expected_stdout_file="$2" |
b241db8f MJ |
223 | shift 2 |
224 | local extra_details_args=("$@") | |
abdabab5 MJ |
225 | |
226 | local temp_trace_dir | |
227 | local ret | |
228 | ||
229 | temp_trace_dir="$(mktemp -d)" | |
230 | ||
231 | # Run the CTF trace generator program to get a CTF trace | |
232 | if ! "$ctf_gen_prog_path" "$temp_trace_dir" 2>/dev/null; then | |
233 | echo "ERROR: \"$ctf_gen_prog_path\" \"$temp_trace_dir\" failed" >&2 | |
234 | rm -rf "$temp_trace_dir" | |
235 | return 1 | |
236 | fi | |
237 | ||
238 | # Compare using the CLI with `sink.text.details` | |
ca5f1044 | 239 | bt_diff_details_ctf_single "$expected_stdout_file" "$temp_trace_dir" "${extra_details_args[@]}" |
abdabab5 MJ |
240 | ret=$? |
241 | rm -rf "$temp_trace_dir" | |
242 | return $ret | |
243 | } | |
244 | ||
245 | ||
246 | ### Functions ### | |
247 | ||
248 | check_coverage() { | |
249 | coverage run "$@" | |
250 | } | |
251 | ||
252 | # Execute a shell command in the appropriate environment to have access to the | |
253 | # bt2 Python bindings. | |
254 | run_python_bt2() { | |
4b2d0d1f MJ |
255 | local env_args |
256 | ||
257 | env_args=( | |
258 | "BABELTRACE_PYTHON_BT2_NO_TRACEBACK=1" \ | |
259 | "BABELTRACE_PLUGIN_PATH=${BT_TESTS_BABELTRACE_PLUGIN_PATH}" \ | |
260 | "LIBBABELTRACE2_PLUGIN_PROVIDER_DIR=${BT_TESTS_PROVIDER_DIR}" \ | |
261 | "BT_CTF_TRACES_PATH=${BT_CTF_TRACES_PATH}" \ | |
262 | "BT_PLUGINS_PATH=${BT_PLUGINS_PATH}" \ | |
263 | "PYTHONPATH=${BT_TESTS_PYTHONPATH}:${BT_TESTS_SRCDIR}/utils/python" | |
264 | ) | |
abdabab5 | 265 | |
2e3c8ba5 SM |
266 | local main_lib_path="${BT_TESTS_BUILDDIR}/../src/lib/.libs" |
267 | ||
abdabab5 | 268 | # Set the library search path so the python interpreter can load libbabeltrace2 |
5200c954 | 269 | if [ "$BT_OS_TYPE" = "mingw" ] || [ "$BT_OS_TYPE" = "cygwin" ]; then |
4b2d0d1f | 270 | env_args+=("PATH=${main_lib_path}:${PATH:-}") |
d660133b | 271 | elif [ "$BT_OS_TYPE" = "darwin" ]; then |
4b2d0d1f | 272 | env_args+=("DYLD_LIBRARY_PATH=${main_lib_path}:${DYLD_LIBRARY_PATH:-}") |
abdabab5 | 273 | else |
4b2d0d1f MJ |
274 | env_args+=("LD_LIBRARY_PATH=${main_lib_path}:${LD_LIBRARY_PATH:-}") |
275 | fi | |
276 | ||
277 | # On Windows, an embedded Python interpreter needs a way to locate the path | |
278 | # to it's internal modules, set the prefix from python-config to the | |
279 | # PYTHONHOME variable. | |
280 | if [ "$BT_OS_TYPE" = "mingw" ]; then | |
281 | env_args+=("PYTHONHOME=$($BT_TESTS_PYTHON_CONFIG_BIN --prefix)") | |
abdabab5 MJ |
282 | fi |
283 | ||
4b2d0d1f | 284 | env "${env_args[@]}" "$@" |
abdabab5 MJ |
285 | } |
286 | ||
287 | # Set the environment and run python tests in the directory. | |
288 | # | |
289 | # $1 : The directory containing the python test scripts | |
290 | # $2 : The pattern to match python test script names (optional) | |
abdabab5 MJ |
291 | run_python_bt2_test() { |
292 | local test_dir="$1" | |
541308b8 | 293 | local test_pattern="${2:-'*'}" # optional, if none default to "*" |
abdabab5 MJ |
294 | |
295 | local ret | |
296 | local test_runner_args=() | |
297 | ||
298 | test_runner_args+=("$test_dir") | |
299 | if [ "x${test_pattern}" != "x" ]; then | |
300 | test_runner_args+=("${test_pattern}") | |
301 | fi | |
302 | ||
303 | if test "x${BT_TESTS_COVERAGE:-}" = "x1"; then | |
304 | python_exec="check_coverage" | |
305 | else | |
306 | python_exec="${BT_TESTS_PYTHON_BIN}" | |
307 | fi | |
308 | ||
309 | run_python_bt2 \ | |
310 | "${python_exec}" \ | |
311 | "${BT_TESTS_SRCDIR}/utils/python/testrunner.py" \ | |
541308b8 FD |
312 | --pattern "$test_pattern" \ |
313 | "$test_dir" \ | |
314 | ||
abdabab5 MJ |
315 | ret=$? |
316 | ||
317 | if test "x${BT_TESTS_COVERAGE_REPORT:-}" = "x1"; then | |
318 | coverage report -m | |
319 | fi | |
320 | ||
321 | if test "x${BT_TESTS_COVERAGE_HTML:-}" = "x1"; then | |
322 | coverage html | |
323 | fi | |
324 | ||
325 | return $ret | |
326 | } |