diff mbox series

[ima-evm-utils,v3,05/11] Add support for creating a new testing environment in functions.sh

Message ID 20230125085030.1568256-6-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>

Add the new functions _run_env(), _exit_env(), _init_env() and
_cleanup_env() to run the tests inside a new environment specified with the
TST_ENV environment variable.

A typical structure of a script with tests is:

trap '_report_exit_and_cleanup _cleanup_env cleanup' \
    SIGINT SIGTERM SIGSEGV EXIT

cleanup() {
	<test cleanup>
}

<tests implementations>

_run_env "$TST_KERNEL" "$PWD/$(basename "$0")" "env_var1=$env_var1 ..."

_exit_env "$TST_KERNEL"

_init_env

<tests init>

<tests call>

If TST_ENV is not set or empty, don't create a new testing environment and
perform the cleanup in the current environment. Don't create a new testing
environment also if the script is already executed in a new environment, to
avoid loops. Instead, for cleanup, do it in the new environment and skip it
in the host environment (if the cleanup function is passed to
_cleanup_env()).

Signal to the creator of the environment failures of tests or of the script
itself run in the new environment (if the exit code is 1 ($FAIL) or 99
($HARDFAIL)) with an unclean shutdown of the system.

Add haveged and systemd as dependencies for the tests in ci/fedora.sh,
respectively for initializing the random number generator and for shutting
down the system in the new environment.

Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
---
 ci/fedora.sh       |  4 ++-
 tests/functions.sh | 83 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 86 insertions(+), 1 deletion(-)

Comments

