From: Simon Marchi Date: Mon, 6 Nov 2023 18:41:38 +0000 (+0000) Subject: tests: add and use bt_grep_ok X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=db01f759a69290bc1608f60c2942c4c36a282708 tests: add and use bt_grep_ok When a test that consists of matching a pattern in a file using grep fails (sometimes just in the CI), it would be useful to see the file contents to understand what happened. Add the `bt_grep_ok` utility function. It tries to match a pattern in a file using `grep`, then issues a test result. If the test failed, it prints the content of the file on stderr. Change-Id: I464e414b334052677799ce201483095fa9bff4c3 Signed-off-by: Simon Marchi Signed-off-by: Philippe Proulx Reviewed-on: https://review.lttng.org/c/babeltrace/+/11169 Reviewed-by: Michael Jeanson Tested-by: jenkins --- diff --git a/tests/cli/convert/test-auto-source-discovery-grouping.sh b/tests/cli/convert/test-auto-source-discovery-grouping.sh index 0e2666c2..75a49770 100755 --- a/tests/cli/convert/test-auto-source-discovery-grouping.sh +++ b/tests/cli/convert/test-auto-source-discovery-grouping.sh @@ -39,8 +39,10 @@ ok "$?" "expected components are instantiated with expected inputs" # Check that expected warning is printed. # shellcheck disable=SC2016 -bt_grep -q 'No trace was found based on input `some_other_non_opt`' "$stderr_actual_file" -ok "$?" "warning is printed" +bt_grep_ok \ + 'No trace was found based on input `some_other_non_opt`' \ + "$stderr_actual_file" \ + "warning is printed" rm -f "$stdout_actual_file" rm -f "$stderr_actual_file" diff --git a/tests/cli/query/test-query.sh b/tests/cli/query/test-query.sh index b5e02374..ca9c001d 100755 --- a/tests/cli/query/test-query.sh +++ b/tests/cli/query/test-query.sh @@ -58,11 +58,15 @@ expect_failure() { # Ensure that a CLI error stack is printed (and that babeltrace doesn't # abort before that). - bt_grep --silent "^ERROR: " "${stderr_file}" - ok $? "${test_name}: babeltrace produces an error stack" - - bt_grep --silent "${expected_str}" "${stderr_file}" - ok "$?" "${test_name}: expect \`${expected_str}\` error message on stderr" + bt_grep_ok \ + "^ERROR: " \ + "${stderr_file}" \ + "${test_name}: babeltrace produces an error stack" + + bt_grep_ok \ + "${expected_str}" \ + "${stderr_file}" \ + "${test_name}: expect \`${expected_str}\` error message on stderr" } expect_success 'the-object:{}' \ diff --git a/tests/cli/test-help.sh b/tests/cli/test-help.sh index a950adc6..882f6c53 100755 --- a/tests/cli/test-help.sh +++ b/tests/cli/test-help.sh @@ -32,8 +32,10 @@ is_empty() bt_cli "${stdout}" "${stderr}" help ctf ok $? "help ctf plugin exit status" -bt_grep --silent 'Description: CTF input and output' "${stdout}" -ok $? "help ctf plugin expected output" +bt_grep_ok \ + 'Description: CTF input and output' \ + "${stdout}" \ + "help ctf plugin expected output" is_empty "${stderr}" ok $? "help ctf plugin produces no error" @@ -42,8 +44,10 @@ ok $? "help ctf plugin produces no error" bt_cli "${stdout}" "${stderr}" help src.ctf.fs ok $? "help src.ctf.fs component class exit status" -bt_grep --silent 'Description: Read CTF traces from the file system.' "${stdout}" -ok $? "help src.ctf.fs component class expected output" +bt_grep_ok \ + 'Description: Read CTF traces from the file system.' \ + "${stdout}" \ + "help src.ctf.fs component class expected output" is_empty "${stderr}" ok $? "help src.ctf.fs component class produces no error" @@ -52,8 +56,10 @@ ok $? "help src.ctf.fs component class produces no error" bt_cli "${stdout}" "${stderr}" help isnt $? 0 "help without parameter exit status" -bt_grep --silent "Missing plugin name or component class descriptor." "${stderr}" -ok $? "help without parameter produces expected error" +bt_grep_ok \ + "Missing plugin name or component class descriptor." \ + "${stderr}" \ + "help without parameter produces expected error" is_empty "${stdout}" ok $? "help without parameter produces no output" @@ -62,8 +68,10 @@ ok $? "help without parameter produces no output" bt_cli "${stdout}" "${stderr}" help ctf fs isnt $? 0 "help with too many parameters exit status" -bt_grep --silent "Extraneous command-line argument specified to \`help\` command:" "${stderr}" -ok $? "help with too many parameters produces expected error" +bt_grep_ok \ + "Extraneous command-line argument specified to \`help\` command:" \ + "${stderr}" \ + "help with too many parameters produces expected error" is_empty "${stdout}" ok $? "help with too many parameters produces no output" @@ -72,8 +80,10 @@ ok $? "help with too many parameters produces no output" bt_cli "${stdout}" "${stderr}" help zigotos isnt $? 0 "help with unknown plugin name" -bt_grep --silent 'Cannot find plugin: plugin-name="zigotos"' "${stderr}" -ok $? "help with unknown plugin name produces expected error" +bt_grep_ok \ + 'Cannot find plugin: plugin-name="zigotos"' \ + "${stderr}" \ + "help with unknown plugin name produces expected error" is_empty "${stdout}" ok $? "help with unknown plugin name produces no output" @@ -82,18 +92,24 @@ ok $? "help with unknown plugin name produces no output" bt_cli "${stdout}" "${stderr}" help src.ctf.bob isnt $? 0 "help with unknown component class name" -bt_grep --silent 'Cannot find component class: plugin-name="ctf", comp-cls-name="bob", comp-cls-type=SOURCE' "${stderr}" -ok $? "help with unknown component class name produces expected error" +bt_grep_ok \ + 'Cannot find component class: plugin-name="ctf", comp-cls-name="bob", comp-cls-type=SOURCE' \ + "${stderr}" \ + "help with unknown component class name produces expected error" -bt_grep --silent 'Description: CTF input and output' "${stdout}" -ok $? "help with unknown component class name prints plugin help" +bt_grep_ok \ + 'Description: CTF input and output' \ + "${stdout}" \ + "help with unknown component class name prints plugin help" # Test with unknown component class plugin bt_cli "${stdout}" "${stderr}" help src.bob.fs isnt $? 0 "help with unknown component class plugin" -bt_grep --silent 'Cannot find plugin: plugin-name="bob"' "${stderr}" -ok $? "help with unknown component class plugin produces expected error" +bt_grep_ok \ + 'Cannot find plugin: plugin-name="bob"' \ + "${stderr}" \ + "help with unknown component class plugin produces expected error" is_empty "${stdout}" ok $? "help with unknown component class plugin produces no output" diff --git a/tests/cli/test-intersection.sh b/tests/cli/test-intersection.sh index 4ed9c107..18711c56 100755 --- a/tests/cli/test-intersection.sh +++ b/tests/cli/test-intersection.sh @@ -58,8 +58,10 @@ test_intersect_fails() { bt_cli "${stdout}" "${stderr}" --stream-intersection "${trace}" isnt "$?" 0 "run with --stream-intersection fails" - bt_grep --silent "${expected_error_message}" "${stderr}" - ok $? "stderr contains expected error message" + bt_grep_ok \ + "${expected_error_message}" \ + "${stderr}" \ + "stderr contains expected error message" } diag "Test the stream intersection feature" diff --git a/tests/plugins/src.ctf.fs/fail/test-fail.sh b/tests/plugins/src.ctf.fs/fail/test-fail.sh index c15e067e..8e01469b 100755 --- a/tests/plugins/src.ctf.fs/fail/test-fail.sh +++ b/tests/plugins/src.ctf.fs/fail/test-fail.sh @@ -41,11 +41,15 @@ test_fail() { # even if Babeltrace aborts (e.g. hits an assert). Check that the # Babeltrace CLI finishes gracefully by checking that the error stream # contains an error stack printed by the CLI. - bt_grep --silent "^CAUSED BY " "${stderr_file}" - ok $? "Trace ${name}: babeltrace produces an error stack" - - bt_grep --silent "${expected_error_msg}" "${stderr_file}" - ok $? "Trace ${name}: babeltrace produces the expected error message" + bt_grep_ok \ + "^CAUSED BY " \ + "$stderr_file" \ + "Trace $name: babeltrace produces an error stack" + + bt_grep_ok \ + "$expected_error_msg" \ + "$stderr_file" \ + "Trace $name: babeltrace produces the expected error message" } diff --git a/tests/plugins/src.ctf.fs/query/test-query-metadata-info.sh b/tests/plugins/src.ctf.fs/query/test-query-metadata-info.sh index 1a0d27cb..6f4545b4 100755 --- a/tests/plugins/src.ctf.fs/query/test-query-metadata-info.sh +++ b/tests/plugins/src.ctf.fs/query/test-query-metadata-info.sh @@ -68,12 +68,15 @@ test_non_existent_trace_dir() { bt_diff "/dev/null" "${stdout_file}" ok $? "non existent trace dir: babeltrace produces the expected stdout" - bt_grep --silent "^CAUSED BY " "${stderr_file}" - ok $? "non existent trace dir: babeltrace produces an error stack" - - bt_grep --silent "Failed to open metadata file: No such file or directory: path=\".*metadata\"" \ - "${stderr_file}" - ok $? "non existent trace dir: babeltrace produces the expected error message" + bt_grep_ok \ + "^CAUSED BY " \ + "${stderr_file}" \ + "non existent trace dir: babeltrace produces an error stack" + + bt_grep_ok \ + "Failed to open metadata file: No such file or directory: path=\".*metadata\"" \ + "$stderr_file" \ + "non existent trace dir: babeltrace produces the expected error message" rm -f "${stdout_file}" "${stderr_file}" rmdir "${empty_dir}" diff --git a/tests/plugins/src.ctf.fs/test-deterministic-ordering.sh b/tests/plugins/src.ctf.fs/test-deterministic-ordering.sh index 6d2eb3a9..f66247b2 100755 --- a/tests/plugins/src.ctf.fs/test-deterministic-ordering.sh +++ b/tests/plugins/src.ctf.fs/test-deterministic-ordering.sh @@ -66,11 +66,15 @@ expect_failure() { -c src.ctf.fs -p "inputs=[${inputs}]" isnt 0 "$?" "${test_name}: exit status is not 0" - bt_grep --silent "^ERROR: " "${stderr_file}" - ok "$?" "${test_name}: error stack is produced" - - bt_grep --silent "No event class with ID of event class ID to use in stream class" "${stderr_file}" - ok "$?" "${test_name}: expected error message is present" + bt_grep_ok \ + "^ERROR: " \ + "${stderr_file}" \ + "${test_name}: error stack is produced" + + bt_grep_ok \ + "No event class with ID of event class ID to use in stream class" \ + "$stderr_file" \ + "$test_name: expected error message is present" } expect_success() { diff --git a/tests/utils/utils.sh b/tests/utils/utils.sh index 24f1b2d2..7f1a4368 100644 --- a/tests/utils/utils.sh +++ b/tests/utils/utils.sh @@ -302,6 +302,29 @@ bt_grep() { "$BT_TESTS_GREP_BIN" "$@" } +# ok() with the test name `$3` on the result of bt_grep() matching the +# pattern `$1` within the file `$2`. +bt_grep_ok() { + local pattern=$1 + local file=$2 + local test_name=$3 + + bt_grep --silent "$pattern" "$file" + + local ret=$? + + if ! ok $ret "$test_name"; then + { + echo "Pattern \`$pattern\` doesn't match the contents of \`$file\`:" + echo '--- 8< ---' + cat "$file" + echo '--- >8 ---' + } >&2 + fi + + return $ret +} + ### Functions ### check_coverage() {