chore: use Run in more tests (part 4) by mdelapenya · Pull Request #3309 · testcontainers/testcontainers-go
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(...) APIcontainer_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 migrationexamples/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 hooksoptions_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)
Estimated code review effort
🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
- chore: use Run function (part 2) #3305: Similar migration from GenericContainer/ContainerRequest to Run(...) with option helpers across tests/examples.
- chore: use the Run funcion in tests and docs (part 1) #3304: Parallel refactor adopting Run(...) API and With... options in various tests.
- chore: use Run function (part 3) #3307: Test refactors to Run(...) pattern plus assertion updates and image access changes.
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.