From bf3a42f62923e3a01dfdcce55bfd6f4ac0219f43 Mon Sep 17 00:00:00 2001 From: lgcmotta Date: Sat, 11 Jan 2025 18:11:09 -0300 Subject: [PATCH 01/19] feat: add exposed API to attach MsSqlContainer to Azure Service Bus emulator --- .../ServiceBusBuilder.cs | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/Testcontainers.ServiceBus/ServiceBusBuilder.cs b/src/Testcontainers.ServiceBus/ServiceBusBuilder.cs index 12d4d0b84..ee98bf4f5 100644 --- a/src/Testcontainers.ServiceBus/ServiceBusBuilder.cs +++ b/src/Testcontainers.ServiceBus/ServiceBusBuilder.cs @@ -53,6 +53,27 @@ public ServiceBusBuilder WithAcceptLicenseAgreement(bool acceptLicenseAgreement) var licenseAgreement = acceptLicenseAgreement ? AcceptLicenseAgreement : DeclineLicenseAgreement; return WithEnvironment(AcceptLicenseAgreementEnvVar, licenseAgreement); } + + /// + /// Attaches the dependent to the Azure Service Bus Emulator. + /// + /// + /// When an exitent instance of is attached to Azure Service Bus Emulator, please ensure that both containers are in the same network. + /// + /// An instance of + /// The dependent MSSQL container network alias. + /// The dependent MSSQL container SA password. + /// A configured instance of . + public ServiceBusBuilder WithMsSqlContainer( + MsSqlContainer msSqlContainer, + string databaseNetworkAlias, + string saPassword = MsSqlBuilder.DefaultPassword) + { + return Merge(DockerResourceConfiguration, new ServiceBusConfiguration(databaseContainer: msSqlContainer)) + .DependsOn(msSqlContainer) + .WithEnvironment("MSSQL_SA_PASSWORD", saPassword) + .WithEnvironment("SQL_SERVER", databaseNetworkAlias); + } /// public override ServiceBusContainer Build() From 3c3c2b90ad24e9be8c7d197cf7746fdc3a5c8a4d Mon Sep 17 00:00:00 2001 From: lgcmotta Date: Sat, 11 Jan 2025 18:11:56 -0300 Subject: [PATCH 02/19] feat: invoke CreateAsync for each network attached to ServiceBusContainer --- src/Testcontainers.ServiceBus/ServiceBusContainer.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Testcontainers.ServiceBus/ServiceBusContainer.cs b/src/Testcontainers.ServiceBus/ServiceBusContainer.cs index 0d676f809..81c971fbd 100644 --- a/src/Testcontainers.ServiceBus/ServiceBusContainer.cs +++ b/src/Testcontainers.ServiceBus/ServiceBusContainer.cs @@ -33,8 +33,11 @@ public string GetConnectionString() /// protected override async Task UnsafeCreateAsync(CancellationToken ct = default) { - await _configuration.Networks.Single().CreateAsync(ct) - .ConfigureAwait(false); + foreach (var network in _configuration.Networks) + { + await network.CreateAsync(ct) + .ConfigureAwait(false); + } await base.UnsafeCreateAsync(ct) .ConfigureAwait(false); From 632aba5637795ea22baafbbb32545503bf6ffd55 Mon Sep 17 00:00:00 2001 From: lgcmotta Date: Sat, 11 Jan 2025 18:12:19 -0300 Subject: [PATCH 03/19] test: create test with custom instance of MsSqlContainer to attach to ServiceBusContainer --- ...rviceBusContainerWithMsSqlContainerTest.cs | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerWithMsSqlContainerTest.cs diff --git a/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerWithMsSqlContainerTest.cs b/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerWithMsSqlContainerTest.cs new file mode 100644 index 000000000..466342166 --- /dev/null +++ b/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerWithMsSqlContainerTest.cs @@ -0,0 +1,69 @@ +using DotNet.Testcontainers.Builders; +using DotNet.Testcontainers.Networks; +using Testcontainers.MsSql; + +namespace Testcontainers.ServiceBus; + +public class ServiceBusContainerWithMsSqlContainerTest : IAsyncLifetime +{ + private static readonly INetwork Network = new NetworkBuilder().Build(); + + private static readonly MsSqlContainer MsSqlContainer = new MsSqlBuilder() + .WithImage("mcr.microsoft.com/azure-sql-edge:latest") + .WithNetwork(Network) + .WithNetworkAliases("sql-server") + .Build(); + + private readonly ServiceBusContainer _serviceBusContainer = new ServiceBusBuilder() + .WithNetwork(Network) + .WithAcceptLicenseAgreement(true) + .WithMsSqlContainer(MsSqlContainer, "sql-server") + .Build(); + + public async Task InitializeAsync() + { + await MsSqlContainer.StartAsync(); + await _serviceBusContainer.StartAsync(); + } + + public async Task DisposeAsync() + { + await MsSqlContainer.DisposeAsync(); + await _serviceBusContainer.DisposeAsync(); + await Network.DisposeAsync(); + } + + [Fact] + [Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))] + public async Task ReceiveMessageReturnsSentMessage() + { + // Given + const string helloServiceBus = "Hello, Service Bus!"; + + // By default, the emulator uses the following configuration: + // https://learn.microsoft.com/en-us/azure/service-bus-messaging/test-locally-with-service-bus-emulator?tabs=automated-script#interact-with-the-emulator. + + // Upload a custom configuration before the container starts using the + // `WithResourceMapping(string, string)` API or one of its overloads: + // `WithResourceMapping("Config.json", "/ServiceBus_Emulator/ConfigFiles/")`. + const string queueName = "queue.1"; + + var message = new ServiceBusMessage(helloServiceBus); + + await using var client = new ServiceBusClient(_serviceBusContainer.GetConnectionString()); + + var sender = client.CreateSender(queueName); + + var receiver = client.CreateReceiver(queueName); + + // When + await sender.SendMessageAsync(message) + .ConfigureAwait(true); + + var receivedMessage = await receiver.ReceiveMessageAsync() + .ConfigureAwait(true); + + // Then + Assert.Equal(helloServiceBus, receivedMessage.Body.ToString()); + } +} \ No newline at end of file From 761cea3edc58993d20177d224b1dbb991b00aed8 Mon Sep 17 00:00:00 2001 From: lgcmotta Date: Sat, 11 Jan 2025 18:27:10 -0300 Subject: [PATCH 04/19] docs: enhance summary documentation for WithMsSqlContainer --- .../ServiceBusBuilder.cs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Testcontainers.ServiceBus/ServiceBusBuilder.cs b/src/Testcontainers.ServiceBus/ServiceBusBuilder.cs index ee98bf4f5..269bea1ea 100644 --- a/src/Testcontainers.ServiceBus/ServiceBusBuilder.cs +++ b/src/Testcontainers.ServiceBus/ServiceBusBuilder.cs @@ -53,19 +53,20 @@ public ServiceBusBuilder WithAcceptLicenseAgreement(bool acceptLicenseAgreement) var licenseAgreement = acceptLicenseAgreement ? AcceptLicenseAgreement : DeclineLicenseAgreement; return WithEnvironment(AcceptLicenseAgreementEnvVar, licenseAgreement); } - + /// - /// Attaches the dependent to the Azure Service Bus Emulator. + /// Sets the dependent MSSQL container for the Azure Service Bus Emulator. /// /// - /// When an exitent instance of is attached to Azure Service Bus Emulator, please ensure that both containers are in the same network. + /// This method allows attaching an existing SQL Server container instance to the Azure Service Bus Emulator. + /// The containers must be in the same network to enable communication between them. /// - /// An instance of - /// The dependent MSSQL container network alias. - /// The dependent MSSQL container SA password. - /// A configured instance of . + /// An existing instance of to use as the database backend. + /// The network alias that will be used to connect to the SQL Server container. + /// The SA password for the SQL Server container. Defaults to . + /// A configured instance of . public ServiceBusBuilder WithMsSqlContainer( - MsSqlContainer msSqlContainer, + MsSqlContainer msSqlContainer, string databaseNetworkAlias, string saPassword = MsSqlBuilder.DefaultPassword) { From 2f74cd335067e94cb7117db0f902098822e5c037 Mon Sep 17 00:00:00 2001 From: lgcmotta Date: Sun, 12 Jan 2025 15:36:19 -0300 Subject: [PATCH 05/19] refactor: remove network and mssql start from asb emulator test --- ...ServiceBusContainerWithCustomMsSqlTest.cs} | 31 +++++++------------ .../Testcontainers.ServiceBus.Tests/Usings.cs | 3 ++ 2 files changed, 14 insertions(+), 20 deletions(-) rename tests/Testcontainers.ServiceBus.Tests/{ServiceBusContainerWithMsSqlContainerTest.cs => ServiceBusContainerWithCustomMsSqlTest.cs} (66%) diff --git a/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerWithMsSqlContainerTest.cs b/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerWithCustomMsSqlTest.cs similarity index 66% rename from tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerWithMsSqlContainerTest.cs rename to tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerWithCustomMsSqlTest.cs index 466342166..83fd9ce18 100644 --- a/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerWithMsSqlContainerTest.cs +++ b/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerWithCustomMsSqlTest.cs @@ -1,36 +1,27 @@ -using DotNet.Testcontainers.Builders; -using DotNet.Testcontainers.Networks; -using Testcontainers.MsSql; +namespace Testcontainers.ServiceBus; -namespace Testcontainers.ServiceBus; - -public class ServiceBusContainerWithMsSqlContainerTest : IAsyncLifetime +public class ServiceBusContainerWithCustomMsSqlTest : IAsyncLifetime { private static readonly INetwork Network = new NetworkBuilder().Build(); - private static readonly MsSqlContainer MsSqlContainer = new MsSqlBuilder() - .WithImage("mcr.microsoft.com/azure-sql-edge:latest") - .WithNetwork(Network) - .WithNetworkAliases("sql-server") - .Build(); - private readonly ServiceBusContainer _serviceBusContainer = new ServiceBusBuilder() .WithNetwork(Network) .WithAcceptLicenseAgreement(true) - .WithMsSqlContainer(MsSqlContainer, "sql-server") + .WithMsSqlContainer(new MsSqlBuilder() + .WithImage("mcr.microsoft.com/azure-sql-edge:latest") + .WithNetwork(Network) + .WithNetworkAliases("sql-server") + .Build(), "sql-server") .Build(); - public async Task InitializeAsync() + public Task InitializeAsync() { - await MsSqlContainer.StartAsync(); - await _serviceBusContainer.StartAsync(); + return _serviceBusContainer.StartAsync(); } - public async Task DisposeAsync() + public Task DisposeAsync() { - await MsSqlContainer.DisposeAsync(); - await _serviceBusContainer.DisposeAsync(); - await Network.DisposeAsync(); + return _serviceBusContainer.DisposeAsync().AsTask(); } [Fact] diff --git a/tests/Testcontainers.ServiceBus.Tests/Usings.cs b/tests/Testcontainers.ServiceBus.Tests/Usings.cs index 9ddd0ad38..2f0b642b6 100644 --- a/tests/Testcontainers.ServiceBus.Tests/Usings.cs +++ b/tests/Testcontainers.ServiceBus.Tests/Usings.cs @@ -1,5 +1,8 @@ global using System; global using System.Threading.Tasks; global using Azure.Messaging.ServiceBus; +global using DotNet.Testcontainers.Builders; global using DotNet.Testcontainers.Commons; +global using DotNet.Testcontainers.Networks; +global using Testcontainers.MsSql; global using Xunit; \ No newline at end of file From fee3432ad51c7151ecbd6ba4827a6365fd4e2043 Mon Sep 17 00:00:00 2001 From: lgcmotta Date: Sun, 12 Jan 2025 15:36:54 -0300 Subject: [PATCH 06/19] refactor: enumerate networks to array before invoking create async --- src/Testcontainers.ServiceBus/ServiceBusContainer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Testcontainers.ServiceBus/ServiceBusContainer.cs b/src/Testcontainers.ServiceBus/ServiceBusContainer.cs index 81c971fbd..95de6d27c 100644 --- a/src/Testcontainers.ServiceBus/ServiceBusContainer.cs +++ b/src/Testcontainers.ServiceBus/ServiceBusContainer.cs @@ -33,7 +33,7 @@ public string GetConnectionString() /// protected override async Task UnsafeCreateAsync(CancellationToken ct = default) { - foreach (var network in _configuration.Networks) + foreach (var network in _configuration.Networks.ToArray()) { await network.CreateAsync(ct) .ConfigureAwait(false); From fee711094a357ae68919e8251382f7b2a00cbc4f Mon Sep 17 00:00:00 2001 From: lgcmotta Date: Sun, 12 Jan 2025 15:38:21 -0300 Subject: [PATCH 07/19] fix: exclude implicit imported IContainer types when checking for IDatabaseContainer implementations --- .../DatabasesContainerTest.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/Testcontainers.Databases.Tests/DatabasesContainerTest.cs b/tests/Testcontainers.Databases.Tests/DatabasesContainerTest.cs index 0c637fedf..a455431c1 100644 --- a/tests/Testcontainers.Databases.Tests/DatabasesContainerTest.cs +++ b/tests/Testcontainers.Databases.Tests/DatabasesContainerTest.cs @@ -37,9 +37,18 @@ public static TheoryData GetContainerImplementations(bool expectDataProvid foreach (var testAssembly in testAssemblies) { + var testAssemblyName = testAssembly.Key.GetName().Name; + // TODO: If a module contains multiple container implementations, it would require all container implementations to implement the interface. foreach (var containerType in testAssembly.Value.Where(type => type.IsAssignableTo(typeof(IContainer)))) { + var typeAssemblyName = containerType.Assembly.GetName().Name; + + if (!string.IsNullOrWhiteSpace(testAssemblyName) && !string.IsNullOrWhiteSpace(typeAssemblyName) && !testAssemblyName.Contains(typeAssemblyName)) + { + continue; + } + var hasDataProvider = testAssembly.Value.Exists(type => type.IsSubclassOf(typeof(DbProviderFactory))); if (expectDataProvider && hasDataProvider) From 89c3040bf4d59eb722946f3f57e87cf694eb9e33 Mon Sep 17 00:00:00 2001 From: lgcmotta Date: Mon, 20 Jan 2025 01:11:56 -0300 Subject: [PATCH 08/19] test: move service bus container test to abstract class and resolve containers from derived classes --- .../ServiceBusContainerTest.cs | 32 +++++++++- .../ServiceBusContainerWithCustomMsSqlTest.cs | 60 ------------------- .../Testcontainers.ServiceBus.Tests/Usings.cs | 1 + 3 files changed, 30 insertions(+), 63 deletions(-) delete mode 100644 tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerWithCustomMsSqlTest.cs diff --git a/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs b/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs index d35a88117..014fd078a 100644 --- a/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs +++ b/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs @@ -1,8 +1,15 @@ namespace Testcontainers.ServiceBus; -public sealed class ServiceBusContainerTest : IAsyncLifetime +public abstract class ServiceBusContainerTest : IAsyncLifetime { - private readonly ServiceBusContainer _serviceBusContainer = new ServiceBusBuilder().WithAcceptLicenseAgreement(true).Build(); + // private readonly ServiceBusContainer _serviceBusContainer = new ServiceBusBuilder().WithAcceptLicenseAgreement(true).Build(); + + private readonly ServiceBusContainer _serviceBusContainer; + + public ServiceBusContainerTest(ServiceBusContainer serviceBusContainer) + { + _serviceBusContainer = serviceBusContainer; + } public Task InitializeAsync() { @@ -47,4 +54,23 @@ await sender.SendMessageAsync(message) // Then Assert.Equal(helloServiceBus, receivedMessage.Body.ToString()); } -} \ No newline at end of file +} + +[UsedImplicitly] +public sealed class ServiceBusContainerWithCustomMsSqlTest() : ServiceBusContainerTest(new ServiceBusBuilder() + .WithNetwork(Network) + .WithAcceptLicenseAgreement(true) + .WithMsSqlContainer(new MsSqlBuilder() + .WithImage("mcr.microsoft.com/azure-sql-edge:latest") + .WithNetwork(Network) + .WithNetworkAliases("sql-server") + .Build(), "sql-server") + .Build()) +{ + private static readonly INetwork Network = new NetworkBuilder().Build(); +} + +[UsedImplicitly] +public sealed class ServiceBusContainerWithDefaultMsSqlTest() : ServiceBusContainerTest(new ServiceBusBuilder() + .WithAcceptLicenseAgreement(true) + .Build()); \ No newline at end of file diff --git a/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerWithCustomMsSqlTest.cs b/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerWithCustomMsSqlTest.cs deleted file mode 100644 index 83fd9ce18..000000000 --- a/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerWithCustomMsSqlTest.cs +++ /dev/null @@ -1,60 +0,0 @@ -namespace Testcontainers.ServiceBus; - -public class ServiceBusContainerWithCustomMsSqlTest : IAsyncLifetime -{ - private static readonly INetwork Network = new NetworkBuilder().Build(); - - private readonly ServiceBusContainer _serviceBusContainer = new ServiceBusBuilder() - .WithNetwork(Network) - .WithAcceptLicenseAgreement(true) - .WithMsSqlContainer(new MsSqlBuilder() - .WithImage("mcr.microsoft.com/azure-sql-edge:latest") - .WithNetwork(Network) - .WithNetworkAliases("sql-server") - .Build(), "sql-server") - .Build(); - - public Task InitializeAsync() - { - return _serviceBusContainer.StartAsync(); - } - - public Task DisposeAsync() - { - return _serviceBusContainer.DisposeAsync().AsTask(); - } - - [Fact] - [Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))] - public async Task ReceiveMessageReturnsSentMessage() - { - // Given - const string helloServiceBus = "Hello, Service Bus!"; - - // By default, the emulator uses the following configuration: - // https://learn.microsoft.com/en-us/azure/service-bus-messaging/test-locally-with-service-bus-emulator?tabs=automated-script#interact-with-the-emulator. - - // Upload a custom configuration before the container starts using the - // `WithResourceMapping(string, string)` API or one of its overloads: - // `WithResourceMapping("Config.json", "/ServiceBus_Emulator/ConfigFiles/")`. - const string queueName = "queue.1"; - - var message = new ServiceBusMessage(helloServiceBus); - - await using var client = new ServiceBusClient(_serviceBusContainer.GetConnectionString()); - - var sender = client.CreateSender(queueName); - - var receiver = client.CreateReceiver(queueName); - - // When - await sender.SendMessageAsync(message) - .ConfigureAwait(true); - - var receivedMessage = await receiver.ReceiveMessageAsync() - .ConfigureAwait(true); - - // Then - Assert.Equal(helloServiceBus, receivedMessage.Body.ToString()); - } -} \ No newline at end of file diff --git a/tests/Testcontainers.ServiceBus.Tests/Usings.cs b/tests/Testcontainers.ServiceBus.Tests/Usings.cs index 2f0b642b6..271b2f691 100644 --- a/tests/Testcontainers.ServiceBus.Tests/Usings.cs +++ b/tests/Testcontainers.ServiceBus.Tests/Usings.cs @@ -4,5 +4,6 @@ global using DotNet.Testcontainers.Builders; global using DotNet.Testcontainers.Commons; global using DotNet.Testcontainers.Networks; +global using JetBrains.Annotations; global using Testcontainers.MsSql; global using Xunit; \ No newline at end of file From 8a5f3a0b613446a55e7eae1cc7998e439d536a9a Mon Sep 17 00:00:00 2001 From: lgcmotta Date: Mon, 20 Jan 2025 01:14:26 -0300 Subject: [PATCH 09/19] refactor: reuse public WithMsSqlContainer when invoking private WithMsSqlContainer for default setup --- src/Testcontainers.ServiceBus/ServiceBusBuilder.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Testcontainers.ServiceBus/ServiceBusBuilder.cs b/src/Testcontainers.ServiceBus/ServiceBusBuilder.cs index 269bea1ea..0c1a8d547 100644 --- a/src/Testcontainers.ServiceBus/ServiceBusBuilder.cs +++ b/src/Testcontainers.ServiceBus/ServiceBusBuilder.cs @@ -140,9 +140,7 @@ private ServiceBusBuilder WithMsSqlContainer() .WithNetworkAliases(DatabaseNetworkAlias) .Build(); - return Merge(DockerResourceConfiguration, new ServiceBusConfiguration(databaseContainer: msSqlContainer)) - .WithEnvironment("MSSQL_SA_PASSWORD", MsSqlBuilder.DefaultPassword) - .WithEnvironment("SQL_SERVER", DatabaseNetworkAlias); + return WithMsSqlContainer(msSqlContainer, DatabaseNetworkAlias); } /// From 562ffd34640c2b8dc41e759ca74d4da9324bd774 Mon Sep 17 00:00:00 2001 From: lgcmotta Date: Mon, 20 Jan 2025 01:16:14 -0300 Subject: [PATCH 10/19] refactor: change summary docs to consistently use MSSQL instead of SQL --- src/Testcontainers.ServiceBus/ServiceBusBuilder.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Testcontainers.ServiceBus/ServiceBusBuilder.cs b/src/Testcontainers.ServiceBus/ServiceBusBuilder.cs index 0c1a8d547..c02579621 100644 --- a/src/Testcontainers.ServiceBus/ServiceBusBuilder.cs +++ b/src/Testcontainers.ServiceBus/ServiceBusBuilder.cs @@ -58,22 +58,22 @@ public ServiceBusBuilder WithAcceptLicenseAgreement(bool acceptLicenseAgreement) /// Sets the dependent MSSQL container for the Azure Service Bus Emulator. /// /// - /// This method allows attaching an existing SQL Server container instance to the Azure Service Bus Emulator. + /// This method allows attaching an existing MSSQL Server container instance to the Azure Service Bus Emulator. /// The containers must be in the same network to enable communication between them. /// /// An existing instance of to use as the database backend. - /// The network alias that will be used to connect to the SQL Server container. + /// The network alias that will be used to connect to the MSSQL Server container. /// The SA password for the SQL Server container. Defaults to . /// A configured instance of . public ServiceBusBuilder WithMsSqlContainer( MsSqlContainer msSqlContainer, - string databaseNetworkAlias, + string msSqlNetworkAlias, string saPassword = MsSqlBuilder.DefaultPassword) { return Merge(DockerResourceConfiguration, new ServiceBusConfiguration(databaseContainer: msSqlContainer)) .DependsOn(msSqlContainer) .WithEnvironment("MSSQL_SA_PASSWORD", saPassword) - .WithEnvironment("SQL_SERVER", databaseNetworkAlias); + .WithEnvironment("SQL_SERVER", msSqlNetworkAlias); } /// From 1d4547fe5cd81b27959d64360842320bf4d3f10f Mon Sep 17 00:00:00 2001 From: lgcmotta Date: Mon, 20 Jan 2025 01:19:55 -0300 Subject: [PATCH 11/19] fix: remove commented code --- .../Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs b/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs index 014fd078a..f66572dea 100644 --- a/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs +++ b/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs @@ -2,8 +2,6 @@ namespace Testcontainers.ServiceBus; public abstract class ServiceBusContainerTest : IAsyncLifetime { - // private readonly ServiceBusContainer _serviceBusContainer = new ServiceBusBuilder().WithAcceptLicenseAgreement(true).Build(); - private readonly ServiceBusContainer _serviceBusContainer; public ServiceBusContainerTest(ServiceBusContainer serviceBusContainer) From d78b7411e0c176265c7858e2770e655c2d70d056 Mon Sep 17 00:00:00 2001 From: lgcmotta Date: Mon, 20 Jan 2025 01:23:43 -0300 Subject: [PATCH 12/19] refactor: make ServiceBusContainerTest constructor protected --- .../Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs b/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs index f66572dea..17a9cca7a 100644 --- a/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs +++ b/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs @@ -4,7 +4,7 @@ public abstract class ServiceBusContainerTest : IAsyncLifetime { private readonly ServiceBusContainer _serviceBusContainer; - public ServiceBusContainerTest(ServiceBusContainer serviceBusContainer) + protected ServiceBusContainerTest(ServiceBusContainer serviceBusContainer) { _serviceBusContainer = serviceBusContainer; } From 9611f8316e37caf9ff1eabe021f81e2a82682c89 Mon Sep 17 00:00:00 2001 From: lgcmotta Date: Mon, 20 Jan 2025 01:24:38 -0300 Subject: [PATCH 13/19] refactor: place service bus test with default MSSQL before the custom --- .../ServiceBusContainerTest.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs b/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs index 17a9cca7a..4bcae7adb 100644 --- a/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs +++ b/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs @@ -54,6 +54,11 @@ await sender.SendMessageAsync(message) } } +[UsedImplicitly] +public sealed class ServiceBusContainerWithDefaultMsSqlTest() : ServiceBusContainerTest(new ServiceBusBuilder() + .WithAcceptLicenseAgreement(true) + .Build()); + [UsedImplicitly] public sealed class ServiceBusContainerWithCustomMsSqlTest() : ServiceBusContainerTest(new ServiceBusBuilder() .WithNetwork(Network) @@ -66,9 +71,4 @@ public sealed class ServiceBusContainerWithCustomMsSqlTest() : ServiceBusContain .Build()) { private static readonly INetwork Network = new NetworkBuilder().Build(); -} - -[UsedImplicitly] -public sealed class ServiceBusContainerWithDefaultMsSqlTest() : ServiceBusContainerTest(new ServiceBusBuilder() - .WithAcceptLicenseAgreement(true) - .Build()); \ No newline at end of file +} \ No newline at end of file From 952839b5d3fe961f99382abbbb1723d24bcffa54 Mon Sep 17 00:00:00 2001 From: lgcmotta Date: Mon, 20 Jan 2025 01:39:37 -0300 Subject: [PATCH 14/19] fix: commit 8a5f3a0 causes MSSQL container to be duplicated when using a custom instance --- src/Testcontainers.ServiceBus/ServiceBusBuilder.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Testcontainers.ServiceBus/ServiceBusBuilder.cs b/src/Testcontainers.ServiceBus/ServiceBusBuilder.cs index c02579621..4fd86f6f9 100644 --- a/src/Testcontainers.ServiceBus/ServiceBusBuilder.cs +++ b/src/Testcontainers.ServiceBus/ServiceBusBuilder.cs @@ -140,7 +140,9 @@ private ServiceBusBuilder WithMsSqlContainer() .WithNetworkAliases(DatabaseNetworkAlias) .Build(); - return WithMsSqlContainer(msSqlContainer, DatabaseNetworkAlias); + return Merge(DockerResourceConfiguration, new ServiceBusConfiguration(databaseContainer: msSqlContainer)) + .WithEnvironment("MSSQL_SA_PASSWORD", MsSqlBuilder.DefaultPassword) + .WithEnvironment("SQL_SERVER", DatabaseNetworkAlias); } /// From 4aa61f22186b9a686a0b59334bcbdee13cf7562e Mon Sep 17 00:00:00 2001 From: lgcmotta Date: Mon, 20 Jan 2025 01:42:39 -0300 Subject: [PATCH 15/19] refactor: move ASB emulator tests to inside ServiceBusContainerTest class with private constructor --- .../ServiceBusContainerTest.cs | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs b/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs index 4bcae7adb..39a208efc 100644 --- a/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs +++ b/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs @@ -4,7 +4,7 @@ public abstract class ServiceBusContainerTest : IAsyncLifetime { private readonly ServiceBusContainer _serviceBusContainer; - protected ServiceBusContainerTest(ServiceBusContainer serviceBusContainer) + private ServiceBusContainerTest(ServiceBusContainer serviceBusContainer) { _serviceBusContainer = serviceBusContainer; } @@ -52,23 +52,23 @@ await sender.SendMessageAsync(message) // Then Assert.Equal(helloServiceBus, receivedMessage.Body.ToString()); } -} - -[UsedImplicitly] -public sealed class ServiceBusContainerWithDefaultMsSqlTest() : ServiceBusContainerTest(new ServiceBusBuilder() - .WithAcceptLicenseAgreement(true) - .Build()); - -[UsedImplicitly] -public sealed class ServiceBusContainerWithCustomMsSqlTest() : ServiceBusContainerTest(new ServiceBusBuilder() - .WithNetwork(Network) - .WithAcceptLicenseAgreement(true) - .WithMsSqlContainer(new MsSqlBuilder() - .WithImage("mcr.microsoft.com/azure-sql-edge:latest") + + [UsedImplicitly] + public sealed class ServiceBusContainerWithDefaultMsSqlTest() : ServiceBusContainerTest(new ServiceBusBuilder() + .WithAcceptLicenseAgreement(true) + .Build()); + + [UsedImplicitly] + public sealed class ServiceBusContainerWithCustomMsSqlTest() : ServiceBusContainerTest(new ServiceBusBuilder() .WithNetwork(Network) - .WithNetworkAliases("sql-server") - .Build(), "sql-server") - .Build()) -{ - private static readonly INetwork Network = new NetworkBuilder().Build(); + .WithAcceptLicenseAgreement(true) + .WithMsSqlContainer(new MsSqlBuilder() + .WithImage("mcr.microsoft.com/azure-sql-edge:latest") + .WithNetwork(Network) + .WithNetworkAliases("sql-server") + .Build(), "sql-server") + .Build()) + { + private static readonly INetwork Network = new NetworkBuilder().Build(); + } } \ No newline at end of file From c54334cca2ef0d66a16bc6d5342760ace21eebe3 Mon Sep 17 00:00:00 2001 From: lgcmotta Date: Mon, 20 Jan 2025 21:50:01 -0300 Subject: [PATCH 16/19] chore: reverting `Single` invoke when creating container's network --- src/Testcontainers.ServiceBus/ServiceBusContainer.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Testcontainers.ServiceBus/ServiceBusContainer.cs b/src/Testcontainers.ServiceBus/ServiceBusContainer.cs index 95de6d27c..0d676f809 100644 --- a/src/Testcontainers.ServiceBus/ServiceBusContainer.cs +++ b/src/Testcontainers.ServiceBus/ServiceBusContainer.cs @@ -33,11 +33,8 @@ public string GetConnectionString() /// protected override async Task UnsafeCreateAsync(CancellationToken ct = default) { - foreach (var network in _configuration.Networks.ToArray()) - { - await network.CreateAsync(ct) - .ConfigureAwait(false); - } + await _configuration.Networks.Single().CreateAsync(ct) + .ConfigureAwait(false); await base.UnsafeCreateAsync(ct) .ConfigureAwait(false); From a4ef713b8223fa07444c12c7a3b5287b969cbaff Mon Sep 17 00:00:00 2001 From: lgcmotta Date: Mon, 20 Jan 2025 21:51:10 -0300 Subject: [PATCH 17/19] feat: move defaults (network and MSSQL) setup from Init to Build --- .../ServiceBusBuilder.cs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Testcontainers.ServiceBus/ServiceBusBuilder.cs b/src/Testcontainers.ServiceBus/ServiceBusBuilder.cs index 4fd86f6f9..0df057604 100644 --- a/src/Testcontainers.ServiceBus/ServiceBusBuilder.cs +++ b/src/Testcontainers.ServiceBus/ServiceBusBuilder.cs @@ -80,7 +80,15 @@ public ServiceBusBuilder WithMsSqlContainer( public override ServiceBusContainer Build() { Validate(); - return new ServiceBusContainer(DockerResourceConfiguration); + + if (DockerResourceConfiguration.DatabaseContainer is not null) + { + return new ServiceBusContainer(DockerResourceConfiguration); + } + + var serviceBusBuilder = WithNetwork(new NetworkBuilder().Build()).WithMsSqlContainer(); + + return new ServiceBusContainer(serviceBusBuilder.DockerResourceConfiguration); } /// @@ -102,10 +110,8 @@ protected override ServiceBusBuilder Init() { return base.Init() .WithImage(ServiceBusImage) - .WithNetwork(new NetworkBuilder().Build()) .WithNetworkAliases(ServiceBusNetworkAlias) .WithPortBinding(ServiceBusPort, true) - .WithMsSqlContainer() .WithWaitStrategy(Wait.ForUnixContainer() .UntilMessageIsLogged("Emulator Service is Successfully Up!") .AddCustomWaitStrategy(new WaitTwoSeconds())); @@ -140,9 +146,7 @@ private ServiceBusBuilder WithMsSqlContainer() .WithNetworkAliases(DatabaseNetworkAlias) .Build(); - return Merge(DockerResourceConfiguration, new ServiceBusConfiguration(databaseContainer: msSqlContainer)) - .WithEnvironment("MSSQL_SA_PASSWORD", MsSqlBuilder.DefaultPassword) - .WithEnvironment("SQL_SERVER", DatabaseNetworkAlias); + return WithMsSqlContainer(msSqlContainer, DatabaseNetworkAlias); } /// From a8b822b9a7c409a2ad13903d0cc9667293468208 Mon Sep 17 00:00:00 2001 From: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com> Date: Fri, 31 Jan 2025 20:50:54 +0100 Subject: [PATCH 18/19] refactor: Rearrange default MSSQL configuration --- .../ServiceBusBuilder.cs | 67 +++++++++---------- src/Testcontainers.ServiceBus/Usings.cs | 3 +- .../Builders/IContainerBuilder`2.cs | 2 +- .../ServiceBusContainerTest.cs | 48 +++++++++---- 4 files changed, 69 insertions(+), 51 deletions(-) diff --git a/src/Testcontainers.ServiceBus/ServiceBusBuilder.cs b/src/Testcontainers.ServiceBus/ServiceBusBuilder.cs index 0df057604..278a85cf7 100644 --- a/src/Testcontainers.ServiceBus/ServiceBusBuilder.cs +++ b/src/Testcontainers.ServiceBus/ServiceBusBuilder.cs @@ -58,22 +58,27 @@ public ServiceBusBuilder WithAcceptLicenseAgreement(bool acceptLicenseAgreement) /// Sets the dependent MSSQL container for the Azure Service Bus Emulator. /// /// - /// This method allows attaching an existing MSSQL Server container instance to the Azure Service Bus Emulator. - /// The containers must be in the same network to enable communication between them. + /// This method allows an existing MSSQL container to be attached to the Azure Service + /// Bus Emulator. The containers must be on the same network to enable communication + /// between them. /// - /// An existing instance of to use as the database backend. - /// The network alias that will be used to connect to the MSSQL Server container. - /// The SA password for the SQL Server container. Defaults to . - /// A configured instance of . + /// The network to connect the container to. + /// The MSSQL container. + /// The MSSQL container network alias. + /// The MSSQL container password. + /// A configured instance of . public ServiceBusBuilder WithMsSqlContainer( - MsSqlContainer msSqlContainer, - string msSqlNetworkAlias, - string saPassword = MsSqlBuilder.DefaultPassword) + INetwork network, + MsSqlContainer container, + string networkAlias, + string password = MsSqlBuilder.DefaultPassword) { - return Merge(DockerResourceConfiguration, new ServiceBusConfiguration(databaseContainer: msSqlContainer)) - .DependsOn(msSqlContainer) - .WithEnvironment("MSSQL_SA_PASSWORD", saPassword) - .WithEnvironment("SQL_SERVER", msSqlNetworkAlias); + return Merge(DockerResourceConfiguration, new ServiceBusConfiguration(databaseContainer: container)) + .DependsOn(container) + .WithNetwork(network) + .WithNetworkAliases(ServiceBusNetworkAlias) + .WithEnvironment("SQL_SERVER", networkAlias) + .WithEnvironment("MSSQL_SA_PASSWORD", password); } /// @@ -81,14 +86,23 @@ public override ServiceBusContainer Build() { Validate(); - if (DockerResourceConfiguration.DatabaseContainer is not null) + if (DockerResourceConfiguration.DatabaseContainer != null) { return new ServiceBusContainer(DockerResourceConfiguration); } - - var serviceBusBuilder = WithNetwork(new NetworkBuilder().Build()).WithMsSqlContainer(); - - return new ServiceBusContainer(serviceBusBuilder.DockerResourceConfiguration); + + // If the user has not provided an existing MSSQL container instance, + // we configure one. + var network = new NetworkBuilder() + .Build(); + + var container = new MsSqlBuilder() + .WithNetwork(network) + .WithNetworkAliases(DatabaseNetworkAlias) + .Build(); + + var serviceBusContainer = WithMsSqlContainer(network, container, DatabaseNetworkAlias); + return new ServiceBusContainer(serviceBusContainer.DockerResourceConfiguration); } /// @@ -110,7 +124,6 @@ protected override ServiceBusBuilder Init() { return base.Init() .WithImage(ServiceBusImage) - .WithNetworkAliases(ServiceBusNetworkAlias) .WithPortBinding(ServiceBusPort, true) .WithWaitStrategy(Wait.ForUnixContainer() .UntilMessageIsLogged("Emulator Service is Successfully Up!") @@ -135,23 +148,9 @@ protected override ServiceBusBuilder Merge(ServiceBusConfiguration oldValue, Ser return new ServiceBusBuilder(new ServiceBusConfiguration(oldValue, newValue)); } - /// - /// Configures the dependent MSSQL container. - /// - /// A configured instance of . - private ServiceBusBuilder WithMsSqlContainer() - { - var msSqlContainer = new MsSqlBuilder() - .WithNetwork(DockerResourceConfiguration.Networks.Single()) - .WithNetworkAliases(DatabaseNetworkAlias) - .Build(); - - return WithMsSqlContainer(msSqlContainer, DatabaseNetworkAlias); - } - /// /// - /// This is a workaround to ensure that the wait strategy does not indicate + /// This is a workaround to ensure that the wait strategy does not indicate /// readiness too early: /// https://github.com/Azure/azure-service-bus-emulator-installer/issues/35#issuecomment-2497164533. /// diff --git a/src/Testcontainers.ServiceBus/Usings.cs b/src/Testcontainers.ServiceBus/Usings.cs index 6f294326f..ea7628ae0 100644 --- a/src/Testcontainers.ServiceBus/Usings.cs +++ b/src/Testcontainers.ServiceBus/Usings.cs @@ -1,7 +1,6 @@ global using System; global using System.Collections.Generic; global using System.Linq; -global using System.Text.RegularExpressions; global using System.Threading; global using System.Threading.Tasks; global using Docker.DotNet.Models; @@ -9,6 +8,6 @@ global using DotNet.Testcontainers.Builders; global using DotNet.Testcontainers.Configurations; global using DotNet.Testcontainers.Containers; -global using DotNet.Testcontainers.Images; +global using DotNet.Testcontainers.Networks; global using JetBrains.Annotations; global using Testcontainers.MsSql; \ No newline at end of file diff --git a/src/Testcontainers/Builders/IContainerBuilder`2.cs b/src/Testcontainers/Builders/IContainerBuilder`2.cs index 1475881f8..26477db49 100644 --- a/src/Testcontainers/Builders/IContainerBuilder`2.cs +++ b/src/Testcontainers/Builders/IContainerBuilder`2.cs @@ -378,7 +378,7 @@ public interface IContainerBuilder : I /// /// Assigns the specified network to the container. /// - /// The network to connect container to. + /// The network to connect the container to. /// A configured instance of . [PublicAPI] TBuilderEntity WithNetwork(INetwork network); diff --git a/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs b/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs index 39a208efc..29fa40eba 100644 --- a/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs +++ b/tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs @@ -52,23 +52,43 @@ await sender.SendMessageAsync(message) // Then Assert.Equal(helloServiceBus, receivedMessage.Body.ToString()); } - + [UsedImplicitly] - public sealed class ServiceBusContainerWithDefaultMsSqlTest() : ServiceBusContainerTest(new ServiceBusBuilder() - .WithAcceptLicenseAgreement(true) - .Build()); + public sealed class ServiceBusDefaultMsSqlConfiguration : ServiceBusContainerTest + { + public ServiceBusDefaultMsSqlConfiguration() + : base(new ServiceBusBuilder().WithAcceptLicenseAgreement(true).Build()) + { + } + } [UsedImplicitly] - public sealed class ServiceBusContainerWithCustomMsSqlTest() : ServiceBusContainerTest(new ServiceBusBuilder() - .WithNetwork(Network) - .WithAcceptLicenseAgreement(true) - .WithMsSqlContainer(new MsSqlBuilder() - .WithImage("mcr.microsoft.com/azure-sql-edge:latest") - .WithNetwork(Network) - .WithNetworkAliases("sql-server") - .Build(), "sql-server") - .Build()) + public sealed class ServiceBusCustomMsSqlConfiguration : ServiceBusContainerTest, IClassFixture { - private static readonly INetwork Network = new NetworkBuilder().Build(); + public ServiceBusCustomMsSqlConfiguration(DatabaseFixture fixture) + : base(new ServiceBusBuilder().WithAcceptLicenseAgreement(true).WithMsSqlContainer(fixture.Network, fixture.Container, fixture.DatabaseNetworkAlias).Build()) + { + } + } + + [UsedImplicitly] + public sealed class DatabaseFixture + { + public DatabaseFixture() + { + Network = new NetworkBuilder() + .Build(); + + Container = new MsSqlBuilder() + .WithNetwork(Network) + .WithNetworkAliases(DatabaseNetworkAlias) + .Build(); + } + + public string DatabaseNetworkAlias => ServiceBusBuilder.DatabaseNetworkAlias; + + public INetwork Network { get; } + + public MsSqlContainer Container { get; } } } \ No newline at end of file From 01c16113b3aa46d47e583ba1a14a6a42d4892c5b Mon Sep 17 00:00:00 2001 From: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com> Date: Fri, 31 Jan 2025 20:54:16 +0100 Subject: [PATCH 19/19] chore: Remove using --- tests/Testcontainers.ServiceBus.Tests/Usings.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Testcontainers.ServiceBus.Tests/Usings.cs b/tests/Testcontainers.ServiceBus.Tests/Usings.cs index 271b2f691..1e0024b2c 100644 --- a/tests/Testcontainers.ServiceBus.Tests/Usings.cs +++ b/tests/Testcontainers.ServiceBus.Tests/Usings.cs @@ -1,4 +1,3 @@ -global using System; global using System.Threading.Tasks; global using Azure.Messaging.ServiceBus; global using DotNet.Testcontainers.Builders;