Skip to content

Commit

Permalink
Merge pull request #89 from ktmitton/optionExtensionsCleanup
Browse files Browse the repository at this point in the history
Option extensions cleanup
  • Loading branch information
ktmitton authored Apr 17, 2022
2 parents 97d3089 + b39c6fe commit 1f06557
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 211 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void Dispose()
}

[Fact]
public async Task Run_WhenCalledWithLabels_ExpectContainerToHaveTheLabelsApplied()
public async Task RunAsync_WhenCalledWithLabels_ExpectContainerToHaveTheLabelsApplied()
{
// Arrange
var imageName = "alpine:3.15";
Expand Down Expand Up @@ -94,15 +94,15 @@ public async Task Run_WhenCalledWithLabels_ExpectContainerToHaveTheLabelsApplied
}

[Fact]
public async Task Run_WhenCalledWithAnImage_ExpectContainerToBeForTheRequestedImage()
public async Task RunAsync_WhenCalledWithAnImage_ExpectContainerToBeForTheRequestedImage()
{
// Arrange
var images = new[] { "alpine:3.15", "alpine:3.14" };

var containerGateway = new ContainerGateway();

// Act
var containers = images.Select(x => (Image: x, Task: containerGateway.RunAsync(x, string.Empty, Enumerable.Empty<Option>(), _cancellationToken)));
var containers = images.Select(x => (Image: x, Task: containerGateway.RunAsync(x, string.Empty, Enumerable.Empty<Option>(), _cancellationToken))).ToArray();
await Task.WhenAll(containers.Select(x => x.Task));

_containerIds.AddRange(containers.Select(x => x.Task.Result));
Expand All @@ -128,7 +128,7 @@ public async Task Run_WhenCalledWithAnImage_ExpectContainerToBeForTheRequestedIm
}

[Fact]
public async Task Run_WhenCalledForAlpineWithNoCommand_ExpectContainerToHaveStartedWithTheDefaultCommand()
public async Task RunAsync_WhenCalledForAlpineWithNoCommand_ExpectContainerToHaveStartedWithTheDefaultCommand()
{
// Arrange
var containerGateway = new ContainerGateway();
Expand Down Expand Up @@ -160,7 +160,7 @@ public async Task Run_WhenCalledForAlpineWithNoCommand_ExpectContainerToHaveStar
}

[Fact]
public async Task Run_WhenCalledForAlpineWithACommand_ExpectContainerToHaveStartedWithTheCommand()
public async Task RunAsync_WhenCalledForAlpineWithACommand_ExpectContainerToHaveStartedWithTheCommand()
{
// Arrange
var containerGateway = new ContainerGateway();
Expand Down Expand Up @@ -190,6 +190,56 @@ public async Task Run_WhenCalledForAlpineWithACommand_ExpectContainerToHaveStart

Assert.Equal("'[/bin/bash]'", output);
}

[Fact]
public async Task RunAsync_WhenOptionsIsNull_ExpectContainerToStillRun()
{
// Arrange
var containerGateway = new ContainerGateway();

// Act
var containerId = await containerGateway.RunAsync("alpine:3.15", "/bin/sh", default(IEnumerable<Option>), _cancellationToken);
_containerIds.Add(containerId);

// Assert
using var proc = new Process();
proc.StartInfo.FileName = "docker";
proc.StartInfo.Arguments = $"ps -aqf Id={containerId}";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;

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

var shortContainerId = proc.StandardOutput.ReadLine();

Assert.StartsWith(shortContainerId, containerId);
}

[Fact]
public async Task RunAsync_WhenOptionsIsEmpty_ExpectContainerToStillRun()
{
// Arrange
var containerGateway = new ContainerGateway();

// Act
var containerId = await containerGateway.RunAsync("alpine:3.15", "/bin/sh", Enumerable.Empty<Option>(), _cancellationToken);
_containerIds.Add(containerId);

// Assert
using var proc = new Process();
proc.StartInfo.FileName = "docker";
proc.StartInfo.Arguments = $"ps -aqf Id={containerId}";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;

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

var shortContainerId = proc.StandardOutput.ReadLine();

Assert.StartsWith(shortContainerId, containerId);
}
}

public class RemoveTests : IDisposable
Expand Down Expand Up @@ -378,7 +428,7 @@ public async Task AddFile_WhenCalledForMissingDirectory_ExpectDirectoryToBeCreat
_containerIds.Add(containerId);

// Act
var tasks = files.Select(x => containerGateway.AddFileAsync(containerId, x.TemporaryFilename, x.ContainerFilename, default(string), default(string), _cancellationToken));
var tasks = files.Select(x => containerGateway.AddFileAsync(containerId, x.TemporaryFilename, x.ContainerFilename, default(string), default(string), _cancellationToken)).ToList();
await Task.WhenAll(tasks);

// Assert
Expand Down Expand Up @@ -425,7 +475,7 @@ public async Task AddFile_WhenCalled_ExpectFileToBeCopiedToTheContainer()
_containerIds.Add(containerId);

