From e316dcfd2dcbbc000cf313a962c3b869c203bffc Mon Sep 17 00:00:00 2001 From: Jan Richter Date: Wed, 10 Apr 2024 14:15:28 +0200 Subject: [PATCH] exec-test desible env variable This commit adds more control over the environment variables in exec-tests for users. It brings a way how to disable env variable during the test runtime so it won't be available to test script. Only thing which user needs to do is to set the variable to `None` in test `kwargs`. Reference: #5889 Signed-off-by: Jan Richter --- avocado/core/nrunner/runnable.py | 2 +- avocado/plugins/runners/exec_test.py | 3 +++ docs/source/guides/writer/chapters/basics.rst | 9 +++++++++ selftests/check.py | 2 +- selftests/functional/basic.py | 19 +++++++++++++++++++ 5 files changed, 33 insertions(+), 2 deletions(-) diff --git a/avocado/core/nrunner/runnable.py b/avocado/core/nrunner/runnable.py index 5cffdeadb0..2a74a80b0e 100644 --- a/avocado/core/nrunner/runnable.py +++ b/avocado/core/nrunner/runnable.py @@ -150,7 +150,7 @@ def identifier(self): # For kwargs we can use the entire list of values or with a specific # index. - kwargs = "-".join(self.kwargs.values()) + kwargs = "-".join(str(self.kwargs.values())) if "kwargs" in fmt and "[" in fmt: kwargs = self.kwargs diff --git a/avocado/plugins/runners/exec_test.py b/avocado/plugins/runners/exec_test.py index 8eaf907e7f..e0fb949b76 100644 --- a/avocado/plugins/runners/exec_test.py +++ b/avocado/plugins/runners/exec_test.py @@ -122,6 +122,9 @@ def _get_env(self, runnable): env = dict(os.environ) if runnable.kwargs: env.update(runnable.kwargs) + for key, value in runnable.kwargs.items(): + if value is None: + env.pop(key, None) # set default Avocado environment variables if running on a valid Task if runnable.uri is not None: diff --git a/docs/source/guides/writer/chapters/basics.rst b/docs/source/guides/writer/chapters/basics.rst index e9c3a8282a..eeb479a7db 100644 --- a/docs/source/guides/writer/chapters/basics.rst +++ b/docs/source/guides/writer/chapters/basics.rst @@ -90,3 +90,12 @@ variable: .. note:: All environment variables set by avocado will be accessible only during the test runtime and it won't change your environment. + +Disabling environment variables +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +Let's imagine that your testing environment has some important variables, but they +could have a negative impact on one of your tests. In that case, avocado let you +disable those variables during the test runtime. To disable the test variable, +you need to set it in test `kwargs` to `None` like this:: + + Runnable("exec-test", "examples/tests/sleeptest.sh", SLEEP_LENGTH=None) diff --git a/selftests/check.py b/selftests/check.py index ced2206df8..11c1800eb5 100755 --- a/selftests/check.py +++ b/selftests/check.py @@ -29,7 +29,7 @@ "nrunner-requirement": 16, "unit": 668, "jobs": 11, - "functional-parallel": 301, + "functional-parallel": 302, "functional-serial": 4, "optional-plugins": 0, "optional-plugins-golang": 2, diff --git a/selftests/functional/basic.py b/selftests/functional/basic.py index 86e67fbc9f..24a2070021 100644 --- a/selftests/functional/basic.py +++ b/selftests/functional/basic.py @@ -98,6 +98,14 @@ def test(self): logging.getLogger("some.other.logger").info("SHOULD BE ON debug.log") """ +EXEC_ENV_VARIABLE_TEST = """#!/bin/bash +if [[ -z "${TEST_ENV}" ]]; then + exit 1 +else + exit 0 +fi +""" + def probe_binary(binary): try: @@ -1093,6 +1101,17 @@ def test_non_absolute_path(self): f"Avocado did not return rc {expected_rc}:\n{result}", ) + @skipUnlessPathExists("/bin/sh") + def test_env_var_disable(self): + with script.TemporaryScript("exec_env_var.sh", EXEC_ENV_VARIABLE_TEST) as tst: + res = process.run( + f"env TEST_ENV=test avocado-runner-exec-test runnable-run -k exec-test -u {tst} TEST_ENV=json:null", + ) + result = res.stdout_text.split("\n")[-2] + self.assertIn( + "'returncode': 1", result, "The env variable was not disabled" + ) + def tearDown(self): self.pass_script.remove() self.fail_script.remove()