Skip to content

Commit

Permalink
exec-test clear testing environment
Browse files Browse the repository at this point in the history
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: avocado-framework#5889
Signed-off-by: Jan Richter <[email protected]>
  • Loading branch information
richtja committed Apr 16, 2024
1 parent e316dcf commit 5a6f95d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
9 changes: 9 additions & 0 deletions avocado/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down
12 changes: 10 additions & 2 deletions avocado/plugins/runners/exec_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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():
Expand Down
7 changes: 7 additions & 0 deletions docs/source/guides/writer/chapters/basics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion selftests/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions selftests/functional/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 5a6f95d

Please sign in to comment.