diff mbox series

[ima-evm-utils,v3,04/11] Pass cleanup function and its arguments to _report_exit_and_cleanup()

Message ID 20230125085030.1568256-5-roberto.sassu@huaweicloud.com (mailing list archive)
State New, archived
Headers show
Series Support testing in new enviroments | expand

Commit Message

Roberto Sassu Jan. 25, 2023, 8:50 a.m. UTC
From: Roberto Sassu <roberto.sassu@huawei.com>

If an error occurs before any test is executed, _report_exit_and_cleanup()
returns 77 ($SKIP) as exit code, which might not reflect the real exit code
at the time the script terminated its execution.

If the function registered in the shell trap() is a cleanup function
calling _report_exit_and_cleanup() inside, the latter will not have access
to the exit code at the time of the trap but instead to the exit code of
the cleanup function.

To solve this issue, pass the cleanup function and its arguments to
_report_exit_and_cleanup(), so that the latter can first get the script
exit code and then can execute the cleanup function.

Finally, if no test was executed, return the exit code at the time of the
trap() instead of 77.

Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
---
 tests/boot_aggregate.test |  2 +-
 tests/fsverity.test       |  3 +--
 tests/functions.sh        | 10 ++++++++--
 3 files changed, 10 insertions(+), 5 deletions(-)

Comments

