feat: provide container lifecycle hooks #1036
Merged
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