Allow port publish ranges

Signed-off-by: Joffrey F <joffrey@docker.com>
This commit is contained in:
Joffrey F 2017-12-01 15:23:32 -08:00
parent 3c232e42c1
commit ceec0595bd
2 changed files with 39 additions and 5 deletions

View File

@ -319,11 +319,19 @@ class ServicePort(namedtuple('_ServicePort', 'target published protocol mode ext
except ValueError:
raise ConfigurationError('Invalid target port: {}'.format(target))
try:
if published:
published = int(published)
except ValueError:
raise ConfigurationError('Invalid published port: {}'.format(published))
if published:
if isinstance(published, six.string_types) and '-' in published: # "x-y:z" format
a, b = published.split('-', 1)
try:
int(a)
int(b)
except ValueError:
raise ConfigurationError('Invalid published port: {}'.format(published))
else:
try:
published = int(published)
except ValueError:
raise ConfigurationError('Invalid published port: {}'.format(published))
return super(ServicePort, cls).__new__(
cls, target, published, *args, **kwargs

View File

@ -100,11 +100,37 @@ class TestServicePort(object):
'published': 25001
} in reprs
def test_parse_port_publish_range(self):
ports = ServicePort.parse('4440-4450:4000')
assert len(ports) == 1
reprs = [p.repr() for p in ports]
assert {
'target': 4000,
'published': '4440-4450'
} in reprs
def test_parse_invalid_port(self):
port_def = '4000p'
with pytest.raises(ConfigurationError):
ServicePort.parse(port_def)
def test_parse_invalid_publish_range(self):
port_def = '-4000:4000'
with pytest.raises(ConfigurationError):
ServicePort.parse(port_def)
port_def = 'asdf:4000'
with pytest.raises(ConfigurationError):
ServicePort.parse(port_def)
port_def = '1234-12f:4000'
with pytest.raises(ConfigurationError):
ServicePort.parse(port_def)
port_def = '1234-1235-1239:4000'
with pytest.raises(ConfigurationError):
ServicePort.parse(port_def)
class TestVolumeSpec(object):