• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.github/H14-Jan-2019-

docker/H14-Jan-2019-

docs/images/H14-Jan-2019-

examples/H14-Jan-2019-

.gitignoreH A D14-Jan-201959

.travis.ymlH A D14-Jan-2019499

CONTRIBUTING.mdH A D14-Jan-20196.6 KiB

Gopkg.lockH A D14-Jan-20193.7 KiB

Gopkg.tomlH A D14-Jan-20191.8 KiB

LICENSEH A D14-Jan-201911.1 KiB

README.mdH A D14-Jan-20194.1 KiB

dockertest.goH A D14-Jan-20199.8 KiB

dockertest_test.goH A D14-Jan-20194.6 KiB

README.md

1<h1 align="center"><img src="./docs/images/banner_dockertest.png" alt="ORY Dockertest"></h1>
2
3[![Build Status](https://travis-ci.org/ory/dockertest.svg)](https://travis-ci.org/ory/dockertest?branch=master)
4[![Coverage Status](https://coveralls.io/repos/github/ory/dockertest/badge.svg?branch=v3)](https://coveralls.io/github/ory/dockertest?branch=v3)
5
6Use Docker to run your Go language integration tests against third party services on **Microsoft Windows, Mac OSX and Linux**!
7
8<!-- START doctoc generated TOC please keep comment here to allow auto update -->
9<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
10**Table of Contents**
11
12- [Why should I use Dockertest?](#why-should-i-use-dockertest)
13- [Installing and using Dockertest](#installing-and-using-dockertest)
14  - [Using Dockertest](#using-dockertest)
15  - [Examples](#examples)
16  - [Setting up Travis-CI](#setting-up-travis-ci)
17- [Troubleshoot & FAQ](#troubleshoot-&-faq)
18  - [Out of disk space](#out-of-disk-space)
19  - [Removing old containers](#removing-old-containers)
20
21<!-- END doctoc generated TOC please keep comment here to allow auto update -->
22
23## Why should I use Dockertest?
24
25When developing applications, it is often necessary to use services that talk to a database system.
26Unit Testing these services can be cumbersome because mocking database/DBAL is strenuous. Making slight changes to the
27schema implies rewriting at least some, if not all of the mocks. The same goes for API changes in the DBAL.
28To avoid this, it is smarter to test these specific services against a real database that is destroyed after testing.
29Docker is the perfect system for running unit tests as you can spin up containers in a few seconds and kill them when
30the test completes. The Dockertest library provides easy to use commands for spinning up Docker containers and using
31them for your tests.
32
33## Installing and using Dockertest
34
35Using Dockertest is straightforward and simple. Check the [releases tab](https://github.com/ory/dockertest/releases)
36for available releases.
37
38To install dockertest, run
39
40```
41dep ensure -add github.com/ory/dockertest@v3.x.y
42```
43
44### Using Dockertest
45
46```go
47package dockertest_test
48
49import (
50	"testing"
51	"log"
52	"github.com/ory/dockertest"
53	_ "github.com/go-sql-driver/mysql"
54	"database/sql"
55	"fmt"
56	"os"
57)
58
59var db *sql.DB
60
61func TestMain(m *testing.M) {
62	// uses a sensible default on windows (tcp/http) and linux/osx (socket)
63	pool, err := dockertest.NewPool("")
64	if err != nil {
65		log.Fatalf("Could not connect to docker: %s", err)
66	}
67
68	// pulls an image, creates a container based on it and runs it
69	resource, err := pool.Run("mysql", "5.7", []string{"MYSQL_ROOT_PASSWORD=secret"})
70	if err != nil {
71		log.Fatalf("Could not start resource: %s", err)
72	}
73
74	// exponential backoff-retry, because the application in the container might not be ready to accept connections yet
75	if err := pool.Retry(func() error {
76		var err error
77		db, err = sql.Open("mysql", fmt.Sprintf("root:secret@(localhost:%s)/mysql", resource.GetPort("3306/tcp")))
78		if err != nil {
79			return err
80		}
81		return db.Ping()
82	}); err != nil {
83		log.Fatalf("Could not connect to docker: %s", err)
84	}
85
86	code := m.Run()
87
88	// You can't defer this because os.Exit doesn't care for defer
89	if err := pool.Purge(resource); err != nil {
90		log.Fatalf("Could not purge resource: %s", err)
91	}
92
93	os.Exit(code)
94}
95
96func TestSomething(t *testing.T) {
97	// db.Query()
98}
99```
100
101### Examples
102
103We provide code examples for well known services in the [examples](examples/) directory, check them out!
104
105### Setting up Travis-CI
106
107You can run the Docker integration on Travis easily:
108
109```yml
110# Sudo is required for docker
111sudo: required
112
113# Enable docker
114services:
115  - docker
116```
117
118## Troubleshoot & FAQ
119
120### Out of disk space
121
122Try cleaning up the images with [docker-cleanup-volumes](https://github.com/chadoe/docker-cleanup-volumes).
123
124### Removing old containers
125
126Sometimes container clean up fails. Check out
127[this stackoverflow question](http://stackoverflow.com/questions/21398087/how-to-delete-dockers-images) on how to fix this. You may also set an absolute lifetime on containers:
128
129```go
130resource.Expire(60) // Tell docker to hard kill the container in 60 seconds
131```