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

..03-May-2022-

.github/H11-Aug-2019-142101

docker/H11-Aug-2019-20,80914,261

docs/H11-Aug-2019-

examples/H11-Aug-2019-275233

v3/H18-Oct-2020-22,07915,356

.gitignoreH A D11-Aug-201959 87

.travis.ymlH A D11-Aug-2019499 2215

CONTRIBUTING.mdH A D11-Aug-20195.2 KiB9465

Gopkg.lockH A D11-Aug-20193.7 KiB161137

Gopkg.tomlH A D11-Aug-20191.8 KiB9576

LICENSEH A D11-Aug-201911.1 KiB203169

README.mdH A D11-Aug-20193.9 KiB11987

SECURITY.mdH A D11-Aug-2019727 2115

dockertest.goH A D11-Aug-201910.4 KiB389301

dockertest_test.goH A D11-Aug-20194.6 KiB207173

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- [Troubleshoot & FAQ](#troubleshoot-&-faq)
17  - [Out of disk space](#out-of-disk-space)
18  - [Removing old containers](#removing-old-containers)
19
20<!-- END doctoc generated TOC please keep comment here to allow auto update -->
21
22## Why should I use Dockertest?
23
24When developing applications, it is often necessary to use services that talk to a database system.
25Unit Testing these services can be cumbersome because mocking database/DBAL is strenuous. Making slight changes to the
26schema implies rewriting at least some, if not all of the mocks. The same goes for API changes in the DBAL.
27To avoid this, it is smarter to test these specific services against a real database that is destroyed after testing.
28Docker is the perfect system for running unit tests as you can spin up containers in a few seconds and kill them when
29the test completes. The Dockertest library provides easy to use commands for spinning up Docker containers and using
30them for your tests.
31
32## Installing and using Dockertest
33
34Using Dockertest is straightforward and simple. Check the [releases tab](https://github.com/ory/dockertest/releases)
35for available releases.
36
37To install dockertest, run
38
39```
40dep ensure -add github.com/ory/dockertest@v3.x.y
41```
42
43### Using Dockertest
44
45```go
46package dockertest_test
47
48import (
49	"database/sql"
50	"fmt"
51	"log"
52	"os"
53	"testing"
54
55	_ "github.com/go-sql-driver/mysql"
56	"github.com/ory/dockertest"
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## Troubleshoot & FAQ
106
107### Out of disk space
108
109Try cleaning up the images with [docker-cleanup-volumes](https://github.com/chadoe/docker-cleanup-volumes).
110
111### Removing old containers
112
113Sometimes container clean up fails. Check out
114[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:
115
116```go
117resource.Expire(60) // Tell docker to hard kill the container in 60 seconds
118```
119