forked from tox-dev/tox-docker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tox_docker.py
276 lines (229 loc) · 8.57 KB
/
tox_docker.py
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
import socket
import sys
import time
from tox import hookimpl
from tox.config import SectionReader
from docker.errors import ImageNotFound
import docker as docker_module
import py
NANOSECONDS = 1000000000
class HealthCheckFailed(Exception):
pass
def escape_env_var(varname):
"""
Convert a string to a form suitable for use as an environment variable.
The result will be all uppercase, and will have all invalid characters
replaced by an underscore.
The result will match the following regex: [a-zA-Z_][a-zA-Z0-9_]*
Example:
"my.private.registry/cat/image" will become
"MY_PRIVATE_REGISTRY_CAT_IMAGE"
"""
varname = list(varname.upper())
if not varname[0].isalpha():
varname[0] = '_'
for i, c in enumerate(varname):
if not c.isalnum() and c != '_':
varname[i] = '_'
return "".join(varname)
def _newaction(venv, message):
try:
# tox 3.7 and later
return venv.new_action(message)
except AttributeError:
return venv.session.newaction(venv, message)
def _get_gateway_ip(container):
if sys.platform == "darwin":
# per https://docs.docker.com/v17.12/docker-for-mac/networking/#use-cases-and-workarounds,
# there is no bridge network available in Docker for Mac, and exposed ports are made
# available on localhost (but 0.0.0.0 works just as well)
return "0.0.0.0"
else:
return container.attrs["NetworkSettings"]["Gateway"] or "0.0.0.0"
@hookimpl
def tox_configure(config):
def getfloat(reader, key):
val = reader.getstring(key)
if val is None:
return None
try:
return float(val)
except ValueError:
msg = "{!r} is not a number (for {} in [{}])".format(
val, key, reader.section_name
)
raise ValueError(msg)
def gettime(reader, key):
return int(getfloat(reader, key) * NANOSECONDS)
def getint(reader, key):
raw = getfloat(reader, key)
val = int(raw)
if val != raw:
msg = "{!r} is not an int (for {} in [{}])".format(
val, key, reader.section_name
)
raise ValueError(msg)
return val
inipath = str(config.toxinipath)
iniparser = py.iniconfig.IniConfig(inipath)
image_configs = {}
for section in iniparser.sections:
if not section.startswith("docker:"):
continue
reader = SectionReader(section, iniparser)
_, _, image = section.partition(":")
image_configs[image] = {
"healthcheck_cmd": reader.getargv("healthcheck_cmd"),
"healthcheck_interval": gettime(reader, "healthcheck_interval"),
"healthcheck_timeout": gettime(reader, "healthcheck_timeout"),
"healthcheck_retries": getint(reader, "healthcheck_retries"),
"healthcheck_start_period": gettime(reader, "healthcheck_start_period"),
}
config._docker_image_configs = image_configs
@hookimpl
def tox_runtest_pre(venv):
envconfig = venv.envconfig
if not envconfig.docker:
return
config = envconfig.config
image_configs = config._docker_image_configs
docker = docker_module.from_env(version="auto")
action = _newaction(venv, "docker")
environment = {}
for value in envconfig.dockerenv:
envvar, _, value = value.partition("=")
environment[envvar] = value
venv.envconfig.setenv[envvar] = value
seen = set()
for image in envconfig.docker:
name, _, tag = image.partition(":")
if name in seen:
raise ValueError(
"Docker image {!r} is specified more than once".format(name)
)
seen.add(name)
try:
docker.images.get(image)
except ImageNotFound:
action.setactivity("docker", "pull {!r}".format(image))
with action:
docker.images.pull(name, tag=tag or None)
envconfig._docker_containers = []
for image in envconfig.docker:
image_config = image_configs.get(image, {})
hc_cmd = image_config.get("healthcheck_cmd")
hc_interval = image_config.get("healthcheck_interval")
hc_timeout = image_config.get("healthcheck_timeout")
hc_retries = image_config.get("healthcheck_retries")
hc_start_period = image_config.get("healthcheck_start_period")
if hc_cmd is not None \
and hc_interval is not None \
and hc_timeout is not None \
and hc_retries is not None \
and hc_start_period is not None:
healthcheck = {
"test": ["CMD-SHELL"] + hc_cmd,
"interval": hc_interval,
"timeout": hc_timeout,
"retries": hc_retries,
"start_period": hc_start_period,
}
else:
healthcheck = None
action.setactivity("docker", "run {!r}".format(image))
with action:
container = docker.containers.run(
image,
detach=True,
publish_all_ports=True,
environment=environment,
healthcheck=healthcheck,
)
envconfig._docker_containers.append(container)
container.reload()
for container in envconfig._docker_containers:
image = container.attrs["Config"]["Image"]
if "Health" in container.attrs["State"]:
action.setactivity("docker", "health check: {!r}".format(image))
with action:
while True:
container.reload()
health = container.attrs["State"]["Health"]["Status"]
if health == "healthy":
break
elif health == "starting":
time.sleep(0.1)
elif health == "unhealthy":
# the health check failed after its own timeout
raise HealthCheckFailed("{!r} failed health check".format(image))
name, _, tag = image.partition(":")
gateway_ip = _get_gateway_ip(container)
for containerport, hostports in container.attrs["NetworkSettings"]["Ports"].items():
for spec in hostports:
if spec["HostIp"] == "0.0.0.0":
hostport = spec["HostPort"]
break
else:
continue
envvar = escape_env_var("{}_HOST".format(
name,
))
venv.envconfig.setenv[envvar] = gateway_ip
envvar = escape_env_var("{}_{}_PORT".format(
name,
containerport,
))
venv.envconfig.setenv[envvar] = hostport
# TODO: remove in 2.0
_, proto = containerport.split("/")
envvar = escape_env_var("{}_{}".format(
name,
containerport,
))
venv.envconfig.setenv[envvar] = hostport
_, proto = containerport.split("/")
if proto == "udp":
continue
# mostly-busy-loop until we can connect to that port; that
# will be our signal that the container is ready (meh)
start = time.time()
while (time.time() - start) < 30:
try:
sock = socket.create_connection(
address=(gateway_ip, int(hostport)),
timeout=0.1,
)
except socket.error:
time.sleep(0.1)
else:
sock.shutdown(socket.SHUT_RDWR)
sock.close()
break
else:
raise Exception(
"Never got answer on port {} from {}".format(containerport, name)
)
@hookimpl
def tox_runtest_post(venv):
envconfig = venv.envconfig
if not envconfig.docker:
return
action = _newaction(venv, "docker")
for container in envconfig._docker_containers:
action.setactivity("docker", "remove '{}' (forced)".format(container.short_id))
with action:
container.remove(v=True, force=True)
@hookimpl
def tox_addoption(parser):
parser.add_testenv_attribute(
name="docker",
type="line-list",
help="Name of docker images, including tag, to start before the test run",
default=[],
)
parser.add_testenv_attribute(
name="dockerenv",
type="line-list",
help="List of ENVVAR=VALUE pairs that will be passed to all containers",
default=[],
)