Pipeline I/O Table of Contents

Testing I/O Transforms in Apache Beam

Examples and design patterns for testing Apache Beam I/O transforms

Note: This guide is still in progress. There is an open issue to finish the guide: BEAM-1025.


This document explains the set of tests that the Beam community recommends based on our past experience writing I/O transforms. If you wish to contribute your I/O transform to the Beam community, we’ll ask you to implement these tests.

While it is standard to write unit tests and integration tests, there are many possible definitions. Our definitions are:

A note on performance benchmarking

We do not advocate writing a separate test specifically for performance benchmarking. Instead, we recommend setting up integration tests that can accept the necessary parameters to cover many different testing scenarios.

For example, if integration tests are written according to the guidelines below, the integration tests can be run on different runners (either local or in a cluster configuration) and against a data store that is a small instance with a small data set, or a large production-ready cluster with larger data set. This can provide coverage for a variety of scenarios - one of them is performance benchmarking.

Test Balance - Unit vs Integration

It’s easy to cover a large amount of code with an integration test, but it is then hard to find a cause for test failures and the test is flakier.

However, there is a valuable set of bugs found by tests that exercise multiple workers reading/writing to data store instances that have multiple nodes (eg, read replicas, etc.). Those scenarios are hard to find with unit tests and we find they commonly cause bugs in I/O transforms.

Our test strategy is a balance of those 2 contradictory needs. We recommend doing as much testing as possible in unit tests, and writing a single, small integration test that can be run in various configurations.




Unit Tests



Implementing unit tests

A general guide to writing Unit Tests for all transforms can be found in the PTransform Style Guide. We have expanded on a few important points below.

If you are using the Source API, make sure to exhaustively unit-test your code. A minor implementation error can lead to data corruption or data loss (such as skipping or duplicating records) that can be hard for your users to detect. Also look into using SourceTestUtilssource_test_utils - it is a key piece of testing Source implementations.

If you are not using the Source API, you can use TestPipeline with PAssertassert_that to help with your testing.

If you are implementing write, you can use TestPipeline to write test data and then read and verify it using a non-Beam client.

Use fakes

Instead of using mocks in your unit tests (pre-programming exact responses to each call for each test), use fakes. The preferred way to use fakes for I/O transform testing is to use a pre-existing in-memory/embeddable version of the service you’re testing, but if one does not exist consider implementing your own. Fakes have proven to be the right mix of “you can get the conditions for testing you need” and “you don’t have to write a million exacting mock function calls”.

Network failure

To help with testing and separation of concerns, code that interacts across a network should be handled in a separate class from your I/O transform. The suggested design pattern is that your I/O transform throws exceptions once it determines that a read or write is no longer possible.

This allows the I/O transform’s unit tests to act as if they have a perfect network connection, and they do not need to retry/otherwise handle network connection problems.


If your I/O transform allows batching of reads/writes, you must force the batching to occur in your test. Having configurable batch size options on your I/O transform allows that to happen easily. These must be marked as test only.

I/O Transform Integration Tests

We do not currently have examples of Python I/O integration tests or integration tests for unbounded or eventually consistent data stores. We would welcome contributions in these areas - please contact the Beam dev@ mailing list for more information.


Integration tests, data stores, and Kubernetes

In order to test I/O transforms in real world conditions, you must connect to a data store instance.

The Beam community hosts the data stores used for integration tests in Kubernetes. In order for an integration test to be run in Beam’s continuous integration environment, it must have Kubernetes scripts that set up an instance of the data store.

However, when working locally, there is no requirement to use Kubernetes. All of the test infrastructure allows you to pass in connection info, so developers can use their preferred hosting infrastructure for local development.

Running integration tests

The high level steps for running an integration test are:

  1. Set up the data store corresponding to the test being run
  2. Run the test, passing it connection info from the just created data store
  3. Clean up the data store

Since setting up data stores and running the tests involves a number of steps, and we wish to time these tests when running performance benchmarks, we use PerfKit Benchmarker to manage the process end to end. With a single command, you can go from an empty Kubernetes cluster to a running integration test.

However, PerfKit Benchmarker is not required for running integration tests. Therefore, we have listed the steps for both using PerfKit Benchmarker, and manually running the tests below.

Using PerfKit Benchmarker


  1. Install PerfKit Benchmarker
  2. Have a running Kubernetes cluster you can connect to locally using kubectl

You won’t need to invoke PerfKit Benchmarker directly. Run mvn verify in the directory of the I/O module you’d like to test, with the parameter io-it-suite when running in jenkins CI or with a kubernetes cluster on the same network or io-it-suite-local when running on a local dev box accessing a kubernetes cluster on a remote network.

Example run with the direct runner:

mvn verify -Dio-it-suite-local -pl sdks/java/io/jdbc,sdks/java/io/jdbc -DpkbLocation="/Users/me/dev/PerfKitBenchmarker/pkb.py" -DforceDirectRunner -DintegrationTestPipelineOptions=["--myTestParam=val"]

Example run with the Cloud Dataflow runner:

mvn verify -Dio-it-suite -pl sdks/java/io/jdbc -DintegrationTestPipelineOptions=["--project=PROJECT","--gcpTempLocation=GSBUCKET"] -DintegrationTestRunner=dataflow -DpkbLocation="/Users/me/dev/PerfKitBenchmarker/pkb.py" 

Parameter descriptions:

Option Function
-Dio-it-suite Invokes the call to PerfKit Benchmarker when running in apache beam's jenkins instance or with a kubernetes cluster on the same network.
-Dio-it-suite-local io-it-suite-local when running on a local dev box accessing a kubernetes cluster on a remote network. May not be supported for all I/O transforms.
-pl sdks/java/io/jdbc Specifies the maven project of the I/O to test.
-Dkubectl="path-to-kubectl" -Dkubeconfig="path-to-kubeconfig" Options for specifying non-standard kubectl configurations. Optional. Defaults to "kubectl" and "~/.kube/config".
integrationTestPipelineOptions Passes pipeline options directly to the test being run.
-DforceDirectRunner Runs the test with the direct runner.

Without PerfKit Benchmarker

If you’re using Kubernetes, make sure you can connect to your cluster locally using kubectl. Otherwise, skip to step 3 below.

  1. Set up the data store corresponding to the test you wish to run. You can find Kubernetes scripts for all currently supported data stores in .test-infra/kubernetes.
    1. In some cases, there is a setup script (*.sh). In other cases, you can just run kubectl create -f [scriptname] to create the data store.
    2. Convention dictates there will be:
      1. A core yml script for the data store itself, plus a NodePort service. The NodePort service opens a port to the data store for anyone who connects to the Kubernetes cluster’s machines.
      2. A separate script, called for-local-dev, which sets up a LoadBalancer service.
    3. Examples:
      1. For JDBC, you can set up Postgres: kubectl create -f .test-infra/kubernetes/postgres/postgres.yml
      2. For Elasticsearch, you can run the setup script: bash .test-infra/kubernetes/elasticsearch/setup.sh
  2. Determine the IP address of the service:
    1. NodePort service: kubectl get pods -l 'component=elasticsearch' -o jsonpath={.items[0].status.podIP}
    2. LoadBalancer service: kubectl get svc elasticsearch-external -o jsonpath='{.status.loadBalancer.ingress[0].ip}'
  3. Run the test using the instructions in the class (e.g. see the instructions in JdbcIOIT.java)
  4. Tell Kubernetes to delete the resources specified in the Kubernetes scripts:
    1. JDBC: kubectl delete -f .test-infra/kubernetes/postgres/postgres.yml
    2. Elasticsearch: bash .test-infra/kubernetes/elasticsearch/teardown.sh

Implementing Integration Tests

There are three components necessary to implement an integration test:

These three pieces are discussed in detail below.

Test Code

These are the conventions used by integration testing code:

An end to end example of these principles can be found in JdbcIOIT.

Kubernetes scripts

As discussed in Integration tests, data stores, and Kubernetes, to have your tests run on Beam’s continuous integration server, you’ll need to implement a Kubernetes script that creates an instance of your data store.

If you would like help with this or have other questions, contact the Beam dev@ mailing list and the community may be able to assist you.

Guidelines for creating a Beam data store Kubernetes script:

  1. You must only provide access to the data store instance via a NodePort service.
    • This is a requirement for security, since it means that only the local network has access to the data store. This is particularly important since many data stores don’t have security on by default, and even if they do, their passwords will be checked in to our public Github repo.
  2. You should define two Kubernetes scripts.
    • This is the best known way to implement item #1.
    • The first script will contain the main datastore instance script (StatefulSet) plus a NodePort service exposing the data store. This will be the script run by the Beam Jenkins continuous integration server.
    • The second script will define a LoadBalancer service, used for local development if the Kubernetes cluster is on another network. This file’s name is usually suffixed with ‘-for-local-dev’.
  3. You must ensure that pods are recreated after crashes.
    • If you use a pod directly, it will not be recreated if the pod crashes or something causes the cluster to move the container for your pod.
    • In most cases, you’ll want to use StatefulSet as it supports persistent disks that last between restarts, and having a stable network identifier associated with the pod using a particular persistent disk. Deployment and ReplicaSet are also possibly useful, but likely in fewer scenarios since they do not have those features.
  4. You should create separate scripts for small and large instances of your data store.
  5. You must use a Docker image from a trusted source and pin the version of the Docker image.
    • You should prefer images in this order:
      1. An image provided by the creator of the data source/sink (if they officially maintain it). For Apache projects, this would be the official Apache repository.
      2. Official Docker images, because they have security fixes and guaranteed maintenance.
      3. Non-official Docker images, or images from other providers that have good maintainers (e.g. quay.io).

Integrate with PerfKit Benchmarker

