Skip to content

Commit

Permalink
Fixed bug where --network was not passed to container create.
Browse files Browse the repository at this point in the history
This solves the issue #81 where --network needs to be emitted when
static ip is assigned to the container. It also adds two unit tests
to ensure both command and fluent API is working properly.
  • Loading branch information
Mario Toffia committed Apr 11, 2019
1 parent 5219715 commit 7585965
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
23 changes: 16 additions & 7 deletions Ductus.FluentDocker.Tests/CommandTests/NetworkCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,26 @@ public void NetworkCreateAndDeleteShallWork()
}

[TestMethod]
[Ignore]
public void UseNetworkAndStaticIpv4ShallWork()
{
string container = null;

string id = null;
try
{
var created = _docker.NetworkCreate("unit-test-nw", new NetworkCreateParams
{
Subnet = new [] {"10.18.0.0/16"}
}, _certificates);

Assert.IsTrue(created.Success);
id = created.Data[0];

var cmd = _docker.Run("postgres:9.6-alpine", new ContainerCreateParams
{
PortMappings = new[] {"40001:5432"},
Environment = new[] {"POSTGRES_PASSWORD=mysecretpassword"},
Network = "mynetwork",
Ipv4 = "1.1.1.1"
Network = "unit-test-nw",
Ipv4 = "10.18.0.22"
}, _certificates);

Assert.IsTrue(cmd.Success);
Expand All @@ -127,14 +134,16 @@ public void UseNetworkAndStaticIpv4ShallWork()
var insp = _docker.InspectContainer(container, _certificates);
Assert.IsTrue(insp.Success);

var ip = insp.Data.NetworkSettings.IPAddress;
Assert.AreEqual("1.1.1.1", ip);

var ip = insp.Data.NetworkSettings.Networks["unit-test-nw"].IPAddress;
Assert.AreEqual("10.18.0.22", ip);
}
finally
{
if (null != container)
_docker.RemoveContainer(container, true, true);

if (null != id)
_docker.NetworkRm(network: id);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,10 +442,9 @@ public void ReuseOfExistingContainerShallWork()
}

[TestMethod]
[Ignore]
public void StaticIpv4InCustomNetworkShallWork()
{
using (var nw = Fd.UseNetwork("mynetwork").Build())
using (var nw = Fd.UseNetwork("unit-test-nw").UseSubnet("10.18.0.0/16").Build())
{
using (
var container =
Expand All @@ -455,13 +454,13 @@ public void StaticIpv4InCustomNetworkShallWork()
.WithEnvironment("POSTGRES_PASSWORD=mysecretpassword")
.ExposePort(5432)
.UseNetwork(nw)
.UseIpV4("1.1.1.1")
.UseIpV4("10.18.0.22")
.WaitForPort("5432/tcp", 30000 /*30s*/)
.Build()
.Start())
{
var ip = container.GetConfiguration().NetworkSettings.IPAddress;
Assert.AreEqual("1.1.1.1", ip);
var ip = container.GetConfiguration().NetworkSettings.Networks["unit-test-nw"].IPAddress;
Assert.AreEqual("10.18.0.22", ip);
}
}
}
Expand Down
19 changes: 17 additions & 2 deletions Ductus.FluentDocker/Builders/ContainerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ public override IContainerService Build()
}
}

// If we have networks, the first is supplied as --network option on docker create
// TODO: This is a ugly hack that needs to be cleaned up.
var cfgNw = (IList<INetworkService>)_config.Networks ?? new INetworkService[0];

var firstNw = null != _config.NetworkNames
? _config.NetworkNames[0]
: (0 == cfgNw.Count ? string.Empty : cfgNw[0].Name);

if (string.Empty != firstNw) _config.CreateParams.Network = firstNw;

var container = host.Value.Create(_config.Image, _config.CreateParams, _config.StopOnDispose,
_config.DeleteOnDispose,
_config.DeleteVolumeOnDispose,
Expand All @@ -58,14 +68,19 @@ public override IContainerService Build()

AddHooks(container);

foreach (var network in (IEnumerable<INetworkService>) _config.Networks ?? new INetworkService[0])
network.Attach(container, true /*detachOnDisposeNetwork*/);
foreach (var network in cfgNw)
{
if (network.Name != firstNw)
network.Attach(container, true /*detachOnDisposeNetwork*/);
}

if (null == _config.NetworkNames) return container;

var nw = host.Value.GetNetworks();
foreach (var network in (IEnumerable<string>) _config.NetworkNames ?? new string[0])
{
if (network == firstNw) continue;

var nets = nw.First(x => x.Name == network);
nets.Attach(container, true /*detachOnDisposeNetwork*/);
}
Expand Down

0 comments on commit 7585965

Please sign in to comment.