From c5408f3a4010c0760d25255ce989ed7cb9fe89dc Mon Sep 17 00:00:00 2001 From: Joffrey F Date: Wed, 25 Oct 2017 14:51:11 -0700 Subject: [PATCH] Add support for extra_hosts in build config Signed-off-by: Joffrey F --- compose/config/config.py | 1 + compose/config/config_schema_v2.3.json | 3 ++- compose/service.py | 1 + tests/integration/service_test.py | 23 +++++++++++++++++++++++ tests/unit/service_test.py | 4 +++- tox.ini | 1 - 6 files changed, 30 insertions(+), 3 deletions(-) diff --git a/compose/config/config.py b/compose/config/config.py index adfb53d8f..4c3f93ddb 100644 --- a/compose/config/config.py +++ b/compose/config/config.py @@ -1023,6 +1023,7 @@ def merge_build(output, base, override): md.merge_mapping('args', parse_build_arguments) md.merge_field('cache_from', merge_unique_items_lists, default=[]) md.merge_mapping('labels', parse_labels) + md.merge_mapping('extra_hosts', parse_extra_hosts) return dict(md) diff --git a/compose/config/config_schema_v2.3.json b/compose/config/config_schema_v2.3.json index 0e709e9d9..6f923871b 100644 --- a/compose/config/config_schema_v2.3.json +++ b/compose/config/config_schema_v2.3.json @@ -92,7 +92,8 @@ "cache_from": {"$ref": "#/definitions/list_of_strings"}, "network": {"type": "string"}, "target": {"type": "string"}, - "shm_size": {"type": ["integer", "string"]} + "shm_size": {"type": ["integer", "string"]}, + "extra_hosts": {"$ref": "#/definitions/list_or_dict"} }, "additionalProperties": false } diff --git a/compose/service.py b/compose/service.py index 453f982f0..d6a3ff0b3 100644 --- a/compose/service.py +++ b/compose/service.py @@ -930,6 +930,7 @@ class Service(object): network_mode=build_opts.get('network', None), target=build_opts.get('target', None), shmsize=parse_bytes(build_opts.get('shm_size')) if build_opts.get('shm_size') else None, + extra_hosts=build_opts.get('extra_hosts', None), ) try: diff --git a/tests/integration/service_test.py b/tests/integration/service_test.py index deced2742..00bacebf5 100644 --- a/tests/integration/service_test.py +++ b/tests/integration/service_test.py @@ -833,6 +833,29 @@ class ServiceTest(DockerClientTestCase): assert service.image() assert service.image()['Config']['Labels']['com.docker.compose.test.target'] == 'one' + @v2_3_only() + def test_build_with_extra_hosts(self): + self.require_api_version('1.27') + base_dir = tempfile.mkdtemp() + self.addCleanup(shutil.rmtree, base_dir) + + with open(os.path.join(base_dir, 'Dockerfile'), 'w') as f: + f.write('\n'.join([ + 'FROM busybox', + 'RUN ping -c1 foobar', + 'RUN ping -c1 baz', + ])) + + service = self.create_service('build_extra_hosts', build={ + 'context': text_type(base_dir), + 'extra_hosts': { + 'foobar': '127.0.0.1', + 'baz': '127.0.0.1' + } + }) + service.build() + assert service.image() + def test_start_container_stays_unprivileged(self): service = self.create_service('web') container = create_and_start_container(service).inspect() diff --git a/tests/unit/service_test.py b/tests/unit/service_test.py index 35f80d11a..8a1090367 100644 --- a/tests/unit/service_test.py +++ b/tests/unit/service_test.py @@ -498,6 +498,7 @@ class ServiceTest(unittest.TestCase): network_mode=None, target=None, shmsize=None, + extra_hosts=None, ) def test_ensure_image_exists_no_build(self): @@ -538,7 +539,8 @@ class ServiceTest(unittest.TestCase): cache_from=None, network_mode=None, target=None, - shmsize=None + shmsize=None, + extra_hosts=None, ) def test_build_does_not_pull(self): diff --git a/tox.ini b/tox.ini index e4f31ec85..749be3faa 100644 --- a/tox.ini +++ b/tox.ini @@ -18,7 +18,6 @@ deps = -rrequirements-dev.txt commands = py.test -v \ - --full-trace \ --cov=compose \ --cov-report html \ --cov-report term \