From: Andrew Jones Subject: [kvm-unit-tests PATCH v3 3/6] arch-run: reduce return code ambiguity Date: Mon, 29 Feb 2016 19:53:20 +0100 Message-ID: <1456772003-27911-4-git-send-email-drjones@redhat.com> References: <1456772003-27911-1-git-send-email-drjones@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: pbonzini@redhat.com, rkrcmar@redhat.com To: kvm@vger.kernel.org Return-path: Received: from mx1.redhat.com ([209.132.183.28]:60011 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753218AbcB2Sxq (ORCPT ); Mon, 29 Feb 2016 13:53:46 -0500 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (Postfix) with ESMTPS id 9F6223B75A for ; Mon, 29 Feb 2016 18:53:40 +0000 (UTC) In-Reply-To: <1456772003-27911-1-git-send-email-drjones@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: qemu/unittest exit codes are convoluted, causing codes 0 and 1 to be ambiguous. Here are the possible meanings .-----------------------------------------------------------------. | | 0 | 1 | |-----------------------------------------------------------------| | QEMU | did something successfully, | FAILURE | | | but probably didn't run the | | | | unittest, OR caught SIGINT, | | | | SIGHUP, or SIGTERM | | |-----------------------------------------------------------------| | unittest | for some reason exited using | SUCCESS | | | ACPI/PSCI, not with debug-exit | | .-----------------------------------------------------------------. As we can see above, an exit code of zero is even ambiguous for each row, i.e. QEMU could exit with zero because it successfully completed, or because it caught a signal. unittest could exit with zero because it successfully powered-off, or because for some odd reason it powered- off instead of calling debug-exit. And, the most fun is that exit-code =3D=3D 1 means QEMU failed, but the unittest succeeded. This patch attempts to reduce that ambiguity, by also looking at stderr= =2E Signed-off-by: Andrew Jones Reviewed-by: Radim Kr=C4=8Dm=C3=A1=C5=99 --- v3: now also powerpc/run (which is a bit different due to already havin= g one exit code fixup in place) arm/run | 6 ++--- powerpc/run | 23 +++++++++++++++---- scripts/arch-run.bash | 61 +++++++++++++++++++++++++++++++++++++++++= ++++++++ scripts/mkstandalone.sh | 2 +- scripts/runtime.bash | 2 +- x86/README | 10 ++++---- x86/run | 7 +++--- 7 files changed, 92 insertions(+), 19 deletions(-) create mode 100644 scripts/arch-run.bash diff --git a/arm/run b/arm/run index dc0a33204c20b..c9463de6dd241 100755 --- a/arm/run +++ b/arm/run @@ -6,6 +6,7 @@ if [ -z "$STANDALONE" ]; then exit 2 fi source config.mak + source scripts/arch-run.bash fi processor=3D"$PROCESSOR" =20 @@ -71,7 +72,4 @@ command=3D"$qemu $M -cpu $processor $chr_testdev" command+=3D" -display none -serial stdio -kernel" echo $command "$@" =20 -$command "$@" -ret=3D$? -echo Return value from qemu: $ret -exit $ret +exit_fixup $command "$@" diff --git a/powerpc/run b/powerpc/run index 45492a1cb8afc..8ac684cc7c12f 100755 --- a/powerpc/run +++ b/powerpc/run @@ -6,6 +6,7 @@ if [ -z "$STANDALONE" ]; then exit 2 fi source config.mak + source scripts/arch-run.bash fi =20 if [ -c /dev/kvm ]; then @@ -46,10 +47,22 @@ command=3D"$qemu $M -bios $FIRMWARE" command+=3D" -display none -serial stdio -kernel" echo $command "$@" =20 -#FIXME: rtas-poweroff always exits with zero, so we have to parse -# the true exit code from the output. -lines=3D$($command "$@") +# powerpc tests currently exit with rtas-poweroff, which exits with 0. +# exit_fixup treats that as a failure exit and returns 1, so we need +# to fixup the fixup below by parsing the true exit code from the outp= ut. +# The second fixup is also a FIXME, because once we add chr-testdev +# support for powerpc, we won't need the second fixup. +lines=3D$(exit_fixup $command "$@") +ret=3D$? echo "$lines" -ret=3D$(grep '^EXIT: ' <<<"$lines" | sed 's/.*STATUS=3D\([0-9][0-9]*\)= =2E*/\1/') -echo Return value from qemu: $ret +if [ $ret -eq 1 ]; then + testret=3D$(grep '^EXIT: ' <<<"$lines" | sed 's/.*STATUS=3D\([0-9][0-= 9]*\).*/\1/') + if [ "$testret" ]; then + if [ $testret -eq 1 ]; then + ret=3D0 + else + ret=3D$testret + fi + fi +fi exit $ret diff --git a/scripts/arch-run.bash b/scripts/arch-run.bash new file mode 100644 index 0000000000000..3a63ed179a7de --- /dev/null +++ b/scripts/arch-run.bash @@ -0,0 +1,61 @@ +######################################################################= ######## +# exit_fixup translates the ambiguous exit status in Table1 to that in= Table2. +# Table3 simply documents the complete status table. +# +# Table1: Before fixup +# -------------------- +# 0 - Unexpected exit from QEMU (possible signal), or the unittes= t did +# not use debug-exit +# 1 - most likely unittest succeeded, or QEMU failed +# +# Table2: After fixup +# ------------------- +# 0 - Everything succeeded +# 1 - most likely QEMU failed +# +# Table3: Complete table +# ---------------------- +# 0 - SUCCESS +# 1 - most likely QEMU failed +# 2 - most likely a run script failed +# 3 - most likely the unittest failed +# 127 - most likely the unittest called abort() +# 1..127 - FAILURE (could be QEMU, a run script, or the unittest) +# >=3D 128 - Signal (signum =3D status - 128) +######################################################################= ######## +exit_fixup () +{ + local stdout errors ret sig + + exec {stdout}>&1 + errors=3D$("${@}" 2>&1 1>&${stdout} | tee >(cat - 1>&2); exit ${PIPES= TATUS[0]}) + ret=3D$? + exec {stdout}>&- + + if [ "$errors" ]; then + sig=3D$(grep 'terminating on signal' <<<"$errors") + if [ "$sig" ]; then + sig=3D$(sed 's/.*terminating on signal \([0-9][0-9]*\).*/\1/' <<<"$= sig") + fi + fi + + if [ $ret -eq 0 ]; then + # Some signals result in a zero return status, but the + # error log tells the truth. + if [ "$sig" ]; then + ((ret=3Dsig+128)) + else + # Exiting with zero (non-debugexit) is an error + ret=3D1 + fi + elif [ $ret -eq 1 ]; then + # Even when ret=3D=3D1 (unittest success) if we also got stderr + # logs, then we assume a QEMU failure. Otherwise we translate + # status of 1 to 0 (SUCCESS) + if [ -z "$errors" ]; then + ret=3D0 + fi + fi + + return $ret +} diff --git a/scripts/mkstandalone.sh b/scripts/mkstandalone.sh index 408bb05480b11..4c201aa9a4743 100755 --- a/scripts/mkstandalone.sh +++ b/scripts/mkstandalone.sh @@ -61,7 +61,7 @@ generate_test () temp_file bin "$kernel" args[3]=3D'$bin' =20 - temp_file RUNTIME_arch_run "$TEST_DIR/run" + temp_file RUNTIME_arch_run <(echo "#!/bin/bash"; cat scripts/arch-run= =2Ebash "$TEST_DIR/run") =20 cat scripts/runtime.bash =20 diff --git a/scripts/runtime.bash b/scripts/runtime.bash index 04adb9d070f13..9c973f26f8de6 100644 --- a/scripts/runtime.bash +++ b/scripts/runtime.bash @@ -48,7 +48,7 @@ function run() eval $cmdline ret=3D$? =20 - if [ $ret -le 1 ]; then + if [ $ret -eq 0 ]; then echo -e "\e[32mPASS\e[0m $1" else echo -e "\e[31mFAIL\e[0m $1" diff --git a/x86/README b/x86/README index 996ed33546d62..218fe1a1c6f1a 100644 --- a/x86/README +++ b/x86/README @@ -41,7 +41,9 @@ Tests in this directory and what they do: pcid: basic functionality test of PCID/INVPCID feature =20 Legacy notes: - The exit status of the binary (and the script) is inconsistent: with - qemu-system, after the unit-test is done, the exit status of qemu is = 1, - different from the 'old style' qemu-kvm, whose exit status in success= ful - completion is 0. + The exit status of the binary is inconsistent; with qemu-system, afte= r + the unit-test is done, the exit status of qemu is 1, different from t= he + 'old style' qemu-kvm, whose exit status in successful completion is 0= =2E + The run script converts the qemu-system exit status to 0 (SUCCESS), a= nd + treats the legacy exit status of 0 as an error, converting it to an e= xit + status of 1. diff --git a/x86/run b/x86/run index 22d7f2cad0d06..b07c815c5a6e1 100755 --- a/x86/run +++ b/x86/run @@ -1,5 +1,7 @@ #!/bin/bash =20 +[ -z "$STANDALONE" ] && source scripts/arch-run.bash + qemubinarysearch=3D"${QEMU:-qemu-kvm qemu-system-x86_64}" =20 for qemucmd in ${qemubinarysearch} @@ -43,7 +45,4 @@ fi command=3D"${qemu} -enable-kvm $pc_testdev -vnc none -serial stdio $pc= i_testdev $hyperv_testdev -kernel" echo ${command} "$@" =20 -${command} "$@" -ret=3D$? -echo Return value from qemu: $ret -exit $ret +exit_fixup ${command} "$@" --=20 2.4.3