Skip to content

Commit

Permalink
Add testcontainer utils for test.
Browse files Browse the repository at this point in the history
  • Loading branch information
RocMarshal committed Jul 17, 2023
1 parent fe3f05c commit 6538b40
Show file tree
Hide file tree
Showing 7 changed files with 473 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<module>streampark-spark</module>
<module>streampark-storage</module>
<module>streampark-console</module>
<module>streampark-test-utils</module>
</modules>

<properties>
Expand Down
40 changes: 40 additions & 0 deletions streampark-test-utils/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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.streampark</groupId>
<artifactId>streampark</artifactId>
<version>2.2.0-SNAPSHOT</version>
</parent>

<artifactId>streampark-test-utils</artifactId>
<packaging>pom</packaging>

<name>streampark-test-utils</name>
<url>http://maven.apache.org</url>
<modules>
<module>streampark-testcontainer</module>
</modules>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
104 changes: 104 additions & 0 deletions streampark-test-utils/streampark-testcontainer/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?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.streampark</groupId>
<artifactId>streampark-test-utils</artifactId>
<version>2.2.0-SNAPSHOT</version>
</parent>

<artifactId>streampark-testcontainer</artifactId>
<packaging>jar</packaging>

<name>streampark-testcontainer</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin-reflect.version>1.6.21</kotlin-reflect.version>
<findbugs.version>3.0.2</findbugs.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
<version>1.16.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>${httpclient5.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>${kotlin-reflect.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.streampark</groupId>
<artifactId>streampark-common_2.12</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>${findbugs.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.
*/

package org.apache.streampark.testcontainer.flink;

import javax.annotation.Nonnull;

enum FlinkComponent {
JOBMANAGER("jobmanager"),
TASKMANAGER("taskmanager");

private final String name;

FlinkComponent(@Nonnull String name) {
this.name = name;
}

@Nonnull
public String getName() {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.
*/

package org.apache.streampark.testcontainer.flink;

import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.utility.DockerImageName;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;

import static org.apache.streampark.testcontainer.flink.FlinkComponent.JOBMANAGER;
import static org.apache.streampark.testcontainer.flink.FlinkComponent.TASKMANAGER;

class FlinkContainer extends GenericContainer<FlinkContainer> {

public static final AtomicInteger TM_COUNT = new AtomicInteger(0);

public static final String FLINK_PROPS_KEY = "FLINK_PROPERTIES";

private final @Nonnull FlinkComponent component;

FlinkContainer(
@Nonnull DockerImageName dockerImageName,
@Nonnull FlinkComponent component,
@Nonnull Network network,
@Nonnull String yamlPropStr,
@Nullable Slf4jLogConsumer slf4jLogConsumer) {
super(dockerImageName);
this.component = component;
this.withCommand("/docker-entrypoint.sh", component.getName());
this.withCreateContainerCmdModifier(
createContainerCmd -> createContainerCmd.withName(getFlinkContainerName()));
this.withNetwork(network);
this.withEnv(FLINK_PROPS_KEY, yamlPropStr);
Optional.ofNullable(slf4jLogConsumer).ifPresent(this::withLogConsumer);
}

protected String getFlinkContainerName() {
if (component == JOBMANAGER) {
return JOBMANAGER.getName();
}
return String.format("%s_%s", TASKMANAGER.getName(), TM_COUNT.incrementAndGet());
}

public void addRequiredFixedExposedPorts(int hostPort, int containerPort) {

this.addFixedExposedPort(hostPort, containerPort);
}
}
Loading

0 comments on commit 6538b40

Please sign in to comment.