Skip to content

Commit

Permalink
Merge pull request #21 from ktmitton/networkLabels
Browse files Browse the repository at this point in the history
Network labels
  • Loading branch information
ktmitton authored Apr 6, 2022
2 parents 5a4c3e2 + 7fae05b commit 64fd0a1
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.Json;
using Mittons.Fixtures.Docker.Attributes;
using Mittons.Fixtures.Docker.Containers;
using Mittons.Fixtures.Docker.Fixtures;
using Xunit;

namespace Mittons.Fixtures.Tests.Integration.Docker.Fixtures
{
[Run("BUILD_BUILDID")]
[Network("network1")]
[Network("network2")]
public class ExampleDockerEnvironmentFixture : DockerEnvironmentFixture
Expand All @@ -29,14 +32,54 @@ public DockerEnvironmentFixtureTests(ExampleDockerEnvironmentFixture dockerEnvir
_dockerEnvironmentFixture = dockerEnvironmentFixture;
}

[Fact]
public void ExpectNetwork1ToHaveRunLabels()
{
using var proc = new Process();
proc.StartInfo.FileName = "docker";
proc.StartInfo.Arguments = $"network inspect network1-{_dockerEnvironmentFixture.InstanceId} --format \"{{{{json .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 ExpectNetwork2ToHaveRunLabels()
{
using var proc = new Process();
proc.StartInfo.FileName = "docker";
proc.StartInfo.Arguments = $"network inspect network2-{_dockerEnvironmentFixture.InstanceId} --format \"{{{{json .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 ExpectAlpineContainerToBeCreated()
{
Assert.NotNull(_dockerEnvironmentFixture.AlpineContainer);

if (_dockerEnvironmentFixture.AlpineContainer is not null)
{
var proc = new Process();
using var proc = new Process();
proc.StartInfo.FileName = "docker";
proc.StartInfo.Arguments = $"ps -a --filter id={_dockerEnvironmentFixture.AlpineContainer.Id} --format '{{{{.ID}}}}'";
proc.StartInfo.UseShellExecute = false;
Expand All @@ -56,7 +99,7 @@ public void ExpectAlpineContainerToBeConnectedToNetwork1()

if (_dockerEnvironmentFixture.AlpineContainer is not null)
{
var proc = new Process();
using var proc = new Process();
proc.StartInfo.FileName = "docker";
proc.StartInfo.Arguments = $"inspect {_dockerEnvironmentFixture.AlpineContainer.Id} --format \"{{{{range $k, $v := .NetworkSettings.Networks}}}}{{{{printf \\\"%s\\n\\\" $k}}}}{{{{end}}}}\"";
proc.StartInfo.UseShellExecute = false;
Expand Down Expand Up @@ -88,7 +131,7 @@ public void ExpectSftpContainerToBeCreated()

if (_dockerEnvironmentFixture.SftpContainer is not null)
{
var proc = new Process();
using var proc = new Process();
proc.StartInfo.FileName = "docker";
proc.StartInfo.Arguments = $"ps -a --filter id={_dockerEnvironmentFixture.SftpContainer.Id} --format '{{{{.ID}}}}'";
proc.StartInfo.UseShellExecute = false;
Expand All @@ -108,7 +151,7 @@ public void ExpectSftpContainerToBeConnectedToNetwork1()

if (_dockerEnvironmentFixture.SftpContainer is not null)
{
var proc = new Process();
using var proc = new Process();
proc.StartInfo.FileName = "docker";
proc.StartInfo.Arguments = $"inspect {_dockerEnvironmentFixture.SftpContainer.Id} --format \"{{{{range $k, $v := .NetworkSettings.Networks}}}}{{{{printf \\\"%s\\n\\\" $k}}}}{{{{end}}}}\"";
proc.StartInfo.UseShellExecute = false;
Expand Down Expand Up @@ -140,7 +183,7 @@ public void ExpectSftpContainerToBeConnectedToNetwork2()

if (_dockerEnvironmentFixture.SftpContainer is not null)
{
var proc = new Process();
using var proc = new Process();
proc.StartInfo.FileName = "docker";
proc.StartInfo.Arguments = $"inspect {_dockerEnvironmentFixture.SftpContainer.Id} --format \"{{{{range $k, $v := .NetworkSettings.Networks}}}}{{{{printf \\\"%s\\n\\\" $k}}}}{{{{end}}}}\"";
proc.StartInfo.UseShellExecute = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Net;
using System.IO;
using System.Text.Json;

namespace Mittons.Fixtures.Tests.Integration.Docker.Gateways
{
Expand Down Expand Up @@ -50,7 +51,7 @@ public void ContainerRun_WhenCalledWithAnImage_ExpectContainerToBeForTheRequeste
_containerIds.Add(containerId);

// Assert
var proc = new Process();
using var proc = new Process();
proc.StartInfo.FileName = "docker";
proc.StartInfo.Arguments = $"inspect {containerId} --format '{{{{.Config.Image}}}}'";
proc.StartInfo.UseShellExecute = false;
Expand Down Expand Up @@ -81,7 +82,7 @@ public void ContainerRun_WhenCalledForAlpineWithNoCommand_ExpectContainerToHaveS
_containerIds.Add(containerId);

// Assert
var proc = new Process();
using var proc = new Process();
proc.StartInfo.FileName = "docker";
proc.StartInfo.Arguments = $"inspect {containerId} --format '{{{{.Config.Cmd}}}}'";
proc.StartInfo.UseShellExecute = false;
Expand Down Expand Up @@ -112,7 +113,7 @@ public void ContainerRun_WhenCalledForAlpineWithACommand_ExpectContainerToHaveSt
_containerIds.Add(containerId);

// Assert
var proc = new Process();
using var proc = new Process();
proc.StartInfo.FileName = "docker";
proc.StartInfo.Arguments = $"inspect {containerId} --format '{{{{.Config.Cmd}}}}'";
proc.StartInfo.UseShellExecute = false;
Expand Down Expand Up @@ -156,7 +157,7 @@ public void ContainerRemove_WhenTheContainerExists_ExpectTheContainerToBeRemoved
gateway.ContainerRemove(containerId);

// Assert
var proc = new Process();
using var proc = new Process();
proc.StartInfo.FileName = "docker";
proc.StartInfo.Arguments = $"ps -a --filter id={containerId} --format '{{{{.ID}}}}'";
proc.StartInfo.UseShellExecute = false;
Expand All @@ -183,7 +184,7 @@ public void ContainerGetDefaultNetworkIpAddress_WhenTheContainerIsOnOneNetwork_R
var ipAddress = gateway.ContainerGetDefaultNetworkIpAddress(containerId);

// Assert
var proc = new Process();
using var proc = new Process();
proc.StartInfo.FileName = "docker";
proc.StartInfo.Arguments = $"inspect {containerId} --format \"{{{{range .NetworkSettings.Networks}}}}{{{{printf \\\"%s\\n\\\" .IPAddress}}}}{{{{end}}}}\"";
proc.StartInfo.UseShellExecute = false;
Expand Down Expand Up @@ -219,7 +220,7 @@ public void ContainerAddFile_WhenCalled_ExpectFileToBeCopiedToTheContainer(strin
gateway.ContainerAddFile(containerId, temporaryFilename, containerFilename, default(string), default(string));

// Assert
var proc = new Process();
using var proc = new Process();
proc.StartInfo.FileName = "docker";
proc.StartInfo.Arguments = $"exec {containerId} cat {containerFilename}";
proc.StartInfo.UseShellExecute = false;
Expand Down Expand Up @@ -255,7 +256,7 @@ public void ContainerAddFile_WhenCalledWithPermissions_ExpectThePermissionsToBeS
gateway.ContainerAddFile(containerId, temporaryFilename, containerFilename, default(string), permissions);

// Assert
var proc = new Process();
using var proc = new Process();
proc.StartInfo.FileName = "docker";
proc.StartInfo.Arguments = $"exec {containerId} stat -c \"%a\" {containerFilename}";
proc.StartInfo.UseShellExecute = false;
Expand Down Expand Up @@ -291,7 +292,7 @@ public void ContainerAddFile_WhenCalledWithAnOwner_ExpectThePermissionsToBeSet(s
gateway.ContainerAddFile(containerId, temporaryFilename, containerFilename, owner, default(string));

// Assert
var proc = new Process();
using var proc = new Process();
proc.StartInfo.FileName = "docker";
proc.StartInfo.Arguments = $"exec {containerId} stat -c \"%U\" {containerFilename}";
proc.StartInfo.UseShellExecute = false;
Expand Down Expand Up @@ -329,7 +330,7 @@ public void ContainerRemoveFile_WhenCalled_ExpectFileToBeRemoved(string containe
gateway.ContainerRemoveFile(containerId, containerFilename);

// Assert
var proc = new Process();
using var proc = new Process();
proc.StartInfo.FileName = "docker";
proc.StartInfo.Arguments = $"exec {containerId} ls {containerFilename}";
proc.StartInfo.UseShellExecute = false;
Expand Down Expand Up @@ -389,10 +390,10 @@ public void NetworkCreate_WhenCalled_ExpectNetworkToBeCreatedWithTheProvidedName
_networkNames.Add(uniqueName);

// Act
gateway.NetworkCreate(uniqueName);
gateway.NetworkCreate(uniqueName, new Dictionary<string, string>());

// Assert
var proc = new Process();
using var proc = new Process();
proc.StartInfo.FileName = "docker";
proc.StartInfo.Arguments = $"network ls -f name={uniqueName} -q";
proc.StartInfo.UseShellExecute = false;
Expand All @@ -406,6 +407,46 @@ public void NetworkCreate_WhenCalled_ExpectNetworkToBeCreatedWithTheProvidedName
Assert.False(string.IsNullOrWhiteSpace(output));
}

[Fact]
public void NetworkCreate_WhenCalledWithLabels_ExpectLabelsToBeAttachedToTheNetwork()
{
// Arrange
var networkName = "test";
var uniqueName = $"{networkName}-{Guid.NewGuid()}";

var expectedLabels = new Dictionary<string, string>
{
{ "first", "second" },
{ "third", "fourth" }
};

var gateway = new DefaultDockerGateway();

_networkNames.Add(uniqueName);

// Act
gateway.NetworkCreate(uniqueName, expectedLabels);

// Assert
using var proc = new Process();
proc.StartInfo.FileName = "docker";
proc.StartInfo.Arguments = $"network inspect {uniqueName} --format \"{{{{json .Labels}}}}\"";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;

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

var output = proc.StandardOutput.ReadToEnd();

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

Assert.True(actualLabels.ContainsKey("first"));
Assert.True(actualLabels.ContainsKey("third"));
Assert.Equal(expectedLabels["first"], actualLabels["first"]);
Assert.Equal(expectedLabels["third"], actualLabels["third"]);
}

[Fact]
public void NetworkRemove_WhenCalled_ExpectNetworkToBeRemoved()
{
Expand All @@ -417,13 +458,13 @@ public void NetworkRemove_WhenCalled_ExpectNetworkToBeRemoved()

_networkNames.Add(uniqueName);

gateway.NetworkCreate(uniqueName);
gateway.NetworkCreate(uniqueName, new Dictionary<string, string>());

// Act
gateway.NetworkRemove(uniqueName);

// Assert
var proc = new Process();
using var proc = new Process();
proc.StartInfo.FileName = "docker";
proc.StartInfo.Arguments = $"network ls -f name={uniqueName} -q";
proc.StartInfo.UseShellExecute = false;
Expand Down Expand Up @@ -451,13 +492,13 @@ public void NetworkConnect_WhenCalled_ExpectContainerToBeConnectedToNetwork()

_networkNames.Add(uniqueName);

gateway.NetworkCreate(uniqueName);
gateway.NetworkCreate(uniqueName, new Dictionary<string, string>());

// Act
gateway.NetworkConnect(uniqueName, containerId, "test.example.com");

// Assert
var proc = new Process();
using var proc = new Process();
proc.StartInfo.FileName = "docker";
proc.StartInfo.Arguments = $"inspect {containerId} --format \"{{{{range $k, $v := .NetworkSettings.Networks}}}}{{{{printf \\\"%s\\n\\\" $k}}}}{{{{end}}}}\"";
proc.StartInfo.UseShellExecute = false;
Expand Down
Loading

0 comments on commit 64fd0a1

Please sign in to comment.