feat: expose functions for resource clean up in tests and examples by stevenh · Pull Request #2738 · testcontainers/testcontainers-go

This was referenced

Aug 19, 2024

@stevenh stevenh marked this pull request as ready for review

August 20, 2024 08:54

mdelapenya

@mdelapenya mdelapenya added the chore

Changes that do not impact the existing functionality

label

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

@stevenh

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.

mdelapenya

@stevenh stevenh deleted the fix/resource-cleanup branch

September 12, 2024 11:04

mdelapenya added a commit that referenced this pull request

Sep 13, 2024
* main:
  fix: resource clean up for tests and examples (#2738)

mdelapenya added a commit to mdelapenya/testcontainers-go that referenced this pull request

Sep 18, 2024

mdelapenya added a commit to mdelapenya/testcontainers-go that referenced this pull request

Sep 23, 2024

mdelapenya added a commit to mdelapenya/testcontainers-go that referenced this pull request

Sep 23, 2024

mdelapenya added a commit that referenced this pull request

Sep 26, 2024
* main: (29 commits)
  fix: template for code generation (#2800)
  fix: update module path (#2797)
  fix: container logging deadlocks (#2791)
  chore: use a much smaller image for testing (#2795)
  fix: parallel containers clean race (#2790)
  fix(registry): wait for (#2793)
  fix: container timeout test (#2792)
  docs: document redpanda options (#2789)
  feat: support databend module (#2779)
  chore: golangci-lint 1.61.0 (#2787)
  fix(mssql): bump Docker image version (#2786)
  fix: handle 127 error code for podman compatibility (#2778)
  fix: do not override ImageBuildOptions.Labels when building from a Dockerfile (#2775)
  feat(mongodb): Wait for mongodb module with a replicaset to finish (#2777)
  fix(postgres): Apply default snapshot name if no name specified (#2783)
  fix: resource clean up for tests and examples (#2738)
  ci: add generate for mocks (#2774)
  fix: docker config error handling when config file does not exist (#2772)
  docs: refine heading badges in README (#2770)
  feat(wait): for file (#2731)
  ...

ttruongatl pushed a commit to ttruongatl/testcontainers-go that referenced this pull request

Feb 15, 2025
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.