Skip to content

Commit

Permalink
Updated the fixture to pass run through to containers
Browse files Browse the repository at this point in the history
  • Loading branch information
ktmitton committed Apr 6, 2022
1 parent c90e59a commit b06f956
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.Json;
Expand Down Expand Up @@ -124,6 +123,31 @@ public void ExpectAlpineContainerToBeConnectedToNetwork1()
}
}

[Fact]
public void ExpectAlpineContainerToHaveRunLabel()
{
Assert.NotNull(_dockerEnvironmentFixture.AlpineContainer);

if (_dockerEnvironmentFixture.AlpineContainer is not null)
{
using var proc = new Process();
proc.StartInfo.FileName = "docker";
proc.StartInfo.Arguments = $"inspect {_dockerEnvironmentFixture.AlpineContainer.Id} --format \"{{{{json .Config.Labels}}}}\"";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;

proc.Start();
proc.WaitForExit();

var output = proc.StandardOutput.ReadToEnd();

var labels = JsonSerializer.Deserialize<Dictionary<string, string>>(output) ?? new Dictionary<string, string>();

Assert.True(labels.ContainsKey("mittons.fixtures.run.id"));
Assert.Equal(Run.DefaultId, labels["mittons.fixtures.run.id"]);
}
}

[Fact]
public void ExpectSftpContainerToBeCreated()
{
Expand Down Expand Up @@ -207,5 +231,30 @@ public void ExpectSftpContainerToBeConnectedToNetwork2()
Assert.Contains(connectedNetworks, x => x.StartsWith($"network2-"));
}
}

[Fact]
public void ExpectSftpContainerToHaveRunLabel()
{
Assert.NotNull(_dockerEnvironmentFixture.SftpContainer);

if (_dockerEnvironmentFixture.SftpContainer is not null)
{
using var proc = new Process();
proc.StartInfo.FileName = "docker";
proc.StartInfo.Arguments = $"inspect {_dockerEnvironmentFixture.SftpContainer.Id} --format \"{{{{json .Config.Labels}}}}\"";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;

proc.Start();
proc.WaitForExit();

var output = proc.StandardOutput.ReadToEnd();

var labels = JsonSerializer.Deserialize<Dictionary<string, string>>(output) ?? new Dictionary<string, string>();

Assert.True(labels.ContainsKey("mittons.fixtures.run.id"));
Assert.Equal(Run.DefaultId, labels["mittons.fixtures.run.id"]);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,30 @@ public void Ctor_WhenInitializedWithRunDetailsFromBuildId_ExpectTheRunIdToBeSet(
// Arrange
var gatewayMock = new Mock<IDockerGateway>();

// Act
using var fixture = new BuildEnvironmentFixture(gatewayMock.Object);

// Assert
gatewayMock.Verify(x => x.NetworkCreate($"network1-{fixture.InstanceId}", It.Is<Dictionary<string, string>>(y => y["mittons.fixtures.run.id"] == _buildId)), Times.Once);
gatewayMock.Verify(x => x.NetworkCreate($"network2-{fixture.InstanceId}", It.Is<Dictionary<string, string>>(y => y["mittons.fixtures.run.id"] == _buildId)), Times.Once);
gatewayMock.Verify(x => x.ContainerRun("alpine:3.15", "", It.Is<Dictionary<string, string>>(y => y["mittons.fixtures.run.id"] == _buildId)), Times.Once);
gatewayMock.Verify(x => x.ContainerRun("redis:alpine", "", It.Is<Dictionary<string, string>>(y => y["mittons.fixtures.run.id"] == _buildId)), Times.Once);
}

[Fact]
public void Ctor_WhenInitializedWithRunDetailsFromReleaseId_ExpectTheRunIdToBeSet()
{
// Arrange
var gatewayMock = new Mock<IDockerGateway>();

// Act
using var fixture = new ReleaseEnvironmentFixture(gatewayMock.Object);

// Assert
gatewayMock.Verify(x => x.NetworkCreate($"network1-{fixture.InstanceId}", It.Is<Dictionary<string, string>>(y => y["mittons.fixtures.run.id"] == _releaseId)), Times.Once);
gatewayMock.Verify(x => x.NetworkCreate($"network2-{fixture.InstanceId}", It.Is<Dictionary<string, string>>(y => y["mittons.fixtures.run.id"] == _releaseId)), Times.Once);
gatewayMock.Verify(x => x.ContainerRun("alpine:3.15", "", It.Is<Dictionary<string, string>>(y => y["mittons.fixtures.run.id"] == _releaseId)), Times.Once);
gatewayMock.Verify(x => x.ContainerRun("redis:alpine", "", It.Is<Dictionary<string, string>>(y => y["mittons.fixtures.run.id"] == _releaseId)), Times.Once);
}

[Fact]
Expand All @@ -119,6 +137,8 @@ public void Ctor_WhenInitializedWithRunDetailsFromUnsetEnvironmentVariables_Expe
// Assert
gatewayMock.Verify(x => x.NetworkCreate($"network1-{fixture.InstanceId}", It.Is<Dictionary<string, string>>(y => y["mittons.fixtures.run.id"] == Run.DefaultId)), Times.Once);
gatewayMock.Verify(x => x.NetworkCreate($"network2-{fixture.InstanceId}", It.Is<Dictionary<string, string>>(y => y["mittons.fixtures.run.id"] == Run.DefaultId)), Times.Once);
gatewayMock.Verify(x => x.ContainerRun("alpine:3.15", "", It.Is<Dictionary<string, string>>(y => y["mittons.fixtures.run.id"] == Run.DefaultId)), Times.Once);
gatewayMock.Verify(x => x.ContainerRun("redis:alpine", "", It.Is<Dictionary<string, string>>(y => y["mittons.fixtures.run.id"] == Run.DefaultId)), Times.Once);
}

[Fact]
Expand All @@ -133,6 +153,8 @@ public void Ctor_WhenInitializedWithoutRunDetails_ExpectTheRunIdToBeDefault()
// Assert
gatewayMock.Verify(x => x.NetworkCreate($"network1-{fixture.InstanceId}", It.Is<Dictionary<string, string>>(y => y["mittons.fixtures.run.id"] == Run.DefaultId)), Times.Once);
gatewayMock.Verify(x => x.NetworkCreate($"network2-{fixture.InstanceId}", It.Is<Dictionary<string, string>>(y => y["mittons.fixtures.run.id"] == Run.DefaultId)), Times.Once);
gatewayMock.Verify(x => x.ContainerRun("alpine:3.15", "", It.Is<Dictionary<string, string>>(y => y["mittons.fixtures.run.id"] == Run.DefaultId)), Times.Once);
gatewayMock.Verify(x => x.ContainerRun("redis:alpine", "", It.Is<Dictionary<string, string>>(y => y["mittons.fixtures.run.id"] == Run.DefaultId)), Times.Once);
}
}

Expand Down
12 changes: 6 additions & 6 deletions Mittons.Fixtures/Docker/Fixtures/DockerEnvironmentFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ public DockerEnvironmentFixture(IDockerGateway dockerGateway)

_containers = new List<Container>();

foreach(var propertyInfo in this.GetType().GetProperties().Where(x => typeof(Container).IsAssignableFrom(x.PropertyType)))
foreach (var propertyInfo in this.GetType().GetProperties().Where(x => typeof(Container).IsAssignableFrom(x.PropertyType)))
{
var attributes = propertyInfo.GetCustomAttributes(false).OfType<Attribute>();
var attributes = propertyInfo.GetCustomAttributes(false).OfType<Attribute>().Concat(new[] { run });

var container = (Container)Activator.CreateInstance(propertyInfo.PropertyType, new object[] { dockerGateway, attributes});
var container = (Container)Activator.CreateInstance(propertyInfo.PropertyType, new object[] { dockerGateway, attributes });
propertyInfo.SetValue(this, container);
_containers.Add(container);

foreach(var networkAlias in attributes.OfType<NetworkAlias>())
foreach (var networkAlias in attributes.OfType<NetworkAlias>())
{
dockerGateway.NetworkConnect($"{networkAlias.NetworkName}-{InstanceId}", container.Id, networkAlias.Alias);
}
Expand All @@ -56,12 +56,12 @@ public DockerEnvironmentFixture(IDockerGateway dockerGateway)

public void Dispose()
{
foreach(var container in _containers)
foreach (var container in _containers)
{
container.Dispose();
}

foreach(var network in _networks)
foreach (var network in _networks)
{
network.Dispose();
}
Expand Down

0 comments on commit b06f956

Please sign in to comment.