Skip to content

fix(udp): expose UDP ports properly#3485

Merged
mdelapenya merged 2 commits intotestcontainers:mainfrom
blueprismo:testcontainers/fix-udp
Nov 5, 2025
Merged

fix(udp): expose UDP ports properly#3485
mdelapenya merged 2 commits intotestcontainers:mainfrom
blueprismo:testcontainers/fix-udp

Conversation

@blueprismo
Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes UDP port exposure.

Problem:
When using ExposedPorts: []string{"8080/udp"} in testcontainers-go, the MappedPort() function always returned "0/udp" instead of the actual mapped host port (e.g., "55051/udp"). This made it impossible to connect to UDP services running in containers.

Root Cause:
The issue was in the mergePortBindings() function in lifecycle.go. When nat.ParsePortSpecs() processes exposed port specifications like 8080/udp, it creates PortBinding structs with empty HostPort fields ({HostIP: "", HostPort: ""})
Docker interprets these differently:

HostPort: "" (empty) → "Don't bind this port to any host port" → results in port 0
HostPort: "0" → "Bind this port to a random available host port" → proper allocation
Solution:
Modified mergePortBindings() to convert empty HostPort values to "0" for automatic port allocation:

Why is it important?

To support UDP connections correctly as now they aren't useful (at least with random port allocatio

Related issues

Follows: #3484

@blueprismo blueprismo requested a review from a team as a code owner November 4, 2025 16:42
@netlify
Copy link
Copy Markdown

netlify Bot commented Nov 4, 2025

Deploy Preview for testcontainers-go ready!

Name Link
🔨 Latest commit 4d3ab52
🔍 Latest deploy log https://app.netlify.com/projects/testcontainers-go/deploys/690b55f12c293c0008ad4cc4
😎 Deploy Preview https://deploy-preview-3485--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 Nov 4, 2025

Summary by CodeRabbit

Bug Fixes

  • Fixed UDP port binding to correctly signal Docker for automatic random port allocation when no specific port is specified.

Tests

  • Added comprehensive test coverage for UDP port binding scenarios and port allocation behavior.

Walkthrough

A fix is applied to the mergePortBindings function to normalize empty HostPort values to "0", enabling Docker to allocate random ports automatically. Comprehensive tests are added to validate UDP port binding behavior and document port parsing edge-cases, with test expectations updated to reflect the normalized HostPort values.

Changes

Cohort / File(s) Summary
UDP Port Binding Fix
lifecycle.go
Post-processes exposed port bindings in mergePortBindings to convert empty HostPort values to "0" for automatic Docker port allocation, fixing UDP port-binding behavior
Test Suite for UDP Port Binding
udp_port_binding_test.go
New test file with comprehensive coverage: UDP auto-allocation via MappedPort, TCP regression tests, mergePortBindings unit tests validating empty-to-"0" conversion, and documentation-style tests for nat.ParsePortSpecs behavior
Updated Test Expectations
lifecycle_test.go
Updates test expectations in PreCreateModifierHook and TestMergePortBindings scenarios to use "0" instead of "" for HostPort values in port binding mappings

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

  • lifecycle.go: The production fix is localized and straightforward—a simple post-processing step normalizing HostPort values. Verify the logic handles all edge-cases and doesn't inadvertently affect existing bindings.
  • udp_port_binding_test.go: The new test suite is comprehensive and well-structured, but requires careful review to ensure all UDP and TCP scenarios are adequately covered and assertions are correct.
  • lifecycle_test.go: Test expectation changes are data-driven updates; cross-reference with the production fix to confirm all expectations align with the new "0" default.

Poem

🐰 A rabbit hops through port allocation,
Where empty strings caused frustration.
Now "0" whispers to Docker's ear,
"Assign a port, make it clear!"
UDP flows and TCP stays true—
All ports now have work to do! 🐇

Pre-merge checks and finishing touches

✅ Passed checks (2 passed)
Check name Status Explanation
Description check ✅ Passed The description provides a clear explanation of the problem, root cause, and solution related to UDP port binding in testcontainers-go, directly addressing the changeset.
Title check ✅ Passed The title 'fix(udp): expose UDP ports properly' directly addresses the main change—fixing UDP port exposure in testcontainers-go by converting empty HostPort values to '0' for proper auto-allocation.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@mdelapenya
Copy link
Copy Markdown
Member

@blueprismo i think we need to update the tests for the new condition. Can you take a look? 🙏

@blueprismo blueprismo force-pushed the testcontainers/fix-udp branch from 02d81d8 to 4d3ab52 Compare November 5, 2025 13:49
@mdelapenya mdelapenya self-assigned this Nov 5, 2025
@mdelapenya mdelapenya added the bug An issue with the library label Nov 5, 2025
@mdelapenya mdelapenya changed the title fix: udp and add test fix(udp): expose UDP ports properly Nov 5, 2025
@mdelapenya mdelapenya merged commit 1ed2735 into testcontainers:main Nov 5, 2025
214 checks passed
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.

2 participants