Skip to content

Commit

Permalink
exec-test disable env variable
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 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: avocado-framework#5889
Signed-off-by: Jan Richter <[email protected]>
  • Loading branch information
richtja committed Apr 23, 2024
1 parent fbb8143 commit 45a54e3
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion avocado/core/nrunner/runnable.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,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

Expand Down
3 changes: 3 additions & 0 deletions avocado/plugins/runners/exec_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
del env[key]

# set default Avocado environment variables if running on a valid Task
if runnable.uri is not None:
Expand Down
9 changes: 9 additions & 0 deletions docs/source/guides/writer/chapters/basics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 lets you
disable those variables during the test runtime. To disable a test variable,
you need to set it in test ``kwargs`` to ``None`` like this::

Runnable("exec-test", "examples/tests/sleeptest.sh", SLEEP_LENGTH=None)
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": 669,
"jobs": 11,
"functional-parallel": 302,
"functional-parallel": 303,
"functional-serial": 4,
"optional-plugins": 0,
"optional-plugins-golang": 2,
Expand Down
23 changes: 23 additions & 0 deletions selftests/functional/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ def test(self):
logging.getLogger("some.other.logger").info("SHOULD BE ON debug.log")
"""

EXEC_ENV_VARIABLE_TEST = """#!/bin/bash
if [[ -n "${TEST_ENV}" ]]; then
exit 1
fi
"""


def probe_binary(binary):
try:
Expand Down Expand Up @@ -1093,6 +1099,23 @@ def test_non_absolute_path(self):
f"Avocado did not return rc {expected_rc}:\n{result}",
)

@skipUnlessPathExists("/bin/bash")
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}",
)
result = res.stdout_text.split("\n")[-2]
self.assertIn("'returncode': 1", result, "The test might be corrupted.")

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': 0", result, "The env variable was not disabled."
)

def tearDown(self):
self.pass_script.remove()
self.fail_script.remove()
Expand Down

0 comments on commit 45a54e3

Please sign in to comment.