Skip to content

Commit

Permalink
Copy the mocked Juju envvars into os.environ again.
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyandrewmeyer committed Feb 11, 2025
1 parent 73419d5 commit 6d13946
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
20 changes: 12 additions & 8 deletions testing/src/scenario/_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,10 @@ def exec(
juju_context = _JujuContext.from_dict(env)
# We need to set JUJU_VERSION in os.environ, because charms may use
# `JujuVersion.from_environ()` to get the (simulated) Juju version.
# All of the other environment variables are exposed to the charm
# through the framework machinery, so don't require this.
previous_juju_version = os.environ.get("JUJU_VERSION")
os.environ["JUJU_VERSION"] = env["JUJU_VERSION"]
# For consistency, we put all the other ones there too, although we'd
# like to change this in the future.
previous_env = os.environ.copy()
os.environ.update(env)

logger.info(" - entering ops.main (mocked)")
from ._ops_main_mock import Ops # noqa: F811
Expand All @@ -352,10 +352,14 @@ def exec(
) from e

finally:
if previous_juju_version is None:
del os.environ["JUJU_VERSION"]
else:
os.environ["JUJU_VERSION"] = previous_juju_version
for key, value in os.environ.copy().items():
if key in previous_env:
os.environ[key] = value
else:
del os.environ[key]
for key, value in previous_env.items():
if key not in os.environ:
os.environ[key] = value
logger.info(" - exited ops.main")

context.emitted_events.extend(captured)
Expand Down
2 changes: 1 addition & 1 deletion testing/tests/test_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def test_env_clean_on_charm_error():
context=Context(my_charm_type, meta=meta),
) as manager:
assert manager._juju_context.remote_app_name == remote_name
assert "JUJU_REMOTE_APP" not in os.environ
assert "JUJU_REMOTE_APP" in os.environ
_ = 1 / 0 # raise some error
# Ensure that some other error didn't occur (like AssertionError!).
assert "ZeroDivisionError" in str(exc.value)
Expand Down

0 comments on commit 6d13946

Please sign in to comment.