Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@james-garner-canonical
Copy link
Contributor

@james-garner-canonical james-garner-canonical commented Oct 23, 2025

This PR adds a new environment variable, SCENARIO_WRAP_CHARM_ERRORS, which controls whether charm errors are wrapped in UncaughtCharmError. The name is up for debate, but was chosen to follow the format of SCENARIO_SKIP_CONSISTENCY_CHECKS.

If unset, it defaults to True, preserving the current behaviour. The idea would be to switch to the other behaviour by default someday.

The environment variable logic is encapsulated in the new private scenario._environ module. This allows the same logic to be used in our internal tests, to handle both cases. Our tests have been updated as needed.

CI has been modified to add a row to our unit test matrix, running with this variable toggled on or off. This would require our expected checks for this repo to be updated, as the job names have changed. An alternative approach might be to have the relevant tests set and unset the environment variable themselves, based on a parametrized input.

tox.ini has been updated to always pass the scenario environment variables along.

Unrelated to these changes, the tracing tests have been updated to run in CI even if the main ops tests fail.

Resolves #1428.

@james-garner-canonical james-garner-canonical marked this pull request as ready for review October 23, 2025 05:00
Copy link
Collaborator

@benhoyt benhoyt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a good start, but I think we should structure this differently.

@james-garner-canonical james-garner-canonical force-pushed the 25-10+feat+env-var-to-control-exception-wrapping branch from d1ccc5c to 04a2b98 Compare October 23, 2025 23:57
@james-garner-canonical
Copy link
Contributor Author

I've refactored as per @benhoyt's suggestions:

  • Dropped the CI changes
  • Inverted the name and default (now SCENARIO_BARE_CHARM_ERRORS defaulting to false)
  • Dropped the helper function and in-lined the logic at the place we raise UncaughtCharmError

I've parametrized the existing tests that caught UncaughtCharmError to set the environment variable using monkeypatch.setenv, which both exercises the environment variable logic and ensures that our expectations about the shape and type of exceptions hold in both cases. I think this is the right approach for the internal testing tests, but I'm less sure about the noise it adds to test/test_model_relation_data_class.py -- maybe the ops tests should just assume the current default.

Copy link
Contributor

@dimaqq dimaqq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How will this env var be documented?

The other env var used to be documented: https://pypi.org/project/ops-scenario/4.0.4.1/
It's no longer documented today.

@TheJJ
Copy link
Member

TheJJ commented Oct 24, 2025

I've implemented the alternative approach that avoids environment variables, and instead controls this through Context in #2145.

Copy link
Collaborator

@benhoyt benhoyt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. This looks better to me. I agree we should probably leave it out of the ops tests. I'll leave off an approval stamp for now, to remind us to finalise which approach we're go for in the end (env var or @TheJJ's Context attribute approach -- let's discuss during engineering week.

@TheJJ
Copy link
Member

TheJJ commented Oct 26, 2025

Thanks for reconsidering. I'm convinced solving this via environment variable is the wrong place to solve this - the test execution becomes dependent on an environment variable, instead of being self-contained and self-descriptive.

Just executing the tests directly without the "correct env" will lead to failures that are not obvious to resolve (and then in the wrong layer: env instead of test configuration).

Instead this exception handling should be configurable per-test (imagine having hundreds to migrate), and be self-contained from within the tests.

If you'd like, we can discuss this in person during the engineering sprint :)

@james-garner-canonical
Copy link
Contributor Author

I lean towards the environment variable as I think it will give us a better pathway to changing the default behavior in future, but I'm definitely open to discussing alternatives.

Regarding the developer experience, it's worth noting that you can still define the behavior in your tests with an environment variable, for example with an autouse fixture. To control the behavior per-test with relatively low boilerplate you could instead use pytest.mark.usefixtures.

@benhoyt
Copy link
Collaborator

benhoyt commented Nov 7, 2025

@james-garner-canonical For the record, decision is to use this (env var) approach.

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.

Better Scenario error message surfacing

4 participants