forked from ory/dockertest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdockertest.go
More file actions
158 lines (134 loc) · 3.91 KB
/
dockertest.go
File metadata and controls
158 lines (134 loc) · 3.91 KB
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
package dockertest
import (
"fmt"
"os"
"runtime"
"time"
"github.com/cenk/backoff"
dc "github.com/fsouza/go-dockerclient"
"github.com/pkg/errors"
)
// Pool represents a connection to the docker API and is used to create and remove docker images.
type Pool struct {
Client *dc.Client
MaxWait time.Duration
}
// Resource represents a docker container.
type Resource struct {
Container *dc.Container
}
// GetPort returns a resource's published port. You can use it to connect to the service via localhost, e.g. tcp://localhost:1231/
func (r *Resource) GetPort(id string) string {
if r.Container == nil {
return ""
} else if r.Container.NetworkSettings == nil {
return ""
}
m, ok := r.Container.NetworkSettings.Ports[dc.Port(id)]
if !ok {
return ""
} else if len(m) == 0 {
return ""
}
return m[0].HostPort
}
// NewTLSPool creates a new pool given an endpoint and the certificate path. This is required for endpoints that
// require TLS communication.
func NewTLSPool(endpoint, certpath string) (*Pool, error) {
ca := fmt.Sprintf("%s/ca.pem", certpath)
cert := fmt.Sprintf("%s/cert.pem", certpath)
key := fmt.Sprintf("%s/key.pem", certpath)
client, err := dc.NewTLSClient(endpoint, cert, key, ca)
if err != nil {
return nil, errors.Wrap(err, "")
}
return &Pool{
Client: client,
}, nil
}
// NewPool creates a new pool. You can pass an empty string to use the default, which is taken from the environment
// variable DOCKER_URL, or from docker-machine if the environment variable DOCKER_MACHINE_NAME is set,
// or if neither is defined a sensible default for the operating system you are on.
func NewPool(endpoint string) (*Pool, error) {
if endpoint == "" {
if os.Getenv("DOCKER_URL") != "" {
endpoint = os.Getenv("DOCKER_URL")
} else if os.Getenv("DOCKER_MACHINE_NAME") != "" {
client, err := dc.NewClientFromEnv()
if err != nil {
return nil, errors.Wrap(err, "")
}
return &Pool{Client: client}, nil
} else if runtime.GOOS == "windows" {
endpoint = "http://localhost:2375"
} else {
endpoint = "unix:///var/run/docker.sock"
}
}
client, err := dc.NewClient(endpoint)
if err != nil {
return nil, errors.Wrap(err, "")
}
return &Pool{
Client: client,
}, nil
}
// Run starts a docker container.
//
// pool.Run("mysql", "5.3", []string{"FOO=BAR", "BAR=BAZ"})
func (d *Pool) Run(repository, tag string, env []string) (*Resource, error) {
if tag == "" {
tag = "latest"
}
_, err := d.Client.InspectImage(fmt.Sprintf("%s:%s", repository, tag))
if err != nil {
if err := d.Client.PullImage(dc.PullImageOptions{
Repository: repository,
Tag: tag,
}, dc.AuthConfiguration{}); err != nil {
return nil, errors.Wrap(err, "")
}
}
c, err := d.Client.CreateContainer(dc.CreateContainerOptions{
Config: &dc.Config{
Image: fmt.Sprintf("%s:%s", repository, tag),
Env: env,
},
HostConfig: &dc.HostConfig{
PublishAllPorts: true,
},
})
if err != nil {
return nil, errors.Wrap(err, "")
}
if err := d.Client.StartContainer(c.ID, nil); err != nil {
return nil, errors.Wrap(err, "")
}
c, err = d.Client.InspectContainer(c.ID)
if err != nil {
return nil, errors.Wrap(err, "")
}
return &Resource{
Container: c,
}, nil
}
// Purge removes a container and linked volumes from docker.
func (d *Pool) Purge(r *Resource) error {
if err := d.Client.KillContainer(dc.KillContainerOptions{ID: r.Container.ID}); err != nil {
return errors.Wrap(err, "")
}
if err := d.Client.RemoveContainer(dc.RemoveContainerOptions{ID: r.Container.ID, Force: true, RemoveVolumes: true}); err != nil {
return errors.Wrap(err, "")
}
return nil
}
// Retry is an exponential backoff retry helper. You can use it to wait for e.g. mysql to boot up.
func (d *Pool) Retry(op func() error) error {
if d.MaxWait == 0 {
d.MaxWait = time.Minute
}
bo := backoff.NewExponentialBackOff()
bo.MaxInterval = time.Second * 5
bo.MaxElapsedTime = d.MaxWait
return backoff.Retry(op, bo)
}