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

Reuse containers #742

Open
tisonkun opened this issue Sep 26, 2024 · 31 comments
Open

Reuse containers #742

tisonkun opened this issue Sep 26, 2024 · 31 comments
Labels
enhancement New feature or request

Comments

@tisonkun
Copy link

tisonkun commented Sep 26, 2024

Like https://java.testcontainers.org/features/reuse/

I currently use bollard directly to reuse containers between tests, while noticed that testcontainers encapsulation is valuable and ergonomic, like all the code to support get_host_port_ipv4.

So if testcontainers can support reuse containers, users like me can benefit from both features.

@tisonkun
Copy link
Author

@DDtKey
Copy link
Collaborator

DDtKey commented Sep 26, 2024

Hi 👋
Thank you for raising the issue!

Yes, it makes sense to support and shouldn't be hard

@DDtKey DDtKey added the enhancement New feature or request label Sep 26, 2024
@gurinderu
Copy link

How do you want to make a reuse ID? Should it be calculated automatically from the source code & call site meta or provided by hand?

@gurinderu
Copy link

Also, it is not clear to understand when to stop the container, lets say we run nextest with the same reuse id in a serial way. Nextest use process-per-test strategy and we don't have ryuk

@DDtKey
Copy link
Collaborator

DDtKey commented Oct 9, 2024

Reusable containers are not expected to be cleaned.

Documentation from dotnet impl:

Enabling reuse will disable the resource reaper, meaning the resource will not be cleaned up

and also from Java:

Reusable containers are not suited for CI usage and as an experimental feature not all Testcontainers features are fully working (e.g., resource cleanup or networking).

You can check java and dotnet docs, because it describes how it works. And we can rely on their impls of identifiers to a certain extent.

Btw, ryuk will be supported soon I think, just need to find spare time 🙂

@tisonkun
Copy link
Author

tisonkun commented Oct 9, 2024

Reusable containers are not suited for CI usage

Just a comment to this statement: We reuse containers in the CI environment, and the runner will destroy everything once the job is finished. So it works well for our CI usage :D

@DDtKey
Copy link
Collaborator

DDtKey commented Oct 10, 2024

Yes, actually it should work if your runners are properly configured and don't store the state between different jobs. E.g DnD approach will work

@the-wondersmith
Copy link
Contributor

@DDtKey I've actually been working on implementing support for this. Would you accept a PR for the feature?

@DDtKey
Copy link
Collaborator

DDtKey commented Nov 19, 2024

@the-wondersmith I will be happy to review the PR.

Feel free to open 🙂

@the-wondersmith
Copy link
Contributor

@the-wondersmith I will be happy to review the PR.

Feel free to open 🙂

Will do, as soon as I'm able 😁

@the-wondersmith
Copy link
Contributor

@DDtKey Looked over what I had and realized it would probably be better to break it up into two PRs. The implementation I've been working on uses labels on the testcontainers-managed containers to figure out if a container should be reaped at drop or at exit, so support for applying the labels to begin with is a prerequisite but also a pretty useful feature all on its own.

tl;dr I just opened a precursor PR that adds support for labels 😅, once we're able to get that merged I'll follow up with the full reusable feature

@DDtKey
Copy link
Collaborator

DDtKey commented Nov 20, 2024

Actually I'd like to see how exactly the labels are going to be used, before merging this part. For now it's more like black box without justification (don't get me wrong)

I mean this PR has references(in documentation) to some internal usage which does not exist yet and it's harder to review without full context

Also I can provide some guidance or highlight something while having the whole context

We still can merge them as 2 separate PRs if agreed, but you can open second one as soon as you are ready.

Thank you!

@the-wondersmith
Copy link
Contributor

the-wondersmith commented Nov 20, 2024

... this PR has references(in documentation) to some internal usage which does not exist yet

Ah! Apologies for the confusion. The note in the docs is an attempt to make it as clear as possible that the com.testcontainers namespace should be treated as reserved for internal use by the testcontainers crate, and therefore users shouldn't try to use labels like com.testcontainers.my-label. Nothing is actually using anything in the com.testcontainers namespace, the idea is just to establish the "standard" or pattern, so to speak, that nothing in the testcontainers crate itself should / will use any label outside of the com.testcontainers namespace with the exception of the managed-by label.

If you look at the code, you'll see that the managed-by: testcontainers label is unconditionally applied to all containers started by testcontainers, and it's explicitly done in a way that it cannot be overridden by user code. The idea is that future features or even expansions / refactors of existing ones will be able to use labels to determine appropriate behavior by doing simple checks like:

// ...

if labels.get("managed-by").is_some_and(|value| value == "testcontainers")
   && labels.get("com.testcontainers.some-feature").is_some_and(|value| value == "true") {
  // feature code
}

// ...

Its uh... similar to Kubernetes' well-known labels concept 😅.

... I'd like to see how exactly the labels are going to be used, before merging this part.

That's totally fair.

We still can merge them as 2 separate PRs if agreed, ...

Of course, naturally happy to do it in whatever way works best 😁. Thank you for taking the time to look it over! I'll have the next PR up as soon as time allows

@DDtKey
Copy link
Collaborator

DDtKey commented Nov 20, 2024

Yeah, thank you for the detailed answer!
I understand possible benefits of labels, just wanted to see the whole picture.
The labels feature itself is most likely will be merged, but it's much better to see how they are coupled with each other first.

This way we can decide what the convention should be used and if it's aligned with other implementations of testcontainers (in other languages, or at least doesn't contradict with them). Labels also can be helpful for introduction of ryuk (#577)

@the-wondersmith
Copy link
Contributor

the-wondersmith commented Nov 22, 2024

@DDtKey Apologies for the delay (life and all 😅). The follow up PR for a working implementation of reusable containers is up.

So far as I'm able to tell, I've maintained behavioral parity with the implementation(s) in other languages. Originally, I'd included global reaping behavior as well that used ctor to clean up containers at the end of a test session. I stripped that out though, as I kinda realized it muddied things up too much for a solid single-feature PR review.

Anyway, eager for your thoughts, whenever you have time 😁

@DDtKey
Copy link
Collaborator

DDtKey commented Nov 23, 2024

@the-wondersmith thank you! I'll review as soon as I can.

There is no a goal to clean reusable containers, at least for now. Because it's pretty complicated, some users may want them to be alive for a long time and reuse once per hour for example.

Also, what I thinking about is to allow controlling this behavior via ENV variable, it's common case that I don't want to change code, but run them in reusable mode only on my machine.

@the-wondersmith
Copy link
Contributor

@the-wondersmith thank you! I'll review as soon as I can.

There is no a goal to clean reusable containers, at least for now. Because it's pretty complicated, some users may want them to be alive for a long time and reuse once per hour for example.

Also, what I thinking about is to allow controlling this behavior via ENV variable, it's common case that I don't want to change code, but run them in reusable mode only on my machine.

Yeah, that's kinda what I figured as well. I hadn't considered that that part though. Adding env var based control shouldn't be hard at all - just pull the value from env instead of hard-coding false where it's set by default 🤔.

I'll add it shortly, if you like 😁

@the-wondersmith
Copy link
Contributor

@DDtKey looked at adding the env control for reusability behavior, and it looks like it might be more complicated than I originally thought? The relatively simple way I outlined above absolutely will work, but it's kinda "quick and dirty" by comparison with the existing paradigm. I did get it working properly such that it uses Config as the source of the default reuse flag value, but it ended up involving a kinda ugly async -> sync -> async section that I really don't love at all.

IMO, the best way forward would be to get the labels feature reviewed and merged, then the current implementation of reusable reviewed and merged, but the review process for reusable should also include identification of followup work that needs doing (for which I'll volunteer by default).

Thoughts?

@DDtKey
Copy link
Collaborator

DDtKey commented Nov 26, 2024

Yes, that totally make sense to implement ENV variable control separately in follow up PR

@the-wondersmith
Copy link
Contributor

@DDtKey Don't want to accidentally come off as pushy, but I do want to push forward on the reusable containers feature if possible. I know Thanksgiving in the U.S. completely wrecked my working week last week personally, and I assume I'm not the only one (especially with the other end-of-year holidays fast approaching), so no progress here as-of-yet is completely understandable 😅.

tl;dr without overstepping or accidentally becoming abrasive, is there anything that needs doing / that I can do to help push forward on this? FTR, "just wait" is totally acceptable, I just figured I'd check is all 🙂

@DDtKey
Copy link
Collaborator

DDtKey commented Dec 2, 2024

I'll review the PRs this week, sorry for the delay 🙏

@the-wondersmith
Copy link
Contributor

I'll review the PRs this week, sorry for the delay 🙏

Please don't apologize! We've all got lives, and PR review is hardly at the top of anyone's list 😅. Thank you for doing it at all 😁

DDtKey pushed a commit that referenced this issue Dec 6, 2024
PR adds support for applying user-configured labels to containers
started by `testcontainers`.

> [!NOTE]
> This PR is a precursor to future support of [reusable
containers](#742)
@the-wondersmith
Copy link
Contributor

@tisonkun reusable containers was officially merged this morning 😁. If you can, please give it a whirl and report back with any thoughts?

For the sake of transparency, there is still planned follow-up work on the feature (e.g. env var control). Still though, feedback would be great 🙂

@tisonkun
Copy link
Author

@the-wondersmith Cool! Thanks everyone helps in this thread.

I'll test this feature in this week.

@the-wondersmith
Copy link
Contributor

@DDtKey where did we end up on the remaining / follow-up work? i.e. what's left to do, what can I (potentially) start on?

@DDtKey
Copy link
Collaborator

DDtKey commented Dec 18, 2024

Thanks one more time @the-wondersmith for implementing this feature! 🙏

I think follow-ups were mentioned here:

#757 (review)

  • env-variable control
  • proper reaper for "current session" (ryuk)

@the-wondersmith
Copy link
Contributor

Thanks one more time @the-wondersmith for implementing this feature! 🙏

It was absolutely my pleasure 😁

I think follow-ups were mentioned here:

#757 (review)

  • env-variable control
  • proper reaper for "current session" (ryuk)

Where's the best place to discuss these? Should we continue here, or maybe open a separate tracking issue?

@DDtKey
Copy link
Collaborator

DDtKey commented Dec 19, 2024

I think it's better to create (follow-up/sub)-issues and discuss them separately

Because basic implementation has been provided already

@the-wondersmith
Copy link
Contributor

I think it's better to create (follow-up/sub)-issues and discuss them separately

Because basic implementation has been provided already

Agreed. Spun up an issue here. On re-read though, maybe I should have made 2? Look at the new one and let me know - I'll spin the ryuk discussion to its own issue if that'd be easier / better.

@DDtKey DDtKey closed this as completed Dec 19, 2024
@tisonkun
Copy link
Author

I tried at tisonkun/morax#19

@the-wondersmith it seems not work. See the PR for reproduce and fail logs.

@tisonkun
Copy link
Author

Also, there is a lack of documents about ReuseDirective. I don't know the different between CurrentSession and Always clearly.

@DDtKey DDtKey reopened this Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants