Skip to content

Commit ae50c59

Browse files
feat(ServiceBus): Add support to use existing MSSQL container instances (#1335)
Co-authored-by: Andre Hofmeister <[email protected]>
1 parent 8ac4b0d commit ae50c59

File tree

5 files changed

+98
-27
lines changed

5 files changed

+98
-27
lines changed

src/Testcontainers.ServiceBus/ServiceBusBuilder.cs

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,55 @@ public ServiceBusBuilder WithAcceptLicenseAgreement(bool acceptLicenseAgreement)
5454
return WithEnvironment(AcceptLicenseAgreementEnvVar, licenseAgreement);
5555
}
5656

57+
/// <summary>
58+
/// Sets the dependent MSSQL container for the Azure Service Bus Emulator.
59+
/// </summary>
60+
/// <remarks>
61+
/// This method allows an existing MSSQL container to be attached to the Azure Service
62+
/// Bus Emulator. The containers must be on the same network to enable communication
63+
/// between them.
64+
/// </remarks>
65+
/// <param name="network">The network to connect the container to.</param>
66+
/// <param name="container">The MSSQL container.</param>
67+
/// <param name="networkAlias">The MSSQL container network alias.</param>
68+
/// <param name="password">The MSSQL container password.</param>
69+
/// <returns>A configured instance of <see cref="ServiceBusBuilder" />.</returns>
70+
public ServiceBusBuilder WithMsSqlContainer(
71+
INetwork network,
72+
MsSqlContainer container,
73+
string networkAlias,
74+
string password = MsSqlBuilder.DefaultPassword)
75+
{
76+
return Merge(DockerResourceConfiguration, new ServiceBusConfiguration(databaseContainer: container))
77+
.DependsOn(container)
78+
.WithNetwork(network)
79+
.WithNetworkAliases(ServiceBusNetworkAlias)
80+
.WithEnvironment("SQL_SERVER", networkAlias)
81+
.WithEnvironment("MSSQL_SA_PASSWORD", password);
82+
}
83+
5784
/// <inheritdoc />
5885
public override ServiceBusContainer Build()
5986
{
6087
Validate();
61-
return new ServiceBusContainer(DockerResourceConfiguration);
88+
89+
if (DockerResourceConfiguration.DatabaseContainer != null)
90+
{
91+
return new ServiceBusContainer(DockerResourceConfiguration);
92+
}
93+
94+
// If the user has not provided an existing MSSQL container instance,
95+
// we configure one.
96+
var network = new NetworkBuilder()
97+
.Build();
98+
99+
var container = new MsSqlBuilder()
100+
.WithNetwork(network)
101+
.WithNetworkAliases(DatabaseNetworkAlias)
102+
.Build();
103+
104+
var serviceBusContainer = WithMsSqlContainer(network, container, DatabaseNetworkAlias);
105+
return new ServiceBusContainer(serviceBusContainer.DockerResourceConfiguration);
62106
}
63107

