Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: raise ModelError on unknown/error status set in Scenario #1417

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions testing/src/mocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
Tuple,
Union,
cast,
get_args,
)

from ops import JujuVersion, pebble
Expand All @@ -42,6 +43,7 @@
SecretRotate,
_format_action_result_dict,
_ModelBackend,
_SettableStatusName,
)
from ops.pebble import Client, ExecError

Expand Down Expand Up @@ -371,6 +373,11 @@ def status_set(
*,
is_app: bool = False,
):
if status not in get_args(_SettableStatusName):
tonyandrewmeyer marked this conversation as resolved.
Show resolved Hide resolved
raise ModelError(
f'ERROR invalid status "{status}", expected one of '
f"[maintenance blocked waiting active]"
tonyandrewmeyer marked this conversation as resolved.
Show resolved Hide resolved
)
self._context._record_status(self._state, is_app)
status_obj = _EntityStatus.from_status_name(status, message)
self._state._update_status(status_obj, is_app)
Expand Down
46 changes: 46 additions & 0 deletions testing/tests/test_e2e/test_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
UnknownStatus,
WaitingStatus,
)
from scenario.errors import UncaughtCharmError
from ..helpers import trigger


Expand Down Expand Up @@ -169,3 +170,48 @@ def test_status_comparison(status):
assert isinstance(status, type(ops_status))
# The repr of the scenario and ops classes should be identical.
assert repr(status) == repr(ops_status)


@pytest.mark.parametrize(
"status",
(
ActiveStatus("foo"),
WaitingStatus("bar"),
BlockedStatus("baz"),
MaintenanceStatus("qux"),
),
)
def test_status_success(status: ops.StatusBase):
class MyCharm(CharmBase):
def __init__(self, framework: Framework):
super().__init__(framework)
framework.observe(self.on.update_status, self._on_update_status)

def _on_update_status(self, _):
self.unit.status = status

ctx = Context(MyCharm, meta={"name": "foo"})
ctx.run(ctx.on.update_status(), State())


@pytest.mark.parametrize(
"status",
(
ErrorStatus("fiz"),
UnknownStatus(),
),
)
def test_status_error(status: ops.StatusBase):
class MyCharm(CharmBase):
def __init__(self, framework: Framework):
super().__init__(framework)
framework.observe(self.on.update_status, self._on_update_status)

def _on_update_status(self, _):
self.unit.status = status

ctx = Context(MyCharm, meta={"name": "foo"})
with pytest.raises(UncaughtCharmError) as excinfo:
ctx.run(ctx.on.update_status(), State())
assert isinstance(excinfo.value.__cause__, ops.ModelError)
assert f'invalid status "{status.name}"' in str(excinfo.value.__cause__)
Loading