Roberto Sassu Jan. 25, 2023, 1:36 p.m. UTC | #1
On Wed, 2023-01-25 at 09:50 +0100, Roberto Sassu wrote:
> From: Roberto Sassu <roberto.sassu@huawei.com>
> 
> Add the new functions _run_env(), _exit_env(), _init_env() and
> _cleanup_env() to run the tests inside a new environment specified with the
> TST_ENV environment variable.
> 
> A typical structure of a script with tests is:
> 
> trap '_report_exit_and_cleanup _cleanup_env cleanup' \
>     SIGINT SIGTERM SIGSEGV EXIT
> 
> cleanup() {
> 	<test cleanup>
> }
> 
> <tests implementations>
> 
> _run_env "$TST_KERNEL" "$PWD/$(basename "$0")" "env_var1=$env_var1 ..."
> 
> _exit_env "$TST_KERNEL"
> 
> _init_env
> 
> <tests init>
> 
> <tests call>
> 
> If TST_ENV is not set or empty, don't create a new testing environment and
> perform the cleanup in the current environment. Don't create a new testing
> environment also if the script is already executed in a new environment, to
> avoid loops. Instead, for cleanup, do it in the new environment and skip it
> in the host environment (if the cleanup function is passed to
> _cleanup_env()).
> 
> Signal to the creator of the environment failures of tests or of the script
> itself run in the new environment (if the exit code is 1 ($FAIL) or 99
> ($HARDFAIL)) with an unclean shutdown of the system.
> 
> Add haveged and systemd as dependencies for the tests in ci/fedora.sh,
> respectively for initializing the random number generator and for shutting
> down the system in the new environment.
> 
> Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
> ---
>  ci/fedora.sh       |  4 ++-
>  tests/functions.sh | 83 ++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 86 insertions(+), 1 deletion(-)
> 
> diff --git a/ci/fedora.sh b/ci/fedora.sh
> index e60de7981c60..198034a34e3c 100755
> --- a/ci/fedora.sh
> +++ b/ci/fedora.sh
> @@ -45,7 +45,9 @@ yum -y install \
>  	vim-common \
>  	wget \
>  	which \
> -	zstd
> +	zstd \
> +	haveged \
> +	systemd
>  
>  yum -y install docbook5-style-xsl || true
>  yum -y install swtpm || true
> diff --git a/tests/functions.sh b/tests/functions.sh
> index cf83ad21562f..9dc9b96d1d7a 100755
> --- a/tests/functions.sh
> +++ b/tests/functions.sh
> @@ -271,6 +271,17 @@ _report_exit_and_cleanup() {
>    [ $testsfail -gt 0 ] && echo -n "$RED" || echo -n "$NORM"
>    echo " FAIL: $testsfail"
>    echo "$NORM"
> +  # Signal failure to the testing environment creator with an unclean shutdown.
> +  if [ -n "$TST_ENV" ] && [ $$ -eq 1 ]; then
> +    if [ -z "$(command -v poweroff)" ]; then
> +      echo "Warning: cannot properly shutdown system"
> +    fi
> +
> +    if [ $testsfail -eq 0 ] && [ $exit_code -ne "$FAIL" ] &&
> +       [ $exit_code -ne "$HARDFAIL" ]; then
> +      poweroff -f
> +    fi

I need to resend this patch.

Having $exit_code equal to $FAIL or $HARDFAIL is a legitimate case if a
negative test was invoked as the last with expect_fail().

$exit_code should be checked only if no test was executed.

Roberto

> +  fi
>    if [ $testsfail -gt 0 ]; then
>      exit "$FAIL"
>    elif [ $testspass -gt 0 ]; then
> @@ -319,3 +330,75 @@ _softhsm_teardown() {
>    unset SOFTHSM_SETUP_CONFIGDIR SOFTHSM2_CONF PKCS11_KEYURI \
>      EVMCTL_ENGINE OPENSSL_ENGINE OPENSSL_KEYFORM
>  }
> +
> +# Syntax: _run_env <kernel> <init> <additional kernel parameters>
> +_run_env() {
> +  if [ -z "$TST_ENV" ]; then
> +    return
> +  fi
> +
> +  if [ $$ -eq 1 ]; then
> +    return
> +  fi
> +
> +  if [ "$TST_ENV" = "um" ]; then
> +    expect_pass "$1" rootfstype=hostfs rw init="$2" quiet mem=2048M "$3"
> +  else
> +    echo $RED"Testing environment $TST_ENV not supported"$NORM
> +    exit "$FAIL"
> +  fi
> +}
> +
> +# Syntax: _exit_env <kernel>
> +_exit_env() {
> +  if [ -z "$TST_ENV" ]; then
> +    return
> +  fi
> +
> +  if [ $$ -eq 1 ]; then
> +    return
> +  fi
> +
> +  exit "$OK"
> +}
> +
> +# Syntax: _init_env
> +_init_env() {
> +  if [ -z "$TST_ENV" ]; then
> +    return
> +  fi
> +
> +  if [ $$ -ne 1 ]; then
> +    return
> +  fi
> +
> +  mount -t tmpfs tmpfs /tmp
> +  mount -t proc proc /proc
> +  mount -t sysfs sysfs /sys
> +  mount -t securityfs securityfs /sys/kernel/security
> +
> +  if [ -n "$(command -v haveged 2> /dev/null)" ]; then
> +    $(command -v haveged) -w 1024 &> /dev/null
> +  fi
> +
> +  pushd "$PWD" > /dev/null || exit "$FAIL"
> +}
> +
> +# Syntax: _cleanup_env <cleanup function>
> +_cleanup_env() {
> +  if [ -z "$TST_ENV" ]; then
> +    $1
> +    return
> +  fi
> +
> +  if [ $$ -ne 1 ]; then
> +    return
> +  fi
> +
> +  $1
> +
> +  umount /sys/kernel/security
> +  umount /sys
> +  umount /proc
> +  umount /tmp
> +}
diff mbox series

Patch

diff --git a/ci/fedora.sh b/ci/fedora.sh
index e60de7981c60..198034a34e3c 100755
--- a/ci/fedora.sh
+++ b/ci/fedora.sh
@@ -45,7 +45,9 @@  yum -y install \
 	vim-common \
 	wget \
 	which \
-	zstd
+	zstd \
+	haveged \
+	systemd
 
 yum -y install docbook5-style-xsl || true
 yum -y install swtpm || true
diff --git a/tests/functions.sh b/tests/functions.sh
index cf83ad21562f..9dc9b96d1d7a 100755
--- a/tests/functions.sh
+++ b/tests/functions.sh
@@ -271,6 +271,17 @@  _report_exit_and_cleanup() {
   [ $testsfail -gt 0 ] && echo -n "$RED" || echo -n "$NORM"
   echo " FAIL: $testsfail"
   echo "$NORM"
+  # Signal failure to the testing environment creator with an unclean shutdown.
+  if [ -n "$TST_ENV" ] && [ $$ -eq 1 ]; then
+    if [ -z "$(command -v poweroff)" ]; then
+      echo "Warning: cannot properly shutdown system"
+    fi
+
+    if [ $testsfail -eq 0 ] && [ $exit_code -ne "$FAIL" ] &&
+       [ $exit_code -ne "$HARDFAIL" ]; then
+      poweroff -f
+    fi
+  fi
   if [ $testsfail -gt 0 ]; then
     exit "$FAIL"
   elif [ $testspass -gt 0 ]; then
@@ -319,3 +330,75 @@  _softhsm_teardown() {
   unset SOFTHSM_SETUP_CONFIGDIR SOFTHSM2_CONF PKCS11_KEYURI \
     EVMCTL_ENGINE OPENSSL_ENGINE OPENSSL_KEYFORM
 }
+
+# Syntax: _run_env <kernel> <init> <additional kernel parameters>
+_run_env() {
+  if [ -z "$TST_ENV" ]; then
+    return
+  fi
+
+  if [ $$ -eq 1 ]; then
+    return
+  fi
+
+  if [ "$TST_ENV" = "um" ]; then
+    expect_pass "$1" rootfstype=hostfs rw init="$2" quiet mem=2048M "$3"
+  else
+    echo $RED"Testing environment $TST_ENV not supported"$NORM
+    exit "$FAIL"
+  fi
+}
+
+# Syntax: _exit_env <kernel>
+_exit_env() {
+  if [ -z "$TST_ENV" ]; then
+    return
+  fi
+
+  if [ $$ -eq 1 ]; then
+    return
+  fi
+
+  exit "$OK"
+}
+
+# Syntax: _init_env
+_init_env() {
+  if [ -z "$TST_ENV" ]; then
+    return
+  fi
+
+  if [ $$ -ne 1 ]; then
+    return
+  fi
+
+  mount -t tmpfs tmpfs /tmp
+  mount -t proc proc /proc
+  mount -t sysfs sysfs /sys
+  mount -t securityfs securityfs /sys/kernel/security
+
+  if [ -n "$(command -v haveged 2> /dev/null)" ]; then
+    $(command -v haveged) -w 1024 &> /dev/null
+  fi
+
+  pushd "$PWD" > /dev/null || exit "$FAIL"
+}
+
+# Syntax: _cleanup_env <cleanup function>
+_cleanup_env() {
+  if [ -z "$TST_ENV" ]; then
+    $1
+    return
+  fi
+
+  if [ $$ -ne 1 ]; then
+    return
+  fi
+
+  $1
+
+  umount /sys/kernel/security
+  umount /sys
+  umount /proc
+  umount /tmp
+}