Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Consul module #1028

Merged
merged 9 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 89 additions & 72 deletions Testcontainers.sln

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/Testcontainers.Consul/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
root = true
81 changes: 81 additions & 0 deletions src/Testcontainers.Consul/ConsulBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
namespace Testcontainers.Consul;

/// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" />
[PublicAPI]
public sealed class ConsulBuilder : ContainerBuilder<ConsulBuilder, ConsulContainer, ConsulConfiguration>
{
public const string ConsulImage = "consul:1.15";

public const ushort ConsulPort = 8500;

/// <summary>
/// Initializes a new instance of the <see cref="ConsulBuilder" /> class.
/// </summary>
public ConsulBuilder()
: this(new ConsulConfiguration())
{
DockerResourceConfiguration = Init().DockerResourceConfiguration;
}

/// <summary>
/// Initializes a new instance of the <see cref="ConsulBuilder" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
private ConsulBuilder(ConsulConfiguration resourceConfiguration)
: base(resourceConfiguration)
{
DockerResourceConfiguration = resourceConfiguration;
}

/// <inheritdoc />
protected override ConsulConfiguration DockerResourceConfiguration { get; }

/// <inheritdoc />
public override ConsulContainer Build()
{
Validate();
return new ConsulContainer(DockerResourceConfiguration, TestcontainersSettings.Logger);
}

/// <inheritdoc />
protected override ConsulBuilder Init()
{
return base.Init()
.WithImage(ConsulImage)
.WithPortBinding(ConsulPort, true)
.WithWaitStrategy(Wait.ForUnixContainer().AddCustomWaitStrategy(new WaitUntil()));
witskeeper marked this conversation as resolved.
Show resolved Hide resolved
}

/// <inheritdoc />
protected override ConsulBuilder Clone(IResourceConfiguration<CreateContainerParameters> resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new ConsulConfiguration(resourceConfiguration));
}

/// <inheritdoc />
protected override ConsulBuilder Clone(IContainerConfiguration resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new ConsulConfiguration(resourceConfiguration));
}

/// <inheritdoc />
protected override ConsulBuilder Merge(ConsulConfiguration oldValue, ConsulConfiguration newValue)
{
return new ConsulBuilder(new ConsulConfiguration(oldValue, newValue));
}

/// <inheritdoc cref="IWaitUntil" />
private sealed class WaitUntil : IWaitUntil
{
private readonly string[] _command = { "curl", "http://localhost:8500/v1/health/service/consul?pretty" };

/// <inheritdoc />
public async Task<bool> UntilAsync(IContainer container)
{
var execResult = await container.ExecAsync(_command)
.ConfigureAwait(false);

return 0L.Equals(execResult.ExitCode);
}
}
}
53 changes: 53 additions & 0 deletions src/Testcontainers.Consul/ConsulConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace Testcontainers.Consul;

/// <inheritdoc cref="ContainerConfiguration" />
[PublicAPI]
public sealed class ConsulConfiguration : ContainerConfiguration
{
/// <summary>
/// Initializes a new instance of the <see cref="ConsulConfiguration" /> class.
/// </summary>
public ConsulConfiguration()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ConsulConfiguration" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
public ConsulConfiguration(IResourceConfiguration<CreateContainerParameters> resourceConfiguration)
: base(resourceConfiguration)
{
// Passes the configuration upwards to the base implementations to create an updated immutable copy.
}

/// <summary>
/// Initializes a new instance of the <see cref="ConsulConfiguration" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
public ConsulConfiguration(IContainerConfiguration resourceConfiguration)
: base(resourceConfiguration)
{
// Passes the configuration upwards to the base implementations to create an updated immutable copy.
}

/// <summary>
/// Initializes a new instance of the <see cref="ConsulConfiguration" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
public ConsulConfiguration(ConsulConfiguration resourceConfiguration)
: this(new ConsulConfiguration(), resourceConfiguration)
{
// Passes the configuration upwards to the base implementations to create an updated immutable copy.
}

/// <summary>
/// Initializes a new instance of the <see cref="ConsulConfiguration" /> class.
/// </summary>
/// <param name="oldValue">The old Docker resource configuration.</param>
/// <param name="newValue">The new Docker resource configuration.</param>
public ConsulConfiguration(ConsulConfiguration oldValue, ConsulConfiguration newValue)
: base(oldValue, newValue)
{
}
}
25 changes: 25 additions & 0 deletions src/Testcontainers.Consul/ConsulContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Testcontainers.Consul;

/// <inheritdoc cref="DockerContainer" />
[PublicAPI]
public sealed class ConsulContainer : DockerContainer
{
/// <summary>
/// Initializes a new instance of the <see cref="ConsulContainer" /> class.
/// </summary>
/// <param name="configuration">The container configuration.</param>
/// <param name="logger">The logger.</param>
public ConsulContainer(ConsulConfiguration configuration, ILogger logger)
: base(configuration, logger)
{
}

/// <summary>
/// Gets the Consul connection string.
/// </summary>
/// <returns>The Consul connection string.</returns>
public string GetConnectionString()
{
return new UriBuilder("http", Hostname, GetMappedPublicPort(ConsulBuilder.ConsulPort)).Uri.Authority;
witskeeper marked this conversation as resolved.
Show resolved Hide resolved
}
}
13 changes: 13 additions & 0 deletions src/Testcontainers.Consul/Testcontainers.Consul.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All"/>
<PackageReference Include="JetBrains.Annotations" Version="2022.3.1" PrivateAssets="All"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(SolutionDir)src/Testcontainers/Testcontainers.csproj"/>
</ItemGroup>
</Project>
11 changes: 11 additions & 0 deletions src/Testcontainers.Consul/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
global using System;
global using System.IO;
global using System.Text;
global using System.Threading;
global using System.Threading.Tasks;
global using Docker.DotNet.Models;
global using DotNet.Testcontainers.Builders;
global using DotNet.Testcontainers.Configurations;
global using DotNet.Testcontainers.Containers;
global using JetBrains.Annotations;
global using Microsoft.Extensions.Logging;
1 change: 1 addition & 0 deletions tests/Testcontainers.Consul.Tests/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
root = true
33 changes: 33 additions & 0 deletions tests/Testcontainers.Consul.Tests/ConsulContainerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Net;
using System.Text;

namespace Testcontainers.Consul;

public sealed class ConsulContainerTest : IAsyncLifetime
{
private readonly ConsulContainer _ConsulContainer = new ConsulBuilder().Build();
witskeeper marked this conversation as resolved.
Show resolved Hide resolved

public Task InitializeAsync()
{
return _ConsulContainer.StartAsync();
}

public Task DisposeAsync()
{
return _ConsulContainer.DisposeAsync().AsTask();
}

[Fact]
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
public async Task ConnectionStateReturnsOpen()
{
using var consulClient = new ConsulClient(option => option.Address = new System.Uri($"http://{_ConsulContainer.GetConnectionString()}"));
var putPair = new KVPair("hello")
{
Value = Encoding.UTF8.GetBytes("Hello Consul")
};
await consulClient.KV.Put(putPair);
var result = await consulClient.KV.Get("hello");
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<IsPublishable>false</IsPublishable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Consul" Version="1.6.10.9" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0" />
<PackageReference Include="xunit" Version="2.5.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(SolutionDir)src/Testcontainers.Consul/Testcontainers.Consul.csproj" />
<ProjectReference Include="$(SolutionDir)tests/Testcontainers.Commons/Testcontainers.Commons.csproj" />
</ItemGroup>
</Project>
4 changes: 4 additions & 0 deletions tests/Testcontainers.Consul.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
global using System.Threading.Tasks;
global using DotNet.Testcontainers.Commons;
global using Consul;
global using Xunit;