Skip to content

Commit

Permalink
test: verify that defer() is not usable on stop,remove,secret-expired…
Browse files Browse the repository at this point in the history
…,secret-rotate (canonical#1233)

Extends the test for not being able to `defer()` an action event to also
cover the other events where this is the case. This was missed in canonical#1122.
  • Loading branch information
tonyandrewmeyer committed Jun 26, 2024
1 parent 2f25425 commit 0054fc8
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions test/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,27 +560,42 @@ def _on_foo_bar_action(self, event: ops.ActionEvent):
charm.on.foo_bar_action.emit(id='1')


def test_action_event_defer_fails(
request: pytest.FixtureRequest, monkeypatch: pytest.MonkeyPatch, fake_script: FakeScript
@pytest.mark.parametrize(
'event,kwargs',
[
('start_action', {'id': 2}),
('stop', {}),
('remove', {}),
('secret_expired', {'id': 'secret:123', 'label': None, 'revision': 0}),
('secret_rotate', {'id': 'secret:234', 'label': 'my-secret'}),
],
)
def test_inappropriate_event_defer_fails(
request: pytest.FixtureRequest,
monkeypatch: pytest.MonkeyPatch,
fake_script: FakeScript,
event: str,
kwargs: typing.Dict[str, typing.Any],
):
cmd_type = 'action'

class MyCharm(ops.CharmBase):
def __init__(self, framework: ops.Framework):
super().__init__(framework)
framework.observe(self.on.start_action, self._on_start_action)
framework.observe(getattr(self.on, event), self._call_defer)

def _on_start_action(self, event: ops.ActionEvent):
def _call_defer(self, event: ops.EventBase):
event.defer()

# This is only necessary for the action event, but is ignored by the others.
cmd_type = 'action'
fake_script.write(f'{cmd_type}-get', """echo '{"foo-name": "name", "silent": true}'""")
monkeypatch.setenv(f'JUJU_{cmd_type.upper()}_NAME', 'start')
meta = _get_action_test_meta()

framework = create_framework(request, meta=meta)
charm = MyCharm(framework)

with pytest.raises(RuntimeError):
charm.on.start_action.emit(id='2')
getattr(charm.on, event).emit(**kwargs)


def test_containers():
Expand Down

0 comments on commit 0054fc8

Please sign in to comment.