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