Skip to content

Commit

Permalink
add manual publications of ports
Browse files Browse the repository at this point in the history
  • Loading branch information
jzelinskie committed Apr 1, 2020
1 parent 01ef31a commit 8251f2f
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion tox_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,25 @@ def getint(reader, key):
"healthcheck_timeout": gettime(reader, "healthcheck_timeout"),
"healthcheck_retries": getint(reader, "healthcheck_retries"),
"healthcheck_start_period": gettime(reader, "healthcheck_start_period"),
"ports": reader.getlist("ports"),
}

config._docker_image_configs = image_configs


def _validate_port(port_line):
host_port, _, container_port_proto = port_line.partition(":")
host_port = int(host_port)

container_port, _, protocol = container_port_proto.partition("/")
container_port = int(container_port)

if protocol.lower() not in ('tcp', 'udp'):
raise ValueError('protocol is not tcp or udp')

return (host_port, container_port_proto)


@hookimpl
def tox_runtest_pre(venv):
envconfig = venv.envconfig
Expand Down Expand Up @@ -163,12 +177,20 @@ def tox_runtest_pre(venv):
else:
healthcheck = None

ports = {}
for port_mapping in image_config.get("ports", []):
host_port, container_port_proto = _validate_port(port_mapping)
existing_ports = set(ports.get(container_port_proto, []))
existing_ports.add(host_port)
ports[container_port] = list(existing_ports)

action.setactivity("docker", "run {!r}".format(image))
with action:
container = docker.containers.run(
image,
detach=True,
publish_all_ports=True,
publish_all_ports=len(ports) == 0,
ports=ports,
environment=environment,
healthcheck=healthcheck,
)
Expand All @@ -195,6 +217,10 @@ def tox_runtest_pre(venv):
name, _, tag = image.partition(":")
gateway_ip = _get_gateway_ip(container)
for containerport, hostports in container.attrs["NetworkSettings"]["Ports"].items():
if hostports is None:
# The port is exposed by the container, but not published.
continue

for spec in hostports:
if spec["HostIp"] == "0.0.0.0":
hostport = spec["HostPort"]
Expand Down

0 comments on commit 8251f2f

Please sign in to comment.