Skip to content

Commit ac3cbb4

Browse files
committed
using port binding from docker-py that includes port ranges
1 parent c802245 commit ac3cbb4

File tree

2 files changed

+1
-72
lines changed

2 files changed

+1
-72
lines changed

compose/service.py

+1-28
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import six
1010
from docker.errors import APIError
1111
from docker.utils import create_host_config, LogConfig
12+
from docker.utils.ports import build_port_bindings
1213

1314
from .config import DOCKER_CONFIG_KEYS, merge_environment
1415
from .container import Container, get_container_name
@@ -654,34 +655,6 @@ def build_volume_binding(volume_spec):
654655
return volume_spec.external, internal
655656

656657

657-
def build_port_bindings(ports):
658-
port_bindings = {}
659-
for port in ports:
660-
internal_port, external = split_port(port)
661-
if internal_port in port_bindings:
662-
port_bindings[internal_port].append(external)
663-
else:
664-
port_bindings[internal_port] = [external]
665-
return port_bindings
666-
667-
668-
def split_port(port):
669-
parts = str(port).split(':')
670-
if not 1 <= len(parts) <= 3:
671-
raise ConfigError('Invalid port "%s", should be '
672-
'[[remote_ip:]remote_port:]port[/protocol]' % port)
673-
674-
if len(parts) == 1:
675-
internal_port, = parts
676-
return internal_port, None
677-
if len(parts) == 2:
678-
external_port, internal_port = parts
679-
return internal_port, external_port
680-
681-
external_ip, external_port, internal_port = parts
682-
return internal_port, (external_ip, external_port or None)
683-
684-
685658
def build_extra_hosts(extra_hosts_config):
686659
if not extra_hosts_config:
687660
return {}

tests/unit/service_test.py

-44
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@
1212
from compose.service import (
1313
APIError,
1414
ConfigError,
15-
build_port_bindings,
1615
build_volume_binding,
1716
get_container_data_volumes,
1817
get_container_name,
1918
merge_volume_bindings,
2019
parse_repository_tag,
2120
parse_volume_spec,
22-
split_port,
2321
)
2422

2523

@@ -111,48 +109,6 @@ def test_get_volumes_from_service_no_container(self):
111109
self.assertEqual(service._get_volumes_from(), [container_id])
112110
from_service.create_container.assert_called_once_with()
113111

114-
def test_split_port_with_host_ip(self):
115-
internal_port, external_port = split_port("127.0.0.1:1000:2000")
116-
self.assertEqual(internal_port, "2000")
117-
self.assertEqual(external_port, ("127.0.0.1", "1000"))
118-
119-
def test_split_port_with_protocol(self):
120-
internal_port, external_port = split_port("127.0.0.1:1000:2000/udp")
121-
self.assertEqual(internal_port, "2000/udp")
122-
self.assertEqual(external_port, ("127.0.0.1", "1000"))
123-
124-
def test_split_port_with_host_ip_no_port(self):
125-
internal_port, external_port = split_port("127.0.0.1::2000")
126-
self.assertEqual(internal_port, "2000")
127-
self.assertEqual(external_port, ("127.0.0.1", None))
128-
129-
def test_split_port_with_host_port(self):
130-
internal_port, external_port = split_port("1000:2000")
131-
self.assertEqual(internal_port, "2000")
132-
self.assertEqual(external_port, "1000")
133-
134-
def test_split_port_no_host_port(self):
135-
internal_port, external_port = split_port("2000")
136-
self.assertEqual(internal_port, "2000")
137-
self.assertEqual(external_port, None)
138-
139-
def test_split_port_invalid(self):
140-
with self.assertRaises(ConfigError):
141-
split_port("0.0.0.0:1000:2000:tcp")
142-
143-
def test_build_port_bindings_with_one_port(self):
144-
port_bindings = build_port_bindings(["127.0.0.1:1000:1000"])
145-
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000")])
146-
147-
def test_build_port_bindings_with_matching_internal_ports(self):
148-
port_bindings = build_port_bindings(["127.0.0.1:1000:1000", "127.0.0.1:2000:1000"])
149-
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000"), ("127.0.0.1", "2000")])
150-
151-
def test_build_port_bindings_with_nonmatching_internal_ports(self):
152-
port_bindings = build_port_bindings(["127.0.0.1:1000:1000", "127.0.0.1:2000:2000"])
153-
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000")])
154-
self.assertEqual(port_bindings["2000"], [("127.0.0.1", "2000")])
155-
156112
def test_split_domainname_none(self):
157113
service = Service('foo', image='foo', hostname='name', client=self.mock_client)
158114
self.mock_client.containers.return_value = []

0 commit comments

Comments
 (0)