aahmed-se closed pull request #2036: Add testcontainer based standalone test URL: https://github.com/apache/incubator-pulsar/pull/2036
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/tests/integration-tc/pom.xml b/tests/integration-tc/pom.xml new file mode 100644 index 0000000000..45e7487a0f --- /dev/null +++ b/tests/integration-tc/pom.xml @@ -0,0 +1,140 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://maven.apache.org/POM/4.0.0 + http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.pulsar.tests</groupId> + <artifactId>tests-parent</artifactId> + <version>2.2.0-incubating-SNAPSHOT</version> + </parent> + + <groupId>org.apache.pulsar.tests</groupId> + <artifactId>integration-tc</artifactId> + <packaging>jar</packaging> + + <name>Apache Pulsar :: Tests :: Container Based integration tests</name> + + <properties> + <testcontainers.version>1.8.0</testcontainers.version> + <commons-compress.version>1.15</commons-compress.version> + </properties> + + <dependencies> + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-compress</artifactId> + <version>${commons-compress.version}</version> + </dependency> + + <dependency> + <groupId>org.apache.zookeeper</groupId> + <artifactId>zookeeper</artifactId> + </dependency> + + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + </dependency> + + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-slf4j-impl</artifactId> + </dependency> + + <dependency> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-databind</artifactId> + </dependency> + + <dependency> + <groupId>com.fasterxml.jackson.dataformat</groupId> + <artifactId>jackson-dataformat-yaml</artifactId> + </dependency> + + <dependency> + <groupId>org.testcontainers</groupId> + <artifactId>testcontainers</artifactId> + <version>1.8.0</version> + </dependency> + + <dependency> + <groupId>org.apache.pulsar</groupId> + <artifactId>pulsar-client</artifactId> + <version>${project.version}</version> + </dependency> + + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <!-- only run tests when -DintegrationTests is specified //--> + <skipTests>true</skipTests> + <systemPropertyVariables> + <currentVersion>${project.version}</currentVersion> + <maven.buildDirectory>${project.build.directory}</maven.buildDirectory> + </systemPropertyVariables> + </configuration> + </plugin> + </plugins> + </build> + + <profiles> + <profile> + <id>integrationTests</id> + <activation> + <property> + <name>integrationTests</name> + </property> + </activation> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <properties> + <property> + <name>listener</name> + <!-- AnnotationListener breaks arquillian, so don't use it //--> + <value>org.apache.pulsar.tests.PulsarTestListener</value> + </property> + </properties> + + <argLine>-Xmx2G -XX:MaxDirectMemorySize=8G + -Dio.netty.leakDetectionLevel=advanced + </argLine> + <skipTests>false</skipTests> + </configuration> + </plugin> + </plugins> + </build> + </profile> + </profiles> +</project> diff --git a/tests/integration-tc/src/main/java/org/apache/pulsar/tests/integration/FUNC_MODE.java b/tests/integration-tc/src/main/java/org/apache/pulsar/tests/integration/FUNC_MODE.java new file mode 100644 index 0000000000..c23a7f42dd --- /dev/null +++ b/tests/integration-tc/src/main/java/org/apache/pulsar/tests/integration/FUNC_MODE.java @@ -0,0 +1,6 @@ +package org.apache.pulsar.tests.integration; + +public enum FUNC_MODE { + BROKER_CLUSTER, + STANDALONE; +} \ No newline at end of file diff --git a/tests/integration-tc/src/main/java/org/apache/pulsar/tests/integration/PulsarStandAloneCluster.java b/tests/integration-tc/src/main/java/org/apache/pulsar/tests/integration/PulsarStandAloneCluster.java new file mode 100644 index 0000000000..07d9de93d4 --- /dev/null +++ b/tests/integration-tc/src/main/java/org/apache/pulsar/tests/integration/PulsarStandAloneCluster.java @@ -0,0 +1,121 @@ +package org.apache.pulsar.tests.integration; + +import com.github.dockerjava.api.command.CreateContainerCmd; +import com.github.dockerjava.api.command.LogContainerCmd; +import com.github.dockerjava.api.model.Frame; +import com.github.dockerjava.core.command.LogContainerResultCallback; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.output.Slf4jLogConsumer; +import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy; + +import java.time.Duration; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +import static java.time.temporal.ChronoUnit.SECONDS; + +public class PulsarStandAloneCluster extends GenericContainer<PulsarStandAloneCluster> { + + private static final Logger LOG = LoggerFactory.getLogger(PulsarStandAloneCluster.class); + + private static final int PULSAR_BROKER_PORT = 6650; + private static final int PULSAR_ADMIN_PORT = 8080; + + private static final String PULSAR_HOST_NAME = "broker"; + + private static final String NAME = "apachepulsar"; + private static final String IMG = "pulsar-test-latest-version"; + + private static final String CONTAINER_NAME_BASE = "pulsar-test"; + private FUNC_MODE func_mode; + private String containerNameAddition; + + public PulsarStandAloneCluster(String containerName){ + this(containerName, FUNC_MODE.STANDALONE); + } + + public PulsarStandAloneCluster(String containerName, FUNC_MODE mode){ + this(containerName, mode, NAME + "/" + IMG + ":" + "latest"); + } + + private PulsarStandAloneCluster(final String containerName, final FUNC_MODE mode, final String imageName) { + super(imageName); + this.func_mode = mode; + this.containerNameAddition = containerName; + } + + @Override + protected void configure() { + addExposedPort(PULSAR_BROKER_PORT); + addExposedPort(PULSAR_ADMIN_PORT); + addEnv("FUNCTION_MODE", func_mode.toString()); + } + + public List<String> getContainerLog() { + final List<String> logs = new ArrayList<>(); + + LogContainerCmd logContainerCmd = this.dockerClient.logContainerCmd(containerId); + logContainerCmd.withStdOut(true).withStdErr(true); + try { + logContainerCmd.exec(new LogContainerResultCallback() { + @Override + public void onNext(Frame item) { + logs.add(item.toString()); + } + }).awaitCompletion(); + } catch (InterruptedException e) { + + } + return logs; + } + + public FUNC_MODE getFuncMode(){ + return this.func_mode; + } + + @Override + public void start() { + this.waitStrategy = new LogMessageWaitStrategy() + .withRegEx(".*Successfully validated clusters on tenant.*\\s") + .withTimes(1) + .withStartupTimeout(Duration.of(60, SECONDS)); + this.withCreateContainerCmdModifier(new Consumer<CreateContainerCmd>() { + @Override + public void accept(CreateContainerCmd createContainerCmd) { + createContainerCmd.withHostName(PULSAR_HOST_NAME); + createContainerCmd.withName(getContainerName()); + } + }); + this.withExposedPorts(PULSAR_BROKER_PORT, PULSAR_ADMIN_PORT); + this.withLogConsumer(new Slf4jLogConsumer(LOG)); + this.withCommand("/pulsar/bin/pulsar", "standalone"); + super.start(); + LOG.info("Container Name : " + this.containerName.substring(1)); + } + + @Override + public void stop() { + super.stop(); + } + + public String getContainerName(){ + return CONTAINER_NAME_BASE + "-" + containerNameAddition + "-" + getFuncMode().toString(); + } + + public int getPulsarBrokerPort(){ + return this.getMappedPort(PULSAR_BROKER_PORT); + } + + public int getPulsarAdminPort(){ + return this.getMappedPort(PULSAR_ADMIN_PORT); + } + + public String getPulsarUrl() { + return "pulsar://" + this.getContainerIpAddress() + ":" + this.getPulsarBrokerPort(); + } + + +} \ No newline at end of file diff --git a/tests/integration-tc/src/test/java/org/apache/pulsar/tests/integration/PulsarSimpleTest.java b/tests/integration-tc/src/test/java/org/apache/pulsar/tests/integration/PulsarSimpleTest.java new file mode 100644 index 0000000000..7414152709 --- /dev/null +++ b/tests/integration-tc/src/test/java/org/apache/pulsar/tests/integration/PulsarSimpleTest.java @@ -0,0 +1,48 @@ +package org.apache.pulsar.tests.integration; + +import org.apache.pulsar.client.api.Consumer; +import org.apache.pulsar.client.api.Message; +import org.apache.pulsar.client.api.Producer; +import org.apache.pulsar.client.api.PulsarClient; +import org.apache.pulsar.client.api.PulsarClientException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.Assert; +import org.testng.annotations.AfterTest; +import org.testng.annotations.BeforeTest; +import org.testng.annotations.Test; + +public class PulsarSimpleTest { + + private static final Logger LOG = LoggerFactory.getLogger(PulsarSimpleTest.class); + + private PulsarStandAloneCluster pulsarCluster; + private PulsarClient client; + private Producer producer; + private Consumer consumer; + + @BeforeTest + public void setup(){ + pulsarCluster = new PulsarStandAloneCluster(PulsarSimpleTest.class.getSimpleName()); + pulsarCluster.start(); + } + + @Test + public void testTopicSendReceive() throws PulsarClientException { + client = PulsarClient.builder().serviceUrl(pulsarCluster.getPulsarUrl()).build(); + consumer = client.newConsumer().topic("test").subscriptionName("subs").subscribe(); + producer = client.newProducer().topic("test").create(); + producer.send("Hello World".getBytes()); + Message message = consumer.receive(); + producer.close(); + consumer.close(); + client.close(); + Assert.assertEquals(new String(message.getData()), "Hello World"); + } + + @AfterTest + public void cleanup(){ + pulsarCluster.stop(); + } + +} diff --git a/tests/pom.xml b/tests/pom.xml index 44e0f2f502..8962d76f97 100644 --- a/tests/pom.xml +++ b/tests/pom.xml @@ -37,6 +37,7 @@ <module>integration-tests-topologies</module> <module>integration-tests-base</module> <module>integration</module> + <module>integration-tc</module> </modules> <build> <plugins> ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
