fix: return copies from Scenario secret_get and action_get#2379
Open
tonyandrewmeyer wants to merge 4 commits intocanonical:mainfrom
Open
fix: return copies from Scenario secret_get and action_get#2379tonyandrewmeyer wants to merge 4 commits intocanonical:mainfrom
tonyandrewmeyer wants to merge 4 commits intocanonical:mainfrom
Conversation
`_MockModelBackend.secret_get()` returned `secret.latest_content` and `secret.tracked_content` directly, and `action_get()` returned `action.params` directly. This meant that mutating the returned dict would silently corrupt the Scenario internal state, a divergence from production behaviour where fresh data is returned each time. Apply the same fix as commit be09012 (which addressed the identical issue in `relation_get`): return `.copy()` / `dict()` so callers get independent copies. Includes regression tests that verify mutating the returned dict does not affect subsequent calls. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
8aa7b17 to
430050e
Compare
dimaqq
reviewed
Mar 16, 2026
dimaqq
reviewed
Mar 16, 2026
Juju requires secrets to have at least one key with string keys and values. Validate this in Secret.__post_init__ so charmers get a clear error at construction time rather than a confusing failure during .run(). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Rather than raising on the first non-string key/value, collect all bad entries and report them together. Also use lowercase dict[str, str] in the error message. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
82b2591 to
f955cf2
Compare
james-garner-canonical
approved these changes
Mar 17, 2026
| ctx.run(ctx.on.action('foo', params={'bar': 10}), State()) | ||
|
|
||
| assert len(results) == 1 | ||
| mutated, fresh = results[0] |
Contributor
There was a problem hiding this comment.
I know we tend to prefer styling this as written already, but doesn't this look cool?
[(mutated, fresh)] = results
Contributor
There was a problem hiding this comment.
James, why not make it more magic: (mut, fsh), = results 🙃
Contributor
There was a problem hiding this comment.
Doesn't look as cool 🤓
- Move _validate_content below __post_init__ in Secret - Use copy.deepcopy for action_get (params can be nested) - Dedent test assertions outside context manager scope Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
dimaqq
reviewed
Mar 18, 2026
Comment on lines
+327
to
+328
| k: v for k, v in content.items() | ||
| if not isinstance(k, str) or not isinstance(v, str) |
Contributor
There was a problem hiding this comment.
ruff wants these on one line...
Comment on lines
+332
to
+333
| f'Secret.{name} should be dict[str, str]; ' | ||
| f'found non-string key(s)/value(s): {bad}', |
Contributor
There was a problem hiding this comment.
And here too, ruff prefers a single line.
I think the tooling that you use didn't catch our custom line lenght limit.
dimaqq
approved these changes
Mar 18, 2026
Contributor
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
secret_get()in_MockModelBackendreturnedsecret.latest_contentandsecret.tracked_contentdirectly, without copying. In production,Secret.get_content()returnsself._content.copy(). Mutating the returned dict would silently corrupt internal Scenario state.action_get()in_MockModelBackendreturnedaction.paramsdirectly. In production,action_get()returns fresh data from Juju, and in Harness it builds a new dict each time. Same mutation-leaks-into-state issue.Both are the same class of bug fixed in commit
be090122forrelation_get, which returneddata.copy()to prevent the mock's internal state from being shared with the caller.Changes
testing/src/scenario/mocking.py:secret_get()now returns.copy()on both thelatest_contentandtracked_contentpaths;action_get()now returnsdict(action.params).test_secrets.pyandtest_actions.pythat verify mutating the returned dict does not affect subsequent calls.🤖 Generated with Claude Code but owned by me.