operator icon indicating copy to clipboard operation
operator copied to clipboard

Improve how ActionFailed appears in traceback

Open sed-i opened this issue 9 months ago • 0 comments

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.

sed-i avatar Apr 26 '24 18:04 sed-i