Class FlinkContainers
- All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterAllCallback,org.junit.jupiter.api.extension.BeforeAllCallback,org.junit.jupiter.api.extension.Extension
This containerized Flink cluster is based on Testcontainers, which simulates a truly distributed
environment for E2E tests. This class can also be used as an Extension of JUnit 5 so that
the lifecycle of the cluster can be easily managed by JUnit Jupiter engine.
Example usage
public class E2ETest {
// Create a Flink cluster using default configurations.
// Remember to declare it as "static" as required by
// JUnit 5.
@RegisterExtension
static FlinkContainers flink =
FlinkContainers.builder().build();
---
// To work together with other containers
static TestcontainersSettings testcontainersSettings =
TestcontainersSettings.builder()
.dependsOn(kafkaContainer)
.build());
@RegisterExtension
static FlinkContainers flink =
FlinkContainers.builder()
.withTestcontainersSettings(testcontainersSettings)
.build();
---
// Customize a Flink cluster
static FlinkContainersSettings flinkContainersSettings =
FlinkContainersSettings.builder()
.numTaskManagers(3)
.numSlotsPerTaskManager(6)
.baseImage(
"ghcr.io/apache/flink-docker:1.16-snapshot-scala_2.12-java11-debian")
.enableZookeeperHA()
.build();
static TestcontainersSettings testcontainersSettings =
TestcontainersSettings.builder()
.setLogger(
LoggerFactory.getLogger(E2ETest.class))
.build());
@RegisterExtension
static FlinkContainers flink =
FlinkContainers.builder()
.withFlinkContainersSettings(flinkContainersSettings)
.withTestcontainersSettings(testcontainersSettings)
.build();
...
}
See FlinkContainersSettings and TestcontainersSettings for available options.
Prerequisites
Docker environment is required in the running machine since this class is based on Testcontainers.
Make sure you have an already-built flink-dist either under the current project, or specify the path manually in builder.
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionvoidafterAll(org.junit.jupiter.api.extension.ExtensionContext context) voidbeforeAll(org.junit.jupiter.api.extension.ExtensionContext context) static FlinkContainers.Builderbuilder()Creates a builder forFlinkContainers.org.testcontainers.containers.GenericContainer<?>Gets JobManager container.Gets JobManager's hostname on the host machine.intGets JobManager's port on the host machine.org.apache.flink.client.program.rest.RestClusterClient<org.apache.flink.client.deployment.StandaloneClusterId>Gets REST client connected to JobManager.List<org.testcontainers.containers.GenericContainer<?>>Gets TaskManager containers.booleanGets the running state of the cluster.voidrestartJobManager(org.apache.flink.util.function.RunnableWithException afterFailAction) Restarts JobManager container.voidrestartTaskManager(org.apache.flink.util.function.RunnableWithException afterFailAction) Restarts all TaskManager containers.voidstart()Starts all containers.voidstop()Stops all containers.org.apache.flink.api.common.JobIDsubmitJob(org.apache.flink.test.util.JobSubmission job) Submits the given job to the cluster.voidsubmitSQLJob(org.apache.flink.test.util.SQLJobSubmission job) Submits an SQL job to the running cluster.
-
Field Details
-
DEFAULT_TIMEOUT
-
-
Method Details
-
builder
Creates a builder forFlinkContainers. -
start
Starts all containers.- Throws:
Exception
-
stop
public void stop()Stops all containers. -
isStarted
public boolean isStarted()Gets the running state of the cluster. -
getJobManager
public org.testcontainers.containers.GenericContainer<?> getJobManager()Gets JobManager container. -
getTaskManagers
Gets TaskManager containers. -
getJobManagerHost
Gets JobManager's hostname on the host machine. -
getJobManagerPort
public int getJobManagerPort()Gets JobManager's port on the host machine. -
getRestClusterClient
@Nullable public org.apache.flink.client.program.rest.RestClusterClient<org.apache.flink.client.deployment.StandaloneClusterId> getRestClusterClient()Gets REST client connected to JobManager. -
restartJobManager
public void restartJobManager(org.apache.flink.util.function.RunnableWithException afterFailAction) throws Exception Restarts JobManager container.Note that the REST port will be changed because the new JM container will be mapped to another random port. Please make sure to get the REST cluster client again after this method is invoked.
- Throws:
Exception
-
restartTaskManager
public void restartTaskManager(org.apache.flink.util.function.RunnableWithException afterFailAction) throws Exception Restarts all TaskManager containers.- Throws:
Exception
-
submitSQLJob
public void submitSQLJob(org.apache.flink.test.util.SQLJobSubmission job) throws IOException, InterruptedException Submits an SQL job to the running cluster.NOTE: You should not use
'\t'.- Throws:
IOExceptionInterruptedException
-
submitJob
public org.apache.flink.api.common.JobID submitJob(org.apache.flink.test.util.JobSubmission job) throws IOException, InterruptedException Submits the given job to the cluster.- Parameters:
job- job to submit- Throws:
IOExceptionInterruptedException
-
beforeAll
- Specified by:
beforeAllin interfaceorg.junit.jupiter.api.extension.BeforeAllCallback- Throws:
Exception
-
afterAll
public void afterAll(org.junit.jupiter.api.extension.ExtensionContext context) - Specified by:
afterAllin interfaceorg.junit.jupiter.api.extension.AfterAllCallback
-