tests: add and use bt_grep_ok
authorSimon Marchi <simon.marchi@efficios.com>
Mon, 6 Nov 2023 18:41:38 +0000 (18:41 +0000)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 9 Nov 2023 16:14:34 +0000 (11:14 -0500)
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 <simon.marchi@efficios.com>
Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11169
Reviewed-by: Michael Jeanson <mjeanson@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
tests/cli/convert/test-auto-source-discovery-grouping.sh
tests/cli/query/test-query.sh
tests/cli/test-help.sh
tests/cli/test-intersection.sh
tests/plugins/src.ctf.fs/fail/test-fail.sh
tests/plugins/src.ctf.fs/query/test-query-metadata-info.sh
tests/plugins/src.ctf.fs/test-deterministic-ordering.sh
tests/utils/utils.sh

index 0e2666c2431935cfb9563e062a8e409f769472df..75a49770d62d8fa031821ff97616b9dd3c33f42d 100755 (executable)
@@ -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"
index b5e023749820aadd0272a5d5aae4da9304f5d67c..ca9c001d6a530f26a3fa7b0d8871fda62848963a 100755 (executable)
@@ -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:{}' \
index a950adc633672bff5f85bd0adb4c9ba405856244..882f6c533f49d9b588ae713d7eafbcf35f01883f 100755 (executable)
@@ -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"
index 4ed9c107291447c86d734002a637949f73087c61..18711c56bda658143be75591a4a3939a269c624c 100755 (executable)
@@ -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"
index c15e067ecfab219bd79ac37e73195b4b3773244f..8e01469bf30e52ff771dc045722526acc4a2e286 100755 (executable)
@@ -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"
 }
 
 
index 1a0d27cb02b5b7233f620ed020cd394f732f7a5a..6f4545b459366ab07f1019b1eb1fe78ef94646bf 100755 (executable)
@@ -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}"
index 6d2eb3a98023cb6120ac2c775d7721259783623b..f66247b2cf3ef13e2d67f5c7aaa956362502185c 100755 (executable)
@@ -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() {
index 24f1b2d26b6c9af59655bf303c4f5e324935d476..7f1a4368008e977b846bf84f49bbbbc4f3e9cfb1 100644 (file)
@@ -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() {
This page took 0.029893 seconds and 4 git commands to generate.