Skip to content

feat: add WithReuseByName for modifying Generic Container Requests #3064

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

Conversation

jm96441n
Copy link
Contributor

@jm96441n jm96441n commented Mar 28, 2025

What does this PR do?

This adds one functional options WithReuseByName

Why is it important?

When creating a GenericContainerRequest you can easily set these fields, but when using something like the postgres.Run function you cannot set these fields.

Related issues

Closes #2726

How to test this PR

  • Run the tests
  • You can use the following gist: https://gist.github.com/jm96441n/74b6fa9b621cdd0c8d6548a6d2abd6d0
    • Clone down the gist
    • run go mod tidy
    • in one terminal window run watch -n 1 docker ps
    • in one terminal window run go test ./..., the test will sleep for 10 seconds to see the container is reused)
    • in the terminal running the watch command see only one container pg-test come up and then go away when ryuk cleans it up (alternatively you can just run docker ps repeatedly to see)

Added a test demonstrating how the Ollama container can be reused

@jm96441n jm96441n requested a review from a team as a code owner March 28, 2025 15:26
Copy link

netlify bot commented Mar 28, 2025

Deploy Preview for testcontainers-go ready!

Name Link
🔨 Latest commit c557cff
🔍 Latest deploy log https://app.netlify.com/sites/testcontainers-go/deploys/680a449d5f536b00083a490d
😎 Deploy Preview https://deploy-preview-3064--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.

options.go Outdated
@@ -96,6 +96,25 @@ func WithHostPortAccess(ports ...int) CustomizeRequestOption {
}
}

