From f680d46d9af868163d3a4887678d3531e88001e0 Mon Sep 17 00:00:00 2001 From: Joffrey F Date: Mon, 16 Oct 2017 11:43:06 -0700 Subject: [PATCH] Move build labels parsing to config module Signed-off-by: Joffrey F --- compose/config/config.py | 12 ++++++------ compose/service.py | 3 +-- tests/integration/service_test.py | 19 +------------------ tests/unit/config/config_test.py | 24 +++++++++++++++++++++++- tests/unit/service_test.py | 4 ++-- 5 files changed, 33 insertions(+), 29 deletions(-) diff --git a/compose/config/config.py b/compose/config/config.py index 948e2376e..68b2be3a6 100644 --- a/compose/config/config.py +++ b/compose/config/config.py @@ -707,16 +707,16 @@ def process_service(service_config): if 'build' in service_dict: if isinstance(service_dict['build'], six.string_types): service_dict['build'] = resolve_build_path(working_dir, service_dict['build']) - elif isinstance(service_dict['build'], dict) and 'context' in service_dict['build']: - path = service_dict['build']['context'] - service_dict['build']['context'] = resolve_build_path(working_dir, path) + elif isinstance(service_dict['build'], dict): + if 'context' in service_dict['build']: + path = service_dict['build']['context'] + service_dict['build']['context'] = resolve_build_path(working_dir, path) + if 'labels' in service_dict['build']: + service_dict['build']['labels'] = parse_labels(service_dict['build']['labels']) if 'volumes' in service_dict and service_dict.get('volume_driver') is None: service_dict['volumes'] = resolve_volume_paths(working_dir, service_dict) - if 'labels' in service_dict: - service_dict['labels'] = parse_labels(service_dict['labels']) - if 'sysctls' in service_dict: service_dict['sysctls'] = build_string_dict(parse_sysctls(service_dict['sysctls'])) diff --git a/compose/service.py b/compose/service.py index e2f72aa5a..1a18c6654 100644 --- a/compose/service.py +++ b/compose/service.py @@ -23,7 +23,6 @@ from . import const from . import progress_stream from .config import DOCKER_CONFIG_KEYS from .config import merge_environment -from .config.config import parse_labels from .config.errors import DependencyError from .config.types import ServicePort from .config.types import VolumeSpec @@ -917,7 +916,7 @@ class Service(object): nocache=no_cache, dockerfile=build_opts.get('dockerfile', None), cache_from=build_opts.get('cache_from', None), - labels=parse_labels(build_opts.get('labels', None)), + labels=build_opts.get('labels', None), buildargs=build_args, network_mode=build_opts.get('network', None), target=build_opts.get('target', None), diff --git a/tests/integration/service_test.py b/tests/integration/service_test.py index a71bc407c..84b54fe41 100644 --- a/tests/integration/service_test.py +++ b/tests/integration/service_test.py @@ -761,7 +761,7 @@ class ServiceTest(DockerClientTestCase): assert service.image() assert "build_version=2" in service.image()['ContainerConfig']['Cmd'] - def test_build_with_build_labels_dict(self): + def test_build_with_build_labels(self): base_dir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, base_dir) @@ -778,23 +778,6 @@ class ServiceTest(DockerClientTestCase): assert service.image() assert service.image()['Config']['Labels']['com.docker.compose.test'] == 'true' - def test_build_with_build_labels_list(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('buildlabels', build={ - 'context': text_type(base_dir), - 'labels': ['com.docker.compose.test=true'] - }) - service.build() - self.addCleanup(self.client.remove_image, service.image_name) - - assert service.image() - assert service.image()['Config']['Labels']['com.docker.compose.test'] == 'true' - @no_cluster('Container networks not on Swarm') def test_build_with_network(self): base_dir = tempfile.mkdtemp() diff --git a/tests/unit/config/config_test.py b/tests/unit/config/config_test.py index c5e40130d..8f2266ed8 100644 --- a/tests/unit/config/config_test.py +++ b/tests/unit/config/config_test.py @@ -892,7 +892,7 @@ class ConfigTest(unittest.TestCase): assert service['build']['args']['opt1'] == '42' assert service['build']['args']['opt2'] == 'foobar' - def test_load_with_build_labels(self): + def test_load_build_labels_dict(self): service = config.load( build_config_details( { @@ -919,6 +919,28 @@ class ConfigTest(unittest.TestCase): assert service['build']['labels']['label1'] == 42 assert service['build']['labels']['label2'] == 'foobar' + def test_load_build_labels_list(self): + base_file = config.ConfigFile( + 'base.yml', + { + 'version': '2.3', + 'services': { + 'web': { + 'build': { + 'context': '.', + 'labels': ['foo=bar', 'baz=true', 'foobar=1'] + }, + }, + }, + } + ) + + details = config.ConfigDetails('.', [base_file]) + service = config.load(details).services[0] + assert service['build']['labels'] == { + 'foo': 'bar', 'baz': 'true', 'foobar': '1' + } + def test_build_args_allow_empty_properties(self): service = config.load( build_config_details( diff --git a/tests/unit/service_test.py b/tests/unit/service_test.py index 5c5c2bf67..7d61807ba 100644 --- a/tests/unit/service_test.py +++ b/tests/unit/service_test.py @@ -473,7 +473,7 @@ class ServiceTest(unittest.TestCase): nocache=False, rm=True, buildargs={}, - labels={}, + labels=None, cache_from=None, network_mode=None, target=None, @@ -514,7 +514,7 @@ class ServiceTest(unittest.TestCase): nocache=False, rm=True, buildargs={}, - labels={}, + labels=None, cache_from=None, network_mode=None, target=None,