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: fix secret-remove and secret-expired events #158

Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "ops-scenario"

version = "6.1.2"
version = "6.1.3"

authors = [
{ name = "Pietro Pasotti", email = "[email protected]" }
Expand Down
2 changes: 2 additions & 0 deletions scenario/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ def _get_event_env(self, state: "State", event: "Event", charm_root: Path):
"JUJU_SECRET_LABEL": secret.label or "",
},
)
if event.name in ("secret_remove", "secret_expired"):
env["JUJU_SECRET_REVISION"] = str(secret.revision)

return env

Expand Down
10 changes: 5 additions & 5 deletions scenario/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@

SECRET_EVENTS = {
"secret_changed",
"secret_removed",
"secret_remove",
"secret_rotate",
"secret_expired",
}
Expand Down Expand Up @@ -292,18 +292,18 @@ def expired_event(self):
"""Sugar to generate a secret-expired event."""
if not self.owner:
raise ValueError(
"This unit will never receive secret-expire for a secret it does not own.",
"This unit will never receive secret-expired for a secret it does not own.",
)
return Event("secret_expire", secret=self)
return Event("secret_expired", secret=self)

@property
def remove_event(self):
"""Sugar to generate a secret-remove event."""
if not self.owner:
raise ValueError(
"This unit will never receive secret-removed for a secret it does not own.",
"This unit will never receive secret-remove for a secret it does not own.",
)
return Event("secret_removed", secret=self)
return Event("secret_remove", secret=self)

def _set_revision(self, revision: int):
"""Set a new tracked revision."""
Expand Down
2 changes: 1 addition & 1 deletion tests/test_e2e/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
("foo_bar_baz_pebble_ready", _EventType.workload),
("foo_pebble_custom_notice", _EventType.workload),
("foo_bar_baz_pebble_custom_notice", _EventType.workload),
("secret_removed", _EventType.secret),
("secret_remove", _EventType.secret),
("pre_commit", _EventType.framework),
("commit", _EventType.framework),
("collect_unit_status", _EventType.framework),
Expand Down
34 changes: 34 additions & 0 deletions tests/test_e2e/test_secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
import warnings

import pytest
from ops import (
SecretChangedEvent,
SecretExpiredEvent,
SecretRemoveEvent,
SecretRotateEvent,
)
from ops.charm import CharmBase
from ops.framework import Framework
from ops.model import ModelError
Expand Down Expand Up @@ -554,3 +560,31 @@ def __init__(self, *args):
secret.remove_all_revisions()

assert not mgr.output.secrets[0].contents # secret wiped


@pytest.mark.parametrize(
"evt,owner,cls",
(
("changed", None, SecretChangedEvent),
("rotate", "app", SecretRotateEvent),
("expired", "app", SecretExpiredEvent),
("remove", "app", SecretRemoveEvent),
),
)
def test_emit_event(evt, owner, cls):
class MyCharm(CharmBase):
def __init__(self, framework):
super().__init__(framework)
for evt in self.on.events().values():
self.framework.observe(evt, self._on_event)
self.events = []

def _on_event(self, event):
self.events.append(event)

ctx = Context(MyCharm, meta={"name": "local"})
secret = Secret(contents={"foo": "bar"}, id="foo", owner=owner)
with ctx.manager(getattr(secret, evt + "_event"), State(secrets=[secret])) as mgr:
mgr.run()
juju_event = mgr.charm.events[0] # Ignore collect-status etc.
assert isinstance(juju_event, cls)
Loading