To allow developers to easily invoke your I/O integration test, you must perform these two steps. The follow sections describe each step in more detail.

  1. Create a PerfKit Benchmarker benchmark configuration file for the data store. Each pipeline option needed by the integration test should have a configuration entry.
  2. Modify the per-I/O Maven pom configuration so that PerfKit Benchmarker can be invoked from Maven.

The goal is that a checked in config has defaults such that other developers can run the test without changing the configuration.

Defining the benchmark configuration file

The benchmark configuration file is a yaml file that defines the set of pipeline options for a specific data store. Some of these pipeline options are static - they are known ahead of time, before the data store is created (e.g. username/password). Others options are dynamic - they are only known once the data store is created (or after we query the Kubernetes cluster for current status).

All known cases of dynamic pipeline options are for extracting the IP address that the test needs to connect to. For I/O integration tests, we must allow users to specify:

The style of dynamic pipeline options used here should support a variety of other types of values derived from Kubernetes, but we do not have specific examples.

The dynamic pipeline options are:

Type name Meaning Selector field name Selector field value
NodePortIp We will be using the IP address of a k8s NodePort service, the value will be an IP address of a Pod podLabel A kubernetes label selector for a pod whose IP address can be used to connect to
LoadBalancerIp We will be using the IP address of a k8s LoadBalancer, the value will be an IP address of the load balancer serviceName The name of the LoadBalancer kubernetes service.

Benchmark configuration files: full example configuration file

A configuration file will look like this:

  -postgresUser: postgres
  -postgresPassword: postgres
  - paramName: PostgresIp
    type: NodePortIp
    podLabel: app=postgres

and may contain the following elements:

Configuration element Description and how to change when adding a new test
static_pipeline_options The set of preconfigured mvn pipeline options.
dynamic_pipeline_options The set of mvn pipeline options that PerfKit Benchmarker will determine at runtime.
dynamic_pipeline_options.name The name of the parameter to be passed to mvn's invocation of the I/O integration test.
dynamic_pipeline_options.type The method of determining the value of the pipeline options.
dynamic_pipeline_options - other attributes These vary depending on the type of the dynamic pipeline option - see the table of dynamic pipeline options for a description.

Per-I/O mvn pom configuration

Each I/O is responsible for adding a section to its pom with a profile that invokes PerfKit Benchmarker with the proper parameters during the verify phase. Below are the set of PerfKit Benchmarker parameters and how to configure them.

The JdbcIO pom has an example of how to put these options together into a profile and invoke Python+PerfKit Benchmarker with them.

PerfKit Benchmarker Parameter Description Example value
benchmarks Defines the PerfKit Benchmarker benchmark to run. This is same for all I/O integration tests. beam_integration_benchmark
beam_location The location where PerfKit Benchmarker can find the Beam repository. ${beamRootProjectDir} - this is a variable you'll need to define for each maven pom. See example pom for an example.
beam_prebuilt Whether or not to rebuild the Beam repository before invoking the I/O integration test command. true
beam_sdk Whether PerfKit Benchmarker will run the Beam SDK for Java or Python. java
beam_runner_profile Optional command line parameter used to override the runner, allowing us to use the direct runner. Always use the predefined variable instead of specifying this parameter ${pkbBeamRunnerProfile}
beam_runner_option Optional command line parameter used to override the runner, allowing us to use the direct runner. Always use the predefined variable instead of specifying this parameter ${pkbBeamRunnerOption}
beam_it_module The path to the pom that contains the test (needed for invoking the test with PerfKit Benchmarker). sdks/java/io/jdbc
beam_it_class The test to run. org.apache.beam.sdk.io.jdbc.JdbcIOIT
beam_it_options Pipeline options for the beam job - meant to be a way to pass pipeline options the user specifies on the commandline when invoking io-it-suite Always use ${integrationTestPipelineOptions}, which allows the user to pass in parameters.
kubeconfig The standard PerfKit Benchmarker parameter `kubeconfig`, which specifies where the Kubernetes config file lives. Always use ${kubeconfig}
kubectl The standard PerfKit Benchmarker parameter `kubectl`, which specifies where the kubectl binary lives. Always use ${kubectl}
beam_kubernetes_scripts The Kubernetes script files to create and teardown via create/delete. Specify absolute path. ${beamRootProjectDir}/.test-infra/kubernetes/postgres/pkb-config.yml

There is also a set of Maven properties which are useful when invoking PerfKit Benchmarker. These properties are configured in the I/O parent pom, and some are only available when the io-it-suite profile is active in Maven.

Small Scale and Large Scale Integration Tests

Apache Beam expects that it can run integration tests in multiple configurations:

You can do this by:

  1. Creating two Kubernetes scripts: one for a small instance of the data store, and one for a large instance.
  2. Having your test take a pipeline option that decides whether to generate a small or large amount of test data (where small and large are sizes appropriate to your data store)

An example of this is HadoopInputFormatIO’s tests.