Skip to content

Commit f974911

Browse files
committed
feat: model error on testing module if invalid status set by charm
1 parent 8a08e8e commit f974911

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

ops/model.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,6 +1790,14 @@ class UnknownStatus(StatusBase):
17901790
A unit-agent has finished calling install, config-changed and start, but the
17911791
charm has not called status-set yet.
17921792
1793+
This status is for READ ONLY and should not be used to set a unit status. The following
1794+
charm code would be an invalid charm code.
1795+
1796+
```
1797+
# Raises ops.model.ModelError : ERROR invalid status "unknown", expected one of
1798+
# [maintenance blocked waiting active]
1799+
self.unit.status = UnknownStatus()
1800+
```
17931801
"""
17941802
name = 'unknown'
17951803

@@ -1807,6 +1815,15 @@ class ErrorStatus(StatusBase):
18071815
18081816
The unit-agent has encountered an error (the application or unit requires
18091817
human intervention in order to operate correctly).
1818+
1819+
This status is for READ ONLY and should not be used to set a unit status. The following
1820+
charm code would be an invalid charm code.
1821+
1822+
```
1823+
# Raises ops.model.ModelError : ERROR invalid status "error", expected one of
1824+
# [maintenance blocked waiting active]
1825+
self.unit.status = ErrorStatus()
1826+
```
18101827
"""
18111828
name = 'error'
18121829

ops/testing.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ def begin_with_initial_hooks(self) -> None:
446446
# the unit status from "Maintenance" to "Unknown". See gh#726
447447
post_setup_sts = self._backend.status_get()
448448
if post_setup_sts.get("status") == "maintenance" and not post_setup_sts.get("message"):
449-
self._backend.status_set("unknown", "", is_app=False)
449+
self._backend.status_set("unknown", "", is_app=False, by_testing_backend=True)
450450
all_ids = list(self._backend._relation_names.items())
451451
random.shuffle(all_ids)
452452
for rel_id, rel_name in all_ids:
@@ -2222,7 +2222,17 @@ def status_get(self, *, is_app: bool = False):
22222222
else:
22232223
return self._unit_status
22242224

2225-
def status_set(self, status: '_StatusName', message: str = '', *, is_app: bool = False):
2225+
def status_set(
2226+
self,
2227+
status: "_StatusName",
2228+
message: str = "",
2229+
*,
2230+
is_app: bool = False,
2231+
by_testing_backend: bool = False,
2232+
):
2233+
if not by_testing_backend and status in {model.ErrorStatus.name, model.UnknownStatus.name}:
2234+
raise model.ModelError(f'ERROR invalid status "{status}", expected one of'
2235+
' [maintenance blocked waiting active]')
22262236
if is_app:
22272237
self._app_status = {'status': status, 'message': message}
22282238
else:

test/test_testing.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2851,6 +2851,29 @@ def _on_collect_unit_status(self, event: ops.CollectStatusEvent):
28512851
self.assertEqual(harness.model.app.status, ops.ActiveStatus('active app'))
28522852
self.assertEqual(harness.model.unit.status, ops.ActiveStatus('active unit'))
28532853

2854+
def test_invalid_status_set(self):
2855+
2856+
class TestCharm(ops.CharmBase):
2857+
def __init__(self, framework: ops.Framework):
2858+
super().__init__(framework)
2859+
self.framework.observe(self.on.collect_app_status, self._on_collect_app_status)
2860+
self.framework.observe(self.on.collect_unit_status, self._on_collect_unit_status)
2861+
self.app_status_to_add = ops.ErrorStatus('errored app')
2862+
self.unit_status_to_add = ops.ErrorStatus('errored unit')
2863+
2864+
def _on_collect_app_status(self, event: ops.CollectStatusEvent):
2865+
event.add_status(self.app_status_to_add)
2866+
2867+
def _on_collect_unit_status(self, event: ops.CollectStatusEvent):
2868+
event.add_status(self.unit_status_to_add)
2869+
2870+
harness = ops.testing.Harness(TestCharm)
2871+
harness.set_leader(True)
2872+
harness.begin()
2873+
2874+
with self.assertRaises(ops.model.ModelError):
2875+
harness.evaluate_status()
2876+
28542877

28552878
class TestNetwork(unittest.TestCase):
28562879
def setUp(self):

0 commit comments

Comments
 (0)