Skip to content

Commit 977d348

Browse files
committed
feat: Custom configuration option for Azure Service Bus
1 parent aa0b5ce commit 977d348

File tree

5 files changed

+78
-4
lines changed

5 files changed

+78
-4
lines changed

docs/modules/servicebus.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,14 @@ The Service Bus module depends on an MSSQL container instance. The module automa
4040
=== "Reuse Existing Resources"
4141
```csharp
4242
--8<-- "tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs:ReuseExistingMsSqlContainer"
43-
```
43+
```
44+
45+
## Use a custom Config.json
46+
47+
The Azure Service Bus Emulator provides a default configuration. If a custom configuration is desired, you can use the following method to provide the custom json file to the builder accordingly:
48+
49+
=== "Use Custom Configuration"
50+
```csharp
51+
--8<-- "tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs:UseCustomConfiguration"
52+
```
53+
See Azure Service Bus emulator documentation for information on creating that json file.

src/Testcontainers.ServiceBus/ServiceBusBuilder.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.IO;
2+
13
namespace Testcontainers.ServiceBus;
24

35
/// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" />
@@ -85,6 +87,17 @@ public ServiceBusBuilder WithMsSqlContainer(
8587
.WithEnvironment("SQL_SERVER", networkAlias)
8688
.WithEnvironment("MSSQL_SA_PASSWORD", password);
8789
}
90+
91+
/// <summary>
92+
/// Sets the configuration file for the Azure Service Bus Emulator.
93+
/// </summary>
94+
/// <param name="customConfigFile">The JSON file containing desired Azure Service Bus configuration</param>
95+
/// <returns>A configured instance of <see cref="ServiceBusBuilder" />.</returns>
96+
public ServiceBusBuilder WithConfig(string customConfigFile)
97+
{
98+
return WithResourceMapping(new FileInfo(customConfigFile),
99+
new FileInfo("/ServiceBus_Emulator/ConfigFiles/Config.json"));
100+
}
88101

89102
/// <inheritdoc />
90103
public override ServiceBusContainer Build()

tests/Testcontainers.ServiceBus.Tests/ServiceBusContainerTest.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public Task DisposeAsync()
2020
return _serviceBusContainer.DisposeAsync().AsTask();
2121
}
2222

23+
protected virtual string QueueName => "queue.1";
24+
2325
[Fact]
2426
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
2527
public async Task ReceiveMessageReturnsSentMessage()
@@ -33,15 +35,14 @@ public async Task ReceiveMessageReturnsSentMessage()
3335
// Upload a custom configuration before the container starts using the
3436
// `WithResourceMapping(string, string)` API or one of its overloads:
3537
// `WithResourceMapping("Config.json", "/ServiceBus_Emulator/ConfigFiles/")`.
36-
const string queueName = "queue.1";
3738

3839
var message = new ServiceBusMessage(helloServiceBus);
3940

4041
await using var client = new ServiceBusClient(_serviceBusContainer.GetConnectionString());
4142

42-
var sender = client.CreateSender(queueName);
43+
var sender = client.CreateSender(QueueName);
4344

44-
var receiver = client.CreateReceiver(queueName);
45+
var receiver = client.CreateReceiver(QueueName);
4546

4647
// When
4748
await sender.SendMessageAsync(message)
@@ -81,6 +82,22 @@ public ServiceBusCustomMsSqlConfiguration(DatabaseFixture fixture)
8182
{
8283
}
8384
}
85+
86+
[UsedImplicitly]
87+
public sealed class ServiceBusCustomConfig : ServiceBusContainerTest, IClassFixture<DatabaseFixture>
88+
{
89+
protected override string QueueName => "custom-queue.1";
90+
91+
public ServiceBusCustomConfig()
92+
: base(new ServiceBusBuilder()
93+
.WithAcceptLicenseAgreement(true)
94+
// # --8<-- [start:UseCustomConfiguration]
95+
.WithConfig("customConfig.json")
96+
// # --8<-- [end:UseCustomConfiguration]
97+
.Build())
98+
{
99+
}
100+
}
84101

85102
[UsedImplicitly]
86103
public sealed class DatabaseFixture

tests/Testcontainers.ServiceBus.Tests/Testcontainers.ServiceBus.Tests.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,10 @@
1717
<ProjectReference Include="../../src/Testcontainers.ServiceBus/Testcontainers.ServiceBus.csproj"/>
1818
<ProjectReference Include="../Testcontainers.Commons/Testcontainers.Commons.csproj"/>
1919
</ItemGroup>
20+
<ItemGroup>
21+
<None Remove="customConfig.json" />
22+
<Content Include="customConfig.json">
23+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
24+
</Content>
25+
</ItemGroup>
2026
</Project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"UserConfig": {
3+
"Namespaces": [
4+
{
5+
"Name": "sbemulatorns",
6+
"Queues": [
7+
{
8+
"Name": "custom-queue.1",
9+
"Properties": {
10+
"DeadLetteringOnMessageExpiration": false,
11+
"DefaultMessageTimeToLive": "PT1H",
12+
"DuplicateDetectionHistoryTimeWindow": "PT20S",
13+
"ForwardDeadLetteredMessagesTo": "",
14+
"ForwardTo": "",
15+
"LockDuration": "PT1M",
16+
"MaxDeliveryCount": 3,
17+
"RequiresDuplicateDetection": false,
18+
"RequiresSession": false
19+
}
20+
}
21+
]
22+
}
23+
],
24+
"Logging": {
25+
"Type": "File"
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)