forked from ory/dockertest
/
docker.go
402 lines (355 loc) · 12.2 KB
/
docker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
Copyright 2014 The Camlistore Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Package dockertest contains helper functions for setting up and tearing down docker containers to aid in testing.
*/
package dockertest
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"log"
"os/exec"
"strings"
"time"
"database/sql"
"math/rand"
"regexp"
"camlistore.org/pkg/netutil"
"github.com/ory-am/common/env"
"github.com/pborman/uuid"
"gopkg.in/mgo.v2"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
)
// Debug, if set, prevents any container from being removed.
var Debug bool
// DockerMachineAvailable, if true, uses docker-machine to run docker commands (for running tests on Windows and Mac OS)
var DockerMachineAvailable bool
// DockerMachineName is the machine's name. You might want to use a dedicated machine for running your tests.
var DockerMachineName string = "default"
var bindDockerToLocalhost = env.Getenv("DOCKER_BIND_LOCALHOST", "")
/// runLongTest checks all the conditions for running a docker container
// based on image.
func runLongTest(image string) error {
DockerMachineAvailable = false
if haveDockerMachine() {
DockerMachineAvailable = true
if !startDockerMachine() {
log.Printf(`Starting docker machine "%s" failed. This could be because the image is already running or because the image does not exist. Tests will fail if the image does not exist.`, DockerMachineName)
}
} else if !haveDocker() {
return errors.New("Neither 'docker' nor 'docker-machine' available on this system.")
}
if ok, err := haveImage(image); !ok || err != nil {
if err != nil {
return fmt.Errorf("Error checking for docker image %s: %v", image, err)
}
log.Printf("Pulling docker image %s ...", image)
if err := Pull(image); err != nil {
return fmt.Errorf("Error pulling %s: %v", image, err)
}
}
return nil
}
func runDockerCommand(command string, args ...string) *exec.Cmd {
if DockerMachineAvailable {
command = "/usr/local/bin/" + strings.Join(append([]string{command}, args...), " ")
cmd := exec.Command("docker-machine", "ssh", DockerMachineName, command)
return cmd
}
return exec.Command(command, args...)
}
// haveDockerMachine returns whether the "docker" command was found.
func haveDockerMachine() bool {
_, err := exec.LookPath("docker-machine")
return err == nil
}
// startDockerMachine starts the docker machine and returns false if the command failed to execute
func startDockerMachine() bool {
_, err := exec.Command("docker-machine", "start", DockerMachineName).Output()
return err == nil
}
// haveDocker returns whether the "docker" command was found.
func haveDocker() bool {
_, err := exec.LookPath("docker")
return err == nil
}
func haveImage(name string) (ok bool, err error) {
out, err := runDockerCommand("docker", "images", "--no-trunc").Output()
if err != nil {
return false, err
}
return bytes.Contains(out, []byte(name)), nil
}
func run(args ...string) (containerID string, err error) {
var stdout, stderr bytes.Buffer
validID := regexp.MustCompile(`^([a-zA-Z0-9]+)$`)
cmd := runDockerCommand("docker", append([]string{"run"}, args...)...)
cmd.Stdout, cmd.Stderr = &stdout, &stderr
if err = cmd.Run(); err != nil {
err = fmt.Errorf("Error running docker\nStdOut: %s\nStdErr: %s\nError: %v\n\n", stdout.String(), stderr.String(), err)
return
}
containerID = strings.TrimSpace(string(stdout.String()))
if !validID.MatchString(containerID) {
return "", fmt.Errorf("Error running docker: %s", containerID)
}
if containerID == "" {
return "", errors.New("Unexpected empty output from `docker run`")
}
return containerID, nil
}
func KillContainer(container string) error {
if container != "" {
return runDockerCommand("docker", "kill", container).Run()
}
return nil
}
// Pull retrieves the docker image with 'docker pull'.
func Pull(image string) error {
out, err := runDockerCommand("docker", "pull", image).CombinedOutput()
if err != nil {
err = fmt.Errorf("%v: %s", err, out)
}
return err
}
// IP returns the IP address of the container.
func IP(containerID string) (string, error) {
out, err := runDockerCommand("docker", "inspect", containerID).Output()
if err != nil {
return "", err
}
type networkSettings struct {
IPAddress string
}
type container struct {
NetworkSettings networkSettings
}
var c []container
if err := json.NewDecoder(bytes.NewReader(out)).Decode(&c); err != nil {
return "", err
}
if len(c) == 0 {
return "", errors.New("no output from docker inspect")
}
if ip := c[0].NetworkSettings.IPAddress; ip != "" {
return ip, nil
}
return "", errors.New("could not find an IP. Not running?")
}
type ContainerID string
func (c ContainerID) IP() (string, error) {
return IP(string(c))
}
func (c ContainerID) Kill() error {
return KillContainer(string(c))
}
// Remove runs "docker rm" on the container
func (c ContainerID) Remove() error {
if Debug || c == "nil" {
return nil
}
return runDockerCommand("docker", "rm", "-v", string(c)).Run()
}
// KillRemove calls Kill on the container, and then Remove if there was
// no error.
func (c ContainerID) KillRemove() error {
if err := c.Kill(); err != nil {
return err
}
return c.Remove()
}
// lookup retrieves the ip address of the container, and tries to reach
// before timeout the tcp address at this ip and given port.
func (c ContainerID) lookup(port int, timeout time.Duration) (ip string, err error) {
if DockerMachineAvailable {
var out []byte
out, err = exec.Command("docker-machine", "ip", DockerMachineName).Output()
ip = strings.TrimSpace(string(out))
} else if bindDockerToLocalhost != "" {
ip = "127.0.0.1"
} else {
ip, err = c.IP()
}
if err != nil {
err = fmt.Errorf("error getting IP: %v", err)
return
}
addr := fmt.Sprintf("%s:%d", ip, port)
err = netutil.AwaitReachable(addr, timeout)
return
}
// setupContainer sets up a container, using the start function to run the given image.
// It also looks up the IP address of the container, and tests this address with the given
// port and timeout. It returns the container ID and its IP address, or makes the test
// fail on error.
func setupContainer(image string, port int, timeout time.Duration, start func() (string, error)) (c ContainerID, ip string, err error) {
err = runLongTest(image)
if err != nil {
return "", "", err
}
containerID, err := start()
if err != nil {
return "", "", err
}
c = ContainerID(containerID)
ip, err = c.lookup(port, timeout)
if err != nil {
c.KillRemove()
return "", "", err
}
return c, ip, nil
}
const (
mongoImage = "mongo"
mysqlImage = "mysql"
postgresImage = "postgres"
MySQLUsername = "root"
MySQLPassword = "root"
PostgresUsername = "postgres" // set up by the dockerfile of postgresImage
PostgresPassword = "docker" // set up by the dockerfile of postgresImage
)
func randInt(min int, max int) int {
rand.Seed(time.Now().UTC().UnixNano())
return min + rand.Intn(max-min)
}
// 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 SetupMongoContainer() (c ContainerID, ip string, port int, err error) {
port = randInt(1024, 49150)
forward := fmt.Sprintf("%d:%d", port, 27017)
if bindDockerToLocalhost != "" {
forward = "127.0.0.1:" + forward
}
c, ip, err = setupContainer(mongoImage, port, 10*time.Second, func() (string, error) {
res, err := run("--name", uuid.New(), "-d", "-P", "-p", forward, mongoImage)
return res, err
})
return
}
// 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.
// Currently using https://index.docker.io/u/orchardup/mysql/
func SetupMySQLContainer() (c ContainerID, ip string, port int, err error) {
port = randInt(1024, 49150)
forward := fmt.Sprintf("%d:%d", port, 3306)
if bindDockerToLocalhost != "" {
forward = "127.0.0.1:" + forward
}
c, ip, err = setupContainer(mysqlImage, port, 10*time.Second, func() (string, error) {
return run("-d", "-p", forward, "-e", fmt.Sprintf("MYSQL_ROOT_PASSWORD=%s", MySQLPassword), mysqlImage)
})
return
}
// 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.
// Currently using https://index.docker.io/u/nornagon/postgres
func SetupPostgreSQLContainer() (c ContainerID, ip string, port int, err error) {
port = randInt(1024, 49150)
forward := fmt.Sprintf("%d:%d", port, 5432)
if bindDockerToLocalhost != "" {
forward = "127.0.0.1:" + forward
}
c, ip, err = setupContainer(postgresImage, port, 15*time.Second, func() (string, error) {
return run("-d", "-p", forward, "-e", fmt.Sprintf("POSTGRES_PASSWORD=%s", PostgresPassword), postgresImage)
})
return
}
type pinger interface {
Ping() error
}
func ping(db pinger, tries int, delay time.Duration) bool {
for i := 0; i <= tries; i++ {
time.Sleep(delay)
if s, ok := db.(*sql.DB); ok {
if _, err := s.Exec("SELECT 1"); err != nil {
continue
}
} else if s, ok := db.(*mgo.Session); ok {
if _, err := s.DatabaseNames(); err != nil {
continue
}
}
if err := db.Ping(); err == nil {
return true
}
log.Printf("Ping try %v failed", i)
}
return false
}
func OpenPostgreSQLContainerConnection(tries int, delay time.Duration) (c ContainerID, db *sql.DB, err error) {
c, ip, port, err := SetupPostgreSQLContainer()
if err != nil {
return c, nil, fmt.Errorf("Could not set up PostgreSQL container: %v", err)
}
for try := 0; try <= tries; try++ {
time.Sleep(delay)
url := fmt.Sprintf("postgres://%s:%s@%s:%d/postgres?sslmode=disable", PostgresUsername, PostgresPassword, ip, port)
log.Printf("Try %d: Connecting %s", try, url)
if db, err = sql.Open("postgres", url); err == nil {
if ping(db, tries, delay) {
log.Printf("Try %d: Successfully connected to %v", try, url)
return c, db, nil
}
log.Printf("Try %d: Could not ping database: %v", try, err)
}
log.Printf("Try %d: Could not set up PostgreSQL container: %v", try, err)
}
return c, nil, errors.New("Could not set up PostgreSQL container.")
}
func OpenMongoDBContainerConnection(tries int, delay time.Duration) (c ContainerID, db *mgo.Session, err error) {
c, ip, port, err := SetupMongoContainer()
if err != nil {
return c, nil, fmt.Errorf("Could not set up MongoDB container: %v", err)
}
for try := 0; try <= tries; try++ {
time.Sleep(delay)
url := fmt.Sprintf("%s:%d", ip, port)
log.Printf("Try %d: Connecting %s", try, url)
if db, err = mgo.Dial(url); err == nil {
if ping(db, tries, delay) {
log.Printf("Try %d: Successfully connected to %v", try, url)
return c, db, nil
}
log.Printf("Try %d: Could not ping database: %v", try, err)
}
log.Printf("Try %d: Could not set up MongoDB container: %v", try, err)
}
return c, nil, errors.New("Could not set up MongoDB container.")
}
func OpenMySQLContainerConnection(tries int, delay time.Duration) (c ContainerID, db *sql.DB, err error) {
c, ip, port, err := SetupMySQLContainer()
if err != nil {
return c, nil, fmt.Errorf("Could not set up MySQL container: %v", err)
}
defer c.KillRemove()
for try := 0; try <= tries; try++ {
time.Sleep(delay)
url := fmt.Sprintf("%s:%s@tcp(%s:%d)/mysql", MySQLUsername, MySQLPassword, ip, port)
log.Printf("Try %d: Connecting %s", try, url)
if db, err = sql.Open("mysql", url); err == nil {
if ping(db, tries, delay) {
log.Printf("Try %d: Successfully connected to %v", try, url)
return c, db, nil
}
log.Printf("Try %d: Could not ping database: %v", try, err)
}
log.Printf("Try %d: Could not set up MySQL container: %v", try, err)
}
return c, nil, errors.New("Could not set up MySQL container.")
}