kopia lustrzana https://github.com/jupyterhub/repo2docker
Update tests and add invalid port spec tests
rodzic
7067663763
commit
9e6dc8f7d7
|
@ -88,6 +88,14 @@ def validate_and_generate_port_mapping(port_mappings):
|
||||||
single container_port to multiple host_ports
|
single container_port to multiple host_ports
|
||||||
(docker-py supports this but repo2docker does not)
|
(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):
|
def check_port_string(p):
|
||||||
parts = p.split('/')
|
parts = p.split('/')
|
||||||
if len(parts) == 2: # 134/tcp
|
if len(parts) == 2: # 134/tcp
|
||||||
|
@ -99,11 +107,7 @@ def validate_and_generate_port_mapping(port_mappings):
|
||||||
port = parts[0]
|
port = parts[0]
|
||||||
protocol = 'tcp'
|
protocol = 'tcp'
|
||||||
|
|
||||||
try:
|
check_port(port)
|
||||||
int(port)
|
|
||||||
except ValueError as e:
|
|
||||||
raise ValueError('Port specification "{}" has '
|
|
||||||
'an invalid port.'.format(mapping))
|
|
||||||
|
|
||||||
return '/'.join((port, protocol))
|
return '/'.join((port, protocol))
|
||||||
|
|
||||||
|
@ -117,9 +121,9 @@ def validate_and_generate_port_mapping(port_mappings):
|
||||||
*host, container_port = parts
|
*host, container_port = parts
|
||||||
# just a port
|
# just a port
|
||||||
if len(host) == 1:
|
if len(host) == 1:
|
||||||
host = host[0]
|
host = check_port(host[0])
|
||||||
else:
|
else:
|
||||||
host = tuple(host)
|
host = tuple((host[0], check_port(host[1])))
|
||||||
|
|
||||||
container_port = check_port_string(container_port)
|
container_port = check_port_string(container_port)
|
||||||
ports[container_port] = host
|
ports[container_port] = host
|
||||||
|
|
|
@ -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
|
# builddir passed in the function will be an argument for the run command
|
||||||
args_list = ['-p', '75000:80', builddir, 'ls']
|
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):
|
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
|
# builddir passed in the function will be an argument for the run command
|
||||||
args_list = ['-p', '80/tpc:8000', builddir, 'ls']
|
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):
|
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
|
# builddir passed in the function will be an argument for the run command
|
||||||
args_list = ['-p', '80:8000/upd', builddir, 'ls']
|
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")
|
@pytest.mark.xfail(reason="Regression in new arg parsing")
|
||||||
|
|
|
@ -78,3 +78,13 @@ def test_byte_spec_validation():
|
||||||
def test_valid_port_mapping(input, expected):
|
def test_valid_port_mapping(input, expected):
|
||||||
actual = utils.validate_and_generate_port_mapping(input)
|
actual = utils.validate_and_generate_port_mapping(input)
|
||||||
assert actual == expected
|
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)
|
||||||
|
|
Ładowanie…
Reference in New Issue