Stefan Berger Jan. 25, 2023, 1:52 p.m. UTC | #1
On 1/25/23 03:50, Roberto Sassu wrote:
> From: Roberto Sassu <roberto.sassu@huawei.com>
> 
> If an error occurs before any test is executed, _report_exit_and_cleanup()
> returns 77 ($SKIP) as exit code, which might not reflect the real exit code
> at the time the script terminated its execution.
> 
> If the function registered in the shell trap() is a cleanup function
> calling _report_exit_and_cleanup() inside, the latter will not have access
> to the exit code at the time of the trap but instead to the exit code of
> the cleanup function.
> 
> To solve this issue, pass the cleanup function and its arguments to
> _report_exit_and_cleanup(), so that the latter can first get the script
> exit code and then can execute the cleanup function.
> 
> Finally, if no test was executed, return the exit code at the time of the
> trap() instead of 77.
> 
> Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
> ---
>   tests/boot_aggregate.test |  2 +-
>   tests/fsverity.test       |  3 +--
>   tests/functions.sh        | 10 ++++++++--
>   3 files changed, 10 insertions(+), 5 deletions(-)
> 
> diff --git a/tests/boot_aggregate.test b/tests/boot_aggregate.test
> index d7115660385f..ca5faf9cd97d 100755
> --- a/tests/boot_aggregate.test
> +++ b/tests/boot_aggregate.test
> @@ -12,7 +12,7 @@
>   # for verifying the calculated boot_aggregate is included in this
>   # directory as well.
>   
> -trap cleanup SIGINT SIGTERM EXIT
> +trap '_report_exit_and_cleanup cleanup' SIGINT SIGTERM EXIT
>   
>   # Base VERBOSE on the environment variable, if set.
>   VERBOSE="${VERBOSE:-0}"
> diff --git a/tests/fsverity.test b/tests/fsverity.test
> index 549c42a32608..be9594010de5 100755
> --- a/tests/fsverity.test
> +++ b/tests/fsverity.test
> @@ -47,7 +47,7 @@ FSVERITY="$(which fsverity)"
>   _require dd mkfs blkid e2fsck tune2fs evmctl setfattr
>   ./gen-keys.sh >/dev/null 2>&1
>   
> -trap cleanup SIGINT SIGTERM EXIT
> +trap '_report_exit_and_cleanup cleanup' SIGINT SIGTERM EXIT
>   
>   cleanup() {
>           if [ -e $TST_MNT ]; then
> @@ -58,7 +58,6 @@ cleanup() {
>   			rm "$TST_IMG"
>   		fi
>   	fi
> -	_report_exit_and_cleanup
>   }
>   
>   # Loopback mount a file
> diff --git a/tests/functions.sh b/tests/functions.sh
> index 8f6f02dfcd95..cf83ad21562f 100755
> --- a/tests/functions.sh
> +++ b/tests/functions.sh
> @@ -250,10 +250,14 @@ _enable_gost_engine() {
>   # Show test stats and exit into automake test system
>   # with proper exit code (same as ours). Do cleanups.
>   _report_exit_and_cleanup() {
> +  local exit_code=$?
> +
>     if [ -n "${WORKDIR}" ]; then
>       rm -rf "${WORKDIR}"
>     fi
>   
> +  "$@"
> +
>     if [ $testsfail -gt 0 ]; then
>       echo "================================="
>       echo " Run with FAILEARLY=1 $0 $*"
> @@ -271,8 +275,10 @@ _report_exit_and_cleanup() {
>       exit "$FAIL"
>     elif [ $testspass -gt 0 ]; then
>       exit "$OK"
> -  else
> +  elif [ $testsskip -gt 0 ]; then
>       exit "$SKIP"
> +  else
> +    exit $exit_code

For consistency reasons (and to make shell checker happy): exit "$exit_code".


With this change:

Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>

>     fi
>   }
>   
> @@ -312,4 +318,4 @@ _softhsm_teardown() {
>     rm -rf "${SOFTHSM_SETUP_CONFIGDIR}"
>     unset SOFTHSM_SETUP_CONFIGDIR SOFTHSM2_CONF PKCS11_KEYURI \
>       EVMCTL_ENGINE OPENSSL_ENGINE OPENSSL_KEYFORM
> -}
> \ No newline at end of file
> +}
diff mbox series

Patch

diff --git a/tests/boot_aggregate.test b/tests/boot_aggregate.test
index d7115660385f..ca5faf9cd97d 100755
--- a/tests/boot_aggregate.test
+++ b/tests/boot_aggregate.test
@@ -12,7 +12,7 @@ 
 # for verifying the calculated boot_aggregate is included in this
 # directory as well.
 
-trap cleanup SIGINT SIGTERM EXIT
+trap '_report_exit_and_cleanup cleanup' SIGINT SIGTERM EXIT
 
 # Base VERBOSE on the environment variable, if set.
 VERBOSE="${VERBOSE:-0}"
diff --git a/tests/fsverity.test b/tests/fsverity.test
index 549c42a32608..be9594010de5 100755
--- a/tests/fsverity.test
+++ b/tests/fsverity.test
@@ -47,7 +47,7 @@  FSVERITY="$(which fsverity)"
 _require dd mkfs blkid e2fsck tune2fs evmctl setfattr
 ./gen-keys.sh >/dev/null 2>&1
 
-trap cleanup SIGINT SIGTERM EXIT
+trap '_report_exit_and_cleanup cleanup' SIGINT SIGTERM EXIT
 
 cleanup() {
         if [ -e $TST_MNT ]; then
@@ -58,7 +58,6 @@  cleanup() {
 			rm "$TST_IMG"
 		fi
 	fi
-	_report_exit_and_cleanup
 }
 
 # Loopback mount a file
diff --git a/tests/functions.sh b/tests/functions.sh
index 8f6f02dfcd95..cf83ad21562f 100755
--- a/tests/functions.sh
+++ b/tests/functions.sh
@@ -250,10 +250,14 @@  _enable_gost_engine() {
 # Show test stats and exit into automake test system
 # with proper exit code (same as ours). Do cleanups.
 _report_exit_and_cleanup() {
+  local exit_code=$?
+
   if [ -n "${WORKDIR}" ]; then
     rm -rf "${WORKDIR}"
   fi
 
+  "$@"
+
   if [ $testsfail -gt 0 ]; then
     echo "================================="
     echo " Run with FAILEARLY=1 $0 $*"
@@ -271,8 +275,10 @@  _report_exit_and_cleanup() {
     exit "$FAIL"
   elif [ $testspass -gt 0 ]; then
     exit "$OK"
-  else
+  elif [ $testsskip -gt 0 ]; then
     exit "$SKIP"
+  else
+    exit $exit_code
   fi
 }
 
@@ -312,4 +318,4 @@  _softhsm_teardown() {
   rm -rf "${SOFTHSM_SETUP_CONFIGDIR}"
   unset SOFTHSM_SETUP_CONFIGDIR SOFTHSM2_CONF PKCS11_KEYURI \
     EVMCTL_ENGINE OPENSSL_ENGINE OPENSSL_KEYFORM
-}
\ No newline at end of file
+}