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

Improve how ActionFailed appears in traceback #1204

Closed
sed-i opened this issue Apr 26, 2024 · 0 comments · Fixed by #1209
Closed

Improve how ActionFailed appears in traceback #1204

sed-i opened this issue Apr 26, 2024 · 0 comments · Fixed by #1209
Labels
feature New feature or request small item

Comments

@sed-i
Copy link
Contributor

sed-i commented Apr 26, 2024

When an action fails in a unit test (#1053), the reason isn't displayed in the traceback:

results = self.harness.run_action("show-config").results
Traceback (most recent call last):
  File "/usr/lib/python3.10/unittest/case.py", line 59, in testPartExecutor
    yield
  File "/usr/lib/python3.10/unittest/case.py", line 591, in run
    self._callTestMethod(testMethod)
  File "/usr/lib/python3.10/unittest/case.py", line 549, in _callTestMethod
    method()
  File "/usr/lib/python3.10/unittest/mock.py", line 1379, in patched
    return func(*newargs, **newkeywargs)
  File "/home/ubuntu/code/o11y/alertmanager-k8s-operator/tests/unit/test_charm.py", line 226, in test_show_config
    results = self.harness.run_action("show-config").results
  File "/home/ubuntu/code/o11y/alertmanager-k8s-operator/.tox/unit/lib/python3.10/site-packages/ops/testing.py", line 1913, in run_action
    raise ActionFailed(
ops.testing.ActionFailed

Wrapping in a try-except helps,

try:
    results = self.harness.run_action("show-config").results
except ActionFailed as e:
    self.fail(f"{e.message}; {e.output}")
Traceback (most recent call last):
  File "/home/ubuntu/code/o11y/alertmanager-k8s-operator/tests/unit/test_charm.py", line 227, in test_show_config
    results = self.harness.run_action("show-config").results
  File "/home/ubuntu/code/o11y/alertmanager-k8s-operator/.tox/unit/lib/python3.10/site-packages/ops/testing.py", line 1913, in run_action
    raise ActionFailed(
ops.testing.ActionFailed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.10/unittest/case.py", line 59, in testPartExecutor
    yield
  File "/usr/lib/python3.10/unittest/case.py", line 591, in run
    self._callTestMethod(testMethod)
  File "/usr/lib/python3.10/unittest/case.py", line 549, in _callTestMethod
    method()
  File "/usr/lib/python3.10/unittest/mock.py", line 1379, in patched
    return func(*newargs, **newkeywargs)
  File "/home/ubuntu/code/o11y/alertmanager-k8s-operator/tests/unit/test_charm.py", line 229, in test_show_config
    self.fail(f"{e.message}; {e.output}")
  File "/usr/lib/python3.10/unittest/case.py", line 675, in fail
    raise self.failureException(msg)
AssertionError: not-found - stat /etc/alertmanager/templates.tmpl: no such file or directory; ActionOutput(logs=['Fetching /etc/alertmanager/alertmanager.yml'], results={})

but ideally we should be able to avoid the boilerplate.

@tonyandrewmeyer tonyandrewmeyer added feature New feature or request small item labels May 1, 2024
tonyandrewmeyer added a commit that referenced this issue May 16, 2024
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request small item
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants