Skip to content

Commit

Permalink
[#129] #EXTEND 'assemblyName: DotNet.Testcontainers; function: IDocke…
Browse files Browse the repository at this point in the history
…rContainer'

{Add GetMappedPublicPort to get the public host port associated with the private container port.}
  • Loading branch information
HofmeisterAn committed Aug 15, 2019
1 parent 8d44cc2 commit 8b71d9b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ public async Task PortBindingsHttpAndHttps()

var https = new { From = 443, To = 80 };

var nginx = new TestcontainersBuilder<TestcontainersContainer>()
var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
.WithImage("nginx");

// When
// Then
foreach (var port in new[] { http, https })
{
using (var testcontainer = nginx
using (var testcontainer = testcontainersBuilder
.WithPortBinding(port.From, port.To)
.WithWaitStrategy(Wait.UntilPortsAreAvailable(port.To))
.Build())
Expand All @@ -137,6 +137,23 @@ public async Task PortBindingsHttpAndHttps()
}
}

[Fact]
public async Task RandomHostPortBindings()
{
// Given
var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
.WithImage("nginx")
.WithPortBinding(80, true);

// When
// Then
using (var testcontainer = testcontainersBuilder.Build())
{
await testcontainer.StartAsync();
Assert.NotEqual(0, testcontainer.GetMappedPublicPort(80));
}
}

[Fact]
public async Task VolumeAndCommand()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ public ITestcontainersBuilder<T> WithExposedPort(string port)

public ITestcontainersBuilder<T> WithPortBinding(int port, bool assignRandomHostPort = false)
{
var hostPort = assignRandomHostPort ? TestcontainersNetworkService.GetAvailablePort() : port;
return this.WithPortBinding(hostPort, port);
return this.WithPortBinding($"{port}", assignRandomHostPort);
}

public ITestcontainersBuilder<T> WithPortBinding(int hostPort, int containerPort)
Expand Down
14 changes: 14 additions & 0 deletions src/DotNet.Testcontainers/Core/Containers/IDockerContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ public interface IDockerContainer : IDisposable
/// <value>Returns the Docker container mac address if present or an empty string instead.</value>
string MacAddress { get; }

/// <summary>
/// Gets the public host port associated with the private container port.
/// </summary>
/// <param name="privatePort">Private container port.</param>
/// <returns>Returns the public host port associated with the private container port.</returns>
int GetMappedPublicPort(int privatePort);

/// <summary>
/// Gets the public host port associated with the private container port.
/// </summary>
/// <param name="privatePort">Private container port.</param>
/// <returns>Returns the public host port associated with the private container port.</returns>
int GetMappedPublicPort(string privatePort);

/// <summary>
/// Starts the Testcontainer. If the image does not exist, it will be downloaded automatically. Non-existing containers are created at first start.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public string IPAddress
}

var ipAddress = this.container.NetworkSettings.Networks.FirstOrDefault();
return ipAddress.Value == null ? string.Empty : ipAddress.Value.IPAddress;
return ipAddress.Value?.IPAddress ?? string.Empty;
}
}

Expand All @@ -80,12 +80,28 @@ public string MacAddress
}

var macAddress = this.container.NetworkSettings.Networks.FirstOrDefault();
return macAddress.Value == null ? string.Empty : macAddress.Value.IPAddress;
return macAddress.Value?.MacAddress ?? string.Empty;
}
}

private TestcontainersConfiguration Configuration { get; }

public int GetMappedPublicPort(int privatePort)
{
return this.GetMappedPublicPort($"{privatePort}");
}

public int GetMappedPublicPort(string privatePort)
{
if (this.container == null)
{
throw new InvalidOperationException("Testcontainer is not running.");
}

var mappedPort = this.container.Ports.FirstOrDefault(port => $"{port.PrivatePort}".Equals(privatePort));
return mappedPort?.PublicPort ?? 0;
}

public async Task StartAsync()
{
await this.Create();
Expand Down

0 comments on commit 8b71d9b

Please sign in to comment.