Skip to content

Commit

Permalink
fix(testing): reset collected statuses in Harness.evaluate_status
Browse files Browse the repository at this point in the history
  • Loading branch information
benhoyt committed Oct 15, 2023
1 parent a88cd4f commit f7c107f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
3 changes: 3 additions & 0 deletions ops/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1634,6 +1634,9 @@ 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.
"""
if self._backend._is_leader:
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 f7c107f

Please sign in to comment.