Azure Module
Note
This module is INCUBATING. While it is ready for use and operational in the current version of Testcontainers, it is possible that it may receive breaking changes in the future. See our contributing guidelines for more information on our incubating modules policy.
Testcontainers module for the Microsoft Azure's SDK.
Currently, the module supports Azurite
and CosmosDB
emulators. In order to use them, you should use the following classes:
Class | Container Image |
---|---|
AzuriteContainer | mcr.microsoft.com/azure-storage/azurite |
CosmosDBEmulatorContainer | mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator |
Usage example
Azurite Storage Emulator
Start Azurite Emulator during a test:
AzuriteContainer emulator = new AzuriteContainer("mcr.microsoft.com/azure-storage/azurite:3.33.0")
Note
SSL configuration is possible using the withSsl(MountableFile, String)
and withSsl(MountableFile, MountableFile)
methods.
If the tested application needs to use more than one set of credentials, the container can be configured to use custom credentials. Please see some examples below.
AzuriteContainer emulator = new AzuriteContainer("mcr.microsoft.com/azure-storage/azurite:3.33.0")
.withEnv("AZURITE_ACCOUNTS", "account1:key1:key2")
AzuriteContainer emulator = new AzuriteContainer("mcr.microsoft.com/azure-storage/azurite:3.33.0")
.withEnv("AZURITE_ACCOUNTS", "account1:key1;account2:key2")
Using with Blob
Build Azure Blob client:
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.connectionString(container.getConnectionString())
.buildClient();
In case the application needs to use custom credentials, we can obtain them with a different method:
String connectionString1 = emulator.getConnectionString("account1", "key1");
// the second account will not have access to the same container
String connectionString2 = emulator.getConnectionString("account2", "key2");
Using with Queue
Build Azure Queue client:
QueueServiceClient queueServiceClient = new QueueServiceClientBuilder()
.connectionString(container.getConnectionString())
.buildClient();
Note
We can use custom credentials the same way as defined in the Blob section.
Using with Table
Build Azure Table client:
TableServiceClient tableServiceClient = new TableServiceClientBuilder()
.connectionString(container.getConnectionString())
.buildClient();
Note
We can use custom credentials the same way as defined in the Blob section.
CosmosDB
Start Azure CosmosDB Emulator during a test:
public CosmosDBEmulatorContainer emulator = new CosmosDBEmulatorContainer(
DockerImageName.parse("mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:latest")
);
Prepare KeyStore to use for SSL.
Path keyStoreFile = tempFolder.newFile("azure-cosmos-emulator.keystore").toPath();
KeyStore keyStore = emulator.buildNewKeyStore();
keyStore.store(new FileOutputStream(keyStoreFile.toFile()), emulator.getEmulatorKey().toCharArray());
Set system trust-store parameters to use already built KeyStore:
System.setProperty("javax.net.ssl.trustStore", keyStoreFile.toString());
System.setProperty("javax.net.ssl.trustStorePassword", emulator.getEmulatorKey());
System.setProperty("javax.net.ssl.trustStoreType", "PKCS12");
Build Azure CosmosDB client:
CosmosAsyncClient client = new CosmosClientBuilder()
.gatewayMode()
.endpointDiscoveryEnabled(false)
.endpoint(emulator.getEmulatorEndpoint())
.key(emulator.getEmulatorKey())
.buildAsyncClient();
Test against the Emulator:
CosmosDatabaseResponse databaseResponse = client.createDatabaseIfNotExists("Azure").block();
assertThat(databaseResponse.getStatusCode()).isEqualTo(201);
CosmosContainerResponse containerResponse = client
.getDatabase("Azure")
.createContainerIfNotExists("ServiceContainer", "/name")
.block();
assertThat(containerResponse.getStatusCode()).isEqualTo(201);
Adding this module to your project dependencies
Add the following dependency to your pom.xml
/build.gradle
file:
testImplementation "org.testcontainers:azure:1.20.4"
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>azure</artifactId>
<version>1.20.4</version>
<scope>test</scope>
</dependency>