From f7c107f34415b659334acd54c238f9f104c4194d Mon Sep 17 00:00:00 2001 From: Ben Hoyt Date: Mon, 16 Oct 2023 11:51:33 +1300 Subject: [PATCH] fix(testing): reset collected statuses in Harness.evaluate_status This addresses https://github.com/canonical/operator/issues/736#issuecomment-1762094066 --- ops/testing.py | 3 +++ test/test_testing.py | 16 ++++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/ops/testing.py b/ops/testing.py index 57a0c52a0..8696392de 100755 --- a/ops/testing.py +++ b/ops/testing.py @@ -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, diff --git a/test/test_testing.py b/test/test_testing.py index 9d4f9225e..5a0a06c9d 100644 --- a/test/test_testing.py +++ b/test/test_testing.py @@ -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):