Home Running testcontainers within drone
Post
Cancel

Running testcontainers within drone

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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

1
2
3
4
5
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.

1
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

This post is licensed under CC BY 4.0 by the author.