-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgrade to TestContainers 3.0 (#776)
- Loading branch information
Showing
17 changed files
with
254 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
25 changes: 7 additions & 18 deletions
25
test/Motor.Extensions.Hosting.Kafka_IntegrationTest/KafkaFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
test/Motor.Extensions.Hosting.NATS_IntegrationTest/NATSBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
test/Motor.Extensions.Hosting.NATS_IntegrationTest/NATSConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
test/Motor.Extensions.Hosting.NATS_IntegrationTest/NATSContainer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
test/Motor.Extensions.Hosting.NATS_IntegrationTest/NATSFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 25 additions & 32 deletions
57
test/Motor.Extensions.Hosting.RabbitMQ_IntegrationTest/RabbitMQFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
test/Motor.Extensions.Hosting.SQS_IntegrationTest/SQSBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
Oops, something went wrong.