// Act
var tasks = files.Select(x => containerGateway.AddFileAsync(containerId, x.TemporaryFilename, x.ContainerFilename, default(string), default(string), _cancellationToken));
var tasks = files.Select(x => containerGateway.AddFileAsync(containerId, x.TemporaryFilename, x.ContainerFilename, default(string), default(string), _cancellationToken)).ToList();
await Task.WhenAll(tasks);

// Assert
Expand Down Expand Up @@ -472,7 +522,7 @@ public async Task AddFile_WhenCalledWithPermissions_ExpectThePermissionsToBeSet(
_containerIds.Add(containerId);

// Act
var tasks = files.Select(x => containerGateway.AddFileAsync(containerId, x.TemporaryFilename, x.ContainerFilename, default(string), x.Permissions, _cancellationToken));
var tasks = files.Select(x => containerGateway.AddFileAsync(containerId, x.TemporaryFilename, x.ContainerFilename, default(string), x.Permissions, _cancellationToken)).ToList();
await Task.WhenAll(tasks);

// Assert
Expand Down Expand Up @@ -519,7 +569,7 @@ public async Task AddFile_WhenCalledWithAnOwner_ExpectThePermissionsToBeSet()
_containerIds.Add(containerId);

// Act
var tasks = files.Select(x => containerGateway.AddFileAsync(containerId, x.TemporaryFilename, x.ContainerFilename, x.Owner, default(string), _cancellationToken));
var tasks = files.Select(x => containerGateway.AddFileAsync(containerId, x.TemporaryFilename, x.ContainerFilename, x.Owner, default(string), _cancellationToken)).ToList();
await Task.WhenAll(tasks);

// Assert
Expand Down Expand Up @@ -604,11 +654,11 @@ public async Task RemoveFile_WhenCalled_ExpectFileToBeRemoved()
var containerId = await containerGateway.RunAsync("atmoz/sftp:alpine", "guest:guest tester:tester", Enumerable.Empty<Option>(), _cancellationToken);
_containerIds.Add(containerId);

var addTasks = files.Select(x => containerGateway.AddFileAsync(containerId, x.TemporaryFilename, x.ContainerFilename, default(string), default(string), _cancellationToken));
var addTasks = files.Select(x => containerGateway.AddFileAsync(containerId, x.TemporaryFilename, x.ContainerFilename, default(string), default(string), _cancellationToken)).ToList();
await Task.WhenAll(addTasks);

// Act
var removeTasks = files.Select(x => containerGateway.RemoveFileAsync(containerId, x.ContainerFilename, _cancellationToken));
var removeTasks = files.Select(x => containerGateway.RemoveFileAsync(containerId, x.ContainerFilename, _cancellationToken)).ToList();
await Task.WhenAll(removeTasks);

// Assert
Expand Down Expand Up @@ -692,7 +742,7 @@ public async Task EmptyDirectory_WhenCalled_ExpectDirectoryToBeEmptied()
var containerId = await containerGateway.RunAsync("atmoz/sftp:alpine", "guest:guest tester:tester", Enumerable.Empty<Option>(), _cancellationToken);
_containerIds.Add(containerId);

var addTasks = files.Select(x => containerGateway.AddFileAsync(containerId, x.TemporaryFilename, x.ContainerFilename, default(string), default(string), _cancellationToken));
var addTasks = files.Select(x => containerGateway.AddFileAsync(containerId, x.TemporaryFilename, x.ContainerFilename, default(string), default(string), _cancellationToken)).ToList();
await Task.WhenAll(addTasks);

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,62 @@ public async Task ConnectAsync_WhenCalled_ExpectContainerToBeConnectedToNetwork(

Assert.Contains(uniqueName, connectedNetworks);
}

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

var networkGateway = new NetworkGateway();

_networkNames.Add(uniqueName);

// Act
await networkGateway.CreateAsync(uniqueName, default(IEnumerable<Option>), _cancellationToken);

// Assert
using var proc = new Process();
proc.StartInfo.FileName = "docker";
proc.StartInfo.Arguments = $"network ls -qf Name={uniqueName}";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;

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

var shortNetworkId = proc.StandardOutput.ReadLine();

Assert.NotEmpty(shortNetworkId);
}

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

var networkGateway = new NetworkGateway();

_networkNames.Add(uniqueName);

// Act
await networkGateway.CreateAsync(uniqueName, Enumerable.Empty<Option>(), _cancellationToken);

// Assert
using var proc = new Process();
proc.StartInfo.FileName = "docker";
proc.StartInfo.Arguments = $"network ls -qf Name={uniqueName}";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;

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

var shortNetworkId = proc.StandardOutput.ReadLine();

Assert.NotEmpty(shortNetworkId);
}
}
144 changes: 0 additions & 144 deletions Mittons.Fixtures.Tests.Unit/Extensions/OptionExtensionsTests.cs

This file was deleted.

Loading

0 comments on commit 1f06557

Please sign in to comment.