Skip to content

Instantly share code, notes, and snippets.

@terrywbrady
Created June 9, 2022 15:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terrywbrady/c8b4f8a619cf74302a4c641cd932e916 to your computer and use it in GitHub Desktop.
Save terrywbrady/c8b4f8a619cf74302a4c641cd932e916 to your computer and use it in GitHub Desktop.
Maven integration tests utilizing external containers for testing

Overview

The io.fabric8 docker-maven-plugin can start and stop docker containers to support junit integration tests.

These tests run during the integration test phase.

Example Container - test S3 api interactions during integration tests

For instance, create a container build on minio/minio to simulate interactions with a cloud service.

Tag this image as dspace/minio-it

FROM minio/minio

RUN mkdir -p /buckets/test-bucket 

EXPOSE 9000 9001

CMD [ "minio", "server", "/buckets", "--address", ":9000", "--console-address", ":9001" ]

Add the following plugin to pom.xml

Maven will assign a high port number to the container. That value will be set as a property minio-it.port.

      <plugin>
        <groupId>io.fabric8</groupId>
        <artifactId>docker-maven-plugin</artifactId>
        <version>0.20.1</version>
        <executions>
            <execution>
                <id>prepare-it-server</id>
                <phase>pre-integration-test</phase>
                <goals>
                    <goal>start</goal>
                </goals>
                <configuration>
                    <images>
                        <image>
                            <name>dspace/minio-it</name>
                            <alias>it-server</alias>
                            <run>
                                <ports>
                                    <port>minio-it.port:9000</port>
                                </ports>
                                <wait>
                                    <time>2000</time>
                                </wait>
                            </run>
                        </image>
                    </images>
                </configuration>
            </execution>
            <execution>
                <id>remove-it-server</id>
                <phase>post-integration-test</phase>
                <goals>
                    <goal>stop</goal>
                </goals>
            </execution>
        </executions>
      </plugin>

Modify the maven-failsafe-plugin configuration

Make the port number accessible to integration tests as an environment variable.

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <configuration>
          <environmentVariables>
            <minio-it.port>${minio-it.port}</minio-it.port>
          </environmentVariables>
        </configuration>
      </plugin>

Write a new integration test

Use localhost:${minio-it.port} for accessing cloud storage

Run mvn verify or mvn install to execute the test

Before tests, you will see the following output

[INFO] DOCKER> [dspace/minio-it:latest] "minio-it": Start container a245c7e4872c
[INFO] 

After tests, you will see the following output

INFO] DOCKER> [dspace/minio-it:latest] "minio-it": Stop and removed container a245c7e4872c after 0 ms

Set the -Ddocker.skip property if you want to skip the start/stop of docker containers

This value should also be set when -DskipITs is set

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment