dockertest

package module
v0.0.0-...-e32f47e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 5, 2016 License: Apache-2.0 Imports: 13 Imported by: 0

README

Dockertest

Build Status Coverage Status

Use Docker to run your Go language integration tests against persistent data storage services like MySQL, Postgres or MongoDB on Microsoft Windows, Mac OSX and Linux! Dockertest uses docker-machine (aka Docker Toolbox) to spin up images on Windows and Mac OSX as well.

A suite for testing with Docker. Based on docker.go from camlistore. This fork detects automatically, if Docker Toolbox is installed. If it is, Docker integration on Windows and Mac OSX can be used without any additional work. To avoid port collisions when using docker-machine, Dockertest chooses a random port to bind the requested image.

Why should I use Dockertest?

When developing applications, it is often necessary to use services that talk to a database system. Unit Testing these services can be cumbersome because mocking database/DBAL is strenuous. Making slight changes to the schema implies rewriting at least some, if not all of the mocks. The same goes for API changes in the DBAL.
To avoid this, it is smarter to test these specific services against a real database that is destroyed after testing. Docker is the perfect system for running unit tests as you can spin up containers in a few seconds and kill them when the test completes. The Dockertest library provides easy to use commands for spinning up Docker containers and using them for your tests.

Using Dockertest

Using Dockertest is straightforward and simple. At present, Dockertest supports MongoDB, Postgres and MySQL containers out of the box. Feel free to extend this list by contributing to this project.

Note: When using the Docker Toolbox (Windows / OSX), make sure that the VM is started by running docker-machine start default.

Start a container
package main

import "github.com/ory-am/dockertest"
import "gopkg.in/mgo.v2"
import "time"

func main() {
	c, err := ConnectToMongoDB(15, time.Millisecond*500, func(url string) bool {
		db, err := mgo.Dial(url)
		if err != nil {
			return false
		}
		defer db.Close()
		return true
	})
	require.Nil(t, err)
	defer c.KillRemove()
}

You can start PostgreSQL and MySQL in a similar fashion with

Write awesome tests

It is a good idea to start up the container only once when running tests.


import (
	"fmt"
	"testing"
   "log"
	"os"

	"database/sql"
	_ "github.com/lib/pq"
	"github.com/ory-am/dockertest"
)

var db *sql.DB

func TestMain(m *testing.M) {
	if c, err := dockertest.ConnectToPostgreSQL(15, time.Second, func(url string) bool {
		var err error
		db, err = sql.Open("postgres", url)
		if err != nil {
			return false
		}
		return db.Ping() == nil
	}); err != nil {
		log.Fatalf("Could not connect to database: %s", err)
	}
	defer c.KillRemove()
	os.Exit(m.Run())
}

func TestFunction(t *testing.T) {
    // db.Exec(...
}
Setting up Travis-CI

You can run the Docker integration on Travis easily:

# Sudo is required for docker
sudo: required

# Enable docker
services:
  - docker

# In Travis, we need to bind to 127.0.0.1 in order to get a working connection. This environment variable
# tells dockertest to do that.
env:
  - DOCKERTEST_BIND_LOCALHOST=true

Thanks to our sponsors: Ory GmbH & Imarum GmbH

Documentation

Overview

Package dockertest contains helper functions for setting up and tearing down docker containers to aid in testing. dockertest supports spinning up MySQL, PostgreSQL and MongoDB out of the box.

Dockertest provides two environment variables

Index

Constants

View Source
const (

	// MySQLUsername must be passed as username when connecting to mysql
	MySQLUsername = "root"

	// MySQLPassword must be passed as password when connecting to mysql
	MySQLPassword = "root"

	// PostgresUsername must be passed as username when connecting to postgres
	PostgresUsername = "postgres"
	// PostgresPassword must be passed as password when connecting to postgres
	PostgresPassword = "docker"
)

Variables

View Source
var (
	// Debug if set, prevents any container from being removed.
	Debug bool

	// DockerMachineAvailable if true, uses docker-machine to run docker commands (for running tests on Windows and Mac OS)
	DockerMachineAvailable bool

	// DockerMachineName is the machine's name. You might want to use a dedicated machine for running your tests.
	// You can set this variable either directly or by defining a DOCKERTEST_IMAGE_NAME env variable.
	DockerMachineName = env.Getenv("DOCKERTEST_IMAGE_NAME", "default")

	// BindDockerToLocalhost if set, forces docker to bind the image to localhost. This for example is required when running tests on travis-ci.
	// You can set this variable either directly or by defining a DOCKERTEST_BIND_LOCALHOST env variable.
	// FIXME DOCKER_BIND_LOCALHOST remove legacy support
	BindDockerToLocalhost = env.Getenv("DOCKERTEST_BIND_LOCALHOST", env.Getenv("DOCKER_BIND_LOCALHOST", ""))
)

Functions

func AwaitReachable

func AwaitReachable(addr string, maxWait time.Duration) error

AwaitReachable tries to make a TCP connection to addr regularly. It returns an error if it's unable to make a connection before maxWait.

func IP

func IP(containerID string) (string, error)

IP returns the IP address of the container.

func KillContainer

func KillContainer(container string) error

KillContainer runs docker kill on a container.

func Pull

func Pull(image string) error

Pull retrieves the docker image with 'docker pull'.

Types

type ContainerID

type ContainerID string

ContainerID represents a container and offers methods like Kill or IP.

func SetupContainer

func SetupContainer(image string, containerPort int, args ...string) (c ContainerID, ip string, port int, err error)

SetupContainer runs docker instance and returns port.

func SetupContainerWithEnv

func SetupContainerWithEnv(image string, containerPort int, env string, args ...string) (c ContainerID, ip string, port int, err error)

SetupContainerWithEnv runs docker instance with env variable and returns port.

func SetupElasticSearchContainer

func SetupElasticSearchContainer() (c ContainerID, ip string, port int, err error)

SetupElasticSearchContainer sets up a real ElasticSearch instance for testing purposes using a Docker container. It returns the container ID and its IP address, or makes the test fail on error.

func SetupFluentdContainer

func SetupFluentdContainer() (c ContainerID, ip string, port int, err error)

SetupFluentdContainer sets up a real natsd instance for testing purposes using Docker container.

func SetupMongoContainer

func SetupMongoContainer(args ...string) (c ContainerID, ip string, port int, err error)

SetupMongoContainer sets up a real MongoDB instance for testing purposes, using a Docker container. It returns the container ID and its IP address, or makes the test fail on error.

func SetupMySQLContainer

func SetupMySQLContainer(args ...string) (c ContainerID, ip string, port int, err error)

SetupMySQLContainer sets up a real MySQL instance for testing purposes, using a Docker container. It returns the container ID and its IP address, or makes the test fail on error.

func SetupNatsContainer

func SetupNatsContainer() (c ContainerID, ip string, port int, err error)

SetupNatsContainer sets up a real natsd instance for testing purposes using Docker container.

func SetupPostgreSQLContainer

func SetupPostgreSQLContainer(args ...string) (c ContainerID, ip string, port int, err error)

SetupPostgreSQLContainer sets up a real PostgreSQL instance for testing purposes, using a Docker container. It returns the container ID and its IP address, or makes the test fail on error.

func SetupRedisContainer

func SetupRedisContainer() (c ContainerID, ip string, port int, err error)

SetupRedisContainer sets up a real Redis instance for testing purposes using a Docker container. It returns the container ID and its IP address, or makes the test fail on error.

func (ContainerID) IP

func (c ContainerID) IP() (string, error)

IP retrieves the container's IP address.

func (ContainerID) Kill

func (c ContainerID) Kill() error

Kill runs "docker kill" on the container.

func (ContainerID) KillRemove

func (c ContainerID) KillRemove() error

KillRemove calls Kill on the container, and then Remove if there was no error.

func (ContainerID) Remove

func (c ContainerID) Remove() error

Remove runs "docker rm" on the container

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL