Skip to content

Commit

Permalink
Prepared Dockerfile support.
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn committed May 26, 2019
1 parent 2e5a33a commit c44f6d4
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 23 deletions.
3 changes: 3 additions & 0 deletions src/DotNet.Testcontainers.Tests/Assets/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM alpine:latest

LABEL maintainer="IAm@AndreHofmeister.com"
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@
</None>
</ItemGroup>
<ItemGroup>
<Folder Include="Fixtures\" />
<Folder Include="Unit\" />
<None Include="Assets\Dockerfile">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Include="Assets\Dockerfile.tar">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<Import Project="$(MSBuildThisFileDirectory)../Shared.msbuild" />
</Project>
40 changes: 40 additions & 0 deletions src/DotNet.Testcontainers.Tests/Unit/ImageFromDockerfileTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace DotNet.Testcontainers.Tests.Unit
{
using System;
using System.Threading.Tasks;
using DotNet.Testcontainers.Core.Builder;
using Xunit;

public class ImageFromDockerfileTest
{
[Fact]
public async Task DockerfileDoesNotExist()
{
await Assert.ThrowsAsync<ArgumentException>(() =>
new ImageFromDockerfileBuilder()
.WithDockerfileDirectory(string.Empty)
.Build());
}

[Fact]
public async Task DockerfileDirectoryDoesNotExist()
{
await Assert.ThrowsAsync<ArgumentException>(() =>
new ImageFromDockerfileBuilder()
.WithDockerfileDirectory("DoesNotExist")
.Build());
}

[Fact]
public async Task SimpleDockerfile()
{
var imageFromDockerfile = await new ImageFromDockerfileBuilder()
.WithName("alpine:custom")
.WithDockerfileDirectory("Assets")
.WithDeleteIfExits(false)
.Build();

Assert.NotEmpty(imageFromDockerfile);
}
}
}
10 changes: 6 additions & 4 deletions src/DotNet.Testcontainers/Clients/ITestcontainersClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ namespace DotNet.Testcontainers.Clients

internal interface ITestcontainersClient
{
Task StartAsync(string id, CancellationToken cancellationToken = default(CancellationToken));
Task StartAsync(string id, CancellationToken cancellationToken = default);

Task StopAsync(string id, CancellationToken cancellationToken = default(CancellationToken));
Task StopAsync(string id, CancellationToken cancellationToken = default);

Task RemoveAsync(string id, CancellationToken cancellationToken = default(CancellationToken));
Task RemoveAsync(string id, CancellationToken cancellationToken = default);

Task AttachAsync(string id, IOutputConsumer outputConsumer, CancellationToken cancellationToken = default(CancellationToken));
Task AttachAsync(string id, IOutputConsumer outputConsumer, CancellationToken cancellationToken = default);

Task ExecAsync(string id, params string[] command);

Task<string> BuildAsync(ImageFromDockerfileConfiguration config);

Task<string> RunAsync(TestcontainersConfiguration config);
}
}
29 changes: 29 additions & 0 deletions src/DotNet.Testcontainers/Clients/TestcontainersClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace DotNet.Testcontainers.Clients
{
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Docker.DotNet.Models;
Expand Down Expand Up @@ -82,6 +83,34 @@ await WaitStrategy.WaitWhile(async () =>
});
}

public async Task<string> BuildAsync(ImageFromDockerfileConfiguration config)
{
var dockerfileDirectoryInfo = new DirectoryInfo(config.DockerfileDirectory);

if (!dockerfileDirectoryInfo.Exists)
{
throw new ArgumentException("Directory does not exist.");
}

if (!dockerfileDirectoryInfo.GetFiles().Any(file => "Dockerfile".Equals(file.Name)))
{
throw new ArgumentException("Dockerfile does not exist.");
}

if (config.DeleteIfExits)
{
await Docker.Images.DeleteImageAsync(config.Image, new ImageDeleteParameters { Force = true });
}

// TODO: Create temp archive (tar) of Dockerfile base directory.
using (var stream = new FileStream($"{config.DockerfileDirectory}/Dockerfile.tar", FileMode.Open))
{
await Docker.Images.BuildImageFromDockerfileAsync(stream, new ImageBuildParameters { Dockerfile = "Dockerfile", Tags = new[] { config.Image } });
}

return config.Image;
}

public async Task<string> RunAsync(TestcontainersConfiguration config)
{
var image = config.Container.Image;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace DotNet.Testcontainers.Core.Builder
{
using System.Threading.Tasks;
using DotNet.Testcontainers.Core.Images;

public interface IImageFromDockerfileBuilder
{
IImageFromDockerfileBuilder WithName(string name);

IImageFromDockerfileBuilder WithName(IDockerImage image);

IImageFromDockerfileBuilder WithDockerfileDirectory(string dockerfileDirectory);

IImageFromDockerfileBuilder WithDeleteIfExits(bool deleteIfExits);

Task<string> Build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace DotNet.Testcontainers.Core.Builder
{
using System.Threading.Tasks;
using DotNet.Testcontainers.Clients;
using DotNet.Testcontainers.Core.Images;
using DotNet.Testcontainers.Core.Models;

public class ImageFromDockerfileBuilder : IImageFromDockerfileBuilder
{
private readonly ImageFromDockerfileConfiguration configuration = new ImageFromDockerfileConfiguration();

public IImageFromDockerfileBuilder WithName(string image)
{
return this.WithName(new TestcontainersImage(image));
}

public IImageFromDockerfileBuilder WithName(IDockerImage image)
{
this.configuration.Image = image.Image;
return this;
}

public IImageFromDockerfileBuilder WithDockerfileDirectory(string dockerfileDirectory)
{
this.configuration.DockerfileDirectory = dockerfileDirectory;
return this;
}

public IImageFromDockerfileBuilder WithDeleteIfExits(bool deleteIfExits)
{
this.configuration.DeleteIfExits = deleteIfExits;
return this;
}

public Task<string> Build()
{
return TestcontainersClient.Instance.BuildAsync(this.configuration);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace DotNet.Testcontainers.Core.Models
{
using System;

internal class ImageFromDockerfileConfiguration
{
public string Image { get; set; } = Guid.NewGuid().ToString("n");

public string DockerfileDirectory { get; set; } = ".";

public bool DeleteIfExits { get; set; } = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public WaitUntilPortIsAvailable(int port)
{
this.commands = new string[][]
{
new string[] { "/bin/bash", "-c", $"while ! timeout 1 bash -c \"echo > /dev/tcp/localhost/{port}\"; do sleep 1; done" },
new string[] { "/bin/bash", "-c", $"while ! timeout 2 bash -c \"echo > /dev/tcp/localhost/{port}\"; do sleep 1; done" },
};
}

Expand Down
15 changes: 0 additions & 15 deletions src/DotNet.Testcontainers/DotNet.Testcontainers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,5 @@
<PackageReference Include="Docker.DotNet" Version="3.125.2" />
<PackageReference Include="LanguageExt.Core" Version="3.1.24" />
</ItemGroup>
<ItemGroup>
<Folder Include="Clients\" />
<Folder Include="Core\Builder\" />
<Folder Include="Core\Containers\" />
<Folder Include="Core\Containers\Database\" />
<Folder Include="Core\Images\" />
<Folder Include="Core\Mapper\" />
<Folder Include="Core\Models\" />
<Folder Include="Core\Models\Database\" />
<Folder Include="Core\Parser\" />
<Folder Include="Diagnostics\" />
</ItemGroup>
<ItemGroup>
<None Remove="Configurations\.gitkeep" />
</ItemGroup>
<Import Project="$(MSBuildThisFileDirectory)../Shared.msbuild" />
</Project>
2 changes: 1 addition & 1 deletion src/Shared.msbuild
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
</ItemGroup>
<!-- StyleCop -->
<PropertyGroup>
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)DotNet.Blazor.ruleset</CodeAnalysisRuleSet>
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)DotNet.Testcontainers.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<AdditionalFiles Include="$(MSBuildThisFileDirectory)stylecop.json" Link="stylecop.json" />
Expand Down

0 comments on commit c44f6d4

Please sign in to comment.