Skip to content

Conversation

@mdelapenya
Copy link
Member

What does this PR do?

It uses the Run function in more places from the core library (no modules yet)

Why is it important?

Consistenty and progress towards the new Run API

Related issues

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

netlify bot commented Sep 26, 2025

Deploy Preview for testcontainers-go ready!

Name Link
🔨 Latest commit f2aac4d
🔍 Latest deploy log https://app.netlify.com/projects/testcontainers-go/deploys/68d684c06af4260007360634
😎 Deploy Preview https://deploy-preview-3309--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 26, 2025

Summary by CodeRabbit

  • Refactor

    • Streamlined container setup to a single Run-based flow across code, improving consistency and readability.
    • Standardized assertions and interface-based access; aligned logging to reference image consistently.
  • Documentation

    • Updated nginx example to use the simplified Run flow and current port lookup method.
  • Tests

    • Added unit tests for startup and after-ready command hooks.
    • Updated existing tests to the new Run pattern and maintained cleanup and assertions.

Walkthrough

Refactors container setup across tests and example to use Run(...) with functional options instead of GenericContainer/ContainerRequest. Updates assertions and image/port accessors accordingly. Adds a new unit test file validating lifecycle hook wiring for startup and after-ready commands.

Changes

Cohort / File(s) Summary
Tests: migrate to Run(...) API
container_test.go, options_test.go
Replace GenericContainer/ContainerRequest with Run(ctx, image, options...). Use WithCmd, WithWaitStrategy, WithImageSubstitutors, WithExposedPorts, WithLogConsumers, WithEntrypoint, WithStartupCommand, WithAfterReadyCommand. Switch some assertions to require. Access image via ctr.Image; avoid concrete DockerContainer type assertions.
Example: nginx migration
examples/nginx/nginx.go
Switch to Run(...) for starting nginx; rename variable to ctr; use ctr.PortEndpoint; return updated nginxContainer constructed from ctr.
New unit tests for lifecycle hooks
options_unit_test.go
Add unit tests verifying WithStartupCommand and WithAfterReadyCommand mutate GenericContainerRequest.LifecycleHooks as expected (one hook; PostStarts or PostReadies action respectively).

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Test as Test/Example
  participant Run as Run(...)
  participant Opts as Options
  participant Engine as Container Runtime
  participant Hooks as Lifecycle Hooks

  Test->>Run: Invoke with image + options
  Run->>Opts: Apply With... (wait, entrypoint, startup/after-ready, logs)
  Run->>Engine: Create & start container
  alt Wait strategy
    Engine-->>Run: Signal ready per wait strategy
  end
  opt Startup command
    Run->>Hooks: Execute PostStart actions
  end
  opt After-ready command
    Run->>Hooks: Execute PostReady actions
  end
  Run-->>Test: Return ctr (with Image, PortEndpoint)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • stevenh

Poem

A rabbit taps Run with a gentle hop,
Options in paw—wait, logs, ports on top.
Hooks twitch awake when containers rise,
Startup to ready, with compact replies.
Nginx hums; tests thump with cheer—
Fewer fields, clearer paths, carrots near! 🥕

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title Check ✅ Passed The title succinctly communicates the primary change by indicating the shift to using the Run function in tests and follows conventional commit style. It is concise, clear, and directly relates to the main objective of the pull request.
Description Check ✅ Passed The description clearly states that the PR uses the Run function in more places within the core library and explains the rationale of consistency and progress towards the new API. It is directly related to the changeset and provides relevant context without being off-topic.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 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: 0

🧹 Nitpick comments (2)
options_test.go (2)

84-88: Reduce flakiness by matching a shorter, stable MySQL log line

The exact message “port: 3306 MySQL Community Server - GPL” can vary across releases/locales. Matching a shorter substring is more robust.

Apply this diff:

- c, err := testcontainers.Run(
-   ctx, "mysql:8.0.36",
-   testcontainers.WithWaitStrategy(wait.ForLog("port: 3306  MySQL Community Server - GPL")),
-   testcontainers.WithLogConsumers(lc),
- )
+ c, err := testcontainers.Run(
+   ctx, "mysql:8.0.36",
+   testcontainers.WithWaitStrategy(wait.ForLog("port: 3306")),
+   testcontainers.WithLogConsumers(lc),
+ )

155-155: Confirm AfterReady hook behavior without an explicit wait

If no WaitingFor strategy is set, verify PostReadies still execute. If not, add a no-op wait to trigger the “ready” phase immediately. Based on learnings

Apply this diff if needed:

- c, err := testcontainers.Run(context.Background(), "alpine", testcontainers.WithEntrypoint("tail", "-f", "/dev/null"), testcontainers.WithAfterReadyCommand(testExec))
+ c, err := testcontainers.Run(
+   context.Background(),
+   "alpine",
+   testcontainers.WithEntrypoint("tail", "-f", "/dev/null"),
+   testcontainers.WithAfterReadyCommand(testExec),
+   testcontainers.WithWaitStrategy(wait.ForNop(func(_ context.Context, _ wait.StrategyTarget) error { return nil })),
+ )
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 038b8a0 and f2aac4d.

