Skip to content

Latest commit

 

History

History
77 lines (48 loc) · 4.2 KB

README.md

File metadata and controls

77 lines (48 loc) · 4.2 KB

Karate and Docker

Standalone JAR

This simple minimalistic Dockerfile is sufficient to package a Java Runtime Environment and the Karate Standalone JAR.

Here we are using an adoptopenjdk Docker image as a base.

FROM adoptopenjdk:11-jre-hotspot

RUN curl -L -o karate.jar https://github.com/karatelabs/karate/releases/download/v1.4.0/karate-1.4.0.jar

The Docker recipe is very simple, just download karate.jar into the root of the docker image. To build a docker image called karate-jre locally, you can do this:

docker build -t karate-jre .

Now to run a set of Karate tests in a src folder within the current directory (outside the docker image) you can do this:

 docker run -it --rm -v "$(pwd)/src":/src -w /src karate-jre java -jar /karate.jar .

If you are on Windows, refer to this for equivalents of the $(pwd) etc.

The explanation of the above command is as follows:

  • -it runs in interactive mode, and --rm removes the temporary image after use
  • use -v to mount the ./src folder as /src.
  • use -w to make /src the working directory
  • now the command java -jar /karate.jar . will run all feature files in the current folder (which is .)
  • note that test reports will appear in ./src/target

All the possible Karate command-line options are explained here: Usage.

You can easily customize the above recipe, for example you could bundle your tests within the docker image. One nice thing about the above example is that the test reports are created outside the image, so you can view them even after the docker process stops.

Adding JARs to the classpath

This Dockerfile which is part of the karate-todo example shows how you can add JAR files to the classpath (from a Maven build), and run a java command when the container starts. This pattern is useful if you want to "ship" a Docker image that embeds a Karate mock.

You can also ADD feature files to the Docker image and run tests when the container starts. In this case, mounting a /target directory may be needed to see reports.

Using Maven

Using the maven:3-jdk-11 image may be sufficient for most use-cases. For example:

docker run -it --rm -v "$(pwd)":/src -w /src -v "$HOME/.m2":/root/.m2 maven:3-jdk-11 mvn test

Here we use -v "$HOME/.m2":/root/.m2 to mount and re-use your local Maven JAR download "cache" (which saves time), but you can omit it if needed for a true "from scratch" experience.

Using Maven and the Standalone JAR

This super-simple Dockerfile is useful for creating a Docker image that can be used as a "worker node" for distributed testing of a Maven project, where you don't need web-browser automation or Chrome installed.

If using this folder, you can build it like so:

docker build -t karate-mvn -f Dockerfile-mvn .

Now you can start a "worker node" like this:

docker run -it --rm karate-mvn java -jar karate.jar -j http://host.docker.internal:8090

Where the -j option is the URL of the "job manager" node that will distribute tests across worker nodes. An example of a job manager for a distributed performance test can be found here: TodoPerfJobConfig.java - and the main() method will start the server.

Further Reading