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

feat!: simplify testing secret management #168

Merged
merged 16 commits into from
Aug 7, 2024

Conversation

tonyandrewmeyer
Copy link
Collaborator

@tonyandrewmeyer tonyandrewmeyer commented Aug 2, 2024

Adjust testing secret management to be simpler - in particular, to avoid needing to manually manage revisions, which should be transparent to charms.

Rather than Secrets having a dictionary of revision:content-dict, they have two content dictionaries, tracked_content (required) and latest_content (set to the same value as tracked_content if not provided). This matches what charms can see: only either the tracked revision or the latest revision.

A new attribute, removed_secret_revisions is added to Context to track removal of secret revisions in the secret-remove and secret-expired hooks. Calling secret-remove --revision in those hooks must be done, so should be tested, but don't actually change the state that's visible to the charm (for secret-remove the whole point is that the secret revision is no longer visible to anyone, so it should be removed). Tests could mock the secret_remove method, but it seems cleaner to provide a mechanism, given that this should be part of any charm that uses secrets.

Charms should only remove specific revisions in the secret-remove and secret-expired hooks, and only remove the revision that's provided, but it is possible to remove arbitrary revisions. Modelling this is complicated (the state of the Juju secret is a mess afterwards) and it is always a mistake, so rather than trying to make the model fit the bad code, an exception is raised instead.

A warning is logged if a secret revision is created that is the same as the existing revision - in the latest Juju this is a no-op, but in earlier version it's a problem, and either way it's something that the charm should avoid if possible.

@tonyandrewmeyer tonyandrewmeyer changed the title feat: simplify testing secret management feat!: simplify testing secret management Aug 2, 2024
Copy link
Collaborator

@PietroPasotti PietroPasotti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Love the simplification. It's the third iteration of the secrets model and finally it's looking like it could be final :)
Just a few nits and naming comments, but feel free to ignore them.

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
scenario/mocking.py Outdated Show resolved Hide resolved
secret = Secret(
id=secret_id,
contents={0: content},
latest=content,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this is why I'd rather more explicit argument names :)

Secret(content=content reads so much better.

scenario/mocking.py Outdated Show resolved Hide resolved
scenario/mocking.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@dimaqq dimaqq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, only a partial review today.

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@@ -441,48 +442,6 @@ def __init__(
):
"""Represents a simulated charm's execution context.

It is the main entry point to running a scenario test.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious why this doc block is being removed in this PR?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's being removed because it's duplicated in the 7.0 HEAD - I moved it to Context.__doc__ when doing the autodoc work but must have incorrectly merged that in some branch because it ended up still in Context.__init__.__doc__ too.

It's being removed in this PR because Pietro noticed that it was duplicated in this PR.

I can tidy it up in a separate PR rather than this drive-by if that's preferred.

tests/test_e2e/test_secrets.py Outdated Show resolved Hide resolved
@tonyandrewmeyer tonyandrewmeyer merged commit ff0e4e8 into canonical:7.0 Aug 7, 2024
2 checks passed
@tonyandrewmeyer tonyandrewmeyer deleted the simpler-secrets branch August 7, 2024 03:37
tonyandrewmeyer added a commit that referenced this pull request Sep 2, 2024
Adjust testing secret management to be simpler - in particular, to avoid
needing to manually manage revisions, which should be transparent to
charms.

Rather than `Secret`s having a dictionary of revision:content-dict, they
have two content dictionaries, `tracked_content` (required) and
`latest_content` (set to the same value as `tracked_content` if not
provided). This matches what charms can see: only either the tracked
revision or the latest revision.

A new attribute, `removed_secret_revisions` is added to `Context` to
track removal of secret revisions in the `secret-remove` and
`secret-expired` hooks. Calling `secret-remove --revision` in those
hooks must be done, so should be tested, but don't actually change the
state that's visible to the charm (for `secret-remove` the whole point
is that the secret revision is no longer visible to anyone, so it should
be removed). Tests could mock the `secret_remove` method, but it seems
cleaner to provide a mechanism, given that this should be part of any
charm that uses secrets.

Charms should only remove specific revisions in the `secret-remove` and
`secret-expired` hooks, and only remove the revision that's provided,
but it is possible to remove arbitrary revisions. Modelling this is
complicated (the state of the Juju secret is a mess afterwards) and it
is always a mistake, so rather than trying to make the model fit the bad
code, an exception is raised instead.

A warning is logged if a secret revision is created that is the same as
the existing revision - in the latest Juju this is a no-op, but in
earlier version it's a problem, and either way it's something that the
charm should avoid if possible.

---------

Co-authored-by: PietroPasotti <[email protected]>
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.

4 participants