-
-
Notifications
You must be signed in to change notification settings - Fork 350
Expand file tree
/
Copy pathServiceBusBuilder.cs
More file actions
192 lines (167 loc) · 8.43 KB
/
Copy pathServiceBusBuilder.cs
File metadata and controls
192 lines (167 loc) · 8.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
namespace Testcontainers.ServiceBus;
/// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" />
[PublicAPI]
public sealed class ServiceBusBuilder : ContainerBuilder<ServiceBusBuilder, ServiceBusContainer, ServiceBusConfiguration>
{
public const string ServiceBusNetworkAlias = "servicebus-container";
public const string DatabaseNetworkAlias = "database-container";
[Obsolete("This constant is obsolete and will be removed in the future. Use the constructor with the image parameter instead: https://github.com/testcontainers/testcontainers-dotnet/discussions/1470#discussioncomment-15185721.")]
public const string ServiceBusImage = "mcr.microsoft.com/azure-messaging/servicebus-emulator:latest";
public const ushort ServiceBusPort = 5672;
public const ushort ServiceBusHttpPort = 5300;
/// <summary>
/// Initializes a new instance of the <see cref="ServiceBusBuilder" /> class.
/// </summary>
[Obsolete("This parameterless constructor is obsolete and will be removed. Use the constructor with the image parameter instead: https://github.com/testcontainers/testcontainers-dotnet/discussions/1470#discussioncomment-15185721.")]
[ExcludeFromCodeCoverage]
public ServiceBusBuilder()
: this(ServiceBusImage)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ServiceBusBuilder" /> class.
/// </summary>
/// <param name="image">
/// The full Docker image name, including the image repository and tag
/// (e.g., <c>mcr.microsoft.com/azure-messaging/servicebus-emulator:latest</c>).
/// </param>
/// <remarks>
/// Docker image tags available at <see href="https://mcr.microsoft.com/en-us/artifact/mar/azure-messaging/servicebus-emulator/tags" />.
/// </remarks>
public ServiceBusBuilder(string image)
: this(new DockerImage(image))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ServiceBusBuilder" /> class.
/// </summary>
/// <param name="image">
/// An <see cref="IImage" /> instance that specifies the Docker image to be used
/// for the container builder configuration.
/// </param>
/// <remarks>
/// Docker image tags available at <see href="https://mcr.microsoft.com/en-us/artifact/mar/azure-messaging/servicebus-emulator/tags" />.
/// </remarks>
public ServiceBusBuilder(IImage image)
: this(new ServiceBusConfiguration())
{
DockerResourceConfiguration = Init().WithImage(image).DockerResourceConfiguration;
}
/// <summary>
/// Initializes a new instance of the <see cref="ServiceBusBuilder" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
private ServiceBusBuilder(ServiceBusConfiguration resourceConfiguration)
: base(resourceConfiguration)
{
DockerResourceConfiguration = resourceConfiguration;
}
/// <inheritdoc />
protected override ServiceBusConfiguration DockerResourceConfiguration { get; }
/// <inheritdoc />
protected override string AcceptLicenseAgreementEnvVar { get; } = "ACCEPT_EULA";
/// <inheritdoc />
protected override string AcceptLicenseAgreement { get; } = "Y";
/// <inheritdoc />
protected override string DeclineLicenseAgreement { get; } = "N";
/// <summary>
/// Accepts the license agreement.
/// </summary>
/// <remarks>
/// When <paramref name="acceptLicenseAgreement" /> is set to <c>true</c>, the Azure Service Bus Emulator <see href="https://github.com/Azure/azure-service-bus-emulator-installer/blob/main/EMULATOR_EULA.txt">license</see> is accepted.
/// </remarks>
/// <param name="acceptLicenseAgreement">A boolean value indicating whether the Azure Service Bus Emulator license agreement is accepted.</param>
/// <returns>A configured instance of <see cref="ServiceBusBuilder" />.</returns>
public override ServiceBusBuilder WithAcceptLicenseAgreement(bool acceptLicenseAgreement)
{
var licenseAgreement = acceptLicenseAgreement ? AcceptLicenseAgreement : DeclineLicenseAgreement;
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);
}
/// <summary>
/// Sets the configuration for the Azure Service Bus Emulator.
/// </summary>
/// <remarks>
/// Default emulator configuration: https://learn.microsoft.com/en-us/azure/service-bus-messaging/test-locally-with-service-bus-emulator?tabs=automated-script#interact-with-the-emulator.
/// </remarks>
/// <param name="configFilePath">The path to the JSON file containing the Azure Service Bus Emulator configuration.</param>
/// <returns>A configured instance of <see cref="ServiceBusBuilder" />.</returns>
public ServiceBusBuilder WithConfig(string configFilePath)
{
return WithResourceMapping(new FileInfo(configFilePath), new FileInfo("/ServiceBus_Emulator/ConfigFiles/Config.json"));
}
/// <inheritdoc />
public override ServiceBusContainer Build()
{
Validate();
ValidateLicenseAgreement();
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("mcr.microsoft.com/mssql/server:2022-CU14-ubuntu-22.04")
.WithNetwork(network)
.WithNetworkAliases(DatabaseNetworkAlias)
.Build();
var serviceBusBuilder = WithMsSqlContainer(network, container, DatabaseNetworkAlias);
return new ServiceBusContainer(serviceBusBuilder.DockerResourceConfiguration);
}
/// <inheritdoc />
protected override ServiceBusBuilder Init()
{
return base.Init()
.WithPortBinding(ServiceBusPort, true)
.WithPortBinding(ServiceBusHttpPort, true)
.WithEnvironment("SQL_WAIT_INTERVAL", "0")
.WithConnectionStringProvider(new ServiceBusConnectionStringProvider())
.WithWaitStrategy(Wait.ForUnixContainer()
// https://github.com/Azure/azure-event-hubs-emulator-installer/issues/69#issuecomment-3762895979.
.UntilMessageIsLogged("Emulator Service is Successfully Up!")
.UntilHttpRequestIsSucceeded(request =>
request.ForPort(ServiceBusHttpPort).ForPath("/health")));
}
/// <inheritdoc />
protected override ServiceBusBuilder Clone(IResourceConfiguration<CreateContainerParameters> resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new ServiceBusConfiguration(resourceConfiguration));
}
/// <inheritdoc />
protected override ServiceBusBuilder Clone(IContainerConfiguration resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new ServiceBusConfiguration(resourceConfiguration));
}
/// <inheritdoc />
protected override ServiceBusBuilder Merge(ServiceBusConfiguration oldValue, ServiceBusConfiguration newValue)
{
return new ServiceBusBuilder(new ServiceBusConfiguration(oldValue, newValue));
}
}