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 11, 2022
1 parent be73ffa commit 420f8e1
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,36 @@ namespace DotNet.Testcontainers.Tests.Fixtures
using JetBrains.Annotations;
using Xunit;

[UsedImplicitly]
public sealed class DockerTlsFixture : IAsyncLifetime
public abstract class DockerTlsFixtures : IAsyncLifetime
{
public readonly string Version = "20.10.18";

private const ushort TlsPort = 2376;

private const string CertsDirectoryName = "certs";

private readonly ITestcontainersContainer container;
private readonly ITestcontainersBuilder<TestcontainersContainer> containerBuilder;

private ITestcontainersContainer container;

public DockerTlsFixture()
protected DockerTlsFixtures()
{
this.container = new TestcontainersBuilder<TestcontainersContainer>()
.WithImage("docker:20.10.18-dind")
this.containerBuilder = new TestcontainersBuilder<TestcontainersContainer>()
.WithImage($"docker:{this.Version}-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();
.UntilPortIsAvailable(TlsPort));
}

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,32 @@ public Task InitializeAsync()
return this.container.StartAsync();
}

public Task DisposeAsync()
public async Task DisposeAsync()
{
await this.container.DisposeAsync();
}

[UsedImplicitly]
public class DockerTlsFixture : DockerTlsFixtures
{
public DockerTlsFixture()
{
this.container = this.containerBuilder
.WithEnvironment("DOCKER_TLS", "1")
.WithCommand("--tlsverify=false")
.Build();
}
}

[UsedImplicitly]
public class DockerMTlsFixture : DockerTlsFixtures
{
return this.container.DisposeAsync().AsTask();
public DockerMTlsFixture()
{
this.container = this.containerBuilder
.WithEnvironment("DOCKER_TLS_VERIFY", "1")
.Build();
}
}
}
}
44 changes: 0 additions & 44 deletions tests/Testcontainers.Tests/Unit/Containers/Unix/DockerTlsTest.cs

This file was deleted.

80 changes: 80 additions & 0 deletions tests/Testcontainers.Tests/Unit/Containers/Unix/DockerTlsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
namespace DotNet.Testcontainers.Tests.Unit.Containers.Unix
{
using System;
using System.Collections.Generic;
using System.IO;
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 static class DockerTlsTests
{
private static ICustomConfiguration CreateConfiguration(DockerTlsFixtures fixture, bool tls = false, bool tlsVerify = false)
{
var properties = new List<string>();
properties.Add($"docker.host={fixture.TcpEndpoint}");
properties.Add($"docker.cert.path={Path.Combine(fixture.HostCertDirectoryPath, "client")}");
properties.Add($"docker.tls={tls}");
properties.Add($"docker.tls.verify={tlsVerify}");
return new PropertiesFileConfiguration(properties.ToArray());
}

public sealed class DockerTlsTest : IClassFixture<DockerTlsFixtures.DockerTlsFixture>
{
private readonly ICustomConfiguration customConfiguration;
private readonly string expectedVersion;

public DockerTlsTest(DockerTlsFixtures.DockerTlsFixture dockerTlsFixture)
{
this.customConfiguration = CreateConfiguration(dockerTlsFixture, true);
this.expectedVersion = dockerTlsFixture.Version;
}

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

// When
var systemOperations = new DockerSystemOperations(Guid.Empty, authConfig, NullLogger.Instance);
var version = await systemOperations.GetVersion()
.ConfigureAwait(false);

// Then
Assert.Equal(this.expectedVersion, version.Version);
}
}

public sealed class DockerMTlsTest : IClassFixture<DockerTlsFixtures.DockerMTlsFixture>
{
private readonly ICustomConfiguration customConfiguration;
private readonly string expectedVersion;

public DockerMTlsTest(DockerTlsFixtures.DockerMTlsFixture dockerMTlsFixture)
{
this.customConfiguration = CreateConfiguration(dockerMTlsFixture, tlsVerify: true);
this.expectedVersion = dockerMTlsFixture.Version;
}

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

// When
var systemOperations = new DockerSystemOperations(Guid.Empty, authConfig, NullLogger.Instance);
var version = await systemOperations.GetVersion()
.ConfigureAwait(false);

// Then
Assert.Equal(this.expectedVersion, version.Version);
}
}
}
}

0 comments on commit 420f8e1

Please sign in to comment.