From 2f07e2ac3628617b9817dc9f7815d6c3a730aaac Mon Sep 17 00:00:00 2001 From: Dimitar Bonev Date: Thu, 7 Jan 2016 21:21:47 +0200 Subject: [PATCH] Ulimits are now merged into extended services Signed-off-by: Dimitar Bonev --- compose/config/config.py | 25 +++++++++++++------ .../extends/common-env-labels-ulimits.yml | 13 ++++++++++ tests/unit/config/config_test.py | 18 +++++++++++++ 3 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 tests/fixtures/extends/common-env-labels-ulimits.yml diff --git a/compose/config/config.py b/compose/config/config.py index 918946b39..11cc3ce9d 100644 --- a/compose/config/config.py +++ b/compose/config/config.py @@ -547,8 +547,15 @@ def merge_service_dicts(base, override, version): base.get(field, default), override.get(field, default)) - merge_field('environment', merge_environment) - merge_field('labels', merge_labels) + def merge_mapping(mapping, parse_func): + if mapping in base or mapping in override: + merged = parse_func(base.get(mapping, None)) + merged.update(parse_func(override.get(mapping, None))) + d[mapping] = merged + + merge_mapping('environment', parse_environment) + merge_mapping('labels', parse_labels) + merge_mapping('ulimits', parse_ulimits) for field in ['volumes', 'devices']: merge_field(field, merge_path_mappings) @@ -723,12 +730,6 @@ def join_path_mapping(pair): return ":".join((host, container)) -def merge_labels(base, override): - labels = parse_labels(base) - labels.update(parse_labels(override)) - return labels - - def parse_labels(labels): if not labels: return {} @@ -747,6 +748,14 @@ def split_label(label): return label, '' +def parse_ulimits(ulimits): + if not ulimits: + return {} + + if isinstance(ulimits, dict): + return dict(ulimits) + + def expand_path(working_dir, path): return os.path.abspath(os.path.join(working_dir, os.path.expanduser(path))) diff --git a/tests/fixtures/extends/common-env-labels-ulimits.yml b/tests/fixtures/extends/common-env-labels-ulimits.yml new file mode 100644 index 000000000..09efb4e75 --- /dev/null +++ b/tests/fixtures/extends/common-env-labels-ulimits.yml @@ -0,0 +1,13 @@ +web: + extends: + file: common.yml + service: web + environment: + - FOO=2 + - BAZ=3 + labels: ['label=one'] + ulimits: + nproc: 65535 + memlock: + soft: 1024 + hard: 2048 diff --git a/tests/unit/config/config_test.py b/tests/unit/config/config_test.py index a59d1d34a..fb324c572 100644 --- a/tests/unit/config/config_test.py +++ b/tests/unit/config/config_test.py @@ -1396,6 +1396,24 @@ class ExtendsTest(unittest.TestCase): } ])) + def test_merging_env_labels_ulimits(self): + service_dicts = load_from_filename('tests/fixtures/extends/common-env-labels-ulimits.yml') + + self.assertEqual(service_sort(service_dicts), service_sort([ + { + 'name': 'web', + 'image': 'busybox', + 'command': '/bin/true', + 'environment': { + "FOO": "2", + "BAR": "1", + "BAZ": "3", + }, + 'labels': {'label': 'one'}, + 'ulimits': {'nproc': 65535, 'memlock': {'soft': 1024, 'hard': 2048}} + } + ])) + def test_nested(self): service_dicts = load_from_filename('tests/fixtures/extends/nested.yml')