Skip to content

Commit

Permalink
fix: update secret label when getting with both id and label (#172)
Browse files Browse the repository at this point in the history
When we get a secret and provide both ID and label, the label should
update to the provided one. This was previously missing in Scenario.

Fixes #95
  • Loading branch information
tonyandrewmeyer authored Aug 12, 2024
1 parent 2894855 commit 9b85ed6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions scenario/mocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,9 @@ def secret_get(
peek: bool = False,
) -> Dict[str, str]:
secret = self._get_secret(id, label)
# If both the id and label are provided, then update the label.
if id is not None and label is not None:
secret._set_label(label)
juju_version = self._context.juju_version
if not (juju_version == "3.1.7" or juju_version >= "3.3.1"):
# In this medieval Juju chapter,
Expand All @@ -424,6 +427,9 @@ def secret_info_get(
label: Optional[str] = None,
) -> SecretInfo:
secret = self._get_secret(id, label)
# If both the id and label are provided, then update the label.
if id is not None and label is not None:
secret._set_label(label)

# only "manage"=write access level can read secret info
self._check_can_manage_secret(secret)
Expand Down
5 changes: 5 additions & 0 deletions scenario/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ def __post_init__(self):
# bypass frozen dataclass
object.__setattr__(self, "latest_content", self.tracked_content)

def _set_label(self, label):
# bypass frozen dataclass
object.__setattr__(self, "label", label)

def _track_latest_revision(self):
"""Set the current revision to the tracked revision."""
# bypass frozen dataclass
Expand All @@ -340,6 +344,7 @@ def _update_metadata(
object.__setattr__(self, "_latest_revision", self._latest_revision + 1)
# TODO: if this is done twice in the same hook, then Juju ignores the
# first call, it doesn't continue to update like this does.
# Fix when https://github.com/canonical/operator/issues/1288 is resolved.
if content:
object.__setattr__(self, "latest_content", content)
if label:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_e2e/test_secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,24 @@ def _on_secret_remove(self, event):
)


def test_set_label_on_get():
class SecretCharm(CharmBase):
def __init__(self, framework):
super().__init__(framework)
self.framework.observe(self.on.start, self._on_start)

def _on_start(self, _):
id = self.unit.add_secret({"foo": "bar"}).id
secret = self.model.get_secret(id=id, label="label1")
assert secret.label == "label1"
secret = self.model.get_secret(id=id, label="label2")
assert secret.label == "label2"

ctx = Context(SecretCharm, meta={"name": "foo"})
state = ctx.run(ctx.on.start(), State())
assert state.get_secret(label="label2").tracked_content == {"foo": "bar"}


def test_no_additional_positional_arguments():
with pytest.raises(TypeError):
Secret({}, {}, None)
Expand Down

0 comments on commit 9b85ed6

Please sign in to comment.