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(ServiceBus): Add support to use existing MSSQL container instances #1335

Merged
merged 23 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
bf3a42f
feat: add exposed API to attach MsSqlContainer to Azure Service Bus e…
lgcmotta Jan 11, 2025
3c3c2b9
feat: invoke CreateAsync for each network attached to ServiceBusConta…
lgcmotta Jan 11, 2025
632aba5
test: create test with custom instance of MsSqlContainer to attach to…
lgcmotta Jan 11, 2025
761cea3
docs: enhance summary documentation for WithMsSqlContainer
lgcmotta Jan 11, 2025
2f74cd3
refactor: remove network and mssql start from asb emulator test
lgcmotta Jan 12, 2025
fee3432
refactor: enumerate networks to array before invoking create async
lgcmotta Jan 12, 2025
fee7110
fix: exclude implicit imported IContainer types when checking for IDa…
lgcmotta Jan 12, 2025
209b2a6
Merge branch 'develop' into develop
lgcmotta Jan 15, 2025
89c3040
test: move service bus container test to abstract class and resolve c…
lgcmotta Jan 20, 2025
8a5f3a0
refactor: reuse public WithMsSqlContainer when invoking private WithM…
lgcmotta Jan 20, 2025
562ffd3
refactor: change summary docs to consistently use MSSQL instead of SQL
lgcmotta Jan 20, 2025
1d4547f
fix: remove commented code
lgcmotta Jan 20, 2025
d78b741
refactor: make ServiceBusContainerTest constructor protected
lgcmotta Jan 20, 2025
9611f83
refactor: place service bus test with default MSSQL before the custom
lgcmotta Jan 20, 2025
952839b
fix: commit 8a5f3a0 causes MSSQL container to be duplicated when usin…
lgcmotta Jan 20, 2025
4aa61f2
refactor: move ASB emulator tests to inside ServiceBusContainerTest c…
lgcmotta Jan 20, 2025
c54334c
chore: reverting `Single` invoke when creating container's network
lgcmotta Jan 21, 2025
a4ef713
feat: move defaults (network and MSSQL) setup from Init to Build
lgcmotta Jan 21, 2025
791f694
chore: merge upstream/develop into develop
lgcmotta Jan 22, 2025
a8b822b
refactor: Rearrange default MSSQL configuration
HofmeisterAn Jan 31, 2025
01c1611
chore: Remove using
HofmeisterAn Jan 31, 2025
e3ce1df
Merge remote-tracking branch 'origin/develop' into fork/lgcmotta/develop
HofmeisterAn Jan 31, 2025
79e36e1
Merge branch 'develop' into develop
lgcmotta Feb 3, 2025
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
67 changes: 46 additions & 21 deletions src/Testcontainers.ServiceBus/ServiceBusBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,55 @@ public ServiceBusBuilder WithAcceptLicenseAgreement(bool acceptLicenseAgreement)
return WithEnvironment(AcceptLicenseAgreementEnvVar, licenseAgreement);
}

/// <summary>
/// Sets the dependent MSSQL container for the Azure Service Bus Emulator.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
/// <param name="network">The network to connect the container to.</param>
/// <param name="container">The MSSQL container.</param>
/// <param name="networkAlias">The MSSQL container network alias.</param>
/// <param name="password">The MSSQL container password.</param>
/// <returns>A configured instance of <see cref="ServiceBusBuilder" />.</returns>
public ServiceBusBuilder WithMsSqlContainer(
INetwork network,
MsSqlContainer container,
string networkAlias,
string password = MsSqlBuilder.DefaultPassword)
{
return Merge(DockerResourceConfiguration, new ServiceBusConfiguration(databaseContainer: container))
.DependsOn(container)
.WithNetwork(network)
.WithNetworkAliases(ServiceBusNetworkAlias)
.WithEnvironment("SQL_SERVER", networkAlias)
.WithEnvironment("MSSQL_SA_PASSWORD", password);
}

/// <inheritdoc />
public override ServiceBusContainer Build()
{
Validate();
return new ServiceBusContainer(DockerResourceConfiguration);

if (DockerResourceConfiguration.DatabaseContainer != null)
{
return new ServiceBusContainer(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);
}

/// <inheritdoc />
Expand All @@ -80,10 +124,7 @@ 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()));
Expand All @@ -107,25 +148,9 @@ protected override ServiceBusBuilder Merge(ServiceBusConfiguration oldValue, Ser
return new ServiceBusBuilder(new ServiceBusConfiguration(oldValue, newValue));
}

/// <summary>
/// Configures the dependent MSSQL container.
/// </summary>
/// <returns>A configured instance of <see cref="ServiceBusBuilder" />.</returns>
private ServiceBusBuilder WithMsSqlContainer()
{
var msSqlContainer = new MsSqlBuilder()
.WithNetwork(DockerResourceConfiguration.Networks.Single())
.WithNetworkAliases(DatabaseNetworkAlias)
.Build();

return Merge(DockerResourceConfiguration, new ServiceBusConfiguration(databaseContainer: msSqlContainer))
.WithEnvironment("MSSQL_SA_PASSWORD", MsSqlBuilder.DefaultPassword)
.WithEnvironment("SQL_SERVER", DatabaseNetworkAlias);
}

/// <inheritdoc cref="IWaitUntil" />
/// <remarks>
/// 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.
/// </remarks>
Expand Down
3 changes: 1 addition & 2 deletions src/Testcontainers.ServiceBus/Usings.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
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;
global using DotNet.Testcontainers;
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;
2 changes: 1 addition & 1 deletion src/Testcontainers/Builders/IContainerBuilder`2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ public interface IContainerBuilder<out TBuilderEntity, out TContainerEntity> : I
/// <summary>
/// Assigns the specified network to the container.
/// </summary>
/// <param name="network">The network to connect container to.</param>
/// <param name="network">The network to connect the container to.</param>
/// <returns>A configured instance of <typeparamref name="TBuilderEntity" />.</returns>
[PublicAPI]
TBuilderEntity WithNetwork(INetwork network);
Expand Down
48 changes: 46 additions & 2 deletions tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
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;

private ServiceBusContainerTest(ServiceBusContainer serviceBusContainer)
{
_serviceBusContainer = serviceBusContainer;
}

public Task InitializeAsync()
{
Expand Down Expand Up @@ -47,4 +52,43 @@ await sender.SendMessageAsync(message)
// Then
Assert.Equal(helloServiceBus, receivedMessage.Body.ToString());
}

[UsedImplicitly]
public sealed class ServiceBusDefaultMsSqlConfiguration : ServiceBusContainerTest
{
public ServiceBusDefaultMsSqlConfiguration()
: base(new ServiceBusBuilder().WithAcceptLicenseAgreement(true).Build())
{
}
}

[UsedImplicitly]
public sealed class ServiceBusCustomMsSqlConfiguration : ServiceBusContainerTest, IClassFixture<DatabaseFixture>
{
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; }
}
}
5 changes: 4 additions & 1 deletion tests/Testcontainers.ServiceBus.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -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 JetBrains.Annotations;
global using Testcontainers.MsSql;
global using Xunit;