Skip to content

fix: use path.Join instead of url.JoinPath when prepending a custom registry to an image#3308

Merged
mdelapenya merged 2 commits intotestcontainers:mainfrom
fedorkanin:fedorkanin/fix-issue-#3306
Sep 30, 2025
Merged

fix: use path.Join instead of url.JoinPath when prepending a custom registry to an image#3308
mdelapenya merged 2 commits intotestcontainers:mainfrom
fedorkanin:fedorkanin/fix-issue-#3306

Conversation

@fedorkanin
Copy link
Copy Markdown
Contributor

What does this PR do?

This MR swaps url.JoinPath with path.Join in image substitutors.

Why is it important?

url.JoinPath uses url.Parse internally, which doesn't correctly parse URLs like example.com:443 without a schema. Docker image reference cannot have a schema [docs]. Incorrectly parsed URL inside url.JoinPath causes the image tag (second argument) to be completely ignored [go playground], which results in 404 errors when pulling an image.

Related issues

How to test this PR

I've added a test case to image_substitutors_test.go:

go test -v -run TestPrependHubRegistrySubstitutor

@fedorkanin fedorkanin requested a review from a team as a code owner September 26, 2025 10:24
@netlify
Copy link
Copy Markdown

netlify Bot commented Sep 26, 2025

Deploy Preview for testcontainers-go ready!

Name Link
🔨 Latest commit 193b35f
🔍 Latest deploy log https://app.netlify.com/projects/testcontainers-go/deploys/68da73cf8262780008e1c126
😎 Deploy Preview https://deploy-preview-3308--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
Copy Markdown

coderabbitai Bot commented Sep 26, 2025

Summary by CodeRabbit

  • Bug Fixes

    • Correctly handle container registries that include a port when prepending a hub/registry to image names, preventing malformed image references.
  • Improvements

    • More consistent and reliable joining of registry and image paths, reducing edge-case errors.
  • Tests

    • Added test coverage for registry-with-port scenarios to ensure accurate image substitution.

Walkthrough

Replaced use of net/url's JoinPath with path.Join for image substitutors, removed related error handling and net/url import, and added a test verifying substitution when the registry prefix includes a port (e.g., "my-registry:5000/foo:latest").

Changes

Cohort / File(s) Summary
Image substitutor implementation
options.go
Replaced url.JoinPath with path.Join in CustomHubSubstitutor.Substitute and prependHubRegistry.Substitute; removed net/url import and error handling; added path import; functions now return joined path and nil error.
Tests
image_substitutors_test.go
Added subtest "registry-with-port" to TestPrependHubRegistrySubstitutor validating that newPrependHubRegistry("my-registry:5000") substitutes "foo:latest" to "my-registry:5000/foo:latest".

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Caller
  participant Substitutor
  Note right of Substitutor: Old flow (url.JoinPath)\ncould drop tag when prefix has port
  Caller->>Substitutor: Substitute(prefix, image)
  alt old (url.JoinPath)
    Substitutor-->>Substitutor: url.Parse(prefix)  (may treat as opaque -> drop second arg)
    Substitutor-->>Caller: joinedPathOrEmpty + error handling
  else new (path.Join)
    Substitutor-->>Substitutor: path.Join(prefix, image)
    Substitutor-->>Caller: "prefix/image" (nil error)
  end
  Note left of Caller: Test validates "my-registry:5000/foo:latest"
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

I nudge the tags with gentle hop,
No scheme, just paths—clean chop-chop!
Ports now nest where images roam,
Join the path and bring them home.
Tests nibble bugs that used to hide—🐇✨

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 (4 passed)
Check name Status Explanation
Title Check ✅ Passed The title succinctly and accurately describes the primary change of replacing url.JoinPath with path.Join when prepending a custom registry to an image, reflecting the core update in the pull request without extraneous detail.
Linked Issues Check ✅ Passed The pull request implements the coding objectives from issue #3306 by replacing url.JoinPath with path.Join, adding a targeted test to cover registries with ports, and closing the issue, thereby addressing the regression and preserving image tags as required.
Out of Scope Changes Check ✅ Passed All modified files and added tests pertain exclusively to the registry path-joining logic for image substitutors, and there are no edits outside the scope of fixing url.JoinPath usage and validating the correction.
Description Check ✅ Passed The description directly explains the swap from url.JoinPath to path.Join, the rationale based on Docker image reference parsing issues, and includes testing instructions, which all relate to the actual code changes in the pull request.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d4fc16c and 193b35f.

📒 Files selected for processing (1)
  • image_substitutors_test.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • image_substitutors_test.go

Tip

👮 Agentic pre-merge checks are now available in preview!

Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.

  • Built-in checks – Quickly apply ready-made checks to enforce title conventions, require pull request descriptions that follow templates, validate linked issues for compliance, and more.
  • Custom agentic checks – Define your own rules using CodeRabbit’s advanced agentic capabilities to enforce organization-specific policies and workflows. For example, you can instruct CodeRabbit’s agent to verify that API documentation is updated whenever API schema files are modified in a PR. Note: Upto 5 custom checks are currently allowed during the preview period. Pricing for this feature will be announced in a few weeks.

Please see the documentation for more information.

Example:

reviews:
  pre_merge_checks:
    custom_checks:
      - name: "Undocumented Breaking Changes"
        mode: "warning"
        instructions: |
          Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).

Please share your feedback with us on this Discord post.


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

@mdelapenya mdelapenya changed the title Use path.Join instead of url.JoinPath when prepending a custom registry to an image fix: use path.Join instead of url.JoinPath when prepending a custom registry to an image Sep 26, 2025
mdelapenya
mdelapenya previously approved these changes Sep 29, 2025
Copy link
Copy Markdown
Member

@mdelapenya mdelapenya left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the detailed report, and for contributing a fix. 🙇

Comment thread image_substitutors_test.go Outdated
Co-authored-by: Manuel de la Peña <social.mdelapenya@gmail.com>
@mdelapenya mdelapenya self-assigned this Sep 30, 2025
@mdelapenya mdelapenya added the bug An issue with the library label Sep 30, 2025
@mdelapenya mdelapenya merged commit 38e1112 into testcontainers:main Sep 30, 2025
213 checks passed
@mdelapenya
Copy link
Copy Markdown
Member

Merged, thank you so much for your work here 🙇

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

Labels

bug An issue with the library

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Incorrect TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX parsing causes 404 on image pull

2 participants