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

test: verify that defer() is not usable on stop,remove,secret-expired,secret-rotate #1233

Merged
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)
dimaqq marked this conversation as resolved.
Show resolved Hide resolved

def _on_start_action(self, event: ops.ActionEvent):
def _call_defer(self, event: ops.EventBase):
tonyandrewmeyer marked this conversation as resolved.
Show resolved Hide resolved
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
Loading