Skip to content

Commit

Permalink
feat: add a __str__ to ActionFailed, for better unexpected failure …
Browse files Browse the repository at this point in the history
…output (#1209)

Add a `__str__` to `ops.testing.ActionFailed` so that an uncaught
`event.fail()` in tests produces a more informative traceback.

Currently, `event.fail()` and `event.fail(message)` will look somewhat
like:

```python
Traceback (most recent call last):
  File "/tmp/1204.py", line 18, in <module>
    harness.run_action("act")
  File "/home/tameyer/code/operator/ops/testing.py", line 2001, in run_action
    raise ActionFailed(
ops.testing.ActionFailed
```

After this PR, `event.fail()` will look like:

```python
Traceback (most recent call last):
  File "/tmp/1204.py", line 19, in <module>
    harness.run_action("act")
  File "/home/tameyer/code/operator/ops/testing.py", line 2006, in run_action
    raise ActionFailed(
ops.testing.ActionFailed: Event handler called `fail()` with no additional details.
```

and `event.fail("Message")` will look like:

```python
Traceback (most recent call last):
  File "/tmp/1204.py", line 18, in <module>
    harness.run_action("act")
  File "/home/tameyer/code/operator/ops/testing.py", line 2006, in run_action
    raise ActionFailed(
ops.testing.ActionFailed: Message

```

Fixes #1204

---------

Co-authored-by: Tiexin Guo <tiexin.guo@canonical.com>
  • Loading branch information
tonyandrewmeyer and IronCore864 authored May 16, 2024
1 parent 9c329e2 commit f1ff257
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions ops/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ def __init__(self, message: str, output: ActionOutput):
self.message = message
self.output = output

def __str__(self):
if self.message:
return self.message
return "Event handler called `fail()` with no additional details."


@dataclasses.dataclass()
class _RunningAction:
Expand Down
12 changes: 11 additions & 1 deletion test/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5584,7 +5584,10 @@ def _on_simple_action(self, event: ops.ActionEvent):
def _on_fail_action(self, event: ops.ActionEvent):
event.fail("this will be ignored")
event.log("some progress")
event.fail("something went wrong")
if event.params.get('empty-failure-message'):
event.fail()
else:
event.fail("something went wrong")
event.log("more progress")
event.set_results(action_results)

Expand Down Expand Up @@ -5645,11 +5648,18 @@ def test_run_action(self):
assert out.results == {}
assert self.harness.charm.simple_was_called

def test_fail_action_no_message(self):
with pytest.raises(ops.testing.ActionFailed) as excinfo:
self.harness.run_action('fail', {'empty-failure-message': True})
assert 'called `fail()`' in str(excinfo.value)
assert excinfo.value.message == ''

def test_fail_action(self):
self._action_results.clear()
self._action_results["partial"] = "foo"
with pytest.raises(ops.testing.ActionFailed) as excinfo:
self.harness.run_action("fail")
assert "something went wrong" in str(excinfo.value)
assert excinfo.value.message == "something went wrong"
assert excinfo.value.output.logs == ["some progress", "more progress"]
assert excinfo.value.output.results == {"partial": "foo"}
Expand Down

0 comments on commit f1ff257

Please sign in to comment.