Skip to content

Commit

Permalink
feat(#370): Added working test for simple TLS endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
vlaskal committed Oct 10, 2022
1 parent be73ffa commit 1cf86c9
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
namespace DotNet.Testcontainers.Tests.Fixtures
{
using System;
using System.IO;
using System.Threading.Tasks;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Configurations;
using DotNet.Testcontainers.Containers;
using JetBrains.Annotations;
using Xunit;

[UsedImplicitly]
public sealed class DockerMTlsFixture : IAsyncLifetime
{
private const ushort TlsPort = 2376;

private const string CertsDirectoryName = "certs";

private readonly ITestcontainersContainer container;

public DockerMTlsFixture()
{
this.container = new TestcontainersBuilder<TestcontainersContainer>()
.WithImage("docker:20.10.18-dind")
.WithPrivileged(true)
.WithEnvironment("DOCKER_CERT_PATH", this.ContainerCertDirectoryPath)
.WithEnvironment("DOCKER_TLS_CERTDIR", this.ContainerCertDirectoryPath)
.WithEnvironment("DOCKER_TLS_VERIFY", "1")
.WithBindMount(this.HostCertDirectoryPath, this.ContainerCertDirectoryPath, AccessMode.ReadWrite)
.WithPortBinding(TlsPort, true)
.WithWaitStrategy(Wait.ForUnixContainer()
.UntilPortIsAvailable(TlsPort))
.Build();
}

public string ContainerCertDirectoryPath { get; }
= Path.Combine("/", CertsDirectoryName);

public string HostCertDirectoryPath { get; }
= Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString(), CertsDirectoryName);

public Uri TcpEndpoint
{
get
{
return new UriBuilder("tcp", this.container.Hostname, this.container.GetMappedPublicPort(TlsPort)).Uri;
}
}

public Task InitializeAsync()
{
_ = Directory.CreateDirectory(this.HostCertDirectoryPath);
return this.container.StartAsync();
}

public async Task DisposeAsync()
{
await this.container.DisposeAsync();
Directory.Delete(this.HostCertDirectoryPath, true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,20 @@ public DockerTlsFixture()
.WithPrivileged(true)
.WithEnvironment("DOCKER_CERT_PATH", this.ContainerCertDirectoryPath)
.WithEnvironment("DOCKER_TLS_CERTDIR", this.ContainerCertDirectoryPath)
.WithEnvironment("DOCKER_TLS_VERIFY", "1")
.WithEnvironment("DOCKER_TLS", "1")
.WithBindMount(this.HostCertDirectoryPath, this.ContainerCertDirectoryPath, AccessMode.ReadWrite)
.WithPortBinding(TlsPort, true)
.WithWaitStrategy(Wait.ForUnixContainer()
.UntilPortIsAvailable(TlsPort))
.WithCommand("--tlsverify=false")
.Build();
}

public string ContainerCertDirectoryPath { get; }
= Path.Combine("/", CertsDirectoryName);

public string HostCertDirectoryPath { get; }
= Path.Combine(Path.GetTempPath(), CertsDirectoryName);
= Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString(), CertsDirectoryName);

public Uri TcpEndpoint
{
Expand All @@ -53,9 +54,10 @@ public Task InitializeAsync()
return this.container.StartAsync();
}

public Task DisposeAsync()
public async Task DisposeAsync()
{
return this.container.DisposeAsync().AsTask();
await this.container.DisposeAsync();
Directory.Delete(this.HostCertDirectoryPath, true);
}
}
}
44 changes: 44 additions & 0 deletions tests/Testcontainers.Tests/Unit/Containers/Unix/DockerMTlsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace DotNet.Testcontainers.Tests.Unit.Containers.Unix
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Clients;
using DotNet.Testcontainers.Configurations;
using DotNet.Testcontainers.Tests.Fixtures;
using Microsoft.Extensions.Logging.Abstractions;
using Xunit;

public sealed class DockerMTlsTest : IClassFixture<DockerMTlsFixture>
{
private readonly ICustomConfiguration customConfiguration;

public DockerMTlsTest(DockerMTlsFixture dockerMTlsFixture)
{
IList<string> properties = new List<string>();
properties.Add($"docker.host={dockerMTlsFixture.TcpEndpoint}");
properties.Add($"docker.cert.path={Path.Combine(dockerMTlsFixture.HostCertDirectoryPath, "client")}");
properties.Add("docker.tls.verify=true");
this.customConfiguration = new PropertiesFileConfiguration(properties.ToArray());
}

[Fact]
public async Task GetVersionReturnsVersion()
{
// Given
var authConfig = new MTlsEndpointAuthenticationProvider(this.customConfiguration).GetAuthConfig();

// When
IDockerSystemOperations systemOperations = new DockerSystemOperations(Guid.Empty, authConfig, NullLogger.Instance);

var version = await systemOperations.GetVersion()
.ConfigureAwait(false);

// Then
Assert.Equal("20.10.18", version.Version);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public sealed class DockerTlsTest : IClassFixture<DockerTlsFixture>
{
private readonly ICustomConfiguration customConfiguration;

public DockerTlsTest(DockerTlsFixture dockerTlsFixture)
public DockerTlsTest(DockerTlsFixture dockerMTlsFixture)
{
IList<string> properties = new List<string>();
properties.Add($"docker.host={dockerTlsFixture.TcpEndpoint}");
properties.Add($"docker.cert.path={Path.Combine(dockerTlsFixture.HostCertDirectoryPath, "client")}");
properties.Add($"docker.host={dockerMTlsFixture.TcpEndpoint}");
properties.Add($"docker.cert.path={Path.Combine(dockerMTlsFixture.HostCertDirectoryPath, "client")}");
properties.Add("docker.tls=true");
this.customConfiguration = new PropertiesFileConfiguration(properties.ToArray());
}
Expand All @@ -29,7 +29,7 @@ public DockerTlsTest(DockerTlsFixture dockerTlsFixture)
public async Task GetVersionReturnsVersion()
{
// Given
var authConfig = new MTlsEndpointAuthenticationProvider(this.customConfiguration).GetAuthConfig();
var authConfig = new TlsEndpointAuthenticationProvider(this.customConfiguration).GetAuthConfig();

// When
IDockerSystemOperations systemOperations = new DockerSystemOperations(Guid.Empty, authConfig, NullLogger.Instance);
Expand Down

0 comments on commit 1cf86c9

Please sign in to comment.