From a3b12468c0321465fbb3daf903d5cd1e823d15e4 Mon Sep 17 00:00:00 2001 From: David Sterba Date: Sat, 21 Mar 2020 00:27:21 +0100 Subject: [PATCH] btrfs-progs: tests: add run_mayfail_stdout helper Add a variant of mayfail helper that will duplicate the output to results log and also provides it to the caller for processing. Can be used for catching unsupported functionality or other special cases. Signed-off-by: David Sterba --- tests/common | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tests/common b/tests/common index 1693070b..e04ceeb6 100644 --- a/tests/common +++ b/tests/common @@ -216,6 +216,52 @@ run_mayfail() fi } +# Same as run_mayfail but does not fail the test if it's handled gracefully by +# the caller, unexpected failure like segfault or abort will exit forcibly, +# output is logged +# +# NOTE: we don't use pipefail to avoid disturbing the rest of the caller +# script, so here we use a temporary output file. Pipes are not supported, +# store the output to a variable for further processing. +run_mayfail_stdout() +{ + local spec + local ins + local ret + + tmp_output=$(mktemp --tmpdir btrfs-progs-test--mayfail-stdtout.XXXXXX) + + ins=$(_get_spec_ins "$@") + spec=$(($ins-1)) + spec=$(_cmd_spec "${@:$spec}") + set -- "${@:1:$(($ins-1))}" $spec "${@: $ins}" + echo "====== RUN MAYFAIL $@" >> "$RESULTS" 2>&1 + if [[ $TEST_LOG =~ tty ]]; then echo "CMD(mayfail): $@" > /dev/tty; fi + if [ "$1" = 'root_helper' ]; then + "$@" 2>&1 > "$tmp_output" + else + $INSTRUMENT "$@" 2>&1 > "$tmp_output" + fi + ret=$? + + cat "$tmp_output" >> "$RESULTS" + cat "$tmp_output" + rm -- "$tmp_output" + + if [ "$ret" != 0 ]; then + echo "failed (ignored, ret=$ret): $@" >> "$RESULTS" + if [ "$ret" == 139 ]; then + _fail "mayfail: returned code 139 (SEGFAULT), not ignored" + elif [ "$ret" == 134 ]; then + _fail "mayfail: returned code 134 (SIGABRT), not ignored" + fi + return "$ret" + fi + # return the command code and let the caller decide what to do based + # on the stdout + return "$ret" +} + # first argument is error message to print if it fails, otherwise # same as run_check but expects the command to fail, output is logged run_mustfail()