// WithReuse will mark a container to be reused if it exists or create a new one if it doesn't.
// It is used in conjunction with WithContainerName to ensure that the container is reused.
func WithReuse() CustomizeRequestOption {
Copy link
Member

Choose a reason for hiding this comment

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

suggestion: given reuse needs a container name, wdyt if we pass the container name in here, to simplify the API?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was debating that, I went with this because of how WithUsername and WithPassword in the postgres module are defined and wanted to keep it somewhat similar but I'm not super tied to the current implementation
https://pkg.go.dev/github.com/testcontainers/testcontainers-go/modules/postgres#WithUsername

Copy link
Contributor Author

Choose a reason for hiding this comment

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

went with your suggestion and simplified the API surface

Copy link
Member

@mdelapenya mdelapenya Mar 28, 2025

Choose a reason for hiding this comment

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

Thanks! In general, we discourage the usage of container names. Actually, for reuse, we have an ongoing PR that improves the reusability conditions hashing the container request: if it did not change, reuse that container: #2768

For that reason I'd advocate against passing a single name, but for reuse, as a requirement, I consider they must go together

Copy link
Contributor Author

Choose a reason for hiding this comment

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

when #2768 lands would it make sense to remove the name are here entirely?

Copy link
Member

Choose a reason for hiding this comment

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

Let's rename the option to WithReuseByName to give us room when the other PR lands.

I'm doing it myself adding commits on top of yours

@jm96441n jm96441n changed the title feat: Add WithReuse and WithContainerName for modifying Generic Container Requests feat: add WithReuse and WithContainerName for modifying Generic Container Requests Mar 28, 2025
@mdelapenya
Copy link
Member

@jm96441n I've added #3070 as a complementary PR to this one. You made me realise we are missing options that are needed at the modules side. Thanks for that!

I think this is good to go, will start a more in depth review asap.

@mdelapenya
Copy link
Member

@jm96441n we merged the PR with all the options, and took the chance to help you with this PR adding the docs for the new option added in here. I'd like you to add another test for the containers being actually reused, as we are including tests for the options, only. Could you please add it? 🙏

@jm96441n
Copy link
Contributor Author

can do! will add it this week once I have some free time

jm96441n and others added 4 commits April 15, 2025 14:38
* main:
  feat(postgres): add WithOrderedInitScripts for Postgres testcontainers (testcontainers#3121)
  feat(redis): add TLS support (testcontainers#3115)
  feat: add Docker Model Runner module (testcontainers#3106)
  feat: add toxiproxy module (testcontainers#3092)
  chore(ci): run core tests on Testcontainers Cloud (testcontainers#3117)
  deps(aerospike): replace core module in go.mod (testcontainers#3116)
  chore(deps): bump golang.org/x/net from 0.36.0 to 0.38.0 in /modules and /examples (testcontainers#3114)
  chore(ci): revert codeql improvements for CI resiliency (testcontainers#3112)
  docs(socat): add missing version marker for new options (testcontainers#3111)
Copy link
Contributor

@stevenh stevenh left a comment

Choose a reason for hiding this comment

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

One tweak needed for the test and don't forget to change the details in the PR as its not WithReuse anymore and no WithContainerName either.

@mdelapenya mdelapenya changed the title feat: add WithReuse and WithContainerName for modifying Generic Container Requests feat: add WithReuseByName for modifying Generic Container Requests Apr 24, 2025
@mdelapenya mdelapenya requested a review from stevenh April 24, 2025 17:25
Copy link
Contributor

@stevenh stevenh left a comment

Choose a reason for hiding this comment

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

LGTM

@mdelapenya mdelapenya merged commit 8a498b7 into testcontainers:main Apr 24, 2025
390 of 392 checks passed
mdelapenya added a commit to georgespalding/testcontainers-go that referenced this pull request Apr 25, 2025
* main:
  chore(ci): close PR if it was sent from main (testcontainers#3123)
  feat: add `WithReuseByName` for modifying Generic Container Requests (testcontainers#3064)
  chore(deps): bump github/codeql-action from 3.28.15 to 3.28.16 (testcontainers#3120)
@mdelapenya mdelapenya self-assigned this Apr 25, 2025
@mdelapenya mdelapenya added the feature New functionality or new behaviors on the existing one label Apr 25, 2025
mdelapenya added a commit to waroir20/testcontainers-go that referenced this pull request May 7, 2025
* main: (76 commits)
  chore(deps): bump mkdocs-include-markdown-plugin from 6.2.2 to 7.1.5 (testcontainers#3137)
  chore(deps): bump github.com/shirou/gopsutil/v4 from 4.25.1 to 4.25.4 (testcontainers#3133)
  chore(deps): bump github.com/docker/docker from 28.0.1+incompatible to 28.1.1+incompatible (testcontainers#3152)
  feat(memcached): add memcached module (testcontainers#3132)
  fix(etcd): single node etcd cluster access (testcontainers#3149)
  feat(valkey): add TLS support for Valkey (testcontainers#3131)
  fix(dockermodelrunner): wait for the model to be pulled (testcontainers#3125)
  fix(localstack): remove checksum before parsing version (testcontainers#3130)
  fix(dockermodelrunner): dependency with socat
  chore: prepare for next minor development cycle (0.38.0)
  chore: use new version (v0.37.0) in modules and examples
  fix: handle stopped containers more gracefully when reuse is enabled (testcontainers#3062)
  feat(gcloud): add option to run firestore in datastore mode (testcontainers#3009)
  feat: support for mounting images (testcontainers#3044)
  chore(ci): close PR if it was sent from main (testcontainers#3123)
  feat: add `WithReuseByName` for modifying Generic Container Requests (testcontainers#3064)
  chore(deps): bump github/codeql-action from 3.28.15 to 3.28.16 (testcontainers#3120)
  chore(deps): bump mkdocs-include-markdown-plugin from 6.2.2 to 7.1.5 (testcontainers#3119)
  chore(deps): bump github.com/magiconair/properties from 1.8.9 to 1.8.10 (testcontainers#3118)
  chore(ci): exclude more files for a full-blown build (testcontainers#3122)
  ...
mdelapenya added a commit to CTrando/testcontainers-go that referenced this pull request May 7, 2025
* main: (302 commits)
  chore(deps): bump mkdocs-include-markdown-plugin from 6.2.2 to 7.1.5 (testcontainers#3137)
  chore(deps): bump github.com/shirou/gopsutil/v4 from 4.25.1 to 4.25.4 (testcontainers#3133)
  chore(deps): bump github.com/docker/docker from 28.0.1+incompatible to 28.1.1+incompatible (testcontainers#3152)
  feat(memcached): add memcached module (testcontainers#3132)
  fix(etcd): single node etcd cluster access (testcontainers#3149)
  feat(valkey): add TLS support for Valkey (testcontainers#3131)
  fix(dockermodelrunner): wait for the model to be pulled (testcontainers#3125)
  fix(localstack): remove checksum before parsing version (testcontainers#3130)
  fix(dockermodelrunner): dependency with socat
  chore: prepare for next minor development cycle (0.38.0)
  chore: use new version (v0.37.0) in modules and examples
  fix: handle stopped containers more gracefully when reuse is enabled (testcontainers#3062)
  feat(gcloud): add option to run firestore in datastore mode (testcontainers#3009)
  feat: support for mounting images (testcontainers#3044)
  chore(ci): close PR if it was sent from main (testcontainers#3123)
  feat: add `WithReuseByName` for modifying Generic Container Requests (testcontainers#3064)
  chore(deps): bump github/codeql-action from 3.28.15 to 3.28.16 (testcontainers#3120)
  chore(deps): bump mkdocs-include-markdown-plugin from 6.2.2 to 7.1.5 (testcontainers#3119)
  chore(deps): bump github.com/magiconair/properties from 1.8.9 to 1.8.10 (testcontainers#3118)
  chore(ci): exclude more files for a full-blown build (testcontainers#3122)
  ...
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.

[Enhancement]: Reuse flag for module Postgres
3 participants