-
-
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 Azure Eventhubs Emulator container to Azure module
- Add Azure Eventhubs Emulator container - Implement new test case - Update Azure documentation Signed-off-by: Esta Nagy <[email protected]>
- Loading branch information
Showing
6 changed files
with
253 additions
and
4 deletions.
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
121 changes: 121 additions & 0 deletions
121
modules/azure/src/main/java/org/testcontainers/azure/AzureEventhubsEmulatorContainer.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,121 @@ | ||
package org.testcontainers.azure; | ||
|
||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.containers.wait.strategy.Wait; | ||
import org.testcontainers.utility.DockerImageName; | ||
import org.testcontainers.utility.MountableFile; | ||
|
||
/** | ||
* Testcontainers implementation for Azure Eventhubs Emulator. | ||
* <p> | ||
* Supported image: {@code "mcr.microsoft.com/azure-messaging/eventhubs-emulator"} | ||
* <p> | ||
* Exposed ports: | ||
* <ul> | ||
* <li>5672 (AMQP port)</li> | ||
* <li>9092 (Kafka port)</li> | ||
* </ul> | ||
*/ | ||
public class AzureEventhubsEmulatorContainer extends GenericContainer<AzureEventhubsEmulatorContainer> { | ||
|
||
private static final String DEFAULT_HOST = "127.0.0.1"; | ||
|
||
private static final int DEFAULT_AMQP_PORT = 5672; | ||
|
||
private static final int DEFAULT_KAFKA_PORT = 9092; | ||
|
||
private static final String CONNECTION_STRING_FORMAT = | ||
"Endpoint=sb://%s:%d;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SAS_KEY_VALUE;UseDevelopmentEmulator=true;"; | ||
|
||
private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse( | ||
"mcr.microsoft.com/azure-messaging/eventhubs-emulator" | ||
); | ||
|
||
private String host = DEFAULT_HOST; | ||
|
||
private AzuriteContainer azuriteContainer; | ||
|
||
private MountableFile config; | ||
|
||
/** | ||
* @param dockerImageName specified docker image name to run | ||
*/ | ||
public AzureEventhubsEmulatorContainer(final DockerImageName dockerImageName) { | ||
super(dockerImageName); | ||
dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); | ||
|
||
waitingFor(Wait.forLogMessage(".*Emulator Service is Successfully Up!.*", 1)); | ||
withExposedPorts(DEFAULT_AMQP_PORT, DEFAULT_KAFKA_PORT); | ||
} | ||
|
||
@Override | ||
public void start() { | ||
if (azuriteContainer == null) { | ||
azuriteContainer = | ||
new AzuriteContainer(AzuriteContainer.DEFAULT_IMAGE_NAME.withTag("3.33.0")).withNetwork(getNetwork()); | ||
} | ||
azuriteContainer.start(); | ||
|
||
super.start(); | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
super.stop(); | ||
if (azuriteContainer != null) { | ||
azuriteContainer.stop(); | ||
} | ||
} | ||
|
||
/** | ||
* Provide the broker configuration to the container. | ||
* | ||
* @param config The file containing the broker configuration | ||
* @return this | ||
*/ | ||
public AzureEventhubsEmulatorContainer withConfig(final MountableFile config) { | ||
this.config = config; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the hostname we want to use to connect to our emulator. (default: {@link #DEFAULT_HOST}) | ||
* | ||
* @param host The host name | ||
* @return this | ||
*/ | ||
public AzureEventhubsEmulatorContainer withHost(final String host) { | ||
this.host = host; | ||
return this; | ||
} | ||
|
||
/** | ||
* Accepts the EULA of the container. | ||
* | ||
* @return this | ||
*/ | ||
public AzureEventhubsEmulatorContainer acceptEula() { | ||
return withEnv("ACCEPT_EULA", "Y"); | ||
} | ||
|
||
@Override | ||
protected void configure() { | ||
dependsOn(azuriteContainer); | ||
final String azuriteHost = azuriteContainer.getNetworkAliases().get(0); | ||
withEnv("BLOB_SERVER", azuriteHost); | ||
withEnv("METADATA_SERVER", azuriteHost); | ||
if (config != null) { | ||
logger().info("Using path for configuration file: '{}'", config); | ||
withCopyFileToContainer(config, "/Eventhubs_Emulator/ConfigFiles/Config.json"); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the connection string. | ||
* | ||
* @return connection string | ||
*/ | ||
public String getConnectionString() { | ||
return String.format(CONNECTION_STRING_FORMAT, host, getMappedPort(DEFAULT_AMQP_PORT)); | ||
} | ||
} |
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
86 changes: 86 additions & 0 deletions
86
...les/azure/src/test/java/org/testcontainers/azure/AzureEventhubsEmulatorContainerTest.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,86 @@ | ||
package org.testcontainers.azure; | ||
|
||
import com.azure.core.util.IterableStream; | ||
import com.azure.messaging.eventhubs.EventData; | ||
import com.azure.messaging.eventhubs.EventHubClientBuilder; | ||
import com.azure.messaging.eventhubs.EventHubConsumerClient; | ||
import com.azure.messaging.eventhubs.EventHubProducerClient; | ||
import com.azure.messaging.eventhubs.models.EventPosition; | ||
import com.azure.messaging.eventhubs.models.PartitionEvent; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.testcontainers.containers.Network; | ||
import org.testcontainers.utility.DockerImageName; | ||
import org.testcontainers.utility.MountableFile; | ||
|
||
import java.time.Duration; | ||
import java.util.Collections; | ||
import java.util.Optional; | ||
import java.util.Properties; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.waitAtMost; | ||
|
||
public class AzureEventhubsEmulatorContainerTest { | ||
|
||
private static Properties originalSystemProperties; | ||
|
||
@BeforeClass | ||
public static void captureOriginalSystemProperties() { | ||
originalSystemProperties = (Properties) System.getProperties().clone(); | ||
} | ||
|
||
@AfterClass | ||
public static void restoreOriginalSystemProperties() { | ||
System.setProperties(originalSystemProperties); | ||
} | ||
|
||
@Rule | ||
// emulatorContainer { | ||
public AzureEventhubsEmulatorContainer emulator = new AzureEventhubsEmulatorContainer( | ||
DockerImageName.parse("mcr.microsoft.com/azure-messaging/eventhubs-emulator:2.0.1") | ||
) | ||
.acceptEula() | ||
.withNetwork(Network.newNetwork()) | ||
.withConfig(MountableFile.forClasspathResource("/eventhubs_config.json")) | ||
.withHost("127.0.0.1"); | ||
|
||
// } | ||
|
||
@Test | ||
public void testWithEventhubsClient() { | ||
try ( | ||
// createProducerAndConsumer { | ||
EventHubProducerClient producer = new EventHubClientBuilder() | ||
.connectionString(emulator.getConnectionString()) | ||
.fullyQualifiedNamespace("emulatorNs1") | ||
.eventHubName("eh1") | ||
.buildProducerClient(); | ||
EventHubConsumerClient consumer = new EventHubClientBuilder() | ||
.connectionString(emulator.getConnectionString()) | ||
.fullyQualifiedNamespace("emulatorNs1") | ||
.eventHubName("eh1") | ||
.consumerGroup("cg1") | ||
.buildConsumerClient(); | ||
// } | ||
) { | ||
producer.send(Collections.singletonList(new EventData("test"))); | ||
|
||
waitAtMost(Duration.ofSeconds(30)) | ||
.pollDelay(Duration.ofSeconds(5)) | ||
.untilAsserted(() -> { | ||
IterableStream<PartitionEvent> events = consumer.receiveFromPartition( | ||
"0", | ||
1, | ||
EventPosition.earliest(), | ||
Duration.ofSeconds(2) | ||
); | ||
Optional<PartitionEvent> event = events.stream().findFirst(); | ||
assertThat(event).isPresent(); | ||
assertThat(event.get().getData().getBodyAsString()).isEqualTo("test"); | ||
}); | ||
} | ||
} | ||
} |
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,24 @@ | ||
{ | ||
"UserConfig": { | ||
"NamespaceConfig": [ | ||
{ | ||
"Type": "EventHub", | ||
"Name": "emulatorNs1", | ||
"Entities": [ | ||
{ | ||
"Name": "eh1", | ||
"PartitionCount": "1", | ||
"ConsumerGroups": [ | ||
{ | ||
"Name": "cg1" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
], | ||
"LoggingConfig": { | ||
"Type": "File" | ||
} | ||
} | ||
} |