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

Reset sys.excepthook. #122

Merged

Conversation

tonyandrewmeyer
Copy link
Collaborator

This code currently has no output, but does fail:

example code
import ops

import scenario

class MyCharm(ops.CharmBase):
    def __init__(self, framework):
        super().__init__(framework)
        framework.observe(self.on.config_changed, self._on_config_changed)

    def _on_config_changed(self, event):
        self.unit.get_container("example").replan()

ctx = scenario.Context(
    MyCharm,
    meta={"name": "my-charm", "containers": {"example": {"resource": "ubuntu-22.04"}}},
)
container = scenario.Container("example", can_connect=False)
state = scenario.State(containers=[container])
out = ctx.run("config-changed", state=state)
$ python /tmp/scenario-102.py ; echo $?
1

This PR resets the sys.excepthook change that ops does (to redirect output to Juju's debug-log), so that instead:

(venv) tameyer@tam-canoncial-1:~/code/ops-scenario$ python /tmp/scenario-102.py ; echo $?
Traceback (most recent call last):
[...]
  File "/home/tameyer/code/ops-scenario/scenario/mocking.py", line 711, in _check_connection
    raise pebble.ConnectionError(msg)
ops.pebble.ConnectionError: Cannot connect to Pebble; did you forget to set can_connect=True for container example?

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
[...]
  File "/home/tameyer/code/ops-scenario/scenario/runtime.py", line 442, in exec
    raise UncaughtCharmError(
scenario.runtime.UncaughtCharmError: Uncaught exception (<class 'ops.pebble.ConnectionError'>) in operator/charm code: <ops.pebble.ConnectionError ('Cannot connect to Pebble; did you forget to set can_connect=True for container example?',)>
1

I think this is more what people would expect (if you're running in pytest then you'd get this type of output anyway, but for cases when not doing that).

Fixes #102

Copy link
Collaborator

@PietroPasotti PietroPasotti left a comment

Choose a reason for hiding this comment

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

nice! good catch

@PietroPasotti PietroPasotti merged commit 97b50f8 into canonical:main Apr 18, 2024
2 checks passed
@tonyandrewmeyer tonyandrewmeyer deleted the reset-sys-excepthook-102 branch May 24, 2024 00:45
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.

Containers should fail when can_connect is false
2 participants