Skip to content

Commit

Permalink
exec-test desible 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 16, 2024
1 parent a034e02 commit e316dcf
Show file tree
Hide file tree
Showing 5 changed files with 33 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 @@ -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

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:
env.pop(key, None)

# 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 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)
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": 301,
"functional-parallel": 302,
"functional-serial": 4,
"optional-plugins": 0,
"optional-plugins-golang": 2,
Expand Down
19 changes: 19 additions & 0 deletions selftests/functional/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit e316dcf

Please sign in to comment.