This is a simple tutorial to show you how easy it can be to run isolated webtests in docker.
Presumed you want to run your webtests in an isolated environment for the following reasons:
isolation from other test runs (same ports/same directories/...)
automatic cleanup of ...
orphaned processes (to avoid memory leaks of test agent)
cleanup of build files (not filling disk up with temporary files)
repeatability (of course)
Btw. with build files I include the temporary container and images as well. (I just had a bunch of stability tests, which means running tests more than 50 times. I had local memory fuckups twice.)
create a script to run the tests (in and outside of docker)
create a Dockerfile
create a script to run docker with tests
dont run as root, make it secure
bundle some convinient code to live happily with
1. create a script to run the tests
There should be a script wrapping test commands to be used inside and outside of docker. No special docker test commands!
File: run_tests.sh
1
2
3
4
5
6
7
8
#!/bin/bash# just for resolving all dependencies
mvn install-DskipTests# actual running teststimeout--preserve-status--kill-after 7m 6m \
mvn clean verify -P sonar-coverage
Line 7 does timout the test run after specified time. I do have in mind a range of time to timeout the complete test run. To make the download of artifact independent line 4 is kept separate.
2. Creating the Dockerfile
I prefer to have a Dockerfile to build a fresh container for each run. For my example I choose lkwg82/mitmproxy-0.11-maven3-jdk8 (contains maven, jdk8, google-chrome and ...).
FROM lkwg82/mitmproxy-0.11-maven3-jdk8
To run my tests I add the current directory while building the container.
FROM lkwg82/mitmproxy-0.11-maven3-jdk8ADD . /home/buildWORKDIR /home/build
3. create a script to run docker
3.1 get it run
File: run_docker.sh
1
2
3
4
5
6
7
#!/bin/bash
docker build -t test-$$. | tee docker_build.log
IMAGE_ID=$(tail-n1 docker_build.log| cut-d\ -f3)
docker run $IMAGE_ID ./run_tests.sh
line 3: build the container and log the output while displaying
line 3: use a tagname which contains the PID to identify when running in parallel
line 4: retrieve the image id
line 5: run the tests in a new container with the retrieved image id
ii acl 2.2.52-2 amd64 Access control list utilities
ii adduser 3.113+nmu3 all add and remove users and groups
ii apt 1.0.9.8.1 amd64 commandline package manager
ii base-files 8+deb8u2 amd64 Debian base system miscellaneous files
ii base-passwd 3.5.37 amd64 Debian base system master password and group files
ii bash 4.3-11+b1 amd64 GNU Bourne Again SHell
...
Dont forget to copy the list from the container.
File: run_docker_cleanup.sh