Skip to content

Commit

Permalink
add docker example
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrthomas committed Jun 19, 2023
1 parent 1337a05 commit 35fef22
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ This repository also contains examples of how to use Karate and demonstrates int
* [Broadcom / CA](https://techdocs.broadcom.com/us/en/ca-enterprise-software/devops/continuous-delivery-director-integrations/1-0/integrations-overview/plug-ins/karate-API-plug-in.html) - Broadcom Continuous Delivery Director has a plug-in for Karate integration
* [CLI / Bash](cli/README.md) - How to use Karate to test the command-line or use shell scripts
* [Database](database/README.md) - Include SQL / database calls into functional (and even performance) test suites
* [Docker](docker/README.md) - Easily inject Karate into CI / CD pipelines using Docker
* [GitHub Codespaces](https://github.com/karatelabs/karate/wiki/Get-Started:-GitHub-Codespaces) - open a GitHub project directly in a browser and run and edit Karate tests
* [gRPC](grpc/README.md) - How to test gRPC services with Karate
* [Gatling](https://github.com/karatelabs/karate/tree/master/karate-gatling) - Karate can re-use API functional tests as performance tests !
Expand Down
7 changes: 7 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM adoptopenjdk:11-jre-hotspot

RUN apt-get update \
&& apt-get install -y wget \
&& rm -rf /var/lib/apt/lists/*

RUN wget -O karate.jar https://github.com/karatelabs/karate/releases/download/v1.4.0/karate-1.4.0.jar
40 changes: 40 additions & 0 deletions docker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Karate and Docker - Standalone JAR

This simple minimalistic [`Dockerfile`](Dockerfile) is sufficient to package a Java Runtime Environment and the Karate [Standalone JAR](#standalone-jar).

Here we are using an [`adoptopenjdk`](https://hub.docker.com/_/adoptopenjdk) Docker image as a base.

```docker
FROM adoptopenjdk:11-jre-hotspot
RUN apt-get update \
&& apt-get install -y wget \
&& rm -rf /var/lib/apt/lists/*
RUN wget -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.

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

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

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](https://karatelabs.github.io/karate/karate-netty/#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.

## Further Reading

* [Get Started - Other Runtime Options](https://github.com/karatelabs/karate/wiki/Get-Started:-Other-Runtime-Options#docker)
* There are some tips, tricks and recipes in this thread: [https://github.com/karatelabs/karate/issues/396](https://github.com/karatelabs/karate/issues/396)
17 changes: 17 additions & 0 deletions docker/src/test.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Feature: sample karate api test script

Background:
* url 'https://jsonplaceholder.typicode.com'

Scenario: get all users and then get the first user by id
* path 'users'
* method get
* status 200
* match response[0] contains { id: 1, name: 'Leanne Graham', website: 'hildegard.org' }

* def first = response[0]

* path 'users', first.id
* method get
* status 200
* match response == first

0 comments on commit 35fef22

Please sign in to comment.