|
| 1 | +package org.testcontainers.pulsar; |
| 2 | + |
| 3 | +import org.testcontainers.containers.GenericContainer; |
| 4 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 5 | +import org.testcontainers.containers.wait.strategy.WaitAllStrategy; |
| 6 | +import org.testcontainers.utility.DockerImageName; |
| 7 | + |
| 8 | +/** |
| 9 | + * Testcontainers implementation for Apache Pulsar. |
| 10 | + * <p> |
| 11 | + * Supported images: {@code apachepulsar/pulsar}, {@code apachepulsar/pulsar-all} |
| 12 | + * <p> |
| 13 | + * Exposed ports: |
| 14 | + * <ul> |
| 15 | + * <li>Pulsar: 6650</li> |
| 16 | + * <li>HTTP: 8080</li> |
| 17 | + * </ul> |
| 18 | + */ |
| 19 | +public class PulsarContainer extends GenericContainer<PulsarContainer> { |
| 20 | + |
| 21 | + public static final int BROKER_PORT = 6650; |
| 22 | + |
| 23 | + public static final int BROKER_HTTP_PORT = 8080; |
| 24 | + |
| 25 | + private static final String ADMIN_CLUSTERS_ENDPOINT = "/admin/v2/clusters"; |
| 26 | + |
| 27 | + /** |
| 28 | + * See <a href="https://github.com/apache/pulsar/blob/master/pulsar-common/src/main/java/org/apache/pulsar/common/naming/SystemTopicNames.java">SystemTopicNames</a>. |
| 29 | + */ |
| 30 | + private static final String TRANSACTION_TOPIC_ENDPOINT = |
| 31 | + "/admin/v2/persistent/pulsar/system/transaction_coordinator_assign/partitions"; |
| 32 | + |
| 33 | + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("apachepulsar/pulsar"); |
| 34 | + |
| 35 | + private final WaitAllStrategy waitAllStrategy = new WaitAllStrategy(); |
| 36 | + |
| 37 | + private boolean functionsWorkerEnabled = false; |
| 38 | + |
| 39 | + private boolean transactionsEnabled = false; |
| 40 | + |
| 41 | + @Deprecated |
| 42 | + public PulsarContainer(String dockerImageName) { |
| 43 | + this(DockerImageName.parse(dockerImageName)); |
| 44 | + } |
| 45 | + |
| 46 | + public PulsarContainer(final DockerImageName dockerImageName) { |
| 47 | + super(dockerImageName); |
| 48 | + dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME, DockerImageName.parse("apachepulsar/pulsar-all")); |
| 49 | + withExposedPorts(BROKER_PORT, BROKER_HTTP_PORT); |
| 50 | + setWaitStrategy(waitAllStrategy); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + protected void configure() { |
| 55 | + super.configure(); |
| 56 | + setupCommandAndEnv(); |
| 57 | + } |
| 58 | + |
| 59 | + public PulsarContainer withFunctionsWorker() { |
| 60 | + functionsWorkerEnabled = true; |
| 61 | + return this; |
| 62 | + } |
| 63 | + |
| 64 | + public PulsarContainer withTransactions() { |
| 65 | + transactionsEnabled = true; |
| 66 | + return this; |
| 67 | + } |
| 68 | + |
| 69 | + public String getPulsarBrokerUrl() { |
| 70 | + return String.format("pulsar://%s:%s", getHost(), getMappedPort(BROKER_PORT)); |
| 71 | + } |
| 72 | + |
| 73 | + public String getHttpServiceUrl() { |
| 74 | + return String.format("http://%s:%s", getHost(), getMappedPort(BROKER_HTTP_PORT)); |
| 75 | + } |
| 76 | + |
| 77 | + protected void setupCommandAndEnv() { |
| 78 | + String standaloneBaseCommand = |
| 79 | + "/pulsar/bin/apply-config-from-env.py /pulsar/conf/standalone.conf " + "&& bin/pulsar standalone"; |
| 80 | + |
| 81 | + if (!functionsWorkerEnabled) { |
| 82 | + standaloneBaseCommand += " --no-functions-worker -nss"; |
| 83 | + } |
| 84 | + |
| 85 | + withCommand("/bin/bash", "-c", standaloneBaseCommand); |
| 86 | + |
| 87 | + final String clusterName = getEnvMap().getOrDefault("PULSAR_PREFIX_clusterName", "standalone"); |
| 88 | + final String response = String.format("[\"%s\"]", clusterName); |
| 89 | + waitAllStrategy.withStrategy( |
| 90 | + Wait.forHttp(ADMIN_CLUSTERS_ENDPOINT).forPort(BROKER_HTTP_PORT).forResponsePredicate(response::equals) |
| 91 | + ); |
| 92 | + |
| 93 | + if (transactionsEnabled) { |
| 94 | + withEnv("PULSAR_PREFIX_transactionCoordinatorEnabled", "true"); |
| 95 | + waitAllStrategy.withStrategy( |
| 96 | + Wait.forHttp(TRANSACTION_TOPIC_ENDPOINT).forStatusCode(200).forPort(BROKER_HTTP_PORT) |
| 97 | + ); |
| 98 | + } |
| 99 | + if (functionsWorkerEnabled) { |
| 100 | + waitAllStrategy.withStrategy(Wait.forLogMessage(".*Function worker service started.*", 1)); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments