2017-12-22 11:38:58 +00:00
|
|
|
"""
|
|
|
|
Tests that runs validity checks on arguments passed in from shell
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
|
2018-12-17 12:11:45 +00:00
|
|
|
import pytest
|
2017-12-25 02:03:17 +00:00
|
|
|
|
2018-12-17 12:11:45 +00:00
|
|
|
|
|
|
|
here = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
test_dir = os.path.dirname(here)
|
|
|
|
docker_simple = os.path.join(test_dir, 'dockerfile', 'simple')
|
|
|
|
|
|
|
|
# default to building in the cwd (a temporary directory)
|
|
|
|
builddir = '.'
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def temp_cwd(tmpdir):
|
|
|
|
tmpdir.chdir()
|
|
|
|
|
|
|
|
|
|
|
|
def validate_arguments(builddir, args_list='.', expected=None, disable_dockerd=False):
|
2017-12-22 11:38:58 +00:00
|
|
|
try:
|
2017-12-24 23:03:09 +00:00
|
|
|
cmd = ['repo2docker']
|
|
|
|
for k in args_list:
|
|
|
|
cmd.append(k)
|
|
|
|
cmd.append(builddir)
|
2018-01-10 02:39:07 +00:00
|
|
|
env = os.environ.copy()
|
|
|
|
if disable_dockerd:
|
|
|
|
env['DOCKER_HOST'] = "INCORRECT"
|
|
|
|
subprocess.check_output(cmd, env=env, stderr=subprocess.STDOUT)
|
2017-12-22 11:38:58 +00:00
|
|
|
return True
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
output = e.output.decode()
|
2018-12-17 12:11:45 +00:00
|
|
|
if expected is not None:
|
|
|
|
assert expected in output
|
2017-12-22 11:38:58 +00:00
|
|
|
return False
|
|
|
|
else:
|
2018-12-17 12:11:45 +00:00
|
|
|
print(output)
|
2017-12-22 11:38:58 +00:00
|
|
|
raise
|
|
|
|
|
2017-12-23 00:17:32 +00:00
|
|
|
|
2018-12-17 12:11:45 +00:00
|
|
|
def test_image_name_fail(temp_cwd):
|
2017-12-22 12:31:41 +00:00
|
|
|
"""
|
2017-12-23 00:17:32 +00:00
|
|
|
Test to check if repo2docker throws image_name validation error on --image-name argument containing
|
|
|
|
uppercase characters and _ characters in incorrect positions.
|
2017-12-22 12:31:41 +00:00
|
|
|
"""
|
|
|
|
|
2017-12-24 23:03:09 +00:00
|
|
|
image_name = 'Test/Invalid_name:1.0.0'
|
|
|
|
args_list = ['--no-run', '--no-build', '--image-name', image_name]
|
2018-03-15 23:54:45 +00:00
|
|
|
expected = (
|
|
|
|
"%r is not a valid docker image name. Image name"
|
|
|
|
"must start with an alphanumeric character and"
|
|
|
|
"can then use _ . or - in addition to alphanumeric." % image_name
|
|
|
|
)
|
2017-12-24 23:03:09 +00:00
|
|
|
assert not validate_arguments(builddir, args_list, expected)
|
2017-12-22 11:38:58 +00:00
|
|
|
|
2017-12-23 00:17:32 +00:00
|
|
|
|
2018-12-17 12:11:45 +00:00
|
|
|
def test_image_name_underscore_fail(temp_cwd):
|
2017-12-23 00:17:32 +00:00
|
|
|
"""
|
|
|
|
Test to check if repo2docker throws image_name validation error on --image-name argument starts with _.
|
|
|
|
"""
|
|
|
|
|
2017-12-24 23:03:09 +00:00
|
|
|
image_name = '_test/invalid_name:1.0.0'
|
|
|
|
args_list = ['--no-run', '--no-build', '--image-name', image_name]
|
2018-03-15 23:54:45 +00:00
|
|
|
expected = (
|
|
|
|
"%r is not a valid docker image name. Image name"
|
|
|
|
"must start with an alphanumeric character and"
|
|
|
|
"can then use _ . or - in addition to alphanumeric." % image_name
|
|
|
|
)
|
2017-12-24 23:03:09 +00:00
|
|
|
assert not validate_arguments(builddir, args_list, expected)
|
2017-12-23 00:17:32 +00:00
|
|
|
|
|
|
|
|
2018-12-17 12:11:45 +00:00
|
|
|
def test_image_name_double_dot_fail(temp_cwd):
|
2017-12-23 00:17:32 +00:00
|
|
|
"""
|
|
|
|
Test to check if repo2docker throws image_name validation error on --image-name argument contains consecutive dots.
|
|
|
|
"""
|
|
|
|
|
2017-12-24 23:03:09 +00:00
|
|
|
image_name = 'test..com/invalid_name:1.0.0'
|
|
|
|
args_list = ['--no-run', '--no-build', '--image-name', image_name]
|
2018-03-15 23:54:45 +00:00
|
|
|
expected = (
|
|
|
|
"%r is not a valid docker image name. Image name"
|
|
|
|
"must start with an alphanumeric character and"
|
|
|
|
"can then use _ . or - in addition to alphanumeric." % image_name
|
|
|
|
)
|
2017-12-24 23:03:09 +00:00
|
|
|
assert not validate_arguments(builddir, args_list, expected)
|
2017-12-23 00:17:32 +00:00
|
|
|
|
|
|
|
|
2018-12-17 12:11:45 +00:00
|
|
|
def test_image_name_valid_restircted_registry_domain_name_fail(temp_cwd):
|
2017-12-23 00:17:32 +00:00
|
|
|
"""
|
|
|
|
Test to check if repo2docker throws image_name validation error on -image-name argument being invalid. Based on the
|
|
|
|
regex definitions first part of registry domain cannot contain uppercase characters
|
|
|
|
"""
|
|
|
|
|
2017-12-24 23:03:09 +00:00
|
|
|
image_name = 'Test.com/valid_name:1.0.0'
|
|
|
|
args_list = ['--no-run', '--no-build', '--image-name', image_name]
|
2018-03-15 23:54:45 +00:00
|
|
|
expected = (
|
|
|
|
"%r is not a valid docker image name. Image name"
|
|
|
|
"must start with an alphanumeric character and"
|
|
|
|
"can then use _ . or - in addition to alphanumeric." % image_name
|
|
|
|
)
|
2017-12-23 00:17:32 +00:00
|
|
|
|
2017-12-24 23:03:09 +00:00
|
|
|
assert not validate_arguments(builddir, args_list, expected)
|
2017-12-23 00:17:32 +00:00
|
|
|
|
|
|
|
|
2018-12-17 12:11:45 +00:00
|
|
|
def test_image_name_valid_registry_domain_name_success(temp_cwd):
|
2017-12-23 00:17:32 +00:00
|
|
|
"""
|
|
|
|
Test to check if repo2docker runs with a valid --image-name argument.
|
|
|
|
"""
|
|
|
|
|
2018-12-17 12:11:45 +00:00
|
|
|
builddir = docker_simple
|
2017-12-24 23:03:09 +00:00
|
|
|
image_name = 'test.COM/valid_name:1.0.0'
|
|
|
|
args_list = ['--no-run', '--no-build', '--image-name', image_name]
|
2017-12-23 00:17:32 +00:00
|
|
|
|
2017-12-24 23:03:09 +00:00
|
|
|
assert validate_arguments(builddir, args_list, None)
|
2017-12-23 00:17:32 +00:00
|
|
|
|
|
|
|
|
2018-12-17 12:11:45 +00:00
|
|
|
def test_image_name_valid_name_success(temp_cwd):
|
2017-12-23 00:17:32 +00:00
|
|
|
"""
|
|
|
|
Test to check if repo2docker runs with a valid --image-name argument.
|
|
|
|
"""
|
|
|
|
|
2018-12-17 12:11:45 +00:00
|
|
|
builddir = docker_simple
|
2017-12-24 23:03:09 +00:00
|
|
|
image_name = 'test.com/valid_name:1.0.0'
|
|
|
|
args_list = ['--no-run', '--no-build', '--image-name', image_name]
|
|
|
|
|
|
|
|
assert validate_arguments(builddir, args_list, None)
|
|
|
|
|
2017-12-25 02:03:17 +00:00
|
|
|
|
2018-12-17 12:11:45 +00:00
|
|
|
def test_volume_no_build_fail(temp_cwd):
|
2017-12-24 23:03:09 +00:00
|
|
|
"""
|
|
|
|
Test to check if repo2docker fails when both --no-build and -v arguments are given
|
|
|
|
"""
|
|
|
|
args_list = ['--no-build', '-v', '/data:/data']
|
|
|
|
|
2018-12-17 12:11:45 +00:00
|
|
|
assert not validate_arguments(
|
|
|
|
builddir,
|
|
|
|
args_list,
|
|
|
|
'Cannot mount volumes if container is not run',
|
|
|
|
)
|
2017-12-24 23:03:09 +00:00
|
|
|
|
|
|
|
|
2018-12-17 12:11:45 +00:00
|
|
|
def test_volume_no_run_fail(temp_cwd):
|
2017-12-24 23:03:09 +00:00
|
|
|
"""
|
|
|
|
Test to check if repo2docker fails when both --no-run and -v arguments are given
|
|
|
|
"""
|
|
|
|
args_list = ['--no-run', '-v', '/data:/data']
|
2017-12-23 00:17:32 +00:00
|
|
|
|
2018-12-17 12:11:45 +00:00
|
|
|
assert not validate_arguments(
|
|
|
|
builddir,
|
|
|
|
args_list,
|
|
|
|
'Cannot mount volumes if container is not run',
|
|
|
|
)
|
2018-01-07 15:43:03 +00:00
|
|
|
|
2017-12-25 02:03:17 +00:00
|
|
|
|
2018-12-17 12:11:45 +00:00
|
|
|
def test_env_no_run_fail(temp_cwd):
|
2018-01-07 15:43:03 +00:00
|
|
|
"""
|
2018-04-24 23:51:39 +00:00
|
|
|
Test to check if repo2docker fails when both --no-run and -e arguments are given
|
2018-01-07 15:43:03 +00:00
|
|
|
"""
|
|
|
|
args_list = ['--no-run', '-e', 'FOO=bar', '--']
|
|
|
|
|
|
|
|
assert not validate_arguments(builddir, args_list, 'To specify environment variables, you also need to run the container')
|
|
|
|
|
2017-12-25 02:03:17 +00:00
|
|
|
|
2018-12-17 12:11:45 +00:00
|
|
|
def test_port_mapping_no_run_fail(temp_cwd):
|
2017-12-25 02:03:17 +00:00
|
|
|
"""
|
|
|
|
Test to check if repo2docker fails when both --no-run and --publish arguments are specified.
|
|
|
|
"""
|
|
|
|
args_list = ['--no-run', '--publish', '8000:8000']
|
|
|
|
|
|
|
|
assert not validate_arguments(builddir, args_list, 'To publish user defined port mappings, the container must also be run')
|
|
|
|
|
|
|
|
|
2018-12-17 12:11:45 +00:00
|
|
|
def test_all_ports_mapping_no_run_fail(temp_cwd):
|
2017-12-25 02:03:17 +00:00
|
|
|
"""
|
|
|
|
Test to check if repo2docker fails when both --no-run and -P arguments are specified.
|
|
|
|
"""
|
|
|
|
args_list = ['--no-run', '-P']
|
|
|
|
|
|
|
|
assert not validate_arguments(builddir, args_list, 'To publish user defined port mappings, the container must also be run')
|
|
|
|
|
|
|
|
|
2018-12-17 12:11:45 +00:00
|
|
|
def test_invalid_port_mapping_fail(temp_cwd):
|
2017-12-25 02:03:17 +00:00
|
|
|
"""
|
|
|
|
Test to check if r2d fails when an invalid port is specified in the port mapping
|
|
|
|
"""
|
|
|
|
# Specifying builddir here itself to simulate passing in a run command
|
|
|
|
# builddir passed in the function will be an argument for the run command
|
|
|
|
args_list = ['-p', '75000:80', builddir, 'ls']
|
|
|
|
|
2018-12-21 15:08:49 +00:00
|
|
|
assert not validate_arguments(builddir, args_list, 'Port specification')
|
2017-12-25 02:03:17 +00:00
|
|
|
|
|
|
|
|
2018-12-17 12:11:45 +00:00
|
|
|
def test_invalid_protocol_port_mapping_fail(temp_cwd):
|
2017-12-25 02:03:17 +00:00
|
|
|
"""
|
|
|
|
Test to check if r2d fails when an invalid protocol is specified in the port mapping
|
|
|
|
"""
|
|
|
|
# Specifying builddir here itself to simulate passing in a run command
|
|
|
|
# builddir passed in the function will be an argument for the run command
|
|
|
|
args_list = ['-p', '80/tpc:8000', builddir, 'ls']
|
|
|
|
|
2018-12-21 15:08:49 +00:00
|
|
|
assert not validate_arguments(builddir, args_list, 'Port specification')
|
2017-12-25 02:03:17 +00:00
|
|
|
|
|
|
|
|
2018-12-17 12:11:45 +00:00
|
|
|
def test_invalid_container_port_protocol_mapping_fail(temp_cwd):
|
2017-12-25 02:03:17 +00:00
|
|
|
"""
|
|
|
|
Test to check if r2d fails when an invalid protocol is specified in the container port in port mapping
|
|
|
|
"""
|
|
|
|
# Specifying builddir here itself to simulate passing in a run command
|
|
|
|
# builddir passed in the function will be an argument for the run command
|
|
|
|
args_list = ['-p', '80:8000/upd', builddir, 'ls']
|
|
|
|
|
2018-12-21 15:08:49 +00:00
|
|
|
assert not validate_arguments(builddir, args_list, 'Port specification')
|
2018-01-10 02:39:07 +00:00
|
|
|
|
|
|
|
|
2018-12-17 12:11:45 +00:00
|
|
|
@pytest.mark.xfail(reason="Regression in new arg parsing")
|
|
|
|
def test_docker_handle_fail(temp_cwd):
|
2018-01-10 02:39:07 +00:00
|
|
|
"""
|
|
|
|
Test to check if r2d fails with minimal error message on not being able to connect to docker daemon
|
|
|
|
"""
|
|
|
|
args_list = []
|
|
|
|
|
2018-12-17 12:11:45 +00:00
|
|
|
assert not validate_arguments(
|
|
|
|
builddir,
|
|
|
|
args_list,
|
|
|
|
"Docker client initialization error. Check if docker is running on the host.",
|
|
|
|
disable_dockerd=True,
|
|
|
|
)
|
2018-01-10 02:39:07 +00:00
|
|
|
|
|
|
|
|
2018-12-17 12:11:45 +00:00
|
|
|
def test_docker_handle_debug_fail(temp_cwd):
|
2018-01-10 02:39:07 +00:00
|
|
|
"""
|
|
|
|
Test to check if r2d fails with stack trace on not being able to connect to docker daemon and debug enabled
|
|
|
|
"""
|
|
|
|
args_list = ['--debug']
|
|
|
|
|
2018-12-17 12:11:45 +00:00
|
|
|
assert not validate_arguments(
|
|
|
|
builddir,
|
|
|
|
args_list,
|
|
|
|
"docker.errors.DockerException",
|
|
|
|
disable_dockerd=True,
|
|
|
|
)
|
2018-01-10 02:39:07 +00:00
|
|
|
|
|
|
|
|
2018-12-17 12:11:45 +00:00
|
|
|
def test_docker_no_build_success(temp_cwd):
|
2018-01-10 02:39:07 +00:00
|
|
|
"""
|
|
|
|
Test to check if r2d succeeds with --no-build argument with not being able to connect to docker daemon
|
|
|
|
"""
|
|
|
|
args_list = ['--no-build', '--no-run']
|
|
|
|
|
2018-12-17 12:11:45 +00:00
|
|
|
assert validate_arguments(builddir, args_list, disable_dockerd=True)
|