-
-
Notifications
You must be signed in to change notification settings - Fork 438
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: provide container lifecycle hooks #1036
Merged
mdelapenya
merged 5 commits into
testcontainers:main
from
mdelapenya:container-lifecycle
Apr 5, 2023
Merged
feat: provide container lifecycle hooks #1036
mdelapenya
merged 5 commits into
testcontainers:main
from
mdelapenya:container-lifecycle
Apr 5, 2023
Conversation
This file contains 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
mdelapenya
added
the
feature
New functionality or new behaviors on the existing one
label
Apr 5, 2023
mdelapenya
requested review from
cristianrgreco,
eddumelendez,
HofmeisterAn and
kiview
April 5, 2023 08:02
✅ Deploy Preview for testcontainers-go ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
mdelapenya
commented
Apr 5, 2023
mdelapenya
commented
Apr 5, 2023
mdelapenya
commented
Apr 5, 2023
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
This was referenced Apr 5, 2023
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.
What does this PR do?
This PR opens the gates to the extension of a container, defining lifecycle hooks that can be leveraged by the consumers of the library to extend how a container is defined.
We have defined 8 lifecycle hooks:
For that, we are defining two types of hooks: one getting access to the container request (which defines the container), and the second getting access to the running container created by the aforementioned request.
Each hook will be backed by an array of functions that returns an error and receive the Go context as first argument plus a second argument:
Creating
hook, as the representation of the container type does not exist at this moment.If a hook returns an error, then the lifecycle will be stopped at that moment. For that reason, I'm not using
defer
to execute the Post hooks.To add the hooks, we have defined a
LifecycleHooks
fiels in the ContainerRequest struct, so that it's possible to define how the container will behave when requesting a container.With this implementation users would be able to define as many functions as they need to customize the container, e.g. passing an array of functions to the PreCreates field of the lifecycle, and/or an array of functions to the PostStarts field of the lifecycle. I've provided with a test demonstrating how it's possible to leverage the lifecycle: the tests adds a "log entry" everytime the life cycle hooks are called, and we assert that the life cycle is honoured (creating > created > starting > started > stopping > stopped > starting > started > terminating > terminated)
Finally, the docs have been updated to include the lifecycle hooks explanation in the "Create Container" section. Render URL here: https://deploy-preview-1036--testcontainers-go.netlify.app/features/creating_container/#lifecycle-hooks
Why is it important?
As a user of the library creating a container, I want to be able to execute code just before/after the container is created/started/stopped/terminated.
Follow-ups
I'd like to identify all code that happens out of the lifecycle hooks defined here but acting as a hook, wrapping them into hooks that will be part of the lifecycle