64108
/// <inheritdoc />
@@ -80,10 +124,7 @@ protected override ServiceBusBuilder Init()
80124
{
81125
return base.Init()
82126
.WithImage(ServiceBusImage)
83-
.WithNetwork(new NetworkBuilder().Build())
84-
.WithNetworkAliases(ServiceBusNetworkAlias)
85127
.WithPortBinding(ServiceBusPort, true)
86-
.WithMsSqlContainer()
87128
.WithWaitStrategy(Wait.ForUnixContainer()
88129
.UntilMessageIsLogged("Emulator Service is Successfully Up!")
89130
.AddCustomWaitStrategy(new WaitTwoSeconds()));
@@ -107,25 +148,9 @@ protected override ServiceBusBuilder Merge(ServiceBusConfiguration oldValue, Ser
107148
return new ServiceBusBuilder(new ServiceBusConfiguration(oldValue, newValue));
108149
}
109150

110-
/// <summary>
111-
/// Configures the dependent MSSQL container.
112-
/// </summary>
113-
/// <returns>A configured instance of <see cref="ServiceBusBuilder" />.</returns>
114-
private ServiceBusBuilder WithMsSqlContainer()
115-
{
116-
var msSqlContainer = new MsSqlBuilder()
117-
.WithNetwork(DockerResourceConfiguration.Networks.Single())
118-
.WithNetworkAliases(DatabaseNetworkAlias)
119-
.Build();
120-
121-
return Merge(DockerResourceConfiguration, new ServiceBusConfiguration(databaseContainer: msSqlContainer))
122-
.WithEnvironment("MSSQL_SA_PASSWORD", MsSqlBuilder.DefaultPassword)
123-
.WithEnvironment("SQL_SERVER", DatabaseNetworkAlias);
124-
}
125-
126151
/// <inheritdoc cref="IWaitUntil" />
127152
/// <remarks>
128-
/// This is a workaround to ensure that the wait strategy does not indicate
153+
/// This is a workaround to ensure that the wait strategy does not indicate
129154
/// readiness too early:
130155
/// https://github.com/Azure/azure-service-bus-emulator-installer/issues/35#issuecomment-2497164533.
131156
/// </remarks>
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
global using System;
22
global using System.Collections.Generic;
33
global using System.Linq;
4-
global using System.Text.RegularExpressions;
54
global using System.Threading;
65
global using System.Threading.Tasks;
76
global using Docker.DotNet.Models;
87
global using DotNet.Testcontainers;
98
global using DotNet.Testcontainers.Builders;
109
global using DotNet.Testcontainers.Configurations;
1110
global using DotNet.Testcontainers.Containers;
12-
global using DotNet.Testcontainers.Images;
11+
global using DotNet.Testcontainers.Networks;
1312
global using JetBrains.Annotations;
1413
global using Testcontainers.MsSql;

src/Testcontainers/Builders/IContainerBuilder`2.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ public interface IContainerBuilder<out TBuilderEntity, out TContainerEntity> : I
378378
/// <summary>
379379
/// Assigns the specified network to the container.
380380
/// </summary>
381-
/// <param name="network">The network to connect container to.</param>
381+
/// <param name="network">The network to connect the container to.</param>
382382
/// <returns>A configured instance of <typeparamref name="TBuilderEntity" />.</returns>
383383
[PublicAPI]
384384
TBuilderEntity WithNetwork(INetwork network);

tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
namespace Testcontainers.ServiceBus;
22

3-
public sealed class ServiceBusContainerTest : IAsyncLifetime
3+
public abstract class ServiceBusContainerTest : IAsyncLifetime
44
{
5-
private readonly ServiceBusContainer _serviceBusContainer = new ServiceBusBuilder().WithAcceptLicenseAgreement(true).Build();
5+
private readonly ServiceBusContainer _serviceBusContainer;
6+
7+
private ServiceBusContainerTest(ServiceBusContainer serviceBusContainer)
8+
{
9+
_serviceBusContainer = serviceBusContainer;
10+
}
611

712
public Task InitializeAsync()
813
{
@@ -47,4 +52,43 @@ await sender.SendMessageAsync(message)
4752
// Then
4853
Assert.Equal(helloServiceBus, receivedMessage.Body.ToString());
4954
}
55+
56+
[UsedImplicitly]
57+
public sealed class ServiceBusDefaultMsSqlConfiguration : ServiceBusContainerTest
58+
{
59+
public ServiceBusDefaultMsSqlConfiguration()
60+
: base(new ServiceBusBuilder().WithAcceptLicenseAgreement(true).Build())
61+
{
62+
}
63+
}
64+
65+
[UsedImplicitly]
66+
public sealed class ServiceBusCustomMsSqlConfiguration : ServiceBusContainerTest, IClassFixture<DatabaseFixture>
67+
{
68+
public ServiceBusCustomMsSqlConfiguration(DatabaseFixture fixture)
69+
: base(new ServiceBusBuilder().WithAcceptLicenseAgreement(true).WithMsSqlContainer(fixture.Network, fixture.Container, fixture.DatabaseNetworkAlias).Build())
70+
{
71+
}
72+
}
73+
74+
[UsedImplicitly]
75+
public sealed class DatabaseFixture
76+
{
77+
public DatabaseFixture()
78+
{
79+
Network = new NetworkBuilder()
80+
.Build();
81+
82+
Container = new MsSqlBuilder()
83+
.WithNetwork(Network)
84+
.WithNetworkAliases(DatabaseNetworkAlias)
85+
.Build();
86+
}
87+
88+
public string DatabaseNetworkAlias => ServiceBusBuilder.DatabaseNetworkAlias;
89+
90+
public INetwork Network { get; }
91+
92+
public MsSqlContainer Container { get; }
93+
}
5094
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
global using System;
21
global using System.Threading.Tasks;
32
global using Azure.Messaging.ServiceBus;
3+
global using DotNet.Testcontainers.Builders;
44
global using DotNet.Testcontainers.Commons;
5+
global using DotNet.Testcontainers.Networks;
6+
global using JetBrains.Annotations;
7+
global using Testcontainers.MsSql;
58
global using Xunit;

0 commit comments

Comments
 (0)