Skip to content

Commit 6cc2f4e

Browse files
authored
Merge pull request #292 from 0xced/fix-ExposeAllPorts
Fix ArgumentNullException in the ExposeAllPorts method
2 parents 83ec85f + 3567396 commit 6cc2f4e

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

Ductus.FluentDocker.Tests/FluentApiTests/FluentContainerBasicTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,40 @@ public void ImplicitPortMappingShouldWork()
191191
}
192192
}
193193

194+
[TestMethod]
195+
[TestCategory("CI")]
196+
public void FullImplicitPortMappingShouldWork()
197+
{
198+
using (
199+
var container =
200+
Fd.UseContainer()
201+
.UseImage("postgres:9.6-alpine")
202+
.ExposeAllPorts()
203+
.WithEnvironment("POSTGRES_PASSWORD=mysecretpassword")
204+
.Build()
205+
.Start())
206+
{
207+
var endpoint = container.ToHostExposedEndpoint("5432/tcp");
208+
AreNotEqual(0, endpoint.Port);
209+
}
210+
}
211+
212+
[TestMethod]
213+
[TestCategory("CI")]
214+
public void ExposeAllPortsIsMutuallyExclusiveWithExposePort()
215+
{
216+
var exception = ThrowsException<FluentDockerNotSupportedException>(() => Fd.UseContainer().ExposePort(5432).ExposeAllPorts());
217+
AreEqual("ExposeAllPorts is mutually exclusive with ExposePort methods. Do not call ExposePort if you want to expose all ports.", exception.Message);
218+
}
219+
220+
[TestMethod]
221+
[TestCategory("CI")]
222+
public void ExposePortIsMutuallyExclusiveWithExposeAllPorts()
223+
{
224+
var exception = ThrowsException<FluentDockerNotSupportedException>(() => Fd.UseContainer().ExposeAllPorts().ExposePort(5432));
225+
AreEqual("ExposePort is mutually exclusive with ExposeAllPorts methods. Do not call ExposeAllPorts if you want to explicitly expose ports.", exception.Message);
226+
}
227+
194228
[TestMethod]
195229
[TestCategory("CI")]
196230
public void WaitForPortShallWork()

Ductus.FluentDocker/Builders/ContainerBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ private void EnsurePublishAllPortsIsFalse()
358358

359359
private void EnsurePortMappingsIsEmpty()
360360
{
361-
if (_config.CreateParams.PortMappings.Any())
361+
if (_config.CreateParams.PortMappings?.Any() == true)
362362
{
363363
throw new FluentDockerNotSupportedException($"{nameof(ExposeAllPorts)} is mutually exclusive with {nameof(ExposePort)} methods. " +
364364
$"Do not call {nameof(ExposePort)} if you want to expose all ports.");

0 commit comments

Comments
 (0)