-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer.go
276 lines (248 loc) · 6.96 KB
/
container.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
package gerrittest
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/crewjam/errset"
"github.com/opalmer/dockertest"
log "github.com/sirupsen/logrus"
)
var (
// DefaultImage defines the default docker image to use in
// NewConfig(). This may be overridden with the $GERRITTEST_DOCKER_IMAGE
// environment variable.
DefaultImage = "opalmer/gerrittest:2.14.5.1"
)
const (
// DefaultImageEnvironmentVar defines the environment variable NewConfig()
// and the tests should be using to locate the default image override.
DefaultImageEnvironmentVar = "GERRITTEST_DOCKER_IMAGE"
// ExportedHTTPPort is the port exported by the docker container
// where the HTTP service is running.
ExportedHTTPPort = 8080
// ExportedSSHPort is the port exported by the docker container
// where the SSHPort service is running.
ExportedSSHPort = 29418
)
func newPort(public uint16, private uint16) (*dockertest.Port, error) {
if private != ExportedSSHPort && private != ExportedHTTPPort {
return nil, errors.New("Unknown private port")
}
// If a random port has been chosen then we need to try and determine
// one instead of letting Docker choose. If we don't, we'll be unable
// to properly set $GERRIT_CANONICAL_URL which Gerrit uses for redirects.
if private == ExportedHTTPPort && public == dockertest.RandomPort {
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return nil, err
}
defer listener.Close() // nolint: errcheck
port, err := strconv.ParseUint(
strings.Split(listener.Addr().String(), ":")[1], 10, 16)
if err != nil {
return nil, err
}
public = uint16(port)
}
return &dockertest.Port{
Private: private,
Public: public,
Protocol: dockertest.ProtocolTCP,
}, nil
}
func waitPort(ctx context.Context, port *dockertest.Port, errs chan error) {
addr := fmt.Sprintf("%s:%d", port.Address, port.Public)
logger := log.WithFields(log.Fields{
"cmp": "container",
"phase": "port-wait",
"addr": addr,
})
logger.WithField("task", "begin").Debug()
started := time.Now()
ticker := time.NewTicker(time.Millisecond * 200)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
errs <- ctx.Err()
return
case <-ticker.C:
conn, err := net.Dial("tcp", addr)
if err != nil {
continue
}
logger.WithFields(log.Fields{
"task": "done",
"elapsed": time.Since(started),
})
errs <- conn.Close()
return
}
}
}
func waitHTTP(ctx context.Context, port *dockertest.Port, errs chan error) {
url := fmt.Sprintf("http://%s:%d/", port.Address, port.Public)
logger := log.WithFields(log.Fields{
"cmp": "container",
"phase": "wait-port",
"url": url,
})
logger.WithField("task", "begin").Debug()
started := time.Now()
ticker := time.NewTicker(time.Millisecond * 200)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
errs <- ctx.Err()
return
case <-ticker.C:
response, err := http.Get(url)
if err != nil {
continue
}
if response.StatusCode != http.StatusOK {
continue
}
logger.WithFields(log.Fields{
"task": "end",
"elapsed": time.Since(started),
})
errs <- nil
return
}
}
}
func getDockerClientInput(http uint16, ssh uint16, image string) (*dockertest.ClientInput, error) {
httpPort, err := newPort(http, ExportedHTTPPort)
if err != nil {
return nil, err
}
sshPort, err := newPort(ssh, ExportedSSHPort)
if err != nil {
return nil, err
}
image = GetDockerImage(image)
input := dockertest.NewClientInput(image)
input.Ports.Add(httpPort)
input.Ports.Add(sshPort)
input.AddEnvironmentVar(
"GERRIT_CANONICAL_URL",
fmt.Sprintf("http://127.0.0.1:%d/", httpPort.Public))
return input, nil
}
// GetDockerImage returns the docker image to use to run the container. If "" is
// provided as the docker then we'll check $GERRITTEST_DOCKER_IMAGE first before
// returning the value defined in DefaultImage.
func GetDockerImage(image string) string {
if image != "" {
return image
}
if value, set := os.LookupEnv(DefaultImageEnvironmentVar); set {
return value
}
return DefaultImage
}
// Container stores information about a Gerrit instance running inside of
// a container.
type Container struct {
ctx context.Context
Docker *dockertest.DockerClient `json:"-"`
HTTP *dockertest.Port `json:"http"`
SSH *dockertest.Port `json:"ssh"`
Image string `json:"image"`
ID string `json:"id"`
}
// Terminate will terminate and remove the running container.
func (c *Container) Terminate() error {
return c.Docker.RemoveContainer(c.ctx, c.ID)
}
// NewContainer will create a new container using dockertest and return
// it. If you prefer to use an existing container use one of the LoadContainer*
// functions instead. This function will not return until the container has
// started and is listening on the requested ports.
func NewContainer(parent context.Context, http uint16, ssh uint16, image string) (*Container, error) {
image = GetDockerImage(image)
logger := log.WithFields(log.Fields{
"cmp": "container",
})
logger.WithField("image", image).Debug()
input, err := getDockerClientInput(http, ssh, image)
if err != nil {
return nil, err
}
client, err := dockertest.NewClient()
if err != nil {
return nil, err
}
startLog := logger.WithField("phase", "service")
service := &dockertest.Service{
Input: input,
Client: client,
Name: "gerrittest",
Ping: func(input *dockertest.PingInput) error {
pingStart := time.Now()
entry := startLog.WithField("status", "ping")
entry.WithField("task", "begin").Debug()
containerSSH, err := input.Container.Port(ExportedSSHPort)
if err != nil {
entry.WithError(err).Error()
return err
}
containerHTTP, err := input.Container.Port(ExportedHTTPPort)
if err != nil {
entry.WithError(err).Error()
return err
}
// Wait for ports to open
errs := make(chan error, 2)
results := errset.ErrSet{}
go waitPort(parent, containerSSH, errs)
go waitHTTP(parent, containerHTTP, errs)
for i := 0; i < 2; i++ {
results = append(results, <-errs)
}
entry.WithFields(log.Fields{
"task": "end",
"elapsed": time.Since(pingStart),
}).Debug()
return results.ReturnValue()
},
}
// Call run which will start the container and wait for Ping() in
// the service above to return.
start := time.Now()
startLog.WithField("task", "begin").Debug()
if err := service.Run(); err != nil {
errs := errset.ErrSet{}
errs = append(errs, err)
errs = append(errs, service.Terminate())
return nil, errs.ReturnValue()
}
startLog.WithFields(log.Fields{
"task": "end",
"elapsed": time.Since(start),
}).Debug()
portSSH, err := service.Container.Port(ExportedSSHPort)
if err != nil {
return nil, err
}
portHTTP, err := service.Container.Port(ExportedHTTPPort)
if err != nil {
return nil, err
}
return &Container{
ctx: parent,
Docker: client,
SSH: portSSH,
HTTP: portHTTP,
Image: image,
ID: service.Container.ID(),
}, nil
}