📒 Files selected for processing (4)
  • container_test.go (6 hunks)
  • examples/nginx/nginx.go (1 hunks)
  • options_test.go (3 hunks)
  • options_unit_test.go (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (4)
examples/nginx/nginx.go (2)
options.go (2)
  • WithExposedPorts (464-469)
  • WithWaitStrategy (376-378)
wait/http.go (1)
  • ForHTTP (149-151)
options_test.go (3)
generic.go (1)
  • Run (122-149)
options.go (5)
  • WithWaitStrategy (376-378)
  • WithLogConsumers (265-274)
  • WithEntrypoint (448-453)
  • WithStartupCommand (330-349)
  • WithAfterReadyCommand (354-373)
wait/log.go (1)
  • ForLog (118-120)
options_unit_test.go (4)
generic.go (1)
  • GenericContainerRequest (21-27)
container.go (1)
  • ContainerRequest (131-171)
options.go (3)
  • NewRawCommand (313-320)
  • WithStartupCommand (330-349)
  • WithAfterReadyCommand (354-373)
exec/processor.go (1)
  • WithWorkingDir (52-56)
container_test.go (5)
generic.go (1)
  • Run (122-149)
options.go (4)
  • WithCmd (472-477)
  • WithWaitStrategy (376-378)
  • WithImageSubstitutors (256-262)
  • WithExposedPorts (464-469)
wait/log.go (1)
  • ForLog (118-120)
testing.go (1)
  • CleanupContainer (91-97)
wait/http.go (1)
  • ForHTTP (149-151)
⏰ 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). (19)
  • GitHub Check: lint (modules/vearch) / lint: modules/vearch
  • GitHub Check: lint (modules/mssql) / lint: modules/mssql
  • GitHub Check: lint (modulegen) / lint: modulegen
  • GitHub Check: lint (modules/inbucket) / lint: modules/inbucket
  • GitHub Check: lint (modules/yugabytedb) / lint: modules/yugabytedb
  • GitHub Check: lint (modules/k3s) / lint: modules/k3s
  • GitHub Check: lint (modules/rabbitmq) / lint: modules/rabbitmq
  • GitHub Check: lint (modules/dockermodelrunner) / lint: modules/dockermodelrunner
  • GitHub Check: lint (modules/aerospike) / lint: modules/aerospike
  • GitHub Check: lint (modules/consul) / lint: modules/consul
  • GitHub Check: lint (modules/openldap) / lint: modules/openldap
  • GitHub Check: lint (modules/memcached) / lint: modules/memcached
  • GitHub Check: lint (modules/gcloud) / lint: modules/gcloud
  • GitHub Check: lint (modules/etcd) / lint: modules/etcd
  • GitHub Check: lint (modules/mockserver) / lint: modules/mockserver
  • GitHub Check: lint (modules/pulsar) / lint: modules/pulsar
  • GitHub Check: lint (modules/milvus) / lint: modules/milvus
  • GitHub Check: lint (modules/compose) / lint: modules/compose
  • GitHub Check: Analyze (go)
🔇 Additional comments (10)
examples/nginx/nginx.go (1)

17-21: LGTM: Run-based refactor is clean

The switch to Run with functional options is correct; PortEndpoint usage and error handling look good.

Also applies to: 23-25, 30-30

options_test.go (1)

136-140: LGTM: Startup command via lifecycle hook works

Good use of WithEntrypoint + WithStartupCommand and validating via Exec afterward.

options_unit_test.go (2)

11-23: LGTM: Validates startup hook wiring

The unit test correctly asserts a single PostStarts hook added by WithStartupCommand.


25-37: LGTM: Validates after-ready hook wiring

The unit test correctly asserts a single PostReadies hook added by WithAfterReadyCommand.

container_test.go (6)

325-328: LGTM: Label assertion stays intact after Run migration

Inspect + label equality remains correct.


365-369: LGTM: Run-based failure-path test is correct

The Run + ForLog + Cleanup pattern is sound; the test’s error expectation remains valid.


465-474: LGTM: Image substitutor test migrated to Run

Using WithImageSubstitutors with Run and asserting ctr.Image is the right approach.


487-491: LGTM: Parallel start using Run with HTTP wait

Good use of WithExposedPorts and ForHTTP with a bounded startup timeout.


509-515: LGTM: Example updated to Run with image substitutors

Example remains concise and accurate post-refactor.


522-523: LGTM: Example prints substituted image correctly

Accessing ctr.Image aligns with the Run return type.

@mdelapenya mdelapenya merged commit 9f0e4cd into testcontainers:main Sep 26, 2025
213 checks passed
@mdelapenya mdelapenya deleted the run-function-modules-4 branch September 26, 2025 14:08
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