Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow specifying the protocol (TCP, UDP, SCTP) to resolve the public assigned host port #1219

Merged
merged 7 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public ContainerConfigurationConverter(IContainerConfiguration configuration)

public IDictionary<string, EndpointSettings> Networks { get; }

private static string GetQualifiedPort(string containerPort)
public static string GetQualifiedPort(string containerPort)
{
return Array.Exists(new[] { UdpPortSuffix, TcpPortSuffix, SctpPortSuffix }, portSuffix => containerPort.EndsWith(portSuffix, StringComparison.OrdinalIgnoreCase)) ? containerPort.ToLowerInvariant() : containerPort + TcpPortSuffix;
}
Expand Down
6 changes: 4 additions & 2 deletions src/Testcontainers/Containers/DockerContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,15 @@ public ushort GetMappedPublicPort(string containerPort)
{
ThrowIfResourceNotFound();

if (_container.NetworkSettings.Ports.TryGetValue($"{containerPort}/tcp", out var portBindings) && ushort.TryParse(portBindings[0].HostPort, out var publicPort))
var qualifiedContainerPort = ContainerConfigurationConverter.GetQualifiedPort(containerPort);

if (_container.NetworkSettings.Ports.TryGetValue(qualifiedContainerPort, out var portBindings) && ushort.TryParse(portBindings[0].HostPort, out var publicPort))
{
return publicPort;
}
else
{
throw new InvalidOperationException($"Exposed port {containerPort} is not mapped.");
throw new InvalidOperationException($"Exposed port {qualifiedContainerPort} is not mapped.");
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/Testcontainers/Containers/IContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ public interface IContainer : IAsyncDisposable
/// <summary>
/// Resolves the public assigned host port.
/// </summary>
/// <remarks>
/// Resolves the public assigned host port for the TCP protocol. To resolve a specific protocol, use <see cref="GetMappedPublicPort(string)" />.
/// </remarks>
/// <param name="containerPort">The container port.</param>
/// <returns>Returns the public assigned host port.</returns>
/// <exception cref="InvalidOperationException">Container has not been created.</exception>
Expand All @@ -140,6 +143,9 @@ public interface IContainer : IAsyncDisposable
/// <summary>
/// Resolves the public assigned host port.
/// </summary>
/// <remarks>
/// Append /tcp|udp|sctp to <paramref name="containerPort" /> to resolve the public assigned host port for a specific protocol e.g. "53/udp".
/// </remarks>
/// <param name="containerPort">The container port.</param>
/// <returns>Returns the public assigned host port.</returns>
/// <exception cref="InvalidOperationException">Container has not been created.</exception>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,29 @@ await container.StartAsync()
}

[Fact]
public async Task RandomPortBinding()
public async Task RandomUdpPortBinding()
{
// Given
const ushort containerPort = 53;

const string qualifiedContainerPort = "53/udp";

await using var container = new ContainerBuilder()
.WithImage(CommonImages.Alpine)
.WithEntrypoint(CommonCommands.SleepInfinity)
.WithPortBinding(qualifiedContainerPort, true)
.Build();

// When
await container.StartAsync()
.ConfigureAwait(true);

// Then
Assert.NotEqual(containerPort, container.GetMappedPublicPort(qualifiedContainerPort));
}

[Fact]
public async Task RandomTcpPortBinding()
{
// Given
const ushort containerPort = 80;
Expand Down
Loading