-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add AzureServiceBusContainer #9795
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
Merged
eddumelendez
merged 2 commits into
testcontainers:main
from
nagyesta:feature/add-azure-servicebus-container
Jan 28, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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" | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.