Demonstrating "Docker Compose JUnit Rule" in a "real-world" project
Demonstrating Docker Compose JUnit Rule in a
Spark project.
You got a Java-microservice, which you want to run using
Docker(-compose). You want to
integration-test this (your application running in Docker), using your build-tool
Apache Maven and your test-framework JUnit. You
are using Git.
This testing shall be capable of parallel builds (on different git-branches) at the same time. You want to test exact the
git-commit-id you are working on, or your CI-system has checked out.
For this demo, Spark is used, “A micro framework for creating web
applications[…]“. The application is very simple, it serves /hello as a “Hello World”-endpoint,
see App.java.
Required
# Builds a docker-image for this git-commit-id, integration-tests against a related container
docker-compose-rule-spark-demo $ mvn verify
...
[INFO] DOCKER> [schulte.markus/docker-compose-rule-spark-demo:8379f8a]: Built image sha256:6d570
...
[INFO] Running schulte.markus.dockercomposerulesparkdemo.AppIT
...
[INFO] BUILD SUCCESS
In this documentation, 8379f8a is the current git-commit-id
java -jar target/docker-compose-rule-spark-demo.jar
.docker-compose-rule-spark-demo $ ls target/ | grep .jar
docker-compose-rule-spark-demo.jar # Executable jar
original-docker-compose-rule-spark-demo.jar
- Now you need to have information about your git-commit-id both while maven-building as well as
useable in your later integration-test. For this purpose, you can use
[Maven git commit id plugin](https://github.com/ktoso/maven-git-commit-id-plugin), see
[pom.xml](pom.xml). The way this Maven-plugin is used, you will have maven-variables while building,
containing information about git (*${git.commit.id}* for example), as well as a
*target/classes/git.properties*-file, containing needed information, useable at runtime.
```bash
docker-compose-rule-spark-demo $ mvn compile
...
[INFO] --- git-commit-id-plugin:2.1.9:revision (default) @ docker-compose-rule-spark-demo ---
...
docker-compose-rule-spark-demo $ cat target/classes/git.properties | grep commit.id
git.commit.id.abbrev=8379f8a
git.commit.id=8379f8ae469a71d10c63b875abe643724efd4092
git.commit.id.describe=8379f8a
maven package
-phase. For this purposemaven package
-phase, tagged with the current git-commit-id. Note the required .maven-dockerignore.docker-compose-rule-spark-demo $ docker images schulte.markus/docker-compose-rule-spark-demo
REPOSITORY TAG IMAGE ID CREATED SIZE
schulte.markus/docker-compose-rule-spark-demo 8379f8a f534f2fa3d3e About a minute ago 83.9MB
### 2. Integration-test
- [AppIT](src/test/java/schulte/markus/dockercomposerulesparkdemo/AppIT.java) will be your
integration-test, using [JUnit](http://junit.org/). Don't forget to configure your
[Maven Failsafe Plugin](https://maven.apache.org/surefire/maven-failsafe-plugin/) for running
while `maven verify`-phase (see [pom.xml](pom.xml)).
- First you have to get information about your git-commit-id. This is extracted from
*target/classes/git.properties*, using
[GitHelper](src/test/java/schulte/markus/dockercomposerulesparkdemo/GitHelper.java).
- Now, you can finally use [Docker Compose JUnit Rule](https://github.com/palantir/docker-compose-rule)
in your test. This starts the correct spark-hello-world-service, defined in your
[src/test/resources/docker-compose.yml](src/test/resources/docker-compose.yml). The correct
version (git-commit-id) is given to the docker-compose.yml, by passing in an environment variable.
```bash
docker-compose-rule-spark-demo $ mvn verify
...
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running schulte.markus.dockercomposerulesparkdemo.AppIT
...
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.419 s - in schulte.markus.dockercomposerulesparkdemo.AppIT
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- maven-failsafe-plugin:2.20:verify (default) @ docker-compose-rule-spark-demo ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
docker-compose-rule-spark-demo $ docker ps #While AppIT had run
3b953a1958f9 schulte.markus/docker-compose-rule-spark-demo:8379f8a "java -jar /app.jar" Less than a second ago Up Less than a second 0.0.0.0:32771->4567/tcp bdc647fb_spark-hello-world-service_1