Merge pull request #5428 from docker/5230-allow-port-publish-ranges

Allow port publish ranges
This commit is contained in:
Joffrey F 2017-12-04 15:18:10 -08:00 committed by GitHub
commit 6cd0bc4883
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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):