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!: add error if meta not passed when config or actions are in ops.testing #1497

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

PietroPasotti
Copy link
Contributor

@PietroPasotti PietroPasotti commented Dec 10, 2024

Currently, if meta isn't passed to Context, and the metadata isn't auto-loaded, a default meta of {'name': '<charm-name>'} is set. This can be confusing to users if they pass in config or actions (which prevents auto-loading) and expect the meta to be auto-loaded.

This PR changes he behaviour so that if actions or config are passed in, then meta must be passed in as well.

Fixes #1492

@tonyandrewmeyer tonyandrewmeyer changed the title added error if meta not passed when config or actions are feat: add error if meta not passed when config or actions are in ops.testing Dec 11, 2024
Copy link
Contributor

@tonyandrewmeyer tonyandrewmeyer left a comment

Choose a reason for hiding this comment

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

What we lose here is being able to do simple contexts like:

ctx = testing.Context(MyCharm, actions={'foo': {'description': '...'}})

Although this isn't that much longer:

ctx = testing.Context(MyCharm, meta={'name': '...'}, actions={'foo': {'description': '...'}})

I think I'm ok with the backwards compatibility breakage - I think generally people are autoloading so this is unlikely to break for anyone. It doesn't seem like there's a backwards compatible option outside of docs or warnings.

An alternative to this would be that if meta/config/actions was None then we could attempt autoloading that specific piece. That does get a bit weird if they're all in charmcraft.yaml, though. Basically, meta/config/actions would be an override over the autoload, rather than an alternative. I think this is what the OP was expecting?

I wonder if we're going to have some backwards compatibility breakage if it would be worth doing more and trying to solve the single-YAML issue too (#1424).

Comment on lines +457 to +460
# def test_foo():
# ctx = Context(MyCharm, config={"foo": "bar"})
# ctx.run(..., State())

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
# def test_foo():
# ctx = Context(MyCharm, config={"foo": "bar"})
# ctx.run(..., State())

I'm assuming this is leftover rather than meant as an example?

Comment on lines +510 to +514
f"Cannot automatically setup scenario with `charm_type`={charm_type}. "
"This is likely because this charm has some nonstandard repository setup and Scenario "
"can't find the charmcraft|(metadata|config|actions) yamls in their usual location. "
"Please parse and provide their contents manually via the `meta`, `config` "
"and `actions` Context parameters.",
Copy link
Contributor

Choose a reason for hiding this comment

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

I suppose if we're improving error messages we ought to avoid Scenario as a proper noun 😞

Suggested change
f"Cannot automatically setup scenario with `charm_type`={charm_type}. "
"This is likely because this charm has some nonstandard repository setup and Scenario "
"can't find the charmcraft|(metadata|config|actions) yamls in their usual location. "
"Please parse and provide their contents manually via the `meta`, `config` "
"and `actions` Context parameters.",
f"Cannot automatically set up with `charm_type`={charm_type}. "
"This is likely because this charm has some non-standard repository setup and the "
"charmcraft|(metadata|config|actions) YAML files aren't in their usual location. "
"Please parse and provide their contents manually via the `meta`, `config` "
"and `actions` Context parameters.",

Comment on lines +519 to +521
raise ContextSetupError(
"Scenario doesn't support overriding `actions|config` without overriding `meta` too."
)
Copy link
Contributor

Choose a reason for hiding this comment

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

And I suppose here also 😞

Suggested change
raise ContextSetupError(
"Scenario doesn't support overriding `actions|config` without overriding `meta` too."
)
raise ContextSetupError(
"To override `actions|config` you must also override `meta`."
)

with create_tempcharm(
tmp_path, legacy=legacy, meta={"type": "charm", "name": "sergio"}
) as charm:
# metadata not found:
Copy link
Contributor

Choose a reason for hiding this comment

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

I found this initially confusing because there's metadata two lines above, maybe this would be clearer?

Suggested change
# metadata not found:
# metadata not found in YAML:

Copy link
Collaborator

@benhoyt benhoyt left a comment

Choose a reason for hiding this comment

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

I think this is reasonable. Re Tony's comment:

Basically, meta/config/actions would be an override over the autoload, rather than an alternative. I think this is what the OP was expecting?

That sounds too magical to me. I think it should be one or the other, not an override of (some) parts -- I think that would just be confusing.

@tonyandrewmeyer tonyandrewmeyer changed the title feat: add error if meta not passed when config or actions are in ops.testing feat!: add error if meta not passed when config or actions are in ops.testing Dec 18, 2024
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.

cleanup testing.Context signature
3 participants