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: expose functions for resource clean up in tests and examples #2738

Merged
merged 1 commit into from
Sep 12, 2024

Conversation

stevenh
Copy link
Collaborator

@stevenh stevenh commented Aug 19, 2024

Ensure that all resources are cleaned up for tests and examples even if they fail.

This leverages new helpers in testcontainers:

  • TerminateContainer for examples
  • CleanupContainer and CleanupNetwork for tests

These are required ensuring that containers that are created but fail in later actions are returned alongside the error so that clean up can be performed.

Consistently clean up created networks using a new context to ensure that the removal gets run even if original context has timed out or been cancelled.

Use fmt.Print instead of log.Fatal to ensure that defers are run in all examples again ensuring that clean up is processed.

Call Stop from Terminate to ensure that child containers are shutdown correctly on clean up as the hard coded timeout using by ContainerRemove is too short to allow this to happen correctly.

Clean up of test logic replacing manual checks and asserts with require to make them more concise and hence easier to understand.

Quiet test output by either capturing or disabling output so it's easier to identify issues when tests are run in non verbose mode.

Clarify source of errors with wrapping and update tests to handle.

Ensure that port forwarding container is shutdown if an error occurs during setup so it isn't orphaned.

Shutdown the port forwarding container on both stop and terminate to prevent it being orphaned when the Stop is used.

Add missing error checks to tests.

Remove unused nolint directives and enable the nolintlint to catch any regressions.

@stevenh stevenh requested a review from a team as a code owner August 19, 2024 15:49
@stevenh stevenh marked this pull request as draft August 19, 2024 15:49
Copy link

netlify bot commented Aug 19, 2024

Deploy Preview for testcontainers-go ready!

Name Link
🔨 Latest commit e04735e
🔍 Latest deploy log https://app.netlify.com/sites/testcontainers-go/deploys/66e1d172d88c0c0008ebbd43
😎 Deploy Preview https://deploy-preview-2738--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 configuration.

@stevenh
Copy link
Collaborator Author

stevenh commented Aug 20, 2024

@mdelapenya sorry about the size of this one, no easy way to get around it. A good amount of the files are just go.mod / go.sum changes, but will still be a long review I'm afraid.

@stevenh stevenh marked this pull request as ready for review August 20, 2024 08:54
Copy link
Collaborator

@mdelapenya mdelapenya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work, changes consistently applied across the codebase. Thanks for making reviews that easy 🙇

I added a few minor comments, but LGTM. I will basically accept the PR, just want some clarification on those points.

docker.go Outdated Show resolved Hide resolved
docker_auth_test.go Show resolved Hide resolved
docker_test.go Outdated Show resolved Hide resolved
docker_test.go Outdated Show resolved Hide resolved
docs/features/common_functional_options.md Outdated Show resolved Hide resolved
modules/pulsar/pulsar_test.go Show resolved Hide resolved
modules/registry/examples_test.go Show resolved Hide resolved
wait/host_port.go Outdated Show resolved Hide resolved
wait/host_port.go Outdated Show resolved Hide resolved
wait/host_port.go Outdated Show resolved Hide resolved
@mdelapenya
Copy link
Collaborator

BTW, I've resolved the conflicts manually

@mdelapenya
Copy link
Collaborator

mdelapenya commented Sep 10, 2024

One last thing the matchFile function is affected by make lint. Could you include it in your changes?

Never mind: I updated it

@mdelapenya
Copy link
Collaborator

@stevenh there is one error in a testable example for the registry module. I think we messed up something in the fmt prints.

@stevenh
Copy link
Collaborator Author

stevenh commented Sep 10, 2024

@stevenh there is one error in a testable example for the registry module. I think we messed up something in the fmt prints.

Just ran the tests locally, no errors, will keep an eye on the CI.

go test -timeout 10m -coverprofile=/tmp/vscode-goDlE8R3/go-code-cover github.com/testcontainers/testcontainers-go/modules/registry -v -count=1

=== RUN   TestRegistry_unauthenticated
2024/09/10 22:32:28 github.com/testcontainers/testcontainers-go - Connected to docker:
  Server Version: 27.1.1
  API Version: 1.46
  Operating System: Docker Desktop
  Total Memory: 31947 MB

...

2024/09/10 22:32:41 🚫 Container terminated: e46301df078f
--- PASS: ExampleRun_pushImage (3.84s)
PASS
coverage: 79.7% of statements
ok  	github.com/testcontainers/testcontainers-go/modules/registry	12.630s	coverage: 79.7% of statements

@stevenh
Copy link
Collaborator Author

stevenh commented Sep 10, 2024

thanks for a very through review on such a big PR @mdelapenya.

I've made 90% of the changes, resolving the comments that match, there's a few questions outstanding, but getting close.

@mdelapenya mdelapenya added the chore Changes that do not impact the existing functionality label Sep 11, 2024
@mdelapenya mdelapenya self-assigned this Sep 11, 2024
@mdelapenya mdelapenya changed the title fix: resource clean up for tests and examples feat: expose functions for resource clean up in tests and examples Sep 11, 2024
@mdelapenya mdelapenya added enhancement New feature or request and removed chore Changes that do not impact the existing functionality labels Sep 11, 2024
@mdelapenya
Copy link
Collaborator

@stevenh I'm marking this PR as an enhancement, as we are exposing new API functions for the cleanup, that are being used everywhere in the codebase. Therefore users of the project will be notified in the release notes about this new available feature. I changed the title in consequence.

Ensure that all resources are cleaned up for tests and examples even
if they fail.

This leverages new helpers in testcontainers:
* TerminateContainer for examples
* CleanupContainer and CleanupNetwork for tests

These are required ensuring that containers that are created but fail
in later actions are returned alongside the error so that clean up can
be performed.

Consistently clean up created networks using a new context to ensure
that the removal gets run even if original context has timed out or
been cancelled.

Use fmt.Print instead of log.Fatal to ensure that defers are run in
all examples again ensuring that clean up is processed.

Call Stop from Terminate to ensure that child containers are shutdown
correctly on clean up as the hard coded timeout using by ContainerRemove
is too short to allow this to happen correctly.

Clean up of test logic replacing manual checks and asserts with require
to make them more concise and hence easier to understand.

Quiet test output by either capturing or disabling output so it's easier
to identify issues when tests are run in non verbose mode.

Clarify source of errors with wrapping and update tests to handle.

Ensure that port forwarding container is shutdown if an error occurs
during setup so it isn't orphaned.

Shutdown the port forwarding container on both stop and terminate to
prevent it being orphaned when the Stop is used.

Add missing error checks to tests.

Remove unused nolint directives and enable the nolintlint to catch any
regressions.

Don't use container as a variable as its overused.
@stevenh
Copy link
Collaborator Author

stevenh commented Sep 11, 2024

@mdelapenya I think this should be good, just two open discussion points which I don't think are blockers.

Copy link
Collaborator

@mdelapenya mdelapenya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, great PR 🙇

@mdelapenya mdelapenya merged commit b60497e into testcontainers:main Sep 12, 2024
112 checks passed
@stevenh stevenh deleted the fix/resource-cleanup branch September 12, 2024 11:04
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

Successfully merging this pull request may close these issues.

2 participants