0

I would like to run tests verifying the correct execution of Flyway migrations using TestContainers.

Using JUnit5, I would like to enable these tests only on a host that have a Docker daemon running (@EnabledIfSystemProperty(named = "docker...", matches = "")) https://junit.org/junit5/docs/current/user-guide/#writing-tests-conditional-execution-system-properties.

My question is: how can I check that a Docker daemon is available on host using environment variables?

PS: I don't have any access to the CI host.

Florian Lopes
  • 1,093
  • 1
  • 13
  • 20

2 Answers2

0

If you can run bash before that, you can run :

export IS_DOCKER_RUNNING =`cat /var/run/docker.pid`

and check if the environment variable is empty or contain an id.

Nima Hashemi
  • 176
  • 4
0

There are several variables involved with this ("does the calling user have permissions" is an important check; "is the Docker I have access to actually local" is another interesting question) and there isn't going to be a magic environment variable that tells you this.

I'd probably try running a throwaway container; something along the lines of

docker run --rm busybox /bin/true

and if it succeeds move forward with other Docker-based end-to-end tests.

Building on @NinaHashemi's answer, if it must be an environment variable, and you can run a shell script before/around your tests (any POSIX shell, not necessarily bash) then you can run

if docker run --rm busybox /bin/true >/dev/null 2>&1; then
    export IS_DOCKER_RUNNING=yes
fi
David Maze
  • 130,717
  • 29
  • 175
  • 215
  • Thanks for your answer. As said previously, I don't have access to the host running the build. I will try to create this variable (either using @NinaHashemi's answer or yours) in my Jenkinsfile, before running the tests. – Florian Lopes Nov 23 '18 at 13:23
  • You can do something similar in Groovy (in the Jenkins pipeline language) but I'm not that well-versed in it. – David Maze Nov 23 '18 at 13:25
  • I have finally decided to exclude tests needing Docker runtime using JUnit5 groups and Maven profiles (dev/ci) to guarantee the same behavior between local builds (on development machine) and integration builds. – Florian Lopes Nov 29 '18 at 11:32