Given that usually Java tests will be ran inside a CI system, and that drone is gaining momentum given it simplicity, this post tries to briefly show a drone pipeline in yaml that allows for testing your project using testcontainer within drone.

Requirements:

  • drone-cli
  • docker

Drone pipeline for testcontainers

kind: pipeline
name: integration tests

platform:
  os: linux
  arch: amd64

steps:
- name: step just to give an example project - remove at will
  image: docker:git
  commands:
  - git clone https://github.com/Ilhicas/quarkus-testcontainers.git

- name: tests
  image: maven:3.6.3-jdk-11-openj9
  commands:
  - cp application.properties quarkus-testcontainers/src/main/resources/application.properties
  - cd quarkus-testcontainers
  - mvn clean test  
  volumes:
  - name: docker
    path: /var/run/docker.sock

- name: cleanup - This is just to ensure we clean first step that git clones example
  image: alpine:3.9
  commands:
  - rm -r quarkus-testcontainers
  when:
    status:
    - failure
    - success

volumes:
- name: docker
  host:
    path: /var/run/docker.sock

Validating pipeline

For the purpose of demonstration we don’t actually need a drone server or agent to validate this pipeline.

We will use drone locally.

So install drone-cli, if you haven’t done yet.

Given that the project we are pulling is setting localhost at properties for the container being launched, and given that we can’t actually access the localhost from inside the docker container where the step will run we need to map it to our docker host ip, default in linux systems is 172.17.0.1

So lets add the following application.properties next to our .drone.yml file

quarkus.datasource.url = jdbc:postgresql://172.17.0.1:5432/testcontainers
quarkus.datasource.driver = org.postgresql.Driver
quarkus.datasource.username = andre
quarkus.datasource.password = ilhicas
quarkus.hibernate-orm.database.generation = drop-and-create

So now, all we need to do is run the pipeline locally. Since we need to map the docker socket via volume in order to grant power to our running container to launch new containers, we also need to run our pipeline as trusted.

drone exec --trusted

Should output something similar (after running the pipeline) to:

[tests:1654] [INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 48.768 s - in com.ilhicas.QuarkusTestContainersTest
[tests:1655] 2020-02-13 07:09:04,716 INFO  [io.quarkus] (main) Quarkus stopped in 0.119s
[tests:1656] [INFO]
[tests:1657] [INFO] Results:
[tests:1658] [INFO]
[tests:1659] [INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[tests:1660] [INFO]
[tests:1661] [INFO] ------------------------------------------------------------------------
[tests:1662] [INFO] BUILD SUCCESS
[tests:1663] [INFO] ------------------------------------------------------------------------
[tests:1664] [INFO] Total time:  01:39 min
[tests:1665] [INFO] Finished at: 2020-02-13T07:09:05Z
[tests:1666] [INFO] ------------------------------------------------------------------------
[cleanup - This is just to ensure we clean first step that git clones example:0] + rm -r quarkus-testcontainers

And that’s it, hope you find it helpful. Don’t forget to share.

As always a companion repository has been created for this post with the files required to run.

Drone with Testcontainers

André Ilhicas dos Santos

Devops Padawan, curious about systems automation, learning new languages, paradigms tools each day.

ilhicas ilhicas


Published