From 2e100353d38a6fb5cf0ad37d0403305ab6c54f65 Mon Sep 17 00:00:00 2001 From: Joffrey F Date: Thu, 29 Mar 2018 16:24:56 -0700 Subject: Add support for build isolation parameter Signed-off-by: Joffrey F --- compose/config/config.py | 1 + compose/config/config_schema_v2.1.json | 3 ++- compose/config/config_schema_v2.2.json | 3 ++- compose/config/config_schema_v2.3.json | 3 ++- compose/service.py | 3 ++- tests/integration/service_test.py | 14 ++++++++++++++ tests/unit/service_test.py | 27 +++++++++++++++++++++++++++ 7 files changed, 50 insertions(+), 4 deletions(-) diff --git a/compose/config/config.py b/compose/config/config.py index 147c1d31..84d2a86a 100644 --- a/compose/config/config.py +++ b/compose/config/config.py @@ -1119,6 +1119,7 @@ def merge_build(output, base, override): md.merge_scalar('network') md.merge_scalar('target') md.merge_scalar('shm_size') + md.merge_scalar('isolation') md.merge_mapping('args', parse_build_arguments) md.merge_field('cache_from', merge_unique_items_lists, default=[]) md.merge_mapping('labels', parse_labels) diff --git a/compose/config/config_schema_v2.1.json b/compose/config/config_schema_v2.1.json index 87a730dd..5ad5a20e 100644 --- a/compose/config/config_schema_v2.1.json +++ b/compose/config/config_schema_v2.1.json @@ -88,7 +88,8 @@ "context": {"type": "string"}, "dockerfile": {"type": "string"}, "args": {"$ref": "#/definitions/list_or_dict"}, - "labels": {"$ref": "#/definitions/labels"} + "labels": {"$ref": "#/definitions/labels"}, + "isolation": {"type": "string"} }, "additionalProperties": false } diff --git a/compose/config/config_schema_v2.2.json b/compose/config/config_schema_v2.2.json index dc707674..26044b65 100644 --- a/compose/config/config_schema_v2.2.json +++ b/compose/config/config_schema_v2.2.json @@ -90,7 +90,8 @@ "args": {"$ref": "#/definitions/list_or_dict"}, "labels": {"$ref": "#/definitions/labels"}, "cache_from": {"$ref": "#/definitions/list_of_strings"}, - "network": {"type": "string"} + "network": {"type": "string"}, + "isolation": {"type": "string"} }, "additionalProperties": false } diff --git a/compose/config/config_schema_v2.3.json b/compose/config/config_schema_v2.3.json index bd7ce166..ac0778f2 100644 --- a/compose/config/config_schema_v2.3.json +++ b/compose/config/config_schema_v2.3.json @@ -93,7 +93,8 @@ "network": {"type": "string"}, "target": {"type": "string"}, "shm_size": {"type": ["integer", "string"]}, - "extra_hosts": {"$ref": "#/definitions/list_or_dict"} + "extra_hosts": {"$ref": "#/definitions/list_or_dict"}, + "isolation": {"type": "string"} }, "additionalProperties": false } diff --git a/compose/service.py b/compose/service.py index 9b03a399..9e5725e0 100644 --- a/compose/service.py +++ b/compose/service.py @@ -1016,7 +1016,8 @@ class Service(object): container_limits={ 'memory': parse_bytes(memory) if memory else None }, - gzip=gzip + gzip=gzip, + isolation=build_opts.get('isolation', self.options.get('isolation', None)), ) try: diff --git a/tests/integration/service_test.py b/tests/integration/service_test.py index 260867fe..d8f4d094 100644 --- a/tests/integration/service_test.py +++ b/tests/integration/service_test.py @@ -1123,6 +1123,20 @@ class ServiceTest(DockerClientTestCase): service.build(gzip=True) assert service.image() + @v2_1_only() + def test_build_with_isolation(self): + base_dir = tempfile.mkdtemp() + self.addCleanup(shutil.rmtree, base_dir) + with open(os.path.join(base_dir, 'Dockerfile'), 'w') as f: + f.write('FROM busybox\n') + + service = self.create_service('build_isolation', build={ + 'context': text_type(base_dir), + 'isolation': 'default', + }) + 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 5002954b..c4d53a2c 100644 --- a/tests/unit/service_test.py +++ b/tests/unit/service_test.py @@ -562,6 +562,33 @@ class ServiceTest(unittest.TestCase): assert called_build_args['arg1'] == build_args['arg1'] assert called_build_args['arg2'] == 'arg2' + def test_build_with_isolation_from_service_config(self): + self.mock_client.build.return_value = [ + b'{"stream": "Successfully built 12345"}', + ] + + service = Service('foo', client=self.mock_client, build={'context': '.'}, isolation='hyperv') + service.build() + + assert self.mock_client.build.call_count == 1 + called_build_args = self.mock_client.build.call_args[1] + assert called_build_args['isolation'] == 'hyperv' + + def test_build_isolation_from_build_override_service_config(self): + self.mock_client.build.return_value = [ + b'{"stream": "Successfully built 12345"}', + ] + + service = Service( + 'foo', client=self.mock_client, build={'context': '.', 'isolation': 'default'}, + isolation='hyperv' + ) + service.build() + + assert self.mock_client.build.call_count == 1 + called_build_args = self.mock_client.build.call_args[1] + assert called_build_args['isolation'] == 'default' + def test_config_dict(self): self.mock_client.inspect_image.return_value = {'Id': 'abcd'} service = Service( -- cgit v1.2.3 From 070474acae92b037dda0966ea3e90f1281beca52 Mon Sep 17 00:00:00 2001 From: Joffrey F Date: Thu, 29 Mar 2018 17:21:47 -0700 Subject: Fix unit tests Signed-off-by: Joffrey F --- tests/unit/service_test.py | 40 ++++------------------------------------ 1 file changed, 4 insertions(+), 36 deletions(-) diff --git a/tests/unit/service_test.py b/tests/unit/service_test.py index c4d53a2c..012bfd5e 100644 --- a/tests/unit/service_test.py +++ b/tests/unit/service_test.py @@ -472,24 +472,8 @@ class ServiceTest(unittest.TestCase): _, args, _ = mock_log.warn.mock_calls[0] assert 'was built because it did not already exist' in args[0] - self.mock_client.build.assert_called_once_with( - tag='default_foo', - dockerfile=None, - path='.', - pull=False, - forcerm=False, - nocache=False, - rm=True, - buildargs={}, - labels=None, - cache_from=None, - network_mode=None, - target=None, - shmsize=None, - extra_hosts=None, - container_limits={'memory': None}, - gzip=False, - ) + assert self.mock_client.build.call_count == 1 + self.mock_client.build.call_args[1]['tag'] == 'default_foo' def test_ensure_image_exists_no_build(self): service = Service('foo', client=self.mock_client, build={'context': '.'}) @@ -515,24 +499,8 @@ class ServiceTest(unittest.TestCase): service.ensure_image_exists(do_build=BuildAction.force) assert not mock_log.warn.called - self.mock_client.build.assert_called_once_with( - tag='default_foo', - dockerfile=None, - path='.', - pull=False, - forcerm=False, - nocache=False, - rm=True, - buildargs={}, - labels=None, - cache_from=None, - network_mode=None, - target=None, - shmsize=None, - extra_hosts=None, - container_limits={'memory': None}, - gzip=False - ) + assert self.mock_client.build.call_count == 1 + self.mock_client.build.call_args[1]['tag'] == 'default_foo' def test_build_does_not_pull(self): self.mock_client.build.return_value = [ -- cgit v1.2.3