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.
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" ]
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>
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>
Use localhost:${minio-it.port} for accessing cloud storage
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
This value should also be set when -DskipITs is set