Skip to content

fix: do not attempt to mutate secret data in grant/revoke during tests#2614

Open
tonyandrewmeyer wants to merge 1 commit into
canonical:mainfrom
tonyandrewmeyer:fix-2613-revoke-frozenset
Open

fix: do not attempt to mutate secret data in grant/revoke during tests#2614
tonyandrewmeyer wants to merge 1 commit into
canonical:mainfrom
tonyandrewmeyer:fix-2613-revoke-frozenset

Conversation

@tonyandrewmeyer

Copy link
Copy Markdown
Collaborator

Audit of the rest of mocking.py for other places affected by the state-type refactor in #2331:

  • open_port/close_port, secret_add/secret_remove: already copy the frozenset to a mutable set, mutate, then reassign via the _update_* helpers.
  • _update_state_check_infos: reassigns check_infos via object.__setattr__ with a fresh frozenset.
  • secret_set: goes through Secret._update_metadata, which uses object.__setattr__.
  • Relation data (local_app_data, etc.) and Container mappings (layers, service_statuses, mounts): annotations were tightened to Mapping, but the state classes still construct plain dicts in __init__, so in-place mutation is still valid at runtime.

remote_grants was the only field that actually flipped from set to frozenset at runtime, and secret_grant/secret_revoke were the only callers assuming mutability.

Pyright would have caught this — running it directly on the pre-fix code flags Cannot access attribute "add"/"remove" for class "frozenset[str]" — but tool.pyright.include in pyproject.toml uses testing/src/*.py, which does not match testing/src/scenario/*.py, so the scenario source is silently unchecked. I'll fix that in a separate PR.

Fixes #2613

Since ops 3.8.0, Secret.remote_grants values are frozensets, but
secret_grant and secret_revoke still called .add() and .remove() on
them, raising AttributeError when a charm revoked a pre-existing
grant.

Replace the in-place mutations with reassignment using set operators.

Fixes canonical#2613
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@tonyandrewmeyer tonyandrewmeyer changed the title fix(testing): Handle frozenset remote_grants in secret grant/revoke fix: do not attempt to mutate secret data in grant/revoke during tests Jul 4, 2026
@tonyandrewmeyer tonyandrewmeyer marked this pull request as ready for review July 4, 2026 01:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ops.testing: revoking a previously-granted secret fails on 3.8.0 (worked on 3.7.1)

1 participant