Update tests and add invalid port spec tests
MODIFY
repo2docker/utils.py
repo2docker/utils.py
@@ -88,6 +88,14 @@ def validate_and_generate_port_mapping(port_mappings):
single container_port to multiple host_ports
(docker-py supports this but repo2docker does not)
"""
+ def check_port(port):
+ try:
+ int(port)
+ except ValueError as e:
+ raise ValueError('Port specification "{}" has '
+ 'an invalid port.'.format(mapping))
+ return port
+
def check_port_string(p):
parts = p.split('/')
if len(parts) == 2: # 134/tcp
@@ -99,11 +107,7 @@ def validate_and_generate_port_mapping(port_mappings):
port = parts[0]
protocol = 'tcp'
- try:
- int(port)
- except ValueError as e:
- raise ValueError('Port specification "{}" has '
- 'an invalid port.'.format(mapping))
+ check_port(port)
return '/'.join((port, protocol))
@@ -117,9 +121,9 @@ def validate_and_generate_port_mapping(port_mappings):
*host, container_port = parts
# just a port
if len(host) == 1:
- host = host[0]
+ host = check_port(host[0])
else:
- host = tuple(host)
+ host = tuple((host[0], check_port(host[1])))
container_port = check_port_string(container_port)
ports[container_port] = host

MODIFY
tests/unit/test_argumentvalidation.py
tests/unit/test_argumentvalidation.py
@@ -190,7 +190,7 @@ def test_invalid_port_mapping_fail(temp_cwd):
# builddir passed in the function will be an argument for the run command
args_list = ['-p', '75000:80', builddir, 'ls']
- assert not validate_arguments(builddir, args_list, 'Invalid port mapping')
+ assert not validate_arguments(builddir, args_list, 'Port specification')
def test_invalid_protocol_port_mapping_fail(temp_cwd):
@@ -201,7 +201,7 @@ def test_invalid_protocol_port_mapping_fail(temp_cwd):
# builddir passed in the function will be an argument for the run command
args_list = ['-p', '80/tpc:8000', builddir, 'ls']
- assert not validate_arguments(builddir, args_list, 'Invalid port mapping')
+ assert not validate_arguments(builddir, args_list, 'Port specification')
def test_invalid_container_port_protocol_mapping_fail(temp_cwd):
@@ -212,7 +212,7 @@ def test_invalid_container_port_protocol_mapping_fail(temp_cwd):
# builddir passed in the function will be an argument for the run command
args_list = ['-p', '80:8000/upd', builddir, 'ls']
- assert not validate_arguments(builddir, args_list, 'Invalid port mapping')
+ assert not validate_arguments(builddir, args_list, 'Port specification')
@pytest.mark.xfail(reason="Regression in new arg parsing")

MODIFY
tests/unit/test_utils.py
tests/unit/test_utils.py
@@ -78,3 +78,13 @@ def test_byte_spec_validation():
def test_valid_port_mapping(input, expected):
actual = utils.validate_and_generate_port_mapping(input)
assert actual == expected
+
+
+@pytest.mark.parametrize("port_spec", [
+ "a8888:8888", "888:888/abc"
+])
+def test_invalid_port_mapping(port_spec):
+ with pytest.raises(ValueError) as e:
+ utils.validate_and_generate_port_mapping([port_spec])
+
+ assert 'Port specification "{}"'.format(port_spec) in str(e.value)

Update tests and add invalid port spec tests