|
| 1 | +package org.testcontainers.azure; |
| 2 | + |
| 3 | +import org.testcontainers.containers.GenericContainer; |
| 4 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 5 | +import org.testcontainers.images.builder.Transferable; |
| 6 | +import org.testcontainers.utility.DockerImageName; |
| 7 | +import org.testcontainers.utility.LicenseAcceptance; |
| 8 | + |
| 9 | +/** |
| 10 | + * Testcontainers implementation for Azure Eventhubs Emulator. |
| 11 | + * <p> |
| 12 | + * Supported image: {@code "mcr.microsoft.com/azure-messaging/eventhubs-emulator"} |
| 13 | + * <p> |
| 14 | + * Exposed ports: |
| 15 | + * <ul> |
| 16 | + * <li>AMQP: 5672</li> |
| 17 | + * </ul> |
| 18 | + */ |
| 19 | +public class AzureEventHubsContainer extends GenericContainer<AzureEventHubsContainer> { |
| 20 | + |
| 21 | + private static final int DEFAULT_AMQP_PORT = 5672; |
| 22 | + |
| 23 | + private static final String CONNECTION_STRING_FORMAT = |
| 24 | + "Endpoint=sb://%s:%d;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SAS_KEY_VALUE;UseDevelopmentEmulator=true;"; |
| 25 | + |
| 26 | + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse( |
| 27 | + "mcr.microsoft.com/azure-messaging/eventhubs-emulator" |
| 28 | + ); |
| 29 | + |
| 30 | + private AzuriteContainer azuriteContainer; |
| 31 | + |
| 32 | + /** |
| 33 | + * @param dockerImageName specified docker image name to run |
| 34 | + */ |
| 35 | + public AzureEventHubsContainer(final String dockerImageName) { |
| 36 | + this(DockerImageName.parse(dockerImageName)); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @param dockerImageName specified docker image name to run |
| 41 | + */ |
| 42 | + public AzureEventHubsContainer(final DockerImageName dockerImageName) { |
| 43 | + super(dockerImageName); |
| 44 | + dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); |
| 45 | + waitingFor(Wait.forLogMessage(".*Emulator Service is Successfully Up!.*", 1)); |
| 46 | + withExposedPorts(DEFAULT_AMQP_PORT); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * * Sets the Azurite dependency needed by the Event Hubs Container, |
| 51 | + * |
| 52 | + * @param azuriteContainer The Azurite container used by Event HUbs as a dependency |
| 53 | + * @return this |
| 54 | + */ |
| 55 | + public AzureEventHubsContainer withAzuriteContainer(final AzuriteContainer azuriteContainer) { |
| 56 | + this.azuriteContainer = azuriteContainer; |
| 57 | + dependsOn(this.azuriteContainer); |
| 58 | + return this; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Provide the broker configuration to the container. |
| 63 | + * |
| 64 | + * @param config The file containing the broker configuration |
| 65 | + * @return this |
| 66 | + */ |
| 67 | + public AzureEventHubsContainer withConfig(final Transferable config) { |
| 68 | + withCopyToContainer(config, "/Eventhubs_Emulator/ConfigFiles/Config.json"); |
| 69 | + return this; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Accepts the EULA of the container. |
| 74 | + * |
| 75 | + * @return this |
| 76 | + */ |
| 77 | + public AzureEventHubsContainer acceptLicense() { |
| 78 | + withEnv("ACCEPT_EULA", "Y"); |
| 79 | + return this; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + protected void configure() { |
| 84 | + if (azuriteContainer == null) { |
| 85 | + throw new IllegalStateException( |
| 86 | + "The image " + |
| 87 | + getDockerImageName() + |
| 88 | + " requires an Azurite container. Please provide one with the withAzuriteContainer method!" |
| 89 | + ); |
| 90 | + } |
| 91 | + final String azuriteHost = azuriteContainer.getNetworkAliases().get(0); |
| 92 | + withEnv("BLOB_SERVER", azuriteHost); |
| 93 | + withEnv("METADATA_SERVER", azuriteHost); |
| 94 | + // If license was not accepted programmatically, check if it was accepted via resource file |
| 95 | + if (!getEnvMap().containsKey("ACCEPT_EULA")) { |
| 96 | + LicenseAcceptance.assertLicenseAccepted(this.getDockerImageName()); |
| 97 | + acceptLicense(); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Returns the connection string. |
| 103 | + * |
| 104 | + * @return connection string |
| 105 | + */ |
| 106 | + public String getConnectionString() { |
| 107 | + return String.format(CONNECTION_STRING_FORMAT, getHost(), getMappedPort(DEFAULT_AMQP_PORT)); |
| 108 | + } |
| 109 | +} |
0 commit comments