Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid changing os.environ in Harness #1359

Merged

Conversation

tonyandrewmeyer
Copy link
Contributor

Harness needs to provide a JUJU_VERSION value to set up the _JujuContext, but it doesn't have to be in os.environ, it can just be in the dictionary passed to create the _JujuContext object.

In production, os.environ would actually have this, so it's more realistic for it to be present, but it would also have lots of the other JUJU_ environment variables as well, and we don't want to have Harness simulate all those in the environment - we want people working with the ops tools to access those, not the environment directly.

This change broke the tests of at least one charm because it patches the environment to have specific values, and then creating the Harness object changes that. It seems better for us to not do this - if we did want to populate the environment to mimic Juju then we'd likely want that to be explicit, or done around the event emitting. It was also an accidental backwards compatiblity break.

@tonyandrewmeyer
Copy link
Contributor Author

It seems like a regression test here isn't required, but let me know if you disagree with that, and I can add one.

I'll also do a super-tox run with and without this change - it's surprising to me that it didn't catch it previously, so I'd like to dig into that.

@dimaqq dimaqq merged commit 8578bdd into canonical:main Sep 5, 2024
32 checks passed
@tonyandrewmeyer
Copy link
Contributor Author

I'll also do a super-tox run with and without this change.

With main@HEAD I have 111 passes and with this branch 112 (charm-simple-streams is the difference, as expected). So this seems safe.

@tonyandrewmeyer tonyandrewmeyer deleted the harness-should-not-change-environ branch September 5, 2024 04:35
tonyandrewmeyer added a commit that referenced this pull request Sep 5, 2024
Harness needs to provide a `JUJU_VERSION` value to set up the
`_JujuContext`, but it doesn't have to be in `os.environ`, it can just
be in the dictionary passed to create the `_JujuContext` object.

In production, `os.environ` would actually have this, so it's more
realistic for it to be present, but it would also have lots of the other
`JUJU_` environment variables as well, and we don't want to have Harness
simulate all those in the environment - we want people working with the
ops tools to access those, not the environment directly.

This change [broke the tests of at least one
charm](canonical/charm-simple-streams#22)
because it patches the environment to have specific values, and then
creating the `Harness` object changes that. It seems better for us to
not do this - if we did want to populate the environment to mimic Juju
then we'd likely want that to be explicit, or done around the event
emitting. It was also an accidental backwards compatiblity break.
@gabrielcocenza
Copy link
Member

Thanks for this fix folks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants