Skip to content

Commit

Permalink
Upgrade to TestContainers 3.0 (#776)
Browse files Browse the repository at this point in the history
  • Loading branch information
rngcntr authored Mar 13, 2023
1 parent 87c93ee commit 897a2da
Show file tree
Hide file tree
Showing 17 changed files with 254 additions and 94 deletions.
6 changes: 5 additions & 1 deletion Motor.NET.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=SQS/@EntryIndexedValue">SQS</s:String></wpf:ResourceDictionary>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=MQ/@EntryIndexedValue">MQ</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=NATS/@EntryIndexedValue">NATS</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=SQS/@EntryIndexedValue">SQS</s:String>
<s:Boolean x:Key="/Default/UserDictionary/Words/=nofile/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=ulimit/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
using System.Threading.Tasks;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Configurations;
using DotNet.Testcontainers.Containers;
using Testcontainers.Kafka;
using Xunit;

namespace Motor.Extensions.Hosting.Kafka_IntegrationTest;

public class KafkaFixture : IAsyncLifetime
{
private readonly KafkaTestcontainer _container;
private const int KafkaPort = 9093;
private readonly KafkaContainer _container = new KafkaBuilder().Build();

public KafkaFixture()
{
_container = new TestcontainersBuilder<KafkaTestcontainer>()
.WithPortBinding(KafkaPort, true)
.WithKafka(new KafkaTestcontainerConfiguration("confluentinc/cp-kafka:6.1.9"))
.Build();
}

public string BootstrapServers => _container.BootstrapServers;
public string BootstrapServers => _container.GetBootstrapAddress();

public async Task InitializeAsync()
public Task InitializeAsync()
{
await _container.StartAsync();
return _container.StartAsync();
}

public async Task DisposeAsync()
public Task DisposeAsync()
{
await _container.StopAsync();
return _container.DisposeAsync().AsTask();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="Moq" Version="4.18.1" />
<PackageReference Include="RandomDataGenerator.Net" Version="1.0.15" />
<PackageReference Include="TestContainers" Version="2.3.0" />
<PackageReference Include="TestContainers.Kafka" Version="3.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<PackageReference Include="Moq" Version="4.18.1" />
<PackageReference Include="RandomDataGenerator.Net" Version="1.0.15" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="TestContainers" Version="2.3.0" />
<PackageReference Include="TestContainers" Version="3.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
52 changes: 52 additions & 0 deletions test/Motor.Extensions.Hosting.NATS_IntegrationTest/NATSBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Docker.DotNet.Models;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Configurations;

namespace Motor.Extensions.Hosting.NATS_IntegrationTest;

public sealed class NATSBuilder : ContainerBuilder<NATSBuilder, NATSContainer, NATSConfiguration>
{
public const string DefaultImage = "nats:2.9.11";
public const int DefaultPort = 4222;

protected override NATSConfiguration DockerResourceConfiguration { get; }

public NATSBuilder() : this(new NATSConfiguration())
{
DockerResourceConfiguration = Init().DockerResourceConfiguration;
}

public NATSBuilder(NATSConfiguration resourceConfiguration) : base(resourceConfiguration)
{
DockerResourceConfiguration = resourceConfiguration;
}

public override NATSContainer Build()
{
Validate();
return new NATSContainer(DockerResourceConfiguration, TestcontainersSettings.Logger);
}

protected override NATSBuilder Init()
{
return base.Init()
.WithImage(DefaultImage)
.WithPortBinding(DefaultPort, true)
.WithWaitStrategy(Wait.ForUnixContainer().UntilMessageIsLogged("Server is ready"));
}

protected override NATSBuilder Clone(IResourceConfiguration<CreateContainerParameters> resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new NATSConfiguration(resourceConfiguration));
}

protected override NATSBuilder Merge(NATSConfiguration oldValue, NATSConfiguration newValue)
{
return new NATSBuilder(new NATSConfiguration(oldValue, newValue));
}

protected override NATSBuilder Clone(IContainerConfiguration resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new NATSConfiguration(resourceConfiguration));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Docker.DotNet.Models;
using DotNet.Testcontainers.Configurations;

namespace Motor.Extensions.Hosting.NATS_IntegrationTest;

public class NATSConfiguration : ContainerConfiguration
{
public NATSConfiguration()
{
}

public NATSConfiguration(IResourceConfiguration<CreateContainerParameters> resourceConfiguration)
: base(resourceConfiguration)
{
}

public NATSConfiguration(IContainerConfiguration resourceConfiguration)
: base(resourceConfiguration)
{
}

public NATSConfiguration(NATSConfiguration resourceConfiguration)
: this(new NATSConfiguration(), resourceConfiguration)
{
}

public NATSConfiguration(NATSConfiguration oldValue, NATSConfiguration newValue)
: base(oldValue, newValue)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using DotNet.Testcontainers.Configurations;
using DotNet.Testcontainers.Containers;
using Microsoft.Extensions.Logging;

namespace Motor.Extensions.Hosting.NATS_IntegrationTest;

public class NATSContainer : DockerContainer
{
public NATSContainer(IContainerConfiguration configuration, ILogger logger) : base(configuration, logger)
{
}
}
24 changes: 6 additions & 18 deletions test/Motor.Extensions.Hosting.NATS_IntegrationTest/NATSFixture.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
using System.Threading.Tasks;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Containers;
using Xunit;

namespace Motor.Extensions.Hosting.NATS_IntegrationTest;

public class NATSFixture : IAsyncLifetime
{
private readonly TestcontainersContainer _container;
private const int NATSPort = 4222;
private readonly NATSContainer _container = new NATSBuilder().Build();

public NATSFixture()
{
_container = new TestcontainersBuilder<TestcontainersContainer>()
.WithImage("nats:2.9.11")
.WithPortBinding(NATSPort, true)
.Build();
}

public string Hostname => _container.Hostname;
public int Port => _container.GetMappedPublicPort(NATSPort);
public string ConnectionString => $"{_container.Hostname}:{_container.GetMappedPublicPort(NATSBuilder.DefaultPort)}";

public async Task InitializeAsync()
public Task InitializeAsync()
{
await _container.StartAsync();
return _container.StartAsync();
}

public async Task DisposeAsync()
public Task DisposeAsync()
{
await _container.StopAsync();
return _container.DisposeAsync().AsTask();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class NATSIntegrationTests : IClassFixture<NATSFixture>
public NATSIntegrationTests(NATSFixture fixture)
{
_randomizerString = RandomizerFactory.GetRandomizer(new FieldOptionsTextRegex { Pattern = @"^[A-Z]{10}" });
_natsUrl = $"{fixture.Hostname}:{fixture.Port}";
_natsUrl = fixture.ConnectionString;
}

[Fact(Timeout = 50000)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="Moq" Version="4.18.1" />
<PackageReference Include="RandomDataGenerator.Net" Version="1.0.15" />
<PackageReference Include="TestContainers" Version="2.3.0" />
<PackageReference Include="TestContainers.RabbitMq" Version="3.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,60 +1,53 @@
using System;
using System.Threading.Tasks;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Configurations;
using DotNet.Testcontainers.Containers;
using Motor.Extensions.Hosting.RabbitMQ;
using Polly;
using RabbitMQ.Client;
using RabbitMQ.Client.Exceptions;
using Testcontainers.RabbitMq;
using Xunit;

namespace Motor.Extensions.Hosting.RabbitMQ_IntegrationTest;

public class RabbitMQFixture : IAsyncLifetime
{
private const int RabbitMqPort = 5672;
public RabbitMQFixture()
{
var conf = new RabbitMqTestcontainerConfiguration("rabbitmq:3.11.7")
{
Password = "guest",
Username = "guest"
};
Container = new TestcontainersBuilder<RabbitMqTestcontainer>()
.WithPortBinding(RabbitMqPort, true)
.WithMessageBroker(conf)
.Build();
}

public IConnection Connection => CreateConnection();
private readonly RabbitMqContainer _container = new RabbitMqBuilder()
.WithUsername("guest")
.WithPassword("guest")
.WithWaitStrategy(Wait.ForUnixContainer().UntilMessageIsLogged("Server startup complete"))
.Build();

public IRabbitMQConnectionFactory<T> ConnectionFactory<T>() =>
new RabbitMQConnectionFactory<T>(CreateConnectionFactory());

public int Port => Container.GetMappedPublicPort(RabbitMqPort);
public string Hostname => Container.Hostname;

private RabbitMqTestcontainer Container { get; }

public async Task InitializeAsync()
public Task InitializeAsync()
{
await Container.StartAsync();
Policy
.Handle<BrokerUnreachableException>()
.WaitAndRetry(10, i => TimeSpan.FromSeconds(Math.Pow(2, i)))
.Execute(CreateConnection);
return _container.StartAsync();
}

public Task DisposeAsync()
{
return Container.StopAsync();
return _container.DisposeAsync().AsTask();
}

public IConnection Connection => CreateConnection();

public IRabbitMQConnectionFactory<T> ConnectionFactory<T>() =>
new RabbitMQConnectionFactory<T>(CreateConnectionFactory());

public int Port => _container.GetMappedPublicPort(RabbitMqPort);
public string Hostname => _container.Hostname;

private IConnectionFactory CreateConnectionFactory() => new ConnectionFactory
{
Uri = new Uri(Container.ConnectionString)
Uri = new Uri(_container.GetConnectionString())
};

private IConnection CreateConnection() => CreateConnectionFactory().CreateConnection();
private IConnection CreateConnection()
{
return Policy
.Handle<BrokerUnreachableException>()
.WaitAndRetry(5, i => TimeSpan.FromSeconds(Math.Pow(2, i)))
.Execute(() => CreateConnectionFactory().CreateConnection());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="Moq" Version="4.18.1" />
<PackageReference Include="RandomDataGenerator.Net" Version="1.0.15" />
<PackageReference Include="TestContainers" Version="2.3.0" />
<PackageReference Include="TestContainers" Version="3.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
55 changes: 55 additions & 0 deletions test/Motor.Extensions.Hosting.SQS_IntegrationTest/SQSBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Collections.Generic;
using Docker.DotNet.Models;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Configurations;

namespace Motor.Extensions.Hosting.SQS_IntegrationTest;

public sealed class SQSBuilder : ContainerBuilder<SQSBuilder, SQSContainer, SQSConfiguration>
{
public const string DefaultImage = "roribio16/alpine-sqs:1.2.0";
public const int DefaultPort = 9324;

protected override SQSConfiguration DockerResourceConfiguration { get; }

public SQSBuilder() : this(new SQSConfiguration())
{
DockerResourceConfiguration = Init().DockerResourceConfiguration;
}

public SQSBuilder(SQSConfiguration resourceConfiguration) : base(resourceConfiguration)
{
DockerResourceConfiguration = resourceConfiguration;
}

public override SQSContainer Build()
{
Validate();
return new SQSContainer(DockerResourceConfiguration, TestcontainersSettings.Logger);
}

protected override SQSBuilder Init()
{
var ulimit = new Ulimit { Name = "nofile", Soft = 1024, Hard = 1024 };
return base.Init()
.WithImage(DefaultImage)
.WithPortBinding(DefaultPort, true)
.WithCreateParameterModifier(g => g.HostConfig.Ulimits = new List<Ulimit> { ulimit })
.WithWaitStrategy(Wait.ForUnixContainer().UntilMessageIsLogged("listening on port"));
}

protected override SQSBuilder Clone(IResourceConfiguration<CreateContainerParameters> resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new SQSConfiguration(resourceConfiguration));
}

protected override SQSBuilder Merge(SQSConfiguration oldValue, SQSConfiguration newValue)
{
return new SQSBuilder(new SQSConfiguration(oldValue, newValue));
}

protected override SQSBuilder Clone(IContainerConfiguration resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new SQSConfiguration(resourceConfiguration));
}
}
Loading

0 comments on commit 897a2da

Please sign in to comment.