public final class StatefulFunctionsAppContainers
extends org.junit.rules.ExternalResource
TestRule
that setups a containerized Stateful Functions
application using Testcontainers. This allows
composing end-to-end tests for Stateful Functions applications easier, by managing the
containerized application as an external test resource whose lifecycle is integrated with the
JUnit test framework.
public class MyE2E {
{@code @Rule}
public StatefulFunctionsAppContainers myApp =
StatefulFunctionsAppContainers.builder("app-name", 3).build();
{@code @Test}
public void runTest() {
// the containers for the app, including master and workers, will already be running
// before the test is run; implement your test logic against the app
}
}
In most cases you'd also need to start an additional system for the test, for example starting a container that runs Kafka from which the application depends on as an ingress or egress. The following demonstrates adding a Kafka container to the setup:
public class MyKafkaE2E {
{@code @Rule}
public KafkaContainer kafka = new KafkaContainer();
{@code @Rule}
public StatefulFunctionsAppContainers myApp =
StatefulFunctionsAppContainers.builder("app-name", 3)
.dependsOn(kafka)
.build();
...
}
Application master and worker containers will always be started after containers that are
added using StatefulFunctionsAppContainers.Builder.dependsOn(GenericContainer)
have started. Moreover, containers being
depended on will also be setup such that they share the same network with the master and workers,
so that they can freely communicate with each other.
Since Testcontainers uses Docker, it is required that you have Docker installed for this test rule to work.
When building the Docker image for the Stateful Functions application under test, the following files are added to the build context:
Dockerfile
found at path /Dockerfile
in the classpath. This is required
to be present. A simple way is to add the Dockerfile to the test resources directory. This
will be added to the root of the Docker image build context.
flink-conf.yaml
found at path /flink-conf.yaml
in the classpath, if
any. You can also add this to the test resources directory. This will be added to the root
of the Docker image build context.
target
folder for the project module that
the test resides in. This is required to be present, so this entails that the tests can
only be ran after artifacts are built. The built artifacts are added to the root of the
Docker image build context.
Modifier and Type | Class and Description |
---|---|
static class |
StatefulFunctionsAppContainers.Builder |
Modifier and Type | Method and Description |
---|---|
protected void |
after() |
protected void |
before() |
static StatefulFunctionsAppContainers.Builder |
builder(java.lang.String appName,
int numWorkers)
Creates a builder for creating a
StatefulFunctionsAppContainers . |
int |
getMasterRestPort() |
void |
restartWorker(int workerIndex)
Restarts a single worker of this Stateful Functions application.
|
public static StatefulFunctionsAppContainers.Builder builder(java.lang.String appName, int numWorkers)
StatefulFunctionsAppContainers
.appName
- the name of the application.numWorkers
- the number of workers to run the application.StatefulFunctionsAppContainers
.protected void before() throws java.lang.Throwable
before
in class org.junit.rules.ExternalResource
java.lang.Throwable
protected void after()
after
in class org.junit.rules.ExternalResource
public int getMasterRestPort()
public void restartWorker(int workerIndex)
workerIndex
- the index of the worker to restart.Copyright © 2014–2024 The Apache Software Foundation. All rights reserved.