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 | ||
27 | # Allow overriding the source and build directories | |
28 | if [ "x${BT_TESTS_SRCDIR:-}" = "x" ]; then | |
f090979c | 29 | BT_TESTS_SRCDIR="$scriptdir/.." |
abdabab5 MJ |
30 | fi |
31 | export BT_TESTS_SRCDIR | |
32 | ||
33 | if [ "x${BT_TESTS_BUILDDIR:-}" = "x" ]; then | |
f090979c | 34 | BT_TESTS_BUILDDIR="$scriptdir/.." |
abdabab5 MJ |
35 | fi |
36 | export BT_TESTS_BUILDDIR | |
37 | ||
38 | # By default, it will not source tap.sh. If you to tap output directly from | |
39 | # the test script, define the 'SH_TAP' variable to '1' before sourcing this | |
40 | # script. | |
41 | if [ "x${SH_TAP:-}" = x1 ]; then | |
42 | . "${BT_TESTS_SRCDIR}/utils/tap/tap.sh" | |
43 | fi | |
44 | ||
45 | # Allow overriding the babeltrace2 executables | |
46 | if [ "x${BT_TESTS_BT2_BIN:-}" = "x" ]; then | |
f090979c | 47 | BT_TESTS_BT2_BIN="$BT_TESTS_BUILDDIR/../src/cli/babeltrace2" |
a8a11adb JR |
48 | if [ "x${MSYSTEM:-}" != "x" ]; then |
49 | BT_TESTS_BT2_BIN="${BT_TESTS_BT2_BIN}.exe" | |
50 | fi | |
abdabab5 MJ |
51 | fi |
52 | export BT_TESTS_BT2_BIN | |
53 | ||
54 | if [ "x${BT_TESTS_BT2LOG_BIN:-}" = "x" ]; then | |
f090979c | 55 | BT_TESTS_BT2LOG_BIN="$BT_TESTS_BUILDDIR/../src/cli/babeltrace2-log" |
a8a11adb JR |
56 | if [ "x${MSYSTEM:-}" != "x" ]; then |
57 | BT_TESTS_BT2LOG_BIN="${BT_TESTS_BT2LOG_BIN}.exe" | |
58 | fi | |
abdabab5 MJ |
59 | fi |
60 | export BT_TESTS_BT2LOG_BIN | |
61 | ||
62 | # TODO: Remove when bindings/python/bt2/test_plugin.py is fixed | |
63 | BT_PLUGINS_PATH="${BT_TESTS_BUILDDIR}/../src/plugins" | |
64 | ||
65 | # Allow overriding the babeltrace2 plugin path | |
66 | if [ "x${BT_TESTS_BABELTRACE_PLUGIN_PATH:-}" = "x" ]; then | |
67 | BT_TESTS_BABELTRACE_PLUGIN_PATH="${BT_PLUGINS_PATH}/ctf:${BT_PLUGINS_PATH}/utils:${BT_PLUGINS_PATH}/text" | |
68 | fi | |
69 | ||
70 | # Allow overriding the babeltrace2 executables | |
71 | if [ "x${BT_TESTS_PYTHONPATH:-}" = "x" ]; then | |
72 | BT_TESTS_PYTHONPATH="${BT_TESTS_BUILDDIR}/../src/bindings/python/bt2/build/build_lib" | |
73 | fi | |
74 | ||
75 | ||
76 | ### External Tools ### | |
77 | if [ "x${BT_TESTS_AWK_BIN:-}" = "x" ]; then | |
78 | BT_TESTS_AWK_BIN="awk" | |
79 | fi | |
80 | export BT_TESTS_AWK_BIN | |
81 | ||
82 | if [ "x${BT_TESTS_GREP_BIN:-}" = "x" ]; then | |
83 | BT_TESTS_GREP_BIN="grep" | |
84 | fi | |
85 | export BT_TESTS_GREP_BIN | |
86 | ||
87 | if [ "x${BT_TESTS_PYTHON_BIN:-}" = "x" ]; then | |
88 | BT_TESTS_PYTHON_BIN="python3" | |
89 | fi | |
90 | export BT_TESTS_PYTHON_BIN | |
91 | ||
92 | if [ "x${BT_TESTS_SED_BIN:-}" = "x" ]; then | |
93 | BT_TESTS_SED_BIN="sed" | |
94 | fi | |
95 | export BT_TESTS_SED_BIN | |
96 | ||
97 | ||
98 | # Data files path | |
99 | BT_TESTS_DATADIR="${BT_TESTS_SRCDIR}/data" | |
100 | BT_CTF_TRACES_PATH="${BT_TESTS_DATADIR}/ctf-traces" | |
101 | BT_DEBUG_INFO_PATH="${BT_TESTS_DATADIR}/debug-info" | |
102 | ||
103 | ||
104 | ### Diff Functions ### | |
105 | ||
106 | # Checks the difference between: | |
107 | # | |
108 | # 1. What the CLI outputs when given the arguments "$1" (passed to | |
109 | # `xargs`, so they can include quoted arguments). | |
110 | # 2. The file with path "$2". | |
111 | # | |
112 | # Returns 0 if there's no difference, and 1 if there is, also printing | |
113 | # said difference to the standard error. | |
114 | bt_diff_cli() { | |
115 | local args="$1" | |
116 | local expected_file="$2" | |
117 | local temp_output_file | |
118 | local temp_diff | |
119 | local ret=0 | |
120 | ||
121 | temp_output_file="$(mktemp)" | |
122 | temp_diff="$(mktemp)" | |
123 | ||
124 | # Run the CLI to get a detailed file. Strip any \r present due to | |
125 | # Windows (\n -> \r\n). "diff --string-trailing-cr" is not used since it | |
126 | # is not present on Solaris. | |
127 | echo "$args" | xargs "$BT_TESTS_BT2_BIN" 2>/dev/null | tr -d "\r" > "$temp_output_file" | |
128 | ||
129 | # Compare output with expected output | |
df185cf8 | 130 | if ! diff -u "$temp_output_file" "$expected_file" 2>/dev/null >"$temp_diff"; then |
abdabab5 MJ |
131 | echo "ERROR: for '$args': actual and expected outputs differ:" >&2 |
132 | cat "$temp_diff" >&2 | |
133 | ret=1 | |
134 | fi | |
135 | ||
136 | rm -f "$temp_output_file" "$temp_diff" | |
137 | ||
138 | return $ret | |
139 | } | |
140 | ||
141 | # Checks the difference between: | |
142 | # | |
143 | # 1. What the CLI outputs when given the arguments: | |
144 | # | |
145 | # "$1" -c sink.text.details $3 | |
146 | # | |
147 | # 2. The file with path "$2". | |
148 | # | |
149 | # Parameter 3 is optional. | |
150 | # | |
151 | # Returns 0 if there's no difference, and 1 if there is, also printing | |
152 | # said difference to the standard error. | |
153 | bt_diff_details_ctf_single() { | |
154 | local trace_dir="$1" | |
155 | local expected_file="$2" | |
156 | local extra_details_args="${3:-}" | |
157 | ||
158 | # Compare using the CLI with `sink.text.details` | |
159 | bt_diff_cli "\"$trace_dir\" -c sink.text.details $extra_details_args" "$expected_file" | |
160 | } | |
161 | ||
162 | # Calls bt_diff_details_ctf_single(), except that "$1" is the path to a | |
163 | # program which generates the CTF trace to compare to. The program "$1" | |
164 | # receives the path to a temporary, empty directory where to write the | |
165 | # CTF trace as its first argument. | |
166 | bt_diff_details_ctf_gen_single() { | |
167 | local ctf_gen_prog_path="$1" | |
168 | local expected_file="$2" | |
169 | local extra_details_args="${3:-}" | |
170 | ||
171 | local temp_trace_dir | |
172 | local ret | |
173 | ||
174 | temp_trace_dir="$(mktemp -d)" | |
175 | ||
176 | # Run the CTF trace generator program to get a CTF trace | |
177 | if ! "$ctf_gen_prog_path" "$temp_trace_dir" 2>/dev/null; then | |
178 | echo "ERROR: \"$ctf_gen_prog_path\" \"$temp_trace_dir\" failed" >&2 | |
179 | rm -rf "$temp_trace_dir" | |
180 | return 1 | |
181 | fi | |
182 | ||
183 | # Compare using the CLI with `sink.text.details` | |
184 | bt_diff_details_ctf_single "$temp_trace_dir" "$expected_file" "$extra_details_args" | |
185 | ret=$? | |
186 | rm -rf "$temp_trace_dir" | |
187 | return $ret | |
188 | } | |
189 | ||
190 | ||
191 | ### Functions ### | |
192 | ||
193 | check_coverage() { | |
194 | coverage run "$@" | |
195 | } | |
196 | ||
197 | # Execute a shell command in the appropriate environment to have access to the | |
198 | # bt2 Python bindings. | |
199 | run_python_bt2() { | |
200 | local lib_search_var | |
201 | local lib_search_path | |
202 | ||
2e3c8ba5 SM |
203 | local python_provider_path="${BT_TESTS_BUILDDIR}/../src/python-plugin-provider/.libs" |
204 | local main_lib_path="${BT_TESTS_BUILDDIR}/../src/lib/.libs" | |
205 | ||
abdabab5 MJ |
206 | # Set the library search path so the python interpreter can load libbabeltrace2 |
207 | if [ "x${MSYSTEM:-}" != "x" ]; then | |
208 | lib_search_var="PATH" | |
2e3c8ba5 | 209 | lib_search_path="${python_provider_path}:${main_lib_path}:${PATH:-}" |
abdabab5 MJ |
210 | else |
211 | lib_search_var="LD_LIBRARY_PATH" | |
2e3c8ba5 | 212 | lib_search_path="${python_provider_path}:${main_lib_path}:${LD_LIBRARY_PATH:-}" |
abdabab5 MJ |
213 | fi |
214 | ||
215 | env \ | |
216 | BABELTRACE_PYTHON_BT2_NO_TRACEBACK=1 \ | |
217 | BABELTRACE_PLUGIN_PATH="${BT_TESTS_BABELTRACE_PLUGIN_PATH}" \ | |
218 | BT_CTF_TRACES_PATH="${BT_CTF_TRACES_PATH}" \ | |
219 | BT_PLUGINS_PATH="${BT_PLUGINS_PATH}" \ | |
220 | PYTHONPATH="${BT_TESTS_PYTHONPATH}:${BT_TESTS_SRCDIR}/utils/python" \ | |
221 | "${lib_search_var}"="${lib_search_path}" \ | |
222 | "$@" | |
223 | } | |
224 | ||
225 | # Set the environment and run python tests in the directory. | |
226 | # | |
227 | # $1 : The directory containing the python test scripts | |
228 | # $2 : The pattern to match python test script names (optional) | |
abdabab5 MJ |
229 | run_python_bt2_test() { |
230 | local test_dir="$1" | |
231 | local test_pattern="${2:-}" # optional | |
abdabab5 MJ |
232 | |
233 | local ret | |
234 | local test_runner_args=() | |
235 | ||
236 | test_runner_args+=("$test_dir") | |
237 | if [ "x${test_pattern}" != "x" ]; then | |
238 | test_runner_args+=("${test_pattern}") | |
239 | fi | |
240 | ||
241 | if test "x${BT_TESTS_COVERAGE:-}" = "x1"; then | |
242 | python_exec="check_coverage" | |
243 | else | |
244 | python_exec="${BT_TESTS_PYTHON_BIN}" | |
245 | fi | |
246 | ||
247 | run_python_bt2 \ | |
248 | "${python_exec}" \ | |
249 | "${BT_TESTS_SRCDIR}/utils/python/testrunner.py" \ | |
250 | "${test_runner_args[@]}" | |
251 | ret=$? | |
252 | ||
253 | if test "x${BT_TESTS_COVERAGE_REPORT:-}" = "x1"; then | |
254 | coverage report -m | |
255 | fi | |
256 | ||
257 | if test "x${BT_TESTS_COVERAGE_HTML:-}" = "x1"; then | |
258 | coverage html | |
259 | fi | |
260 | ||
261 | return $ret | |
262 | } |