-
-
Notifications
You must be signed in to change notification settings - Fork 547
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
feat: add WithReuseByName
for modifying Generic Container Requests
#3064
Conversation
GenericContainerRequest
✅ Deploy Preview for testcontainers-go ready!
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 { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
WithReuse
and WithContainerName
for modifying Generic Container RequestsWithReuse
and WithContainerName
for modifying Generic Container Requests
* main: security(compose): upgrade github.com/docker/compose/v2 to fix security vulnerability (testcontainers#3095)
@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? 🙏 |
can do! will add it this week once I have some free time |
* 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)
There was a problem hiding this 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.
WithReuse
and WithContainerName
for modifying Generic Container RequestsWithReuseByName
for modifying Generic Container Requests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
* 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)
* 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) ...
* 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) ...
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 thepostgres.Run
function you cannot set these fields.Related issues
Closes #2726
How to test this PR
go mod tidy
watch -n 1 docker ps
go test ./...
, the test will sleep for 10 seconds to see the container is reused)watch
command see only one containerpg-test
come up and then go away when ryuk cleans it up (alternatively you can just rundocker ps
repeatedly to see)Added a test demonstrating how the Ollama container can be reused