项目作者: SchulteMarkus

项目描述 :
Demonstrating "Docker Compose JUnit Rule" in a "real-world" project
高级语言: Java
项目地址: git://github.com/SchulteMarkus/docker-compose-rule-spark-demo.git
创建时间: 2017-06-06T11:52:57Z
项目社区:https://github.com/SchulteMarkus/docker-compose-rule-spark-demo

开源协议:Apache License 2.0

下载


Docker Compose JUnit Rule Spark demo

Build Status
Checkstyle count

Demonstrating Docker Compose JUnit Rule in a
Spark project.

Challenge

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.

Demo setup

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.

Demo usage

Required

  1. # Builds a docker-image for this git-commit-id, integration-tests against a related container
  2. docker-compose-rule-spark-demo $ mvn verify
  3. ...
  4. [INFO] DOCKER> [schulte.markus/docker-compose-rule-spark-demo:8379f8a]: Built image sha256:6d570
  5. ...
  6. [INFO] Running schulte.markus.dockercomposerulesparkdemo.AppIT
  7. ...
  8. [INFO] BUILD SUCCESS

Solution

  1. Create a docker-image (service), containing the current state of the application, tagged with the
    git-commit-id
  2. Integration-test the service, running in a related docker-container

In this documentation, 8379f8a is the current git-commit-id

1. Create docker-images, tagged with git-commit-id

  • First of all, you need to create an executable jar for your Spark-application. You can do so by
    using maven-shade-plugin, see
    pom.xml. This way, you wil have a target/docker-compose-rule-spark-demo.jar, which you
    can run via java -jar target/docker-compose-rule-spark-demo.jar.
    ```bash
    docker-compose-rule-spark-demo $ mvn package

    [INFO] —- maven-shade-plugin:3.0.0:shade (default) @ docker-compose-rule-spark-demo —-
    [INFO] Including com.sparkjava:spark-core:jar:2.6.0 in the shaded jar.

    [INFO] Replacing original artifact with shaded artifact.
    [INFO] Replacing /home/markus-tarent/workspace/docker-compose-rule-spark-demo/target/docker-compose-rule-spark-demo.jar with /home/markus-tarent/workspace/docker-compose-rule-spark-demo/target/docker-compose-rule-spark-demo-shaded.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

  1. - Now you need to have information about your git-commit-id both while maven-building as well as
  2. useable in your later integration-test. For this purpose, you can use
  3. [Maven git commit id plugin](https://github.com/ktoso/maven-git-commit-id-plugin), see
  4. [pom.xml](pom.xml). The way this Maven-plugin is used, you will have maven-variables while building,
  5. containing information about git (*${git.commit.id}* for example), as well as a
  6. *target/classes/git.properties*-file, containing needed information, useable at runtime.
  7. ```bash
  8. docker-compose-rule-spark-demo $ mvn compile
  9. ...
  10. [INFO] --- git-commit-id-plugin:2.1.9:revision (default) @ docker-compose-rule-spark-demo ---
  11. ...
  12. docker-compose-rule-spark-demo $ cat target/classes/git.properties | grep commit.id
  13. git.commit.id.abbrev=8379f8a
  14. git.commit.id=8379f8ae469a71d10c63b875abe643724efd4092
  15. git.commit.id.describe=8379f8a
  • Have a Dockerfile for building a docker-image for your service.
  • Now, build your docker-image as part of maven package-phase. For this purpose
    fabric8io/docker-maven-plugin is used, see
    pom.xml. The way this Maven-plugin is used, a docker-image will be created while
    maven package-phase, tagged with the current git-commit-id. Note the required .maven-dockerignore.
    ```bash
    docker-compose-rule-spark-demo $ mvn package

    [INFO] —- docker-maven-plugin:0.21.0:build (default) @ docker-compose-rule-spark-demo —-
    [INFO] Building tar: /home/markus-tarent/workspace/docker-compose-rule-spark-demo/target/docker/schulte.markus/docker-compose-rule-spark-demo/8379f8a/tmp/docker-build.tar
    [INFO] DOCKER> [schulte.markus/docker-compose-rule-spark-demo:8379f8a]: Created docker-build.tar in 107 milliseconds
    [INFO] DOCKER> [schulte.markus/docker-compose-rule-spark-demo:8379f8a]: Built image sha256:ee9c7
    [INFO] DOCKER> [schulte.markus/docker-compose-rule-spark-demo:8379f8a]: Removed old image sha256:6d570
    [INFO] ————————————————————————————————————
    [INFO] BUILD SUCCESS

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

  1. ### 2. Integration-test
  2. - [AppIT](src/test/java/schulte/markus/dockercomposerulesparkdemo/AppIT.java) will be your
  3. integration-test, using [JUnit](http://junit.org/). Don't forget to configure your
  4. [Maven Failsafe Plugin](https://maven.apache.org/surefire/maven-failsafe-plugin/) for running
  5. while `maven verify`-phase (see [pom.xml](pom.xml)).
  6. - First you have to get information about your git-commit-id. This is extracted from
  7. *target/classes/git.properties*, using
  8. [GitHelper](src/test/java/schulte/markus/dockercomposerulesparkdemo/GitHelper.java).
  9. - Now, you can finally use [Docker Compose JUnit Rule](https://github.com/palantir/docker-compose-rule)
  10. in your test. This starts the correct spark-hello-world-service, defined in your
  11. [src/test/resources/docker-compose.yml](src/test/resources/docker-compose.yml). The correct
  12. version (git-commit-id) is given to the docker-compose.yml, by passing in an environment variable.
  13. ```bash
  14. docker-compose-rule-spark-demo $ mvn verify
  15. ...
  16. [INFO] -------------------------------------------------------
  17. [INFO] T E S T S
  18. [INFO] -------------------------------------------------------
  19. [INFO] Running schulte.markus.dockercomposerulesparkdemo.AppIT
  20. ...
  21. [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.419 s - in schulte.markus.dockercomposerulesparkdemo.AppIT
  22. [INFO]
  23. [INFO] Results:
  24. [INFO]
  25. [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
  26. [INFO]
  27. [INFO]
  28. [INFO] --- maven-failsafe-plugin:2.20:verify (default) @ docker-compose-rule-spark-demo ---
  29. [INFO] ------------------------------------------------------------------------
  30. [INFO] BUILD SUCCESS
  31. docker-compose-rule-spark-demo $ docker ps #While AppIT had run
  32. 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