• Stars
    star
    422
  • Rank 98,913 (Top 3 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created over 8 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A JUnit rule to manage docker containers using docker-compose

Autorelease

build status Download

Docker Compose JUnit Rule

This is a library for executing JUnit tests that interact with Docker Compose managed containers. It supports the following:

  • Starting containers defined in a docker-compose.yml before tests and tearing them down afterwards
  • Waiting for services to become available before running tests
  • Recording log files from containers to aid debugging test failures

Why should I use this?

The code here started out as the end to end tests for one of our products. We needed to test this product in a variety of different configurations and environments which were mutually incompatible, thus multiple Docker Compose files were needed and so a simplistic model of running docker-compose up in Gradle was insufficient.

If you're experiencing any of the following using Docker for your testing this library should hopefully help:

  • Orchestrating multiple services and mapping the ports to outside the Docker machine so assertions can be made in tests
  • Needing to know when services are up to prevent flickering tests caused by slow to start services or complicated service dependencies
  • Lack of insight into what has happened in Docker containers during tests on CI servers due to loss of logs
  • Tests failing due to needing open ports on the CI build host which conflict with the test configuration

Simple Use

Add a dependency to your project. For example, in gradle:

repositories {
    maven {
        url 'https://dl.bintray.com/palantir/releases' // docker-compose-rule is published on bintray
    }
}
dependencies {
    testCompile 'com.palantir.docker.compose:docker-compose-rule-junit4:<latest-tag-from-bintray>'
}

For the most basic use simply add a DockerComposeRule object as a @ClassRule or @Rule in a JUnit test class.

public class MyIntegrationTest {

    @ClassRule
    public static DockerComposeRule docker = DockerComposeRule.builder()
            .file("src/test/resources/docker-compose.yml")
            .build();

    @Test
    public void testThatUsesSomeDockerServices() throws InterruptedException, IOException {
       ...
    }

}

This will cause the containers defined in src/test/resources/docker-compose.yml to be started by Docker Compose before the test executes and then the containers will be killed and removed (along with associated volumes) once the test has finished executing. If the containers have healthchecks specified, either in the docker image or in the docker-compose config, the test will wait for them to become healthy.

The docker-compose.yml file is referenced using the path given, relative to the working directory of the test. It will not be copied elsewhere and so references to shared directories and other resources for your containers can be made using path relative to this file as normal. If you wish to manually run the Docker containers for debugging the tests simply run docker-compose up in the same directory as the docker-compose.yml.

JUnit 5

If you'd prefer to use JUnit 5 (aka JUnit Jupiter), use the following dependency and replace your usages of DockerComposeRule with DockerComposeExtension.

dependencies {
    testCompile 'com.palantir.docker.compose:docker-compose-junit-jupiter:<latest-tag-from-bintray>'
}
public class MyIntegrationTest {

    @RegisterExtension
    public static DockerComposeExtension docker = DockerComposeExtension.builder()
            .file("src/test/resources/docker-compose.yml")
            .build();

    @Test
    public void testThatUsesSomeDockerServices() throws InterruptedException, IOException {
       ...
    }

}

Running on a Mac

The above example will work out of the box on Linux machines with Docker installed. On Mac you will first need to install Docker using the instructions here.

Once Docker is installed to run from the command line you will need to execute docker-machine env <machine_name> and follow the instructions to set the environment variables. Any tests can now be executed through Gradle in the usual way.

To run the tests from your IDE you will need to add the environment variables given from running docker-machine env <machine_name> to the run configuration for the test in your IDE. This is documented for Eclipse and IntelliJ.

Waiting for a service to be available

To wait for services to be available before executing tests, either add health checks to the configuration, or use the following methods on the DockerComposeRule object:

public class MyEndToEndTest {

    @ClassRule
    public static DockerComposeRule docker = DockerComposeRule.builder()
        .file("src/test/resources/docker-compose.yml")
        .waitingForService("db", HealthChecks.toHaveAllPortsOpen())
        .waitingForService("web", HealthChecks.toRespondOverHttp(8080, (port) -> port.inFormat("https://$HOST:$EXTERNAL_PORT")))
        .waitingForService("other", container -> customServiceCheck(container), Duration.standardMinutes(2))
        .waitingForServices(ImmutableList.of("node1", "node2"), toBeHealthyAsACluster())
        .waitingForHostNetworkedPort(5432, toBeOpen())
        .build();

    @Test
    public void testThatDependsServicesHavingStarted() throws InterruptedException, IOException {
        ...
    }
}

The entrypoint method waitingForService(String container, HealthCheck<Container> check[, Duration timeout]) will make sure the healthcheck passes for that container before the tests start. The entrypoint method waitingForServices(List<String> containers, HealthCheck<List<Container>> check[, Duration timeout]) will make sure the healthcheck passes for the cluster of containers before the tests start. The entrypoint method waitingForHostNetworkedPort(int portNumber, HealthCheck<DockerPort> check[, Duration timeout]) will make sure the healthcheck passes for a particular host networked port.

We provide 2 default healthChecks in the HealthChecks class:

  1. toHaveAllPortsOpen - this waits till all ports can be connected to that are exposed on the container
  2. toRespondOverHttp - which waits till the specified URL responds to a HTTP request.

Accessing services in containers from outside a container

In tests it is likely services inside containers will need to be accessed in order to assert that they are behaving correctly. In addition, when tests run on Mac the Docker containers will be inside a Virtual Box machine and so must be accessed on an external IP address rather than the loopback interface.

It is recommended to only specify internal ports in the docker-compose.yml as described in the (reference)[https://docs.docker.com/compose/compose-file/#ports]. This makes tests independent of the environment on the host machine and of each other. Docker will then randomly allocate an external port. For example:

postgres:
  image: postgres:9.5
  ports:
    - 5432

Given a DockerComposeRule instance called docker, you could then access a service called postgres as follows

DockerPort postgres = docker.containers()
        .container("postgres")
        .port(5432);

You could then interpolate the host IP address and random external port as follows:

String url = postgres.inFormat("jdbc:postgresql://$HOST:$EXTERNAL_PORT/mydb");
// e.g. "jdbc:postgresql://192.168.99.100:33045/mydb"

Run docker-compose exec

We support docker-compose exec command which runs a new command in a running container.

dockerCompose.exec(dockerComposeExecOption, containerName, dockerComposeExecArgument)

Just be aware that you need at least docker-compose 1.7 to run docker-compose exec

Collecting logs

To record the logs from your containers specify a location:

public class DockerComposeRuleTest {

    @ClassRule
    public static DockerComposeRule docker = DockerComposeRule.builder()
            .file("src/test/resources/docker-compose.yml")
            .saveLogsTo("build/dockerLogs/dockerComposeRuleTest")
            .build();

    @Test
    public void testRecordsLogs() throws InterruptedException, IOException {
       ...
    }

}

This will collect logs for all containers. Collection will occur when after the tests are finished executing.

The LogDirectory class contains utility methods to generate these paths. For example, you can write logs directly into the $CIRCLE_ARTIFACTS directory on CI (but fall back to build/dockerLogs locally) using:

    .saveLogsTo(circleAwareLogDirectory(MyTest.class))

Methods in LogDirectory are intended to be statically imported for readability.

Skipping shutdown

To skip shutdown of containers after tests are finished executing:

public class DockerComposeRuleTest {
    @ClassRule
    public static DockerComposeRule docker = DockerComposeRule.builder()
            .file("src/test/resources/docker-compose.yml")
            .skipShutdown(true)
            .build();
}

This can shorten iteration time when services take a long time to start. Remember to never leave it on in CI!

Pull images on startup

To pull images before starting the containers:

public class DockerCompositionTest {
    @ClassRule
    public static DockerComposition composition = DockerComposition.of("src/test/resources/docker-compose.yml")
                                                .pullOnStartup(true)
                                                .build();
}

This will make sure you are using the most up-to-date version of all the images included in the docker-compose.yml.

Docker Machine

Docker is able to connect to daemons that either live on the machine where the client is running, or somewhere remote. Using the docker client, you are able to control which daemon to connect to using the DOCKER_HOST environment variable.

Local Machine

The default out-of-the-box behaviour will configure docker-compose to connect to a Docker daemon that is running locally. That is, if you're on Linux, it will use the Docker daemon that exposes its socket. In the case of Mac OS X - which doesn't support Docker natively - we have to connect to a technically "remote" (but local) Docker daemon which is running in a virtual machine via docker-machine.

If you're on Mac OS X, the docker cli expects the following environment variables:

  • DOCKER_HOST
  • If the Docker daemon is secured by TLS, DOCKER_TLS_VERIFY and DOCKER_CERT_PATH need to be set.

Similarly, if you're using a LocalMachine, you need to ensure the Run Configuration (in your IDE, command line etc.) has those same variables set.

An example of creating a DockerMachine that connects to a local docker daemon:

DockerMachine.localMachine()
             .build()

Remote Machine

You may not always want to connect to a Docker daemon that is running on your local computer or a virtual machine running on your local computer.

An example of this would be running containers in a clustered manner with Docker Swarm. Since Docker Swarm implements the Docker API, setting the right environment variables would allow us to use Docker containers on the swarm.

An example of connecting to a remote Docker daemon that has also been secured by TLS:

DockerMachine.remoteMachine()
             .host("tcp://remote-docker-host:2376")
             .withTLS("/path/to/cert")
             .build()

Additional Environment Variables

It may also be useful to pass environment variables to the process that will call docker-compose.

You can do so in the following manner:

DockerMachine.localMachine()
             .withEnvironmentVariable("SOME_VARIABLE", "SOME_VALUE")
             .build()

The variable SOME_VARIABLE will be available in the process that calls docker-compose, and can be used for Variable Interpolation inside the compose file.

How to use a DockerMachine

When creating a DockerComposeRule, a custom DockerMachine may be specified. If no DockerMachine is specified, DockerComposeRule will connect to the local Docker daemon, similarly to how the docker cli works.

private final DockerMachine dockerMachine = DockerMachine.localMachine()
                                                         .withAdditionalEnvironmentVariable("SOME_VARIABLE", "SOME_VALUE")
                                                         .build();

@Rule
DockerComposeRule docker = DockerComposeRule.builder()
            .file("docker-compose.yaml")
            .machine(dockerMachine)
            .build();

Composing docker compose files

docker-compose (at least as of version 1.5.0) allows us to specify multiple docker-compose files. On the command line, you can do this with this example command:

docker-compose -f file1.yml -f file2.yml -f file3.yml

Semantics of how this works is explained here: Docker compose reference

To use this functionality inside docker-compose-rule, supply a DockerComposeFiles object to your DockerComposeRule builder:

DockerComposeRule docker = DockerComposeRule.builder()
            .files(DockerComposeFiles.from("file1.yml", "file2.yml"))
            .build()

Using a custom version of docker-compose

docker-compose-rule tries to use the docker-compose binary located at /usr/local/bin/docker-compose. This can be overriden by setting DOCKER_COMPOSE_LOCATION to be the path to a valid file.

More Repositories

1

blueprint

A React-based UI toolkit for the web
TypeScript
19,885
star
2

tslint

๐Ÿšฆ An extensible linter for the TypeScript language
TypeScript
5,916
star
3

plottable

๐Ÿ“Š A library of modular chart components built on D3
TypeScript
2,926
star
4

python-language-server

An implementation of the Language Server Protocol for Python
Python
2,579
star
5

windows-event-forwarding

A repository for using windows event forwarding for incident detection and response
Roff
1,186
star
6

pyspark-style-guide

This is a guide to PySpark code style presenting common situations and the associated best practices based on the most frequent recurring topics across the PySpark repos we've encountered.
Python
945
star
7

osquery-configuration

A repository for using osquery for incident detection and response
800
star
8

tslint-react

๐Ÿ“™ Lint rules related to React & JSX for TSLint.
TypeScript
752
star
9

bulldozer

GitHub Pull Request Auto-Merge Bot
Go
725
star
10

gradle-docker

a Gradle plugin for orchestrating docker builds and pushes.
Groovy
723
star
11

policy-bot

A GitHub App that enforces approval policies on pull requests
Go
700
star
12

alerting-detection-strategy-framework

A framework for developing alerting and detection strategies for incident response.
610
star
13

stacktrace

Stack traces for Go errors
Go
498
star
14

conjure

Strongly typed HTTP/JSON APIs for browsers and microservices
Java
393
star
15

palantir-java-format

A modern, lambda-friendly, 120 character Java formatter.
Java
373
star
16

eclipse-typescript

An Eclipse plug-in for developing in the TypeScript language.
JavaScript
341
star
17

gradle-git-version

a Gradle plugin that uses `git describe` to produce a version string.
Java
339
star
18

go-githubapp

A simple Go framework for building GitHub Apps
Go
317
star
19

godel

Go tool for formatting, checking, building, distributing and publishing projects
Go
304
star
20

gradle-baseline

A set of Gradle plugins that configure default code quality tools for developers.
Java
283
star
21

jamf-pro-scripts

A collection of scripts and extension attributes created for managing Mac workstations via Jamf Pro.
Shell
277
star
22

gradle-graal

A plugin for Gradle that adds tasks to download, extract and interact with GraalVM tooling.
Java
225
star
23

log4j-sniffer

A tool that scans archives to check for vulnerable log4j versions
Go
193
star
24

tfjson

Terraform plan file to JSON
Go
182
star
25

k8s-spark-scheduler

A Kubernetes Scheduler Extender to provide gang scheduling support for Spark on Kubernetes
Go
175
star
26

Sysmon

A lightweight platform monitoring tool for Java VMs
Java
155
star
27

typesettable

๐Ÿ“ A typesetting library for SVG and Canvas
TypeScript
146
star
28

documentalist

๐Ÿ“ A sort-of-static site generator optimized for living documentation of software projects
TypeScript
145
star
29

exploitguard

Documentation and supporting script sample for Windows Exploit Guard
PowerShell
144
star
30

bouncer

An application to cycle (bounce) all nodes in a coordinated fashion in an AWS ASG or set of related ASGs
Go
130
star
31

gradle-consistent-versions

Compact, constraint-friendly lockfiles for your dependencies
Java
111
star
32

Cinch

A Java library that manages component action/event bindings for MVC patterns
Java
110
star
33

redoodle

An addon library for Redux that enhances its integration with TypeScript.
TypeScript
99
star
34

gradle-jacoco-coverage

Groovy
99
star
35

sqlite3worker

A threadsafe sqlite worker for Python
Python
94
star
36

phishcatch

A browser extension and API server for detecting corporate password use on external websites
CSS
83
star
37

python-jsonrpc-server

A Python 2 and 3 asynchronous JSON RPC server
Python
80
star
38

conjure-java-runtime

Opinionated libraries for HTTP&JSON-based RPC using Retrofit, Feign, OkHttp as clients and Jetty/Jersey as servers
Java
78
star
39

stashbot

A plugin for Atlassian Stash to allow easy, self-service continuous integration with Jenkins
Java
67
star
40

go-baseapp

A lightweight starting point for Go web servers
Go
67
star
41

stash-codesearch-plugin

Provides global repository, commit, and file content search for Atlassian Stash instances
Java
62
star
42

gradle-processors

Gradle plugin for integrating Java annotation processors
Groovy
62
star
43

go-java-launcher

A simple Go program for launching Java programs from a fixed configuration. This program replaces Gradle-generated Bash launch scripts which are susceptible to attacks via injection of environment variables of the form JAVA_OPTS='$(rm -rf /)'.
Go
59
star
44

pkg

A collection of stand-alone Go packages
Go
53
star
45

rust-zipkin

A library for logging and propagating Zipkin trace information in Rust
Rust
53
star
46

grunt-tslint

A Grunt plugin for tslint.
JavaScript
51
star
47

witchcraft-go-server

A highly opinionated Go embedded application server for RESTy APIs
Go
50
star
48

spark-influx-sink

A Spark metrics sink that pushes to InfluxDb
Scala
50
star
49

giraffe

Gracefully Integrated Remote Access For Files and Execution
Java
49
star
50

language-servers

[Deprecated and No longer supported] A collection of implementations for the Microsoft Language Server Protocol
Java
46
star
51

go-license

Go tool that applies and verifies that proper license headers are applied to Go files
Go
44
star
52

hadoop-crypto

Library for per-file client-side encyption in Hadoop FileSystems such as HDFS or S3.
Java
41
star
53

tritium

Tritium is a library for instrumenting applications to provide better observability at runtime
Java
39
star
54

sls-packaging

A set of Gradle plugins for creating SLS-compatible packages
Shell
38
star
55

roboslack

A pluggable, fluent, straightforward Java library for interacting with Slack.
Java
37
star
56

dropwizard-web-security

A Dropwizard bundle for applying default web security functionality
Java
37
star
57

goastwriter

Go library for writing Go source code programatically
Go
31
star
58

gradle-gitsemver

Java
31
star
59

palantir-python-sdk

Palantir Python SDK
Python
30
star
60

gradle-revapi

Gradle plugin that uses Revapi to check whether you have introduced API/ABI breaks in your Java public API
Java
29
star
61

checks

Go libraries and programs for performing static checks on Go projects
Go
29
star
62

gradle-circle-style

๐Ÿš€๐Ÿš€๐Ÿš€MOVED TO Baseline
Java
28
star
63

trove

Patched version of the Trove 3 library - changes the Collections semantics to match proper java.util.Map semantics
Java
27
star
64

dialogue

A client-side RPC library for conjure-java
Java
27
star
65

atlasdb

Transactional Distributed Database Layer
Java
27
star
66

stylelint-config-palantir

Palantir's stylelint config
JavaScript
25
star
67

typedjsonrpc

A typed decorator-based JSON-RPC library for Python
Python
24
star
68

encrypted-config-value

Tooling for encrypting certain configuration parameter values in dropwizard apps
Java
22
star
69

typescript-service-generator

Java
21
star
70

streams

Utilities for working with Java 8 streams
Java
21
star
71

distgo

Go tool for building, distributing and publishing Go projects
Go
21
star
72

gradle-npm-run-plugin

Groovy
20
star
73

conjure-python

Conjure generator for Python clients
Java
19
star
74

conjure-java

Conjure generator for Java clients and servers
Java
19
star
75

amalgomate

Go tool for combining multiple different main packages into a single program or library
Go
19
star
76

serde-encrypted-value

A crate which wraps Serde deserializers and decrypts values
Rust
19
star
77

gradle-docker-test-runner

Gradle plugin for running tests in Docker environments
Groovy
19
star
78

gerrit-ci

Plugin for Gerrit enabling self-service continuous integration workflows with Jenkins.
Java
18
star
79

conjure-rust

Conjure support for Rust
Rust
18
star
80

conjure-typescript

Conjure generator for TypeScript clients
TypeScript
17
star
81

gpg-tap-notifier-macos

Show a macOS notification when GPG is waiting for you to tap/touch a security device (e.g. YubiKey).
Swift
17
star
82

tracing-java

Java library providing zipkin-like tracing functionality
Java
16
star
83

plottable-moment

Plottable date/time formatting library built on Moment.js
JavaScript
16
star
84

spark-tpcds-benchmark

Utility for benchmarking changes in Spark using TPC-DS workloads
Java
16
star
85

assertj-automation

Automatic code rewriting for AssertJ using error-prone and refaster
Java
15
star
86

metric-schema

Schema for standard metric definitions
Java
14
star
87

safe-logging

Interfaces and utilities for safe log messages
Java
14
star
88

resource-identifier

Common resource identifier specification for inter-application object sharing
Java
14
star
89

dropwizard-web-logger

WebLoggerBundle is a Dropwizard bundle used to help log web activity to log files on a serverโ€™s backend
Java
14
star
90

gradle-shadow-jar

Gradle plugin to precisely shadow either a dependency or its transitives
Groovy
14
star
91

gradle-miniconda-plugin

Plugin that sets up a Python environment for building and running tests using Miniconda.
Java
13
star
92

conjure-go-runtime

Go implementation of the Conjure runtime
Go
12
star
93

gulp-count

Counts files in vinyl streams.
CoffeeScript
12
star
94

gradle-configuration-resolver-plugin

Groovy
12
star
95

asana_mailer

A script that uses Asana's RESTful API to generate plaintext and HTML emails.
Python
12
star
96

human-readable-types

A collection of human-readable types
Java
11
star
97

dropwizard-index-page

A Dropwizard bundle that serves the index page for a single page application
Java
11
star
98

eclipse-less

An Eclipse plug-in for compiling LESS files.
Java
11
star
99

go-compiles

Go check that checks that Go source and tests compiles
Go
11
star
100

go-generate

Go tool that runs and verifies the output of go generate
Go
11
star