-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AzureServiceBusContainer (#9795)
Signed-off-by: Esta Nagy <[email protected]> Co-authored-by: Eddú Meléndez <[email protected]>
- Loading branch information
1 parent
e1dc19f
commit 4eeec15
Showing
5 changed files
with
278 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
modules/azure/src/main/java/org/testcontainers/azure/AzureServiceBusContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package org.testcontainers.azure; | ||
|
||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.containers.MSSQLServerContainer; | ||
import org.testcontainers.containers.wait.strategy.Wait; | ||
import org.testcontainers.images.builder.Transferable; | ||
import org.testcontainers.utility.DockerImageName; | ||
import org.testcontainers.utility.LicenseAcceptance; | ||
|
||
/** | ||
* Testcontainers implementation for Azure Service Bus Emulator. | ||
* <p> | ||
* Supported image: {@code mcr.microsoft.com/azure-messaging/servicebus-emulator} | ||
* <p> | ||
* Exposed port: 5672 | ||
*/ | ||
public class AzureServiceBusContainer extends GenericContainer<AzureServiceBusContainer> { | ||
|
||
private static final String CONNECTION_STRING_FORMAT = | ||
"Endpoint=sb://%s:%d;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SAS_KEY_VALUE;UseDevelopmentEmulator=true;"; | ||
|
||
private static final int DEFAULT_PORT = 5672; | ||
|
||
private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse( | ||
"mcr.microsoft.com/azure-messaging/servicebus-emulator" | ||
); | ||
|
||
private MSSQLServerContainer<?> msSqlServerContainer; | ||
|
||
/** | ||
* @param dockerImageName The specified docker image name to run | ||
*/ | ||
public AzureServiceBusContainer(final String dockerImageName) { | ||
this(DockerImageName.parse(dockerImageName)); | ||
} | ||
|
||
/** | ||
* @param dockerImageName The specified docker image name to run | ||
*/ | ||
public AzureServiceBusContainer(final DockerImageName dockerImageName) { | ||
super(dockerImageName); | ||
dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); | ||
withExposedPorts(DEFAULT_PORT); | ||
waitingFor(Wait.forLogMessage(".*Emulator Service is Successfully Up!.*", 1)); | ||
} | ||
|
||
/** | ||
* Sets the MS SQL Server dependency needed by the Service Bus Container, | ||
* | ||
* @param msSqlServerContainer The MS SQL Server container used by Service Bus as a dependency | ||
* @return this | ||
*/ | ||
public AzureServiceBusContainer withMsSqlServerContainer(final MSSQLServerContainer<?> msSqlServerContainer) { | ||
dependsOn(msSqlServerContainer); | ||
this.msSqlServerContainer = msSqlServerContainer; | ||
return this; | ||
} | ||
|
||
/** | ||
* Provide the Service Bus configuration JSON. | ||
* | ||
* @param config The configuration | ||
* @return this | ||
*/ | ||
public AzureServiceBusContainer withConfig(final Transferable config) { | ||
withCopyToContainer(config, "/ServiceBus_Emulator/ConfigFiles/Config.json"); | ||
return this; | ||
} | ||
|
||
/** | ||
* Accepts the EULA of the container. | ||
* | ||
* @return this | ||
*/ | ||
public AzureServiceBusContainer acceptLicense() { | ||
withEnv("ACCEPT_EULA", "Y"); | ||
return this; | ||
} | ||
|
||
@Override | ||
protected void configure() { | ||
if (msSqlServerContainer == null) { | ||
throw new IllegalStateException( | ||
"The image " + | ||
getDockerImageName() + | ||
" requires a Microsoft SQL Server container. Please provide one with the withMsSqlServerContainer method!" | ||
); | ||
} | ||
withEnv("SQL_SERVER", msSqlServerContainer.getNetworkAliases().get(0)); | ||
withEnv("MSSQL_SA_PASSWORD", msSqlServerContainer.getPassword()); | ||
// If license was not accepted programmatically, check if it was accepted via resource file | ||
if (!getEnvMap().containsKey("ACCEPT_EULA")) { | ||
LicenseAcceptance.assertLicenseAccepted(this.getDockerImageName()); | ||
acceptLicense(); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the connection string. | ||
* | ||
* @return connection string | ||
*/ | ||
public String getConnectionString() { | ||
return String.format(CONNECTION_STRING_FORMAT, getHost(), getMappedPort(DEFAULT_PORT)); | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
modules/azure/src/test/java/org/testcontainers/azure/AzureServiceBusContainerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package org.testcontainers.azure; | ||
|
||
import com.azure.messaging.servicebus.ServiceBusClientBuilder; | ||
import com.azure.messaging.servicebus.ServiceBusErrorContext; | ||
import com.azure.messaging.servicebus.ServiceBusException; | ||
import com.azure.messaging.servicebus.ServiceBusMessage; | ||
import com.azure.messaging.servicebus.ServiceBusProcessorClient; | ||
import com.azure.messaging.servicebus.ServiceBusReceivedMessageContext; | ||
import com.azure.messaging.servicebus.ServiceBusSenderClient; | ||
import com.github.dockerjava.api.model.Capability; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.testcontainers.containers.MSSQLServerContainer; | ||
import org.testcontainers.containers.Network; | ||
import org.testcontainers.utility.MountableFile; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Consumer; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
public class AzureServiceBusContainerTest { | ||
|
||
@Rule | ||
// network { | ||
public Network network = Network.newNetwork(); | ||
|
||
// } | ||
|
||
@Rule | ||
// sqlContainer { | ||
public MSSQLServerContainer<?> mssqlServerContainer = new MSSQLServerContainer<>( | ||
"mcr.microsoft.com/mssql/server:2022-CU14-ubuntu-22.04" | ||
) | ||
.acceptLicense() | ||
.withPassword("yourStrong(!)Password") | ||
.withCreateContainerCmdModifier(cmd -> { | ||
cmd.getHostConfig().withCapAdd(Capability.SYS_PTRACE); | ||
}) | ||
.withNetwork(network); | ||
|
||
// } | ||
|
||
@Rule | ||
// emulatorContainer { | ||
public AzureServiceBusContainer emulator = new AzureServiceBusContainer( | ||
"mcr.microsoft.com/azure-messaging/servicebus-emulator:1.0.1" | ||
) | ||
.acceptLicense() | ||
.withConfig(MountableFile.forClasspathResource("/service-bus-config.json")) | ||
.withNetwork(network) | ||
.withMsSqlServerContainer(mssqlServerContainer); | ||
|
||
// } | ||
|
||
@Test | ||
public void testWithClient() { | ||
assertThat(emulator.getConnectionString()).startsWith("Endpoint=sb://"); | ||
|
||
// senderClient { | ||
ServiceBusSenderClient senderClient = new ServiceBusClientBuilder() | ||
.connectionString(emulator.getConnectionString()) | ||
.sender() | ||
.queueName("queue.1") | ||
.buildClient(); | ||
// } | ||
|
||
await() | ||
.atMost(20, TimeUnit.SECONDS) | ||
.ignoreException(ServiceBusException.class) | ||
.until(() -> { | ||
senderClient.sendMessage(new ServiceBusMessage("Hello, Testcontainers!")); | ||
return true; | ||
}); | ||
senderClient.close(); | ||
|
||
final List<String> received = new CopyOnWriteArrayList<>(); | ||
Consumer<ServiceBusReceivedMessageContext> messageConsumer = m -> { | ||
received.add(m.getMessage().getBody().toString()); | ||
m.complete(); | ||
}; | ||
Consumer<ServiceBusErrorContext> errorConsumer = e -> Assertions.fail("Unexpected error: " + e); | ||
// processorClient { | ||
ServiceBusProcessorClient processorClient = new ServiceBusClientBuilder() | ||
.connectionString(emulator.getConnectionString()) | ||
.processor() | ||
.queueName("queue.1") | ||
.processMessage(messageConsumer) | ||
.processError(errorConsumer) | ||
.buildProcessorClient(); | ||
// } | ||
processorClient.start(); | ||
|
||
await() | ||
.atMost(20, TimeUnit.SECONDS) | ||
.untilAsserted(() -> { | ||
assertThat(received).hasSize(1).containsExactlyInAnyOrder("Hello, Testcontainers!"); | ||
}); | ||
processorClient.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"UserConfig": { | ||
"Namespaces": [ | ||
{ | ||
"Name": "sbemulatorns", | ||
"Queues": [ | ||
{ | ||
"Name": "queue.1", | ||
"Properties": { | ||
"DeadLetteringOnMessageExpiration": false, | ||
"DefaultMessageTimeToLive": "PT1H", | ||
"DuplicateDetectionHistoryTimeWindow": "PT20S", | ||
"ForwardDeadLetteredMessagesTo": "", | ||
"ForwardTo": "", | ||
"LockDuration": "PT1M", | ||
"MaxDeliveryCount": 3, | ||
"RequiresDuplicateDetection": false, | ||
"RequiresSession": false | ||
} | ||
} | ||
], | ||
"Topics": [] | ||
} | ||
], | ||
"Logging": { | ||
"Type": "File" | ||
} | ||
} | ||
} |