Always convert port values in ServicePort to integer

Signed-off-by: Joffrey F <joffrey@docker.com>
This commit is contained in:
Joffrey F 2017-06-05 14:33:20 -07:00 committed by Joffrey F
parent ff720ba6b2
commit bfc7ac4995
2 changed files with 29 additions and 8 deletions

View File

@ -263,6 +263,22 @@ class ServiceSecret(namedtuple('_ServiceSecret', 'source target uid gid mode')):
class ServicePort(namedtuple('_ServicePort', 'target published protocol mode external_ip')):
def __new__(cls, target, published, *args, **kwargs):
try:
if target:
target = int(target)
except ValueError:
raise ConfigurationError('Invalid target port: {}'.format(target))
try:
if published:
published = int(published)
except ValueError:
raise ConfigurationError('Invalid published port: {}'.format(published))
return super(ServicePort, cls).__new__(
cls, target, published, *args, **kwargs
)
@classmethod
def parse(cls, spec):

View File

@ -57,15 +57,15 @@ class TestServicePort(object):
def test_parse_simple_target_port(self):
ports = ServicePort.parse(8000)
assert len(ports) == 1
assert ports[0].target == '8000'
assert ports[0].target == 8000
def test_parse_complete_port_definition(self):
port_def = '1.1.1.1:3000:3000/udp'
ports = ServicePort.parse(port_def)
assert len(ports) == 1
assert ports[0].repr() == {
'target': '3000',
'published': '3000',
'target': 3000,
'published': 3000,
'external_ip': '1.1.1.1',
'protocol': 'udp',
}
@ -77,7 +77,7 @@ class TestServicePort(object):
assert len(ports) == 1
assert ports[0].legacy_repr() == port_def + '/tcp'
assert ports[0].repr() == {
'target': '3000',
'target': 3000,
'external_ip': '1.1.1.1',
}
@ -86,14 +86,19 @@ class TestServicePort(object):
assert len(ports) == 2
reprs = [p.repr() for p in ports]
assert {
'target': '4000',
'published': '25000'
'target': 4000,
'published': 25000
} in reprs
assert {
'target': '4001',
'published': '25001'
'target': 4001,
'published': 25001
} in reprs
def test_parse_invalid_port(self):
port_def = '4000p'
with pytest.raises(ConfigurationError):
ServicePort.parse(port_def)
class TestVolumeSpec(object):