Skip to content

Commit

Permalink
fix(testing): reset collected statuses in Harness.evaluate_status (#1048
Browse files Browse the repository at this point in the history
)

This addresses #736 (comment)
  • Loading branch information
benhoyt authored Oct 17, 2023
1 parent b9194ca commit 82c6061
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ops/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ class CollectStatusEvent(EventBase):
The framework will trigger these events after the hook code runs
successfully (``collect_app_status`` will only be triggered on the leader
unit). If any statuses were added by the event handlers using
unit). If any statuses were added by the event handler using
:meth:`add_status`, the framework will choose the highest-priority status
and set that as the status (application status for ``collect_app_status``,
or unit status for ``collect_unit_status``).
Expand Down
5 changes: 5 additions & 0 deletions ops/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1633,7 +1633,12 @@ def evaluate_status(self) -> None:
Tests should normally call this and then assert that ``self.model.app.status``
or ``self.model.unit.status`` is the value expected.
Evaluation is not "additive"; this method resets the added statuses before
triggering each collect-status event.
"""
self.charm.app._collected_statuses = []
self.charm.unit._collected_statuses = []
charm._evaluate_status(self.charm)

def handle_exec(self,
Expand Down
16 changes: 12 additions & 4 deletions test/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2828,20 +2828,28 @@ def __init__(self, framework: ops.Framework):
super().__init__(framework)
self.framework.observe(self.on.collect_app_status, self._on_collect_app_status)
self.framework.observe(self.on.collect_unit_status, self._on_collect_unit_status)
self.app_status_to_add = ops.BlockedStatus('blocked app')
self.unit_status_to_add = ops.BlockedStatus('blocked unit')

def _on_collect_app_status(self, event: ops.CollectStatusEvent):
event.add_status(ops.ActiveStatus())
event.add_status(self.app_status_to_add)

def _on_collect_unit_status(self, event: ops.CollectStatusEvent):
event.add_status(ops.BlockedStatus('bar'))
event.add_status(self.unit_status_to_add)

harness = ops.testing.Harness(TestCharm)
harness.set_leader(True)
harness.begin()
# Tests for the behaviour of status evaluation are in test_charm.py
harness.evaluate_status()
self.assertEqual(harness.model.app.status, ops.ActiveStatus())
self.assertEqual(harness.model.unit.status, ops.BlockedStatus('bar'))
self.assertEqual(harness.model.app.status, ops.BlockedStatus('blocked app'))
self.assertEqual(harness.model.unit.status, ops.BlockedStatus('blocked unit'))

harness.charm.app_status_to_add = ops.ActiveStatus('active app')
harness.charm.unit_status_to_add = ops.ActiveStatus('active unit')
harness.evaluate_status()
self.assertEqual(harness.model.app.status, ops.ActiveStatus('active app'))
self.assertEqual(harness.model.unit.status, ops.ActiveStatus('active unit'))


class TestNetwork(unittest.TestCase):
Expand Down

0 comments on commit 82c6061

Please sign in to comment.