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

refactor: remove dependency on ops.testing #190

Merged

Conversation

tonyandrewmeyer
Copy link
Collaborator

@tonyandrewmeyer tonyandrewmeyer commented Sep 10, 2024

The goal is that after doing pip install ops[testing] you can run code like:

from ops import testing

ctx = testing.Context(MyCharm)
ctx.run(ctx.on.start(), testing.State())

Basically: everything that Scenario exports will be available in the ops.testing namespace. This means that Scenario importing from the ops.testing namespace is problematic, because the imports end up being circular.

To avoid this, ops has moved the actual Harness code into a new ops._private.harness module (ops.testing now just handles setting up the namespace). This PR adjusts for that change, importing from ops._private.harness instead of ops.testing.

Rather than just switch to this and require ops 2.17, I've adjusted the imports to work for either case (ops 2.15+). This is messier, but avoids forcing to update their ops and also means that we don't need to wait for two ops releases (potentially two months) in order to make the second change there. There's already a bunch of code to handle other (nominally private) changes in ops 2.16 and 2.17, so a bit more doesn't seem to hurt (for a short time - it will be good to clear all this out soon).

In addition, Scenario already defined a CharmType type, so use that consistently rather than using that in some cases and ops.testing.CharmType in others.

There's one technically breaking change: ActionFailed requires the state argument to be passed by keyword. Tests shouldn't be creating ActionFailed objects so this should have no impact on anyone. This will make it easier to handle the clash between Harness's ActionFailed and Scenario's ActionFailed.

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.

Yeah, the TestingPebbleClient thing is a bit messy. Let's brainstorm a little bit during daily sync. I wonder if we can just wait till after this code lives in the ops repo?

scenario/context.py Outdated Show resolved Hide resolved
pyproject.toml Outdated Show resolved Hide resolved
@tonyandrewmeyer
Copy link
Collaborator Author

Let's brainstorm a little bit during daily sync.

Sure.

I wonder if we can just wait till after this code lives in the ops repo?

We can definitely do these in the opposite order, or in a single PR. However, the circular issue will still exist, unless we don't add Scenario's code in a new top-level location outside of ops (which is the cleanest for having multiple packages, but could be worked around), or we don't have both Harness and Scenario in the same ops.testing namespace.

For the Pebble test client, another option would be late importing. The Scenario test Pebble client could contain an ops.testing._TestingPebbleClient rather than subclassing from it and have a bunch of wrapper methods, and the import could be done at instantiation time. Or the class could still subclass but the class could be defined only in the get_pebble method with a lazy import there. My guess is that doing either of these would be regretted in the future.

@tonyandrewmeyer tonyandrewmeyer marked this pull request as draft September 11, 2024 02:13
@tonyandrewmeyer
Copy link
Collaborator Author

We're going to move the existing ops.testing code to ops._private/, so this can be reworked to use that after that's done, and can avoid the duplication, just changing the imports instead.

@tonyandrewmeyer
Copy link
Collaborator Author

This should be ok now, but it needs the ops change done first (and released) and then I've also assumed that #191 will get done first as well. I'll move it from draft after those.

@tonyandrewmeyer
Copy link
Collaborator Author

This should be ok now, but it needs the ops change done first (and released)

I've adjusted the PR so that it doesn't need to have the ops change released, because that would slow things down a lot and it would be great to get this piece finished.

@tonyandrewmeyer tonyandrewmeyer marked this pull request as ready for review September 17, 2024 22:05
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.

LGTM!

@tonyandrewmeyer tonyandrewmeyer merged commit 2b968e5 into canonical:main Sep 19, 2024
2 checks passed
@tonyandrewmeyer tonyandrewmeyer deleted the remove-ops-testing-dep branch September 19, 2024 12:03
@tonyandrewmeyer
Copy link
Collaborator Author

Drat, I I think I accidentally merged my way out of this one getting a release :(

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.

3 participants