From 35c6e0314c07b9a6d2e97831a1bc4523526a6344 Mon Sep 17 00:00:00 2001 From: Aanand Prasad Date: Mon, 23 Mar 2015 11:40:44 -0700 Subject: [PATCH] Fix volume merging when there's no explicit host path Signed-off-by: Aanand Prasad --- compose/config.py | 25 +++++++++++++------- tests/unit/config_test.py | 48 +++++++++++++++++++++++++++------------ 2 files changed, 50 insertions(+), 23 deletions(-) diff --git a/compose/config.py b/compose/config.py index af30a702f..0cd7c1ae6 100644 --- a/compose/config.py +++ b/compose/config.py @@ -324,15 +324,24 @@ def dict_from_volumes(volumes): return {} -def split_volume(volume): - if ':' in volume: - return reversed(volume.split(':', 1)) - else: - return (volume, None) - - def volumes_from_dict(d): - return ["%s:%s" % (host_path, container_path) for (container_path, host_path) in d.items()] + return [join_volume(v) for v in d.items()] + + +def split_volume(string): + if ':' in string: + (host, container) = string.split(':', 1) + return (container, host) + else: + return (string, None) + + +def join_volume(pair): + (container, host) = pair + if host is None: + return container + else: + return ":".join((host, container)) def expand_path(working_dir, path): diff --git a/tests/unit/config_test.py b/tests/unit/config_test.py index 323323d41..8deb457af 100644 --- a/tests/unit/config_test.py +++ b/tests/unit/config_test.py @@ -40,26 +40,44 @@ class ConfigTest(unittest.TestCase): class MergeTest(unittest.TestCase): - def test_merge_volumes(self): + def test_merge_volumes_empty(self): service_dict = config.merge_service_dicts({}, {}) self.assertNotIn('volumes', service_dict) - service_dict = config.merge_service_dicts({ - 'volumes': ['/foo:/data'], - }, {}) - self.assertEqual(service_dict['volumes'], ['/foo:/data']) + def test_merge_volumes_no_override(self): + service_dict = config.merge_service_dicts( + {'volumes': ['/foo:/code', '/data']}, + {}, + ) + self.assertEqual(set(service_dict['volumes']), set(['/foo:/code', '/data'])) - service_dict = config.merge_service_dicts({}, { - 'volumes': ['/bar:/data'], - }) - self.assertEqual(service_dict['volumes'], ['/bar:/data']) + def test_merge_volumes_no_base(self): + service_dict = config.merge_service_dicts( + {}, + {'volumes': ['/bar:/code']}, + ) + self.assertEqual(set(service_dict['volumes']), set(['/bar:/code'])) - service_dict = config.merge_service_dicts({ - 'volumes': ['/foo:/data'], - }, { - 'volumes': ['/bar:/data'], - }) - self.assertEqual(service_dict['volumes'], ['/bar:/data']) + def test_merge_volumes_override_explicit_path(self): + service_dict = config.merge_service_dicts( + {'volumes': ['/foo:/code', '/data']}, + {'volumes': ['/bar:/code']}, + ) + self.assertEqual(set(service_dict['volumes']), set(['/bar:/code', '/data'])) + + def test_merge_volumes_add_explicit_path(self): + service_dict = config.merge_service_dicts( + {'volumes': ['/foo:/code', '/data']}, + {'volumes': ['/bar:/code', '/quux:/data']}, + ) + self.assertEqual(set(service_dict['volumes']), set(['/bar:/code', '/quux:/data'])) + + def test_merge_volumes_remove_explicit_path(self): + service_dict = config.merge_service_dicts( + {'volumes': ['/foo:/code', '/quux:/data']}, + {'volumes': ['/bar:/code', '/data']}, + ) + self.assertEqual(set(service_dict['volumes']), set(['/bar:/code', '/data'])) class EnvTest(unittest.TestCase):