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

Merged
merged 5 commits into from
Apr 5, 2023

Conversation

mdelapenya
Copy link
Collaborator

@mdelapenya mdelapenya commented Apr 5, 2023

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:

  1. Creating, executed right before the container is actually created by the Docker engine
  2. Created, executed right after the container is created by the Docker engine
  3. Starting, executed right before the container enters the Running state
  4. Started, executed right after the container enters the Running state
  5. Stopping, executed right before the container is stopped
  6. Stopped, executed right after the container is stopper
  7. Terminating, executed right before the container is terminated, removed from the Docker engine
  8. Terminated, executed right after the container is terminated

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:

  1. the container request for the Creating hook, as the representation of the container type does not exist at this moment.
  2. the container for the rest of the hooks, as the representation of the container type has just been created.

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

@mdelapenya mdelapenya requested a review from a team as a code owner April 5, 2023 08:02
@mdelapenya mdelapenya added the feature New functionality or new behaviors on the existing one label Apr 5, 2023
@mdelapenya mdelapenya self-assigned this Apr 5, 2023
@netlify
Copy link

netlify bot commented Apr 5, 2023

Deploy Preview for testcontainers-go ready!

Name Link
🔨 Latest commit 91dce02
🔍 Latest deploy log https://app.netlify.com/sites/testcontainers-go/deploys/642d4dd9051df90008ff8c38
😎 Deploy Preview https://deploy-preview-1036--testcontainers-go.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@sonarcloud
Copy link

sonarcloud bot commented Apr 5, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@mdelapenya mdelapenya merged commit 13da621 into testcontainers:main Apr 5, 2023
53 checks passed
@mdelapenya mdelapenya deleted the container-lifecycle branch April 5, 2023 11:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality or new behaviors on the existing one
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant