Skip to content

Conversation

@mdelapenya
Copy link
Member

  • chore: use Run in log consumer tests
  • chore: use Run in reaper tests
  • chore: use Run in sshd container
  • chore: use Run in wait strategies
  • chore: use Run in reuse

What does this PR do?

This PR uses the Run function instead of GenericContainer in a reduced list of places:

  • reaper tests
  • log consumer tests
  • the sshd container creation for HostPort access
  • wait strategies
  • reused containers

Why is it important?

Continue with the migration from GenericContainer to Run

Related issues

@mdelapenya mdelapenya requested a review from a team as a code owner September 25, 2025 14:40
@mdelapenya mdelapenya added the chore Changes that do not impact the existing functionality label Sep 25, 2025
@mdelapenya mdelapenya self-assigned this Sep 25, 2025
@mdelapenya mdelapenya added the chore Changes that do not impact the existing functionality label Sep 25, 2025
@mdelapenya mdelapenya requested a review from stevenh September 25, 2025 14:40
@netlify
Copy link

netlify bot commented Sep 25, 2025

Deploy Preview for testcontainers-go ready!

Name Link
🔨 Latest commit 4779ce1
🔍 Latest deploy log https://app.netlify.com/projects/testcontainers-go/deploys/68d5693c880f840008f425d8
😎 Deploy Preview https://deploy-preview-3305--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 project configuration.

@coderabbitai
Copy link

coderabbitai bot commented Sep 25, 2025

Summary by CodeRabbit

  • New Features
    • Added container reuse by name via a new public option.
  • Refactor
    • Migrated container creation to a Run-based API with functional options across the codebase.
  • Documentation
    • Updated guides for creating containers and following logs to reflect the Run API and option-based configuration, including log consumer setup.
  • Tests
    • Updated tests to use the Run API and functional options, with consistent timeouts and path handling.

Walkthrough

Migrates tests and docs from GenericContainer/ContainerRequest to a new Run(ctx, image, opts...) functional-options API, introduces WithReuseByName, updates log consumer configuration to WithLogConsumerConfig, and refactors code to build option slices prior to calling Run.

Changes

Cohort / File(s) Summary of changes
Docs: Run API & logs
docs/features/creating_container.md, docs/features/follow_logs.md
Replace ContainerRequest examples with Run(..., opts...). Document and demonstrate WithReuseByName and WithLogConsumerConfig; remove prior Reuse semantics.
Tests: general Run migration
logconsumer_test.go, reaper_test.go, reuse_test.go, wait/exec_test.go, wait/file_test.go, wait/http_test.go, wait/tls_test.go
Replace GenericContainer/ContainerRequest usage with Run(ctx, image, opts...). Use functional options (WithExposedPorts, WithWaitStrategy, WithEntrypoint, WithDockerfile, WithLogConsumerConfig, WithReuseByName, etc.). Update cleanup to use returned container handle and adjust test setup paths to filepath.Join.
Port forwarding & runtime callers
port_forwarding.go
Refactor newSshdContainer to build options (moduleOpts) and call Run with those options; replace inline request construction and adjust error text.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Dev as Developer / Test
  participant RunAPI as Run(ctx, image, opts...)
  participant Opts as Option Builders
  participant Engine as Container Engine

  Dev->>RunAPI: Run(ctx, image, WithExposedPorts..., WithWaitStrategy..., WithReuseByName?)
  RunAPI->>Opts: Apply and compose options
  Note over RunAPI,Opts: Build final container configuration
  alt WithReuseByName provided
    RunAPI->>Engine: Lookup container by name
    alt Found
      Engine-->>RunAPI: Existing container handle
      RunAPI-->>Dev: Return reused container
    else Not found
      RunAPI->>Engine: Create & start new container
      Engine-->>RunAPI: New container handle
      RunAPI-->>Dev: Return new container
    end
  else No reuse option
    RunAPI->>Engine: Create & start new container
    Engine-->>RunAPI: New container handle
    RunAPI-->>Dev: Return new container
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • stevenh

Poem

I hopped from Request to Run with glee,
Options in my pouch — tidy as can be.
Reuse by name, logs snug in a row,
Ports exposed, waits set to go.
A rabbit's cheer for code that flows! 🐇

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title Check ✅ Passed The title clearly and concisely summarizes the primary change of migrating to the Run function as part of a chore update and indicates this is the second part of that effort, directly reflecting the main purpose of the pull request.
Description Check ✅ Passed The description clearly describes the use of the Run function in various modules and tests, relates directly to the changeset, and provides context for the migration effort without deviating from the content of the pull request.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
port_forwarding.go (1)

95-99: Guard TerminateContainer when sshdContainer is nil on error path

If newSshdContainer fails before assigning sshdContainer, passing a typed-nil to an interface can still be non-nil and cause a panic when Terminate is invoked. Add a nil check.

Apply this diff:

 defer func() {
-    if err != nil {
-        err = errors.Join(err, TerminateContainer(sshdContainer))
-    }
+    if err != nil && sshdContainer != nil {
+        err = errors.Join(err, TerminateContainer(sshdContainer))
+    }
 }()
🧹 Nitpick comments (3)
docs/features/follow_logs.md (3)

29-30: Clarify Run-based usage in the preferred approach sentence

Suggest minor wording to explicitly reference Run(...) and tighten phrasing.

-This will represent the current way for associating `LogConsumer`s. You simply define your consumers, and attach them as a slice using the `WithLogConsumerConfig` functional option.
+This is the preferred way to associate `LogConsumer`s when using `Run(...)`. Define your consumers and pass them via the `WithLogConsumerConfig` option.

79-79: Fix typo: StartLogProducer

“StarLogProducer” → “StartLogProducer”.

-c.FollowOutput(&g) // must be called before StarLogProducer
+c.FollowOutput(&g) // must be called before StartLogProducer

131-133: Align StartLogProducer usage with option pattern

Elsewhere you document LogProductionOption and WithLogProductionTimeout; mirror that here.

-				startErr := container.StartLogProducer(ctx, timeout)
+				startErr := container.StartLogProducer(ctx, WithLogProductionTimeout(timeout))
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 21b248e and d7bce9d.

📒 Files selected for processing (10)
  • docs/features/creating_container.md (3 hunks)
  • docs/features/follow_logs.md (1 hunks)
  • logconsumer_test.go (13 hunks)
  • port_forwarding.go (1 hunks)
  • reaper_test.go (3 hunks)
  • reuse_test.go (4 hunks)
  • wait/exec_test.go (2 hunks)
  • wait/file_test.go (1 hunks)
  • wait/http_test.go (9 hunks)
  • wait/tls_test.go (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (10)
docs/features/follow_logs.md (4)
options.go (1)
  • WithLogConsumerConfig (280-285)
docker.go (2)
  • c (444-450)
  • c (428-433)
options_test.go (2)
  • TestWithLogConsumerConfig (103-138)
  • TestWithLogConsumers (80-101)
modules/pulsar/pulsar.go (1)
  • Container (33-36)
reaper_test.go (2)
generic.go (1)
  • Run (122-149)
options.go (1)
  • WithExposedPorts (464-469)
wait/tls_test.go (4)
generic.go (1)
  • Run (122-149)
options.go (2)
  • WithDockerfile (47-53)
  • WithWaitStrategy (376-378)
container.go (2)
  • FromDockerfile (90-108)
  • FromDockerfile (131-171)
docker_auth_test.go (1)
  • TestBuildContainerFromDockerfile (160-173)
wait/http_test.go (5)
generic.go (1)
  • Run (122-149)
options.go (4)
  • WithExposedPorts (464-469)
  • WithWaitStrategy (376-378)
  • WithDockerfile (47-53)
  • ContainerCustomizer (22-24)
wait/http.go (1)
  • ForHTTP (149-151)
container.go (1)
  • FromDockerfile (90-108)
options_test.go (1)
  • TestWithDockerfile (656-677)
reuse_test.go (3)
options.go (3)
  • ContainerCustomizer (22-24)
  • WithExposedPorts (464-469)
  • WithReuseByName (129-138)
options_test.go (1)
  • TestWithReuseByName_Succeeds (736-747)
modules/ollama/ollama.go (1)
  • RunContainer (71-73)
logconsumer_test.go (4)
options.go (11)
  • ContainerCustomizer (22-24)
  • WithDockerfile (47-53)
  • WithExposedPorts (464-469)
  • WithWaitStrategy (376-378)
  • WithLogConsumerConfig (280-285)
  • WithEnv (75-85)
  • WithHostConfigModifier (88-94)
  • WithCmd (472-477)
  • WithConfigModifier (56-62)
  • WithAlwaysPull (432-437)
  • WithNoStart (120-125)
container.go (1)
  • FromDockerfile (90-108)
generic.go (1)
  • Run (122-149)
docker.go (2)
  • LogProductionOption (747-747)
  • WithLogProductionTimeout (751-755)
wait/exec_test.go (3)
generic.go (1)
  • Run (122-149)
options.go (2)
  • WithEntrypoint (448-453)
  • WithWaitStrategy (376-378)
wait/exec.go (1)
  • ForExec (71-73)
docs/features/creating_container.md (2)
options_test.go (2)
  • TestWithReuseByName_ErrorsWithoutContainerNameProvided (749-759)
  • TestWithReuseByName_Succeeds (736-747)
options.go (1)
  • WithReuseByName (129-138)
port_forwarding.go (3)
options.go (4)
  • ContainerCustomizer (22-24)
  • WithExposedPorts (464-469)
  • WithEnv (75-85)
  • WithWaitStrategy (376-378)
wait/host_port.go (1)
  • ForListeningPort (67-69)
generic.go (1)
  • Run (122-149)
wait/file_test.go (2)
generic.go (1)
  • Run (122-149)
testing.go (1)
  • CleanupContainer (91-97)
🪛 markdownlint-cli2 (0.18.1)
docs/features/creating_container.md

224-224: Hard tabs
Column: 1

(MD010, no-hard-tabs)


225-225: Hard tabs
Column: 1

(MD010, no-hard-tabs)


226-226: Hard tabs
Column: 1

(MD010, no-hard-tabs)


227-227: Hard tabs
Column: 1

(MD010, no-hard-tabs)


228-228: Hard tabs
Column: 1

(MD010, no-hard-tabs)


247-247: Hard tabs
Column: 1

(MD010, no-hard-tabs)


248-248: Hard tabs
Column: 1

(MD010, no-hard-tabs)


249-249: Hard tabs
Column: 1

(MD010, no-hard-tabs)


250-250: Hard tabs
Column: 1

(MD010, no-hard-tabs)


251-251: Hard tabs
Column: 1

(MD010, no-hard-tabs)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (18)
  • GitHub Check: lint (modules/weaviate) / lint: modules/weaviate
  • GitHub Check: lint (modules/openldap) / lint: modules/openldap
  • GitHub Check: lint (modules/scylladb) / lint: modules/scylladb
  • GitHub Check: lint (modules/dind) / lint: modules/dind
  • GitHub Check: lint (modules/dockermcpgateway) / lint: modules/dockermcpgateway
  • GitHub Check: lint (modules/kafka) / lint: modules/kafka
  • GitHub Check: lint (modules/ollama) / lint: modules/ollama
  • GitHub Check: lint (modules/cassandra) / lint: modules/cassandra
  • GitHub Check: lint (modules/milvus) / lint: modules/milvus
  • GitHub Check: lint (modules/pinecone) / lint: modules/pinecone
  • GitHub Check: lint (modules/gcloud) / lint: modules/gcloud
  • GitHub Check: lint (modules/memcached) / lint: modules/memcached
  • GitHub Check: lint (modules/vault) / lint: modules/vault
  • GitHub Check: lint (examples/nginx) / lint: examples/nginx
  • GitHub Check: lint (modules/mockserver) / lint: modules/mockserver
  • GitHub Check: lint (modules/grafana-lgtm) / lint: modules/grafana-lgtm
  • GitHub Check: lint (modules/solace) / lint: modules/solace
  • GitHub Check: Analyze (go)
🔇 Additional comments (13)
wait/tls_test.go (2)

11-11: LGTM: required import added

Importing filepath is necessary for the new docker build context join.


111-116: LGTM: Run + WithDockerfile + WithWaitStrategy migration

The example correctly uses Run with a FromDockerfile context and applies the TLS wait strategy.

Please verify that wait/testdata/http contains a valid Dockerfile so the example build context resolves at runtime.

wait/file_test.go (1)

81-97: LGTM: Run-based setup with FileStrategy

Clean migration to Run with a chained FileStrategy and matcher; CleanupContainer usage matches project pattern.

reaper_test.go (1)

110-113: LGTM: Use Run(...) with exposed ports in reaper tests

Switching to Run keeps semantics (started container) and simplifies setup across start/stop/terminate helpers.

Also applies to: 165-168, 188-191

wait/exec_test.go (2)

24-28: LGTM: Example uses Run + Entrypoint + Exec wait strategy

Concise, correct example demonstrating ExecStrategy with startup timeout.


175-189: LGTM: Test uses Run with Exec wait strategy and custom matchers

The exit code and response matchers are correctly wired via WithWaitStrategy.

port_forwarding.go (1)

179-186: Migration to Run + options looks solid

Good use of WithExposedPorts, WithEnv, and WithWaitStrategy and then delegating to Run. Error wrapping message is clear.

Also applies to: 187-195

reuse_test.go (2)

17-23: Run + WithReuseByName adoption LGTM

Switching to Run with WithExposedPorts and WithReuseByName is consistent with the new API and keeps the test intent intact.


60-63: Double-cleanup of the same reused container can be harmless, but verify idempotency

If the second Run returns a handle to the same container, you register CleanupContainer twice. Ensure CleanupContainer/TerminateContainer are idempotent to avoid noisy errors at test teardown.

logconsumer_test.go (2)

79-92: Run + WithDockerfile + WithLogConsumerConfig migration looks correct

Option set mirrors previous request fields and keeps behavior. Using filepath.Join for testdata is a good portability improvement.


238-246: dind setup via Run is correct

Privileged host config, DOCKER_TLS_CERTDIR unset, port exposure, and wait-for-port are properly captured with options.

wait/http_test.go (2)

34-37: Example migration to Run is accurate

Using WithExposedPorts and WithWaitStrategy maintains previous readiness semantics.


80-96: Option-based build-and-run flow LGTM

Switch to WithDockerfile and complex WithWaitStrategy chain reads cleanly and matches prior behavior.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d7bce9d and 4779ce1.

📒 Files selected for processing (1)
  • docs/features/creating_container.md (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (20)
  • GitHub Check: lint (modules/kafka) / lint: modules/kafka
  • GitHub Check: lint (modules/surrealdb) / lint: modules/surrealdb
  • GitHub Check: lint (modules/redis) / lint: modules/redis
  • GitHub Check: lint (modules/aerospike) / lint: modules/aerospike
  • GitHub Check: lint (modules/azurite) / lint: modules/azurite
  • GitHub Check: lint (modules/mysql) / lint: modules/mysql
  • GitHub Check: lint (modules/scylladb) / lint: modules/scylladb
  • GitHub Check: lint (modules/ollama) / lint: modules/ollama
  • GitHub Check: lint (modules/localstack) / lint: modules/localstack
  • GitHub Check: lint (modules/openldap) / lint: modules/openldap
  • GitHub Check: lint (examples/nginx) / lint: examples/nginx
  • GitHub Check: lint (modules/chroma) / lint: modules/chroma
  • GitHub Check: lint (modules/qdrant) / lint: modules/qdrant
  • GitHub Check: lint (modules/dynamodb) / lint: modules/dynamodb
  • GitHub Check: lint (modules/databend) / lint: modules/databend
  • GitHub Check: lint (modules/grafana-lgtm) / lint: modules/grafana-lgtm
  • GitHub Check: lint (modules/mariadb) / lint: modules/mariadb
  • GitHub Check: lint (modules/openfga) / lint: modules/openfga
  • GitHub Check: lint (modules/gcloud) / lint: modules/gcloud
  • GitHub Check: Analyze (go)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

chore Changes that do not impact the existing functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant