From 71da9f7828dd4b48a63455a5a1ee5cb8d4dac150 Mon Sep 17 00:00:00 2001 From: Michael Levy Date: Wed, 24 Feb 2021 09:48:57 +0200 Subject: [PATCH] NO-ISSUE support publish ports and set default net to bridge in docker desktop --- README.md | 11 ++ skipper/cli.py | 50 ++++++++- skipper/runner.py | 43 ++++++-- tests/test_cli.py | 211 +++++++++++++++++++++++++++++++----- tests/test_runner.py | 148 +++++++++++++------------ tests/test_runner_podman.py | 79 +++++++------- tests/test_utils.py | 5 +- 7 files changed, 395 insertions(+), 152 deletions(-) diff --git a/README.md b/README.md index 523e10f..1c30af1 100644 --- a/README.md +++ b/README.md @@ -169,6 +169,17 @@ Using the above configuration file, we now can run a simplified version of the m skipper make tests ``` +### Published ports +For `shell`, `run` & `make` commands: +By default, when you run skipper on a linux machine it will use the host network and no mapping required. +For macos and windows machines where the host network is unsupported or for a custom network, you can publish a port and make it available to services outside of the container using the --publish or -p flag. + +```` +skipper make -p 123:123 tests +skipper make -p 123-130:123-130 tests +```` + + ### Environment variables: For `shell`, `run` & `make` commands: You can use `-e` in order to pass environment variables to the container. diff --git a/skipper/cli.py b/skipper/cli.py index b4ebb68..1457e79 100644 --- a/skipper/cli.py +++ b/skipper/cli.py @@ -5,6 +5,7 @@ import os.path import sys +from re import compile as compile_expression import click import six import tabulate @@ -18,12 +19,47 @@ DOCKER_TAG_FOR_CACHE = "cache" +def _validate_publish(ctx, param, value): + # pylint: disable=unused-argument + if value: + matcher = compile_expression(r'^((\d+)(-(\d+))?):((\d+)(-(\d+))?)$') + + for port_mapping in value: + _validate_port(matcher, port_mapping) + + return value + + +def _validate_port(matcher, port_forwarding): + match = matcher.match(port_forwarding) + if not match: + raise click.BadParameter("Publish need to be in format port:port or port-port:port-port") + + host_port_start_range, host_port_end_range, container_port_start_range, container_port_end_range = match.group(2, 4, 6, 8) + _validate_port_out_of_range(host_port_start_range) + _validate_port_out_of_range(host_port_end_range) + _validate_port_out_of_range(container_port_start_range) + _validate_port_out_of_range(container_port_end_range) + _validate_port_range(host_port_start_range, host_port_end_range) + _validate_port_range(container_port_start_range, container_port_end_range) + + +def _validate_port_range(start, end): + if start and end and end < start: + raise click.BadParameter("Invalid port range: {0} should be bigger than {1}".format(start, end)) + + +def _validate_port_out_of_range(port): + if port and not 1 <= int(port) <= 65535: + raise click.BadParameter("Invalid port number: port {0} is out of range".format(port)) + + @click.group() @click.option('-v', '--verbose', help='Increase verbosity', is_flag=True, default=False) @click.option('--registry', help='URL of the docker registry') @click.option('--build-container-image', help='Image to use as build container') @click.option('--build-container-tag', help='Tag of the build container') -@click.option('--build-container-net', help='Network to connect the build container', default='host') +@click.option('--build-container-net', help='Network to connect the build container') @click.option('--env-file', help='Environment variable file to load') @click.pass_context def cli(ctx, registry, build_container_image, build_container_tag, build_container_net, verbose, env_file): @@ -209,9 +245,10 @@ def rmi(ctx, remote, image, tag): @click.option('-n', '--name', help='Container name', default=None) @click.option('-e', '--env', multiple=True, help='Environment variables to pass the container') @click.option('-c', '--cache', help='Use cache image', is_flag=True, default=False, envvar='SKIPPER_USE_CACHE_IMAGE') +@click.option('-p', '--publish', multiple=True, help="Publish a port", callback=_validate_publish) @click.argument('command', nargs=-1, type=click.UNPROCESSED, required=True) @click.pass_context -def run(ctx, interactive, name, env, cache, command): +def run(ctx, interactive, name, env, publish, cache, command): """ Run arbitrary commands """ @@ -231,6 +268,7 @@ def run(ctx, interactive, name, env, cache, command): interactive=interactive, name=name, net=ctx.obj['build_container_net'], + publish=publish, volumes=ctx.obj.get('volumes'), workdir=ctx.obj.get('workdir'), use_cache=cache, @@ -244,9 +282,10 @@ def run(ctx, interactive, name, env, cache, command): @click.option('-e', '--env', multiple=True, help='Environment variables to pass the container') @click.option('-f', 'makefile', help='Makefile to use', default='Makefile') @click.option('-c', '--cache', help='Use cache image', is_flag=True, default=False, envvar='SKIPPER_USE_CACHE_IMAGE') +@click.option('-p', '--publish', multiple=True, help="Publish a port", callback=_validate_publish) @click.argument('make_params', nargs=-1, type=click.UNPROCESSED, required=False) @click.pass_context -def make(ctx, interactive, name, env, makefile, cache, make_params): +def make(ctx, interactive, name, env, makefile, cache, publish, make_params): """ Execute makefile target(s) """ @@ -267,6 +306,7 @@ def make(ctx, interactive, name, env, makefile, cache, make_params): interactive=interactive, name=name, net=ctx.obj['build_container_net'], + publish=publish, volumes=ctx.obj.get('volumes'), workdir=ctx.obj.get('workdir'), use_cache=cache, @@ -278,8 +318,9 @@ def make(ctx, interactive, name, env, makefile, cache, make_params): @click.option('-e', '--env', multiple=True, help='Environment variables to pass the container') @click.option('-n', '--name', help='Container name', default=None) @click.option('-c', '--cache', help='Use cache image', is_flag=True, default=False, envvar='SKIPPER_USE_CACHE_IMAGE') +@click.option('-p', '--publish', multiple=True, help="Publish a port", callback=_validate_publish) @click.pass_context -def shell(ctx, env, name, cache): +def shell(ctx, env, name, cache, publish): """ Start a shell """ @@ -299,6 +340,7 @@ def shell(ctx, env, name, cache): interactive=True, name=name, net=ctx.obj['build_container_net'], + publish=publish, volumes=ctx.obj.get('volumes'), workdir=ctx.obj.get('workdir'), use_cache=cache, diff --git a/skipper/runner.py b/skipper/runner.py index d047854..bbb23a9 100644 --- a/skipper/runner.py +++ b/skipper/runner.py @@ -9,11 +9,21 @@ from skipper import utils +def get_default_net(): + # The host networking driver only works on Linux hosts, and is not supported on Docker Desktop for Mac, + # Docker Desktop for Windows, or Docker EE for Windows Server. + return 'host' if sys.platform != 'darwin' and sys.platform != 'win32' else 'bridge' + + # pylint: disable=too-many-arguments -def run(command, fqdn_image=None, environment=None, interactive=False, name=None, net='host', volumes=None, +def run(command, fqdn_image=None, environment=None, interactive=False, name=None, net=None, publish=(), volumes=None, workdir=None, use_cache=False, workspace=None, env_file=None): + + if not net: + net = get_default_net() + if fqdn_image is not None: - return _run_nested(fqdn_image, environment, command, interactive, name, net, volumes, + return _run_nested(fqdn_image, environment, command, interactive, name, net, publish, volumes, workdir, use_cache, workspace, env_file) return _run(command) @@ -31,8 +41,7 @@ def _run(cmd_args): # pylint: disable=too-many-locals # pylint: disable=too-many-arguments -def _run_nested(fqdn_image, environment, command, interactive, name, net, - volumes, workdir, use_cache, workspace, env_file): +def _run_nested(fqdn_image, environment, command, interactive, name, net, publish, volumes, workdir, use_cache, workspace, env_file): cwd = os.getcwd() if workspace is None: workspace = os.path.dirname(cwd) @@ -53,7 +62,7 @@ def _run_nested(fqdn_image, environment, command, interactive, name, net, cmd += ['--privileged'] - cmd += ['--net', net] + cmd = handle_networking(cmd, publish, net) if env_file: cmd += ['--env-file', env_file] @@ -80,10 +89,7 @@ def _run_nested(fqdn_image, environment, command, interactive, name, net, cmd = handle_volumes_bind_mount(cmd, homedir, volumes, workspace) - if workdir: - cmd += ['-w', workdir] - else: - cmd += ['-w', '%(workdir)s' % dict(workdir=cwd)] + cmd = handle_workdir(cmd, cwd, workdir) cmd += ['--entrypoint', '/opt/skipper/skipper-entrypoint.sh'] cmd += [fqdn_image] @@ -95,6 +101,25 @@ def _run_nested(fqdn_image, environment, command, interactive, name, net, return ret +def handle_workdir(cmd, cwd, workdir): + if workdir: + cmd += ['-w', workdir] + else: + cmd += ['-w', '%(workdir)s' % dict(workdir=cwd)] + return cmd + + +def handle_networking(cmd, publish, net): + if publish: + for port_mapping in publish: + cmd += ['-p', port_mapping] + + if net is not None: + cmd += ['--net', net] + + return cmd + + def handle_volumes_bind_mount(docker_cmd, homedir, volumes, workspace): volumes = volumes or [] volumes.extend(['%(homedir)s/.netrc:%(homedir)s/.netrc:ro' % dict(homedir=homedir), diff --git a/tests/test_cli.py b/tests/test_cli.py index 39d6260..14499dc 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,7 +1,7 @@ -import mock import os -from six.moves import http_client import unittest +import mock +from six.moves import http_client import click import six from click import testing @@ -303,7 +303,7 @@ def test_make_without_build_container_tag_with_context(self, skipper_runner_run_ 'Dockerfile.build-container-image', SKIPPER_CONF_CONTAINER_CONTEXT]), mock.call(['make'] + make_params, fqdn_image='build-container-image', environment=[], - interactive=False, name=None, net='host', volumes=None, workdir=None, + interactive=False, name=None, net=None, publish=(), volumes=None, workdir=None, use_cache=False, workspace=None, env_file=None), ] skipper_runner_run_mock.assert_has_calls(expected_commands) @@ -1039,7 +1039,8 @@ def test_run_with_existing_local_build_container(self, skipper_runner_run_mock): ) expected_image_name = 'build-container-image:build-container-tag' skipper_runner_run_mock.assert_called_once_with(command, fqdn_image=expected_image_name, environment=[], - interactive=False, name=None, net='host', volumes=None, + interactive=False, name=None, net=None, publish=(), + volumes=None, workdir=None, workspace=None, use_cache=False, env_file=None) @@ -1065,7 +1066,8 @@ def test_run_with_existing_remote_build_container(self, skipper_runner_run_mock, ) expected_image_name = 'registry.io:5000/build-container-image:build-container-tag' skipper_runner_run_mock.assert_called_once_with(command, fqdn_image=expected_image_name, environment=[], - interactive=False, name=None, net='host', volumes=None, + interactive=False, name=None, net=None, publish=(), + volumes=None, workdir=None, workspace=None, use_cache=False, env_file=None) @@ -1105,7 +1107,8 @@ def test_run_with_defaults_from_config_file(self, skipper_runner_run_mock): ) expected_fqdn_image = 'skipper-conf-build-container-image:skipper-conf-build-container-tag' skipper_runner_run_mock.assert_called_once_with(command, fqdn_image=expected_fqdn_image, environment=[], - interactive=False, name=None, net='host', volumes=None, + interactive=False, name=None, net=None, publish=(), + volumes=None, workdir=None, workspace=None, use_cache=False, env_file=None) @@ -1126,7 +1129,8 @@ def test_run_with_defaults_and_env_from_config_file(self, skipper_runner_run_moc env = ["%s=%s" % (key, value) for key, value in six.iteritems(CONFIG_ENV_EVALUATION)] expected_fqdn_image = 'skipper-conf-build-container-image:skipper-conf-build-container-tag' skipper_runner_run_mock.assert_called_once_with(command, fqdn_image=expected_fqdn_image, environment=env, - interactive=False, name=None, net='host', volumes=None, + interactive=False, name=None, net=None, publish=(), + volumes=None, workdir=None, workspace=None, use_cache=False, env_file=None) @@ -1154,7 +1158,7 @@ def test_run_with_defaults_and_env_from_env_file( fqdn_image=expected_fqdn_image, environment=[], interactive=False, - name=None, net='host', + name=None, net=None, publish=(), volumes=None, workdir=None, workspace=None, @@ -1178,7 +1182,8 @@ def test_run_with_env_overriding_config_file(self, skipper_runner_run_mock): env = ["%s=%s" % (key, value) for key, value in six.iteritems(CONFIG_ENV_EVALUATION)] + ENV expected_fqdn_image = 'skipper-conf-build-container-image:skipper-conf-build-container-tag' skipper_runner_run_mock.assert_called_once_with(command, fqdn_image=expected_fqdn_image, environment=env, - interactive=False, name=None, net='host', volumes=None, + interactive=False, name=None, net=None, publish=(), + volumes=None, workdir=None, workspace=None, use_cache=False, env_file=None) @@ -1200,7 +1205,8 @@ def test_run_with_env_list(self, skipper_runner_run_mock): env = ['key1=value1'] + ENV expected_fqdn_image = 'skipper-conf-build-container-image:skipper-conf-build-container-tag' skipper_runner_run_mock.assert_called_once_with(command, fqdn_image=expected_fqdn_image, environment=env, - interactive=False, name=None, net='host', volumes=None, + interactive=False, name=None, net=None, publish=(), + volumes=None, workdir=None, workspace=None, use_cache=False, env_file=None) @@ -1222,7 +1228,8 @@ def test_run_with_env_list_get_from_env(self, skipper_runner_run_mock): env = ['key1=value1', 'key2=value2'] + ENV expected_fqdn_image = 'skipper-conf-build-container-image:skipper-conf-build-container-tag' skipper_runner_run_mock.assert_called_once_with(command, fqdn_image=expected_fqdn_image, environment=env, - interactive=False, name=None, net='host', volumes=None, + interactive=False, name=None, net=None, publish=(), + volumes=None, workdir=None, workspace=None, use_cache=False, env_file=None) @@ -1255,7 +1262,8 @@ def test_run_with_env(self, skipper_runner_run_mock): ) expected_fqdn_image = 'build-container-image:build-container-tag' skipper_runner_run_mock.assert_called_once_with(command, fqdn_image=expected_fqdn_image, environment=ENV, - interactive=False, name=None, net='host', volumes=None, + interactive=False, name=None, net=None, publish=(), + volumes=None, workdir=None, workspace=None, use_cache=False, env_file=None) @@ -1272,7 +1280,7 @@ def test_run_interactive_from_environment(self, skipper_runner_run_mock): ) expected_fqdn_image = 'build-container-image:build-container-tag' skipper_runner_run_mock.assert_called_once_with(command, fqdn_image=expected_fqdn_image, environment=[], - interactive=True, name=None, net='host', volumes=None, + interactive=True, name=None, net=None, publish=(), volumes=None, workdir=None, workspace=None, use_cache=False, env_file=None) del os.environ['SKIPPER_INTERACTIVE'] @@ -1290,7 +1298,8 @@ def test_run_non_interactive_from_environment(self, skipper_runner_run_mock): ) expected_fqdn_image = 'build-container-image:build-container-tag' skipper_runner_run_mock.assert_called_once_with(command, fqdn_image=expected_fqdn_image, environment=[], - interactive=False, name=None, net='host', volumes=None, + interactive=False, name=None, net=None, publish=(), + volumes=None, workdir=None, workspace=None, use_cache=False, env_file=None) del os.environ['SKIPPER_INTERACTIVE'] @@ -1307,7 +1316,7 @@ def test_run_non_interactive(self, skipper_runner_run_mock): ) expected_fqdn_image = 'build-container-image:build-container-tag' skipper_runner_run_mock.assert_called_once_with(command, fqdn_image=expected_fqdn_image, environment=[], - interactive=True, name=None, net='host', volumes=None, + interactive=True, name=None, net=None, publish=(), volumes=None, workdir=None, workspace=None, use_cache=False, env_file=None) @@ -1326,7 +1335,7 @@ def test_run_without_build_container_tag(self, skipper_runner_run_mock): mock.call(['build', '--network=host', '-t', 'build-container-image', '-f', 'Dockerfile.build-container-image', '.']), mock.call(command, fqdn_image='build-container-image', environment=[], - interactive=False, name=None, net='host', volumes=None, workdir=None, workspace=None, + interactive=False, name=None, net=None, publish=(), volumes=None, workdir=None, workspace=None, use_cache=False, env_file=None), ] skipper_runner_run_mock.assert_has_calls(expected_commands) @@ -1344,7 +1353,7 @@ def test_run_without_build_container_tag_cached(self, skipper_runner_run_mock): ) expected_commands = [ mock.call(command, fqdn_image='build-container-image', environment=[], - interactive=False, name=None, net='host', volumes=None, workdir=None, workspace=None, + interactive=False, name=None, net=None, publish=(), volumes=None, workdir=None, workspace=None, use_cache=True, env_file=None), ] skipper_runner_run_mock.assert_has_calls(expected_commands) @@ -1364,9 +1373,145 @@ def test_run_with_non_default_net(self, skipper_runner_run_mock): expected_fqdn_image = 'build-container-image:build-container-tag' skipper_runner_run_mock.assert_called_once_with(command, fqdn_image=expected_fqdn_image, environment=[], interactive=False, name=None, net='non-default-net', - volumes=None, workdir=None, workspace=None, + publish=(), volumes=None, workdir=None, workspace=None, use_cache=False, env_file=None) + @mock.patch('subprocess.check_output', mock.MagicMock(autospec=True, return_value='1234567')) + @mock.patch('skipper.runner.run', autospec=True) + def test_run_with_publish_single_port(self, skipper_runner_run_mock): + global_params = self.global_params + global_params += ['--build-container-net', 'non-default-net'] + + makefile = 'Makefile' + target = 'all' + make_params = ['-p', '123:123', '-f', makefile, target] + + self._invoke_cli( + global_params=global_params, + subcmd='make', + subcmd_params=make_params + ) + + expected_command = ['make', '-f', makefile, target] + + expected_fqdn_image = 'build-container-image:build-container-tag' + skipper_runner_run_mock.assert_called_once_with(expected_command, fqdn_image=expected_fqdn_image, + environment=[], + interactive=False, name=None, net=u'non-default-net', + publish=(u'123:123',), volumes=None, + workdir=None, workspace=None, use_cache=False, + env_file=None) + + @mock.patch('subprocess.check_output', mock.MagicMock(autospec=True, return_value='1234567')) + @mock.patch('skipper.runner.run', autospec=True) + def test_run_with_publish_multiple_ports(self, skipper_runner_run_mock): + global_params = self.global_params + global_params += ['--build-container-net', 'non-default-net'] + + makefile = 'Makefile' + target = 'all' + make_params = ['-p', '123:123', '-p', '12:12', '-f', makefile, target] + + self._invoke_cli( + global_params=global_params, + subcmd='make', + subcmd_params=make_params + ) + + expected_command = ['make', '-f', makefile, target] + + expected_fqdn_image = 'build-container-image:build-container-tag' + skipper_runner_run_mock.assert_called_once_with(expected_command, fqdn_image=expected_fqdn_image, + environment=[], + interactive=False, name=None, net=u'non-default-net', + publish=('123:123', '12:12'), volumes=None, + workdir=None, workspace=None, use_cache=False, + env_file=None) + + @mock.patch('subprocess.check_output', mock.MagicMock(autospec=True, return_value='1234567')) + @mock.patch('skipper.runner.run', autospec=True) + def test_run_with_publish_port_range(self, skipper_runner_run_mock): + global_params = self.global_params + global_params += ['--build-container-net', 'non-default-net'] + + makefile = 'Makefile' + target = 'all' + make_params = ['-p', '123:123', '-p', '12-13:12-13', '-f', makefile, target] + + self._invoke_cli( + global_params=global_params, + subcmd='make', + subcmd_params=make_params + ) + + expected_command = ['make', '-f', makefile, target] + + expected_fqdn_image = 'build-container-image:build-container-tag' + skipper_runner_run_mock.assert_called_once_with(expected_command, fqdn_image=expected_fqdn_image, + environment=[], + interactive=False, name=None, net=u'non-default-net', + publish=('123:123', '12-13:12-13'), volumes=None, + workdir=None, workspace=None, use_cache=False, + env_file=None) + + def test_run_with_publish_textual_port(self): + global_params = self.global_params + global_params += ['--build-container-net', 'non-default-net'] + + makefile = 'Makefile' + target = 'all' + make_params = ['-p', '123:a1', '-p', '12:12', '-f', makefile, target] + + result = self._invoke_cli(global_params=global_params, subcmd='make', subcmd_params=make_params) + self.assertIsInstance(result.exception, click.BadParameter) + self.assertEqual("Publish need to be in format port:port or port-port:port-port", result.exception.message) + self.assertEqual(-1, result.exit_code) + + def test_run_with_publish_textual_port_range(self): + global_params = self.global_params + global_params += ['--build-container-net', 'non-default-net'] + + makefile = 'Makefile' + target = 'all' + make_params = ['-p', '123-1:1-a1', '-p', '12:12', '-f', makefile, target] + + result = self._invoke_cli(global_params=global_params, subcmd='make', subcmd_params=make_params) + self.assertIsInstance(result.exception, click.BadParameter) + self.assertEqual("Publish need to be in format port:port or port-port:port-port", result.exception.message) + self.assertEqual(-1, result.exit_code) + + def test_run_with_invalid_port_range(self): + global_params = self.global_params + global_params += ['--build-container-net', 'non-default-net'] + + makefile = 'Makefile' + target = 'all' + make_params = ['-p', '15-25:25-15', '-p', '12:12', '-f', makefile, target] + + result = self._invoke_cli(global_params=global_params, subcmd='make', subcmd_params=make_params) + self.assertIsInstance(result.exception, click.BadParameter) + self.assertEqual("Invalid port range: 25 should be bigger than 15", result.exception.message) + self.assertEqual(-1, result.exit_code) + + make_params = ['-p', '25-15:15-25', '-p', '12:12', '-f', makefile, target] + result = self._invoke_cli(global_params=global_params, subcmd='make', subcmd_params=make_params) + self.assertIsInstance(result.exception, click.BadParameter) + self.assertEqual("Invalid port range: 25 should be bigger than 15", result.exception.message) + self.assertEqual(-1, result.exit_code) + + def test_run_with_publish_out_of_range_port(self): + global_params = self.global_params + global_params += ['--build-container-net', 'non-default-net'] + + makefile = 'Makefile' + target = 'all' + make_params = ['-p', '123:1', '-p', '12:121111111', '-f', makefile, target] + + result = self._invoke_cli(global_params=global_params, subcmd='make', subcmd_params=make_params) + self.assertIsInstance(result.exception, click.BadParameter) + self.assertEqual("Invalid port number: port 121111111 is out of range", result.exception.message) + self.assertEqual(-1, result.exit_code) + @mock.patch('__builtin__.open', mock.MagicMock(create=True)) @mock.patch('os.path.exists', mock.MagicMock(autospec=True, return_value=True)) @mock.patch('yaml.safe_load', mock.MagicMock(autospec=True, return_value=SKIPPER_CONF_WITH_VOLUMES)) @@ -1382,7 +1527,7 @@ def test_run_with_defaults_from_config_file_including_volumes(self, skipper_runn ) expected_fqdn_image = 'skipper-conf-build-container-image:skipper-conf-build-container-tag' skipper_runner_run_mock.assert_called_once_with(command, fqdn_image=expected_fqdn_image, environment=[], - interactive=False, name=None, net='host', + interactive=False, name=None, net=None, publish=(), volumes=['volume1', 'volume2'], workspace=None, workdir=None, use_cache=False, env_file=None) @@ -1401,7 +1546,8 @@ def test_run_with_defaults_from_config_file_including_workdir(self, skipper_runn ) expected_fqdn_image = 'skipper-conf-build-container-image:skipper-conf-build-container-tag' skipper_runner_run_mock.assert_called_once_with(command, fqdn_image=expected_fqdn_image, environment=[], - interactive=False, name=None, net='host', volumes=None, + interactive=False, name=None, net=None, publish=(), + volumes=None, workdir='test-workdir', workspace=None, use_cache=False, env_file=None) @@ -1420,7 +1566,8 @@ def test_run_with_defaults_from_config_file_including_workspace(self, skipper_ru ) expected_fqdn_image = 'skipper-conf-build-container-image:skipper-conf-build-container-tag' skipper_runner_run_mock.assert_called_once_with(command, fqdn_image=expected_fqdn_image, environment=[], - interactive=False, name=None, net='host', volumes=None, + interactive=False, name=None, net=None, publish=(), + volumes=None, workdir=None, workspace="/test/workspace", use_cache=False, env_file=None) @@ -1440,7 +1587,8 @@ def test_run_with_config_including_git_revision_with_uncommitted_changes(self, s ) expected_fqdn_image = 'skipper-conf-build-container-image:1234567' skipper_runner_run_mock.assert_called_once_with(command, fqdn_image=expected_fqdn_image, environment=[], - interactive=False, name=None, net='host', volumes=None, + interactive=False, name=None, net=None, publish=(), + volumes=None, workdir=None, use_cache=False, workspace=None, env_file=None) @@ -1460,7 +1608,8 @@ def test_run_with_config_including_git_revision_without_uncommitted_changes(self ) expected_fqdn_image = 'skipper-conf-build-container-image:1234567' skipper_runner_run_mock.assert_called_once_with(command, fqdn_image=expected_fqdn_image, environment=[], - interactive=False, name=None, net='host', volumes=None, + interactive=False, name=None, net=None, publish=(), + volumes=None, workdir=None, workspace=None, use_cache=False, env_file=None) @@ -1479,7 +1628,8 @@ def test_make(self, skipper_runner_run_mock): expected_fqdn_image = 'build-container-image:build-container-tag' skipper_runner_run_mock.assert_called_once_with(expected_command, fqdn_image=expected_fqdn_image, environment=[], - interactive=False, name=None, net='host', volumes=None, + interactive=False, name=None, net=None, publish=(), + volumes=None, workdir=None, workspace=None, use_cache=False, env_file=None) @@ -1494,7 +1644,8 @@ def test_make_with_default_params(self, skipper_runner_run_mock): expected_fqdn_image = 'build-container-image:build-container-tag' skipper_runner_run_mock.assert_called_once_with(expected_command, fqdn_image=expected_fqdn_image, environment=[], - interactive=False, name=None, net='host', volumes=None, + interactive=False, name=None, net=None, publish=(), + volumes=None, workdir=None, workspace=None, use_cache=False, env_file=None) @@ -1512,7 +1663,8 @@ def test_make_with_additional_make_params(self, skipper_runner_run_mock): expected_fqdn_image = 'build-container-image:build-container-tag' skipper_runner_run_mock.assert_called_once_with(expected_command, fqdn_image=expected_fqdn_image, environment=[], - interactive=False, name=None, net='host', volumes=None, + interactive=False, name=None, net=None, publish=(), + volumes=None, workdir=None, workspace=None, use_cache=False, env_file=None) @@ -1534,7 +1686,8 @@ def test_make_with_defaults_from_config_file(self, skipper_runner_run_mock): expected_fqdn_image = 'skipper-conf-build-container-image:skipper-conf-build-container-tag' skipper_runner_run_mock.assert_called_once_with(expected_command, fqdn_image=expected_fqdn_image, environment=[], - interactive=False, name=None, net='host', volumes=None, + interactive=False, name=None, net=None, publish=(), + volumes=None, workdir=None, workspace=None, use_cache=False, env_file=None) @@ -1554,7 +1707,7 @@ def test_make_without_build_container_tag(self, skipper_runner_run_mock): mock.call(['build', '--network=host', '-t', 'build-container-image', '-f', 'Dockerfile.build-container-image', '.']), mock.call(['make'] + make_params, fqdn_image='build-container-image', environment=[], - interactive=False, name=None, net='host', volumes=None, workdir=None, workspace=None, + interactive=False, name=None, net=None, publish=(), volumes=None, workdir=None, workspace=None, use_cache=False, env_file=None), ] skipper_runner_run_mock.assert_has_calls(expected_commands) @@ -1568,7 +1721,7 @@ def test_shell(self, skipper_runner_run_mock): ) expected_fqdn_image = 'build-container-image:build-container-tag' skipper_runner_run_mock.assert_called_once_with(['bash'], fqdn_image=expected_fqdn_image, environment=[], - interactive=True, name=None, net='host', volumes=None, + interactive=True, name=None, net=None, publish=(), volumes=None, workdir=None, workspace=None, use_cache=False, env_file=None) diff --git a/tests/test_runner.py b/tests/test_runner.py index 2461d9e..34510e7 100644 --- a/tests/test_runner.py +++ b/tests/test_runner.py @@ -1,9 +1,10 @@ -import mock +import sys import os import unittest +import mock from skipper import utils from skipper import runner - +from skipper.runner import get_default_net USER_ID = 1000 GROUP_ID = 2000 @@ -22,6 +23,14 @@ ENV_FILE_PATH = '/home/envfile.env' +def get_volume_mapping(volume_mapping): + if sys.platform == 'darwin': + if volume_mapping.startswith('/etc/') or volume_mapping.startswith('/var/lib/'): + return '/private' + volume_mapping + + return volume_mapping + + class TestRunner(unittest.TestCase): NET_LS = 'NETWORK ID NAME DRIVER SCOPE\n' \ @@ -73,19 +82,19 @@ def test_run_simple_command_nested_network_exist(self, resource_filename_mock, c '-t', '-e', 'KEEP_CONTAINERS=True', '--privileged', - '--net', 'host', + '--net', get_default_net(), '-e', 'SKIPPER_USERNAME=testuser', '-e', 'SKIPPER_UID=%(user_uid)s' % dict(user_uid=USER_ID), '-e', 'HOME=%(homedir)s' % dict(homedir=HOME_DIR), '-e', 'SKIPPER_DOCKER_GID=978', - '-v', '%(homedir)s/.netrc:%(homedir)s/.netrc:ro' % dict(homedir=HOME_DIR), - '-v', '%(homedir)s/.gitconfig:%(homedir)s/.gitconfig:ro' % dict(homedir=HOME_DIR), - '-v', '%(homedir)s/.docker/config.json:%(homedir)s/.docker/config.json:ro' % dict(homedir=HOME_DIR), - '-v', '/etc/docker:/etc/docker:ro', - '-v', '%(workdir)s:%(workdir)s:rw' % dict(workdir=WORKDIR), - '-v', '/var/run/docker.sock:/var/run/docker.sock:rw', - '-v', 'entrypoint.sh:/opt/skipper/skipper-entrypoint.sh', - '-v', '/var/lib/osmosis:/var/lib/osmosis:rw', + '-v', get_volume_mapping('%(homedir)s/.netrc:%(homedir)s/.netrc:ro' % dict(homedir=HOME_DIR)), + '-v', get_volume_mapping('%(homedir)s/.gitconfig:%(homedir)s/.gitconfig:ro' % dict(homedir=HOME_DIR)), + '-v', get_volume_mapping('%(homedir)s/.docker/config.json:%(homedir)s/.docker/config.json:ro' % dict(homedir=HOME_DIR)), + '-v', get_volume_mapping('/etc/docker:/etc/docker:ro'), + '-v', get_volume_mapping('%(workdir)s:%(workdir)s:rw' % dict(workdir=WORKDIR)), + '-v', get_volume_mapping('/var/run/docker.sock:/var/run/docker.sock:rw'), + '-v', get_volume_mapping('entrypoint.sh:/opt/skipper/skipper-entrypoint.sh'), + '-v', get_volume_mapping('/var/lib/osmosis:/var/lib/osmosis:rw'), '-w', PROJECT_DIR, '--entrypoint', '/opt/skipper/skipper-entrypoint.sh', FQDN_IMAGE, @@ -117,19 +126,19 @@ def test_run_simple_command_nested_network_not_exist(self, resource_filename_moc '-t', '-e', 'KEEP_CONTAINERS=True', '--privileged', - '--net', 'host', + '--net', get_default_net(), '-e', 'SKIPPER_USERNAME=testuser', '-e', 'SKIPPER_UID=%(user_uid)s' % dict(user_uid=USER_ID), '-e', 'HOME=%(homedir)s' % dict(homedir=HOME_DIR), '-e', 'SKIPPER_DOCKER_GID=978', - '-v', '%(homedir)s/.netrc:%(homedir)s/.netrc:ro' % dict(homedir=HOME_DIR), - '-v', '%(homedir)s/.gitconfig:%(homedir)s/.gitconfig:ro' % dict(homedir=HOME_DIR), - '-v', '%(homedir)s/.docker/config.json:%(homedir)s/.docker/config.json:ro' % dict(homedir=HOME_DIR), - '-v', '/etc/docker:/etc/docker:ro', - '-v', '%(workdir)s:%(workdir)s:rw' % dict(workdir=WORKDIR), - '-v', '/var/run/docker.sock:/var/run/docker.sock:rw', - '-v', 'entrypoint.sh:/opt/skipper/skipper-entrypoint.sh', - '-v', '/var/lib/osmosis:/var/lib/osmosis:rw', + '-v', get_volume_mapping('%(homedir)s/.netrc:%(homedir)s/.netrc:ro' % dict(homedir=HOME_DIR)), + '-v', get_volume_mapping('%(homedir)s/.gitconfig:%(homedir)s/.gitconfig:ro' % dict(homedir=HOME_DIR)), + '-v', get_volume_mapping('%(homedir)s/.docker/config.json:%(homedir)s/.docker/config.json:ro' % dict(homedir=HOME_DIR)), + '-v', get_volume_mapping('/etc/docker:/etc/docker:ro'), + '-v', get_volume_mapping('%(workdir)s:%(workdir)s:rw' % dict(workdir=WORKDIR)), + '-v', get_volume_mapping('/var/run/docker.sock:/var/run/docker.sock:rw'), + '-v', get_volume_mapping('entrypoint.sh:/opt/skipper/skipper-entrypoint.sh'), + '-v', get_volume_mapping('/var/lib/osmosis:/var/lib/osmosis:rw'), '-w', PROJECT_DIR, '--entrypoint', '/opt/skipper/skipper-entrypoint.sh', FQDN_IMAGE, @@ -160,21 +169,21 @@ def test_run_simple_command_nested_with_env(self, resource_filename_mock, check_ '-t', '-e', 'KEEP_CONTAINERS=True', '--privileged', - '--net', 'host', + '--net', get_default_net(), '-e', 'KEY1=VAL1', '-e', 'KEY2=VAL2', '-e', 'SKIPPER_USERNAME=testuser', '-e', 'SKIPPER_UID=%(user_uid)s' % dict(user_uid=USER_ID), '-e', 'HOME=%(homedir)s' % dict(homedir=HOME_DIR), '-e', 'SKIPPER_DOCKER_GID=978', - '-v', '%(homedir)s/.netrc:%(homedir)s/.netrc:ro' % dict(homedir=HOME_DIR), - '-v', '%(homedir)s/.gitconfig:%(homedir)s/.gitconfig:ro' % dict(homedir=HOME_DIR), - '-v', '%(homedir)s/.docker/config.json:%(homedir)s/.docker/config.json:ro' % dict(homedir=HOME_DIR), - '-v', '/etc/docker:/etc/docker:ro', - '-v', '%(workdir)s:%(workdir)s:rw' % dict(workdir=WORKDIR), - '-v', '/var/run/docker.sock:/var/run/docker.sock:rw', - '-v', 'entrypoint.sh:/opt/skipper/skipper-entrypoint.sh', - '-v', '/var/lib/osmosis:/var/lib/osmosis:rw', + '-v', get_volume_mapping('%(homedir)s/.netrc:%(homedir)s/.netrc:ro' % dict(homedir=HOME_DIR)), + '-v', get_volume_mapping('%(homedir)s/.gitconfig:%(homedir)s/.gitconfig:ro' % dict(homedir=HOME_DIR)), + '-v', get_volume_mapping('%(homedir)s/.docker/config.json:%(homedir)s/.docker/config.json:ro' % dict(homedir=HOME_DIR)), + '-v', get_volume_mapping('/etc/docker:/etc/docker:ro'), + '-v', get_volume_mapping('%(workdir)s:%(workdir)s:rw' % dict(workdir=WORKDIR)), + '-v', get_volume_mapping('/var/run/docker.sock:/var/run/docker.sock:rw'), + '-v', get_volume_mapping('entrypoint.sh:/opt/skipper/skipper-entrypoint.sh'), + '-v', get_volume_mapping('/var/lib/osmosis:/var/lib/osmosis:rw'), '-w', PROJECT_DIR, '--entrypoint', '/opt/skipper/skipper-entrypoint.sh', FQDN_IMAGE, @@ -213,23 +222,23 @@ def test_run_simple_command_nested_with_env_file( '-t', '-e', 'KEEP_CONTAINERS=True', '--privileged', - '--net', 'host', + '--net', get_default_net(), '--env-file', ENV_FILE_PATH, '-e', 'SKIPPER_USERNAME=testuser', '-e', 'SKIPPER_UID=%(user_uid)s' % dict(user_uid=USER_ID), '-e', 'HOME=%(homedir)s' % dict(homedir=HOME_DIR), '-e', 'SKIPPER_DOCKER_GID=978', - '-v', '%(homedir)s/.netrc:%(homedir)s/.netrc:ro' % dict( - homedir=HOME_DIR), - '-v', '%(homedir)s/.gitconfig:%(homedir)s/.gitconfig:ro' % dict( - homedir=HOME_DIR), - '-v', '%(homedir)s/.docker/config.json:%(homedir)s/.docker/config.json:ro' % dict( - homedir=HOME_DIR), - '-v', '/etc/docker:/etc/docker:ro', - '-v', '%(workdir)s:%(workdir)s:rw' % dict(workdir=WORKDIR), - '-v', '/var/run/docker.sock:/var/run/docker.sock:rw', - '-v', 'entrypoint.sh:/opt/skipper/skipper-entrypoint.sh', - '-v', '/var/lib/osmosis:/var/lib/osmosis:rw', + '-v', get_volume_mapping('%(homedir)s/.netrc:%(homedir)s/.netrc:ro' % dict( + homedir=HOME_DIR)), + '-v', get_volume_mapping('%(homedir)s/.gitconfig:%(homedir)s/.gitconfig:ro' % dict( + homedir=HOME_DIR)), + '-v', get_volume_mapping('%(homedir)s/.docker/config.json:%(homedir)s/.docker/config.json:ro' % dict( + homedir=HOME_DIR)), + '-v', get_volume_mapping('/etc/docker:/etc/docker:ro'), + '-v', get_volume_mapping('%(workdir)s:%(workdir)s:rw' % dict(workdir=WORKDIR)), + '-v', get_volume_mapping('/var/run/docker.sock:/var/run/docker.sock:rw'), + '-v', get_volume_mapping('entrypoint.sh:/opt/skipper/skipper-entrypoint.sh'), + '-v', get_volume_mapping('/var/lib/osmosis:/var/lib/osmosis:rw'), '-w', PROJECT_DIR, '--entrypoint', '/opt/skipper/skipper-entrypoint.sh', FQDN_IMAGE, @@ -256,6 +265,7 @@ def test_run_simple_command_nested_interactive(self, resource_filename_mock, os_getuid_mock.return_value = USER_ID command = ['pwd'] runner.run(command, FQDN_IMAGE, interactive=True) + expected_nested_command = [ 'docker', 'run', '-i', @@ -263,19 +273,19 @@ def test_run_simple_command_nested_interactive(self, resource_filename_mock, '-t', '-e', 'KEEP_CONTAINERS=True', '--privileged', - '--net', 'host', + '--net', get_default_net(), '-e', 'SKIPPER_USERNAME=testuser', '-e', 'SKIPPER_UID=%(user_uid)s' % dict(user_uid=USER_ID), '-e', 'HOME=%(homedir)s' % dict(homedir=HOME_DIR), '-e', 'SKIPPER_DOCKER_GID=978', - '-v', '%(homedir)s/.netrc:%(homedir)s/.netrc:ro' % dict(homedir=HOME_DIR), - '-v', '%(homedir)s/.gitconfig:%(homedir)s/.gitconfig:ro' % dict(homedir=HOME_DIR), - '-v', '%(homedir)s/.docker/config.json:%(homedir)s/.docker/config.json:ro' % dict(homedir=HOME_DIR), - '-v', '/etc/docker:/etc/docker:ro', - '-v', '%(workdir)s:%(workdir)s:rw' % dict(workdir=WORKDIR), - '-v', '/var/run/docker.sock:/var/run/docker.sock:rw', - '-v', 'entrypoint.sh:/opt/skipper/skipper-entrypoint.sh', - '-v', '/var/lib/osmosis:/var/lib/osmosis:rw', + '-v', get_volume_mapping('%(homedir)s/.netrc:%(homedir)s/.netrc:ro' % dict(homedir=HOME_DIR)), + '-v', get_volume_mapping('%(homedir)s/.gitconfig:%(homedir)s/.gitconfig:ro' % dict(homedir=HOME_DIR)), + '-v', get_volume_mapping('%(homedir)s/.docker/config.json:%(homedir)s/.docker/config.json:ro' % dict(homedir=HOME_DIR)), + '-v', get_volume_mapping('/etc/docker:/etc/docker:ro'), + '-v', get_volume_mapping('%(workdir)s:%(workdir)s:rw' % dict(workdir=WORKDIR)), + '-v', get_volume_mapping('/var/run/docker.sock:/var/run/docker.sock:rw'), + '-v', get_volume_mapping('entrypoint.sh:/opt/skipper/skipper-entrypoint.sh'), + '-v', get_volume_mapping('/var/lib/osmosis:/var/lib/osmosis:rw'), '-w', PROJECT_DIR, '--entrypoint', '/opt/skipper/skipper-entrypoint.sh', FQDN_IMAGE, @@ -306,19 +316,19 @@ def test_run_complex_command_nested(self, resource_filename_mock, check_output_m '-t', '-e', 'KEEP_CONTAINERS=True', '--privileged', - '--net', 'host', + '--net', get_default_net(), '-e', 'SKIPPER_USERNAME=testuser', '-e', 'SKIPPER_UID=%(user_uid)s' % dict(user_uid=USER_ID), '-e', 'HOME=%(homedir)s' % dict(homedir=HOME_DIR), '-e', 'SKIPPER_DOCKER_GID=978', - '-v', '%(homedir)s/.netrc:%(homedir)s/.netrc:ro' % dict(homedir=HOME_DIR), - '-v', '%(homedir)s/.gitconfig:%(homedir)s/.gitconfig:ro' % dict(homedir=HOME_DIR), - '-v', '%(homedir)s/.docker/config.json:%(homedir)s/.docker/config.json:ro' % dict(homedir=HOME_DIR), - '-v', '/etc/docker:/etc/docker:ro', - '-v', '%(workdir)s:%(workdir)s:rw' % dict(workdir=WORKDIR), - '-v', '/var/run/docker.sock:/var/run/docker.sock:rw', - '-v', 'entrypoint.sh:/opt/skipper/skipper-entrypoint.sh', - '-v', '/var/lib/osmosis:/var/lib/osmosis:rw', + '-v', get_volume_mapping('%(homedir)s/.netrc:%(homedir)s/.netrc:ro' % dict(homedir=HOME_DIR)), + '-v', get_volume_mapping('%(homedir)s/.gitconfig:%(homedir)s/.gitconfig:ro' % dict(homedir=HOME_DIR)), + '-v', get_volume_mapping('%(homedir)s/.docker/config.json:%(homedir)s/.docker/config.json:ro' % dict(homedir=HOME_DIR)), + '-v', get_volume_mapping('/etc/docker:/etc/docker:ro'), + '-v', get_volume_mapping('%(workdir)s:%(workdir)s:rw' % dict(workdir=WORKDIR)), + '-v', get_volume_mapping('/var/run/docker.sock:/var/run/docker.sock:rw'), + '-v', get_volume_mapping('entrypoint.sh:/opt/skipper/skipper-entrypoint.sh'), + '-v', get_volume_mapping('/var/lib/osmosis:/var/lib/osmosis:rw'), '-w', PROJECT_DIR, '--entrypoint', '/opt/skipper/skipper-entrypoint.sh', FQDN_IMAGE, @@ -351,21 +361,21 @@ def test_run_complex_command_nested_with_env(self, resource_filename_mock, check '-t', '-e', 'KEEP_CONTAINERS=True', '--privileged', - '--net', 'host', + '--net', get_default_net(), '-e', 'KEY1=VAL1', '-e', 'KEY2=VAL2', '-e', 'SKIPPER_USERNAME=testuser', '-e', 'SKIPPER_UID=%(user_uid)s' % dict(user_uid=USER_ID), '-e', 'HOME=%(homedir)s' % dict(homedir=HOME_DIR), '-e', 'SKIPPER_DOCKER_GID=978', - '-v', '%(homedir)s/.netrc:%(homedir)s/.netrc:ro' % dict(homedir=HOME_DIR), - '-v', '%(homedir)s/.gitconfig:%(homedir)s/.gitconfig:ro' % dict(homedir=HOME_DIR), - '-v', '%(homedir)s/.docker/config.json:%(homedir)s/.docker/config.json:ro' % dict(homedir=HOME_DIR), - '-v', '/etc/docker:/etc/docker:ro', - '-v', '%(workdir)s:%(workdir)s:rw' % dict(workdir=WORKDIR), - '-v', '/var/run/docker.sock:/var/run/docker.sock:rw', - '-v', 'entrypoint.sh:/opt/skipper/skipper-entrypoint.sh', - '-v', '/var/lib/osmosis:/var/lib/osmosis:rw', + '-v', get_volume_mapping('%(homedir)s/.netrc:%(homedir)s/.netrc:ro' % dict(homedir=HOME_DIR)), + '-v', get_volume_mapping('%(homedir)s/.gitconfig:%(homedir)s/.gitconfig:ro' % dict(homedir=HOME_DIR)), + '-v', get_volume_mapping('%(homedir)s/.docker/config.json:%(homedir)s/.docker/config.json:ro' % dict(homedir=HOME_DIR)), + '-v', get_volume_mapping('/etc/docker:/etc/docker:ro'), + '-v', get_volume_mapping('%(workdir)s:%(workdir)s:rw' % dict(workdir=WORKDIR)), + '-v', get_volume_mapping('/var/run/docker.sock:/var/run/docker.sock:rw'), + '-v', get_volume_mapping('entrypoint.sh:/opt/skipper/skipper-entrypoint.sh'), + '-v', get_volume_mapping('/var/lib/osmosis:/var/lib/osmosis:rw'), '-w', PROJECT_DIR, '--entrypoint', '/opt/skipper/skipper-entrypoint.sh', FQDN_IMAGE, @@ -404,7 +414,7 @@ def test_run_complex_command_nested_with_special_case_verification(self, resourc '-t', '-e', 'KEEP_CONTAINERS=True', '--privileged', - '--net', 'host', + '--net', get_default_net(), '-e', 'KEY1=VAL1', '-e', 'KEY2=VAL2', '-e', 'SKIPPER_USERNAME=testuser', diff --git a/tests/test_runner_podman.py b/tests/test_runner_podman.py index b3a3574..bd612d8 100644 --- a/tests/test_runner_podman.py +++ b/tests/test_runner_podman.py @@ -1,9 +1,10 @@ -import mock import os import unittest +import mock from skipper import utils from skipper import runner - +from skipper.runner import get_default_net +from tests.test_runner import get_volume_mapping USER_ID = 1000 GROUP_ID = 2000 @@ -64,18 +65,18 @@ def test_run_simple_command_nested_network_exist(self, resource_filename_mock, c '-t', '-e', 'KEEP_CONTAINERS=True', '--privileged', - '--net', 'host', + '--net', get_default_net(), '-e', 'SKIPPER_USERNAME=testuser', '-e', 'SKIPPER_UID=%(user_uid)s' % dict(user_uid=USER_ID), '-e', 'HOME=%(homedir)s' % dict(homedir=HOME_DIR), - '-v', '%(homedir)s/.netrc:%(homedir)s/.netrc:ro' % dict(homedir=HOME_DIR), - '-v', '%(homedir)s/.gitconfig:%(homedir)s/.gitconfig:ro' % dict(homedir=HOME_DIR), - '-v', '%(homedir)s/.docker/config.json:%(homedir)s/.docker/config.json:ro' % dict(homedir=HOME_DIR), - '-v', '/etc/docker:/etc/docker:ro', - '-v', '%(workdir)s:%(workdir)s:rw,shared' % dict(workdir=WORKDIR), - '-v', 'entrypoint.sh:/opt/skipper/skipper-entrypoint.sh:rw', - '-v', '/var/run/docker.sock:/var/run/docker.sock:rw', - '-v', '/var/lib/osmosis:/var/lib/osmosis:rw', + '-v', get_volume_mapping('%(homedir)s/.netrc:%(homedir)s/.netrc:ro' % dict(homedir=HOME_DIR)), + '-v', get_volume_mapping('%(homedir)s/.gitconfig:%(homedir)s/.gitconfig:ro' % dict(homedir=HOME_DIR)), + '-v', get_volume_mapping('%(homedir)s/.docker/config.json:%(homedir)s/.docker/config.json:ro' % dict(homedir=HOME_DIR)), + '-v', get_volume_mapping('/etc/docker:/etc/docker:ro'), + '-v', get_volume_mapping('%(workdir)s:%(workdir)s:rw,shared' % dict(workdir=WORKDIR)), + '-v', get_volume_mapping('entrypoint.sh:/opt/skipper/skipper-entrypoint.sh:rw'), + '-v', get_volume_mapping('/var/run/docker.sock:/var/run/docker.sock:rw'), + '-v', get_volume_mapping('/var/lib/osmosis:/var/lib/osmosis:rw'), '-w', PROJECT_DIR, '--entrypoint', '/opt/skipper/skipper-entrypoint.sh', FQDN_IMAGE, @@ -103,18 +104,18 @@ def test_run_simple_command_nested_network_not_exist(self, resource_filename_moc '-t', '-e', 'KEEP_CONTAINERS=True', '--privileged', - '--net', 'host', + '--net', get_default_net(), '-e', 'SKIPPER_USERNAME=testuser', '-e', 'SKIPPER_UID=%(user_uid)s' % dict(user_uid=USER_ID), '-e', 'HOME=%(homedir)s' % dict(homedir=HOME_DIR), - '-v', '%(homedir)s/.netrc:%(homedir)s/.netrc:ro' % dict(homedir=HOME_DIR), - '-v', '%(homedir)s/.gitconfig:%(homedir)s/.gitconfig:ro' % dict(homedir=HOME_DIR), - '-v', '%(homedir)s/.docker/config.json:%(homedir)s/.docker/config.json:ro' % dict(homedir=HOME_DIR), - '-v', '/etc/docker:/etc/docker:ro', - '-v', '%(workdir)s:%(workdir)s:rw,shared' % dict(workdir=WORKDIR), - '-v', 'entrypoint.sh:/opt/skipper/skipper-entrypoint.sh:rw', - '-v', '/var/run/docker.sock:/var/run/docker.sock:rw', - '-v', '/var/lib/osmosis:/var/lib/osmosis:rw', + '-v', get_volume_mapping('%(homedir)s/.netrc:%(homedir)s/.netrc:ro' % dict(homedir=HOME_DIR)), + '-v', get_volume_mapping('%(homedir)s/.gitconfig:%(homedir)s/.gitconfig:ro' % dict(homedir=HOME_DIR)), + '-v', get_volume_mapping('%(homedir)s/.docker/config.json:%(homedir)s/.docker/config.json:ro' % dict(homedir=HOME_DIR)), + '-v', get_volume_mapping('/etc/docker:/etc/docker:ro'), + '-v', get_volume_mapping('%(workdir)s:%(workdir)s:rw,shared' % dict(workdir=WORKDIR)), + '-v', get_volume_mapping('entrypoint.sh:/opt/skipper/skipper-entrypoint.sh:rw'), + '-v', get_volume_mapping('/var/run/docker.sock:/var/run/docker.sock:rw'), + '-v', get_volume_mapping('/var/lib/osmosis:/var/lib/osmosis:rw'), '-w', PROJECT_DIR, '--entrypoint', '/opt/skipper/skipper-entrypoint.sh', FQDN_IMAGE, @@ -142,18 +143,18 @@ def test_run_complex_command_nested(self, resource_filename_mock, check_output_m '-t', '-e', 'KEEP_CONTAINERS=True', '--privileged', - '--net', 'host', + '--net', get_default_net(), '-e', 'SKIPPER_USERNAME=testuser', '-e', 'SKIPPER_UID=%(user_uid)s' % dict(user_uid=USER_ID), '-e', 'HOME=%(homedir)s' % dict(homedir=HOME_DIR), - '-v', '%(homedir)s/.netrc:%(homedir)s/.netrc:ro' % dict(homedir=HOME_DIR), - '-v', '%(homedir)s/.gitconfig:%(homedir)s/.gitconfig:ro' % dict(homedir=HOME_DIR), - '-v', '%(homedir)s/.docker/config.json:%(homedir)s/.docker/config.json:ro' % dict(homedir=HOME_DIR), - '-v', '/etc/docker:/etc/docker:ro', - '-v', '%(workdir)s:%(workdir)s:rw,shared' % dict(workdir=WORKDIR), - '-v', 'entrypoint.sh:/opt/skipper/skipper-entrypoint.sh:rw', - '-v', '/var/run/docker.sock:/var/run/docker.sock:rw', - '-v', '/var/lib/osmosis:/var/lib/osmosis:rw', + '-v', get_volume_mapping('%(homedir)s/.netrc:%(homedir)s/.netrc:ro' % dict(homedir=HOME_DIR)), + '-v', get_volume_mapping('%(homedir)s/.gitconfig:%(homedir)s/.gitconfig:ro' % dict(homedir=HOME_DIR)), + '-v', get_volume_mapping('%(homedir)s/.docker/config.json:%(homedir)s/.docker/config.json:ro' % dict(homedir=HOME_DIR)), + '-v', get_volume_mapping('/etc/docker:/etc/docker:ro'), + '-v', get_volume_mapping('%(workdir)s:%(workdir)s:rw,shared' % dict(workdir=WORKDIR)), + '-v', get_volume_mapping('entrypoint.sh:/opt/skipper/skipper-entrypoint.sh:rw'), + '-v', get_volume_mapping('/var/run/docker.sock:/var/run/docker.sock:rw'), + '-v', get_volume_mapping('/var/lib/osmosis:/var/lib/osmosis:rw'), '-w', PROJECT_DIR, '--entrypoint', '/opt/skipper/skipper-entrypoint.sh', FQDN_IMAGE, @@ -169,7 +170,7 @@ def test_run_complex_command_nested(self, resource_filename_mock, check_output_m @mock.patch('subprocess.Popen', autospec=False) @mock.patch('subprocess.check_output', autospec=False) @mock.patch('pkg_resources.resource_filename', autospec=False) - def test_run_complex_command_nested_with_env(self, resource_filename_mock, check_output_mock, popen_mock, os_getuid_mock): + def test_run_complex_command_nested_with_env(self, resource_filename_mock, check_output_mock, popen_mock, os_getuid_mock): resource_filename_mock.return_value = "entrypoint.sh" popen_mock.return_value.stdout.readline.side_effect = ['aaa', 'bbb', 'ccc', ''] popen_mock.return_value.poll.return_value = -1 @@ -181,20 +182,20 @@ def test_run_complex_command_nested_with_env(self, resource_filename_mock, chec '-t', '-e', 'KEEP_CONTAINERS=True', '--privileged', - '--net', 'host', + '--net', get_default_net(), '-e', 'KEY1=VAL1', '-e', 'KEY2=VAL2', '-e', 'SKIPPER_USERNAME=testuser', '-e', 'SKIPPER_UID=%(user_uid)s' % dict(user_uid=USER_ID), '-e', 'HOME=%(homedir)s' % dict(homedir=HOME_DIR), - '-v', '%(homedir)s/.netrc:%(homedir)s/.netrc:ro' % dict(homedir=HOME_DIR), - '-v', '%(homedir)s/.gitconfig:%(homedir)s/.gitconfig:ro' % dict(homedir=HOME_DIR), - '-v', '%(homedir)s/.docker/config.json:%(homedir)s/.docker/config.json:ro' % dict(homedir=HOME_DIR), - '-v', '/etc/docker:/etc/docker:ro', - '-v', '%(workdir)s:%(workdir)s:rw,shared' % dict(workdir=WORKDIR), - '-v', 'entrypoint.sh:/opt/skipper/skipper-entrypoint.sh:rw', - '-v', '/var/run/docker.sock:/var/run/docker.sock:rw', - '-v', '/var/lib/osmosis:/var/lib/osmosis:rw', + '-v', get_volume_mapping('%(homedir)s/.netrc:%(homedir)s/.netrc:ro' % dict(homedir=HOME_DIR)), + '-v', get_volume_mapping('%(homedir)s/.gitconfig:%(homedir)s/.gitconfig:ro' % dict(homedir=HOME_DIR)), + '-v', get_volume_mapping('%(homedir)s/.docker/config.json:%(homedir)s/.docker/config.json:ro' % dict(homedir=HOME_DIR)), + '-v', get_volume_mapping('/etc/docker:/etc/docker:ro'), + '-v', get_volume_mapping('%(workdir)s:%(workdir)s:rw,shared' % dict(workdir=WORKDIR)), + '-v', get_volume_mapping('entrypoint.sh:/opt/skipper/skipper-entrypoint.sh:rw'), + '-v', get_volume_mapping('/var/run/docker.sock:/var/run/docker.sock:rw'), + '-v', get_volume_mapping('/var/lib/osmosis:/var/lib/osmosis:rw'), '-w', PROJECT_DIR, '--entrypoint', '/opt/skipper/skipper-entrypoint.sh', FQDN_IMAGE, diff --git a/tests/test_utils.py b/tests/test_utils.py index 98ad29a..c5a078c 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,8 +1,9 @@ -import mock import os import unittest + +import mock + from skipper import utils -from skipper import runner class TestUtils(unittest.TestCase):