-
Notifications
You must be signed in to change notification settings - Fork 10
/
manager.go
236 lines (184 loc) · 4.64 KB
/
manager.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
package conex
import (
"fmt"
"log"
"os"
"strings"
"testing"
"time"
"github.com/docker/docker/pkg/stringid"
units "github.com/docker/go-units"
docker "github.com/fsouza/go-dockerclient"
)
// New returns a new conex manager.
func New(retcode int, pullImages bool, images ...string) Manager {
return &manager{
retcode: retcode,
pullImages: pullImages,
images: images,
counter: &counter{seqs: make(map[string]int)},
}
}
type manager struct {
retcode int
pullImages bool
name string
images []string
client *docker.Client
counter *counter
}
// Run prepares a docker client, pulls the provided list of images
// and then runs your tests.
func (mn *manager) Run(m *testing.M, images ...string) int {
// Pull images
var err error
mn.name, err = testContainersPrefix()
if err != nil {
return mn.retcode
}
mn.images = append(mn.images, images...)
mn.client, err = docker.NewClientFromEnv()
if err != nil {
fmt.Println(err)
return mn.retcode
}
if mn.pullImages {
err = mn.pull(images)
} else {
err = mn.ensure(images)
}
if err != nil {
fmt.Println(err)
return mn.retcode
}
log.Println() // print a timestamp. Helps to see how long tests take on it's own.
fmt.Printf("=== conex: Starting your tests.\n")
ret := m.Run()
err = mn.cleanup()
if err != nil {
// TODO: If cleanup fails, tests shouldn't fail, or should they?
log.Print(err)
}
return ret
}
func (mn *manager) boxName(test string, image string, params []string) string {
image = strings.Replace(image, ":", ".", -1)
image = strings.Replace(image, "/", "_", -1)
name := fmt.Sprintf("%s-%s-%s", mn.name, test, image)
name = fmt.Sprintf("%s_%d", name, mn.counter.Count(name))
return name
}
// Box returns the required container by image name and any tags.
func (mn *manager) Box(t testing.TB, conf *Config) Container {
name := mn.boxName(t.Name(), conf.Image, conf.Cmd)
// cname is a simple canonical name that includes the
// container image name and params.
cname := conf.Image
if len(conf.Cmd) != 0 {
cname = cname + ": " + strings.Join(conf.Cmd, " ")
}
logf(t, "creating (%s) as %s", cname, name)
exposedPorts := make(map[docker.Port]struct{})
for _, port := range conf.Expose {
exposedPorts[docker.Port(port)] = struct{}{}
}
c, err := mn.client.CreateContainer(
docker.CreateContainerOptions{
Name: name,
Config: &docker.Config{
Image: conf.Image,
Cmd: conf.Cmd,
Env: conf.Env,
Hostname: conf.Hostname,
Domainname: conf.Domainname,
User: conf.User,
Tty: true,
ExposedPorts: exposedPorts,
},
},
)
if err != nil {
fatalf(t, "Failed to create container: %s", err)
}
err = mn.client.StartContainer(c.ID, nil)
if err != nil {
fatalf(t, "Failed to start container: %v", err)
}
logf(t, "started (%s) as %s", cname, name)
cjson, err := mn.client.InspectContainer(c.ID)
if err != nil {
fatalf(t, "Failed to inspect: %v", err)
}
return &container{j: cjson, c: mn.client, t: t}
}
func (mn *manager) pull(images []string) error {
if len(images) == 0 {
return nil
}
log.Println() // Print a timestamp, handy to check if something is stack.
fmt.Printf("=== conex: Pulling Images\n")
l := len(images)
for i, image := range images {
fmt.Printf("--- Pulling %s (%d of %d)\n", image, i+1, l)
repo, tag := docker.ParseRepositoryTag(image)
err := mn.client.PullImage(
docker.PullImageOptions{
Repository: repo,
Tag: tag,
OutputStream: os.Stdout,
},
docker.AuthConfiguration{},
)
if err != nil {
return err
}
}
fmt.Printf("=== conex: Pulling Done\n")
log.Println() // Helps seeing how long the tests take.
return nil
}
func (mn *manager) ensure(images []string) error {
if len(images) == 0 {
return nil
}
log.Println() // Print a timestamp, handy to check if something is stack.
fmt.Printf("=== conex: Checking for Images\n\n")
is := len(images)
width := maxWidth(images)
for index, ref := range images {
img, err := mn.client.InspectImage(ref)
if err != nil {
return err
}
err = printImg(width, ref, index, is, img)
if err != nil {
return err
}
}
fmt.Printf("\n=== conex: All Images Found.\n")
return nil
}
func (mn *manager) cleanup() error {
return nil
}
func printImg(width int, ref string, index int, total int, img *docker.Image) error {
fmt.Printf("--- Found (%d of %d) %-*s %s %10s ago\n",
index+1,
total,
width,
ref,
stringid.TruncateID(img.ID),
units.HumanDuration(time.Now().UTC().Sub(img.Created)),
)
return nil
}
func maxWidth(str []string) int {
max := 0
for _, s := range str {
w := len(s)
if w > max {
max = w
}
}
return max
}