From 5a6f95d29eb75cf7d7eea7026cf8b01402f6ede3 Mon Sep 17 00:00:00 2001 From: Jan Richter Date: Thu, 11 Apr 2024 10:54:39 +0200 Subject: [PATCH] exec-test clear testing environment This commit adds more control over the environment variables in exec-tests for users. It brings a way how to clear the test environment during the test runtime so it won't affect the test script. Only thing which user needs to do is to set the AVOCADO_CLEAR_ENV to `TRUE` in test `kwargs`. Reference: #5889 Signed-off-by: Jan Richter --- avocado/core/__init__.py | 9 +++++++++ avocado/plugins/runners/exec_test.py | 12 ++++++++++-- docs/source/guides/writer/chapters/basics.rst | 7 +++++++ selftests/check.py | 2 +- selftests/functional/basic.py | 9 +++++++++ 5 files changed, 36 insertions(+), 3 deletions(-) diff --git a/avocado/core/__init__.py b/avocado/core/__init__.py index 3120a35dbe..23d02e2b5b 100644 --- a/avocado/core/__init__.py +++ b/avocado/core/__init__.py @@ -178,6 +178,15 @@ def register_core_options(): help_msg=help_msg, ) + help_msg = "When this is enabled, avocado will disable all environment variables during exec testing" + stgs.register_option( + section="runner.exectest", + key="clear_env", + key_type=bool, + default=False, + help_msg=help_msg, + ) + help_msg = ( "By default Avocado runners will use the {uri} of a test as " "its identifier. Use a custom f-string identifier in order to " diff --git a/avocado/plugins/runners/exec_test.py b/avocado/plugins/runners/exec_test.py index e0fb949b76..37ff282030 100644 --- a/avocado/plugins/runners/exec_test.py +++ b/avocado/plugins/runners/exec_test.py @@ -33,7 +33,11 @@ class ExecTestRunner(BaseRunner): name = "exec-test" description = "Runner for standalone executables treated as tests" - CONFIGURATION_USED = ["run.keep_tmp", "runner.exectest.exitcodes.skip"] + CONFIGURATION_USED = [ + "run.keep_tmp", + "runner.exectest.exitcodes.skip", + "runner.exectest.clear_env", + ] def _process_final_status( self, process, runnable, stdout=None, stderr=None @@ -119,7 +123,11 @@ def _is_uri_a_file_on_cwd(uri): return False def _get_env(self, runnable): - env = dict(os.environ) + clear_env = runnable.config.get("runner.exectest.clear_env", False) + if clear_env: + env = {} + else: + env = dict(os.environ) if runnable.kwargs: env.update(runnable.kwargs) for key, value in runnable.kwargs.items(): diff --git a/docs/source/guides/writer/chapters/basics.rst b/docs/source/guides/writer/chapters/basics.rst index eeb479a7db..0f2d47e53d 100644 --- a/docs/source/guides/writer/chapters/basics.rst +++ b/docs/source/guides/writer/chapters/basics.rst @@ -99,3 +99,10 @@ 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) + +If you need to clear the whole environment before your test, then you can set +`runner.exectest.clear_env` config variable to `True`. When this variable is set, avocado will clear +the entire environment during the test runtime and keeps only avocado env variables +and variables from test's `kwargs`:: + + Runnable("exec-test", "examples/tests/sleeptest.sh", config={'runner.exectest.clear_env': True}, SLEEP_LENGTH=1) diff --git a/selftests/check.py b/selftests/check.py index 11c1800eb5..436044fc89 100755 --- a/selftests/check.py +++ b/selftests/check.py @@ -29,7 +29,7 @@ "nrunner-requirement": 16, "unit": 668, "jobs": 11, - "functional-parallel": 302, + "functional-parallel": 303, "functional-serial": 4, "optional-plugins": 0, "optional-plugins-golang": 2, diff --git a/selftests/functional/basic.py b/selftests/functional/basic.py index 24a2070021..8beebb39b6 100644 --- a/selftests/functional/basic.py +++ b/selftests/functional/basic.py @@ -1112,6 +1112,15 @@ def test_env_var_disable(self): "'returncode': 1", result, "The env variable was not disabled" ) + @skipUnlessPathExists("/bin/sh") + def test_env_clear(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} -c '{{\"runner.exectest.clear_env\": true}}'" + ) + result = res.stdout_text.split("\n")[-2] + self.assertIn("'returncode': 1", result, "The environment was not cleared.") + def tearDown(self): self.pass_script.remove() self.fail_script.remove()