Skip to content

A JUnit Rule that will create, start, stop and remove a container during a JUnit execution.

License

Notifications You must be signed in to change notification settings

klousiaj/docker-junit-rule

Repository files navigation

docker-junit-rule Download Build Status codecov.io

A junit rule to run docker containers. This repository is based on, and extended from the excellent library written by Geoffroy Warin. You can find the original code here.

Versions

Check out the ChangeLog

Installation

This is hosted on bintray.

Maven

Add the following to your pom.xml:

<repositories>
    <repository>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
        <id>central</id>
        <name>bintray</name>
        <url>http://jcenter.bintray.com</url>
    </repository>
</repositories>

...

<dependency>
    <groupId>com.github.klousiaj</groupId>
    <artifactId>docker-junit-rule</artifactId>
    <version>1.3.5</version>
    <scope>test</scope>
</dependency>

Gradle

Add the following to your build.gradle:

repositories {
  jcenter()
}

dependencies {
  testCompile 'com.github.klousiaj:docker-junit-rule:1.3.5'
}

Usage

Example for rabbitMQ:

import DockerRule;
import com.rabbitmq.client.ConnectionFactory;
import org.junit.ClassRule;
import org.junit.Test;

public class RabbitIntegrationTest {

  @ClassRule
  public static DockerRule rabbitRule =
    DockerRule.builder()
      .image("rabbitmq:management")
      .ports("5672", ":32779", "32880:5671")
      .envs("RABBITMQ_DEFAULT_PASS=password1234")
//    .containerName("specific-name")    
//    .leaveRunning(false)
//    .useRunning(false)
//    .labels("com.github.klousiaj.example:a label example")
//    .cleanVolumes(false)
      .waitForLog("Server startup complete")
      .build();

  @Test
  public void testConnectsToDocker() throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(rabbitRule.getDockerHost());
    factory.setPort(rabbitRule.getHostPort("5672/tcp"));
    factory.newConnection();
  }
}

Principle

This plugin relies on https://github.com/spotify/docker-client to connect to the docker daemon API.

You can see the latest and greatest build status by checking on the build at travis-ci. Travis CI runs the tests against multiple versions of Docker:

  • 1.11.2
  • 1.12.3

It has also been validated using docker-toolbox. You should probably set the DOCKER_HOST and DOCKER_CERT_PATH on your machine. If they are not set and your are not on UNIX, the client will try to connect to https://192.168.99.100:2376, which is the address of my default docker machine. It works great for me but your mileage may vary.

About

A JUnit Rule that will create, start, stop and remove a container during a JUnit execution.

Resources

License

Stars

Watchers

Forks

Packages

No packages published