Pārlūkot izejas kodu

Merge pull request #1633 from jeffk/parse_env_vars_in_all_volumes

Added env var parsing to volume container paths
Aanand Prasad 10 gadi atpakaļ
vecāks
revīzija
a6b9982a1e
2 mainītis faili ar 26 papildinājumiem un 7 dzēšanām
  1. 7 7
      compose/config.py
  2. 19 0
      tests/unit/config_test.py

+ 7 - 7
compose/config.py

@@ -184,7 +184,7 @@ def process_container_options(service_dict, working_dir=None):
     service_dict = service_dict.copy()
 
     if 'volumes' in service_dict:
-        service_dict['volumes'] = resolve_host_paths(service_dict['volumes'], working_dir=working_dir)
+        service_dict['volumes'] = resolve_volume_paths(service_dict['volumes'], working_dir=working_dir)
 
     if 'build' in service_dict:
         service_dict['build'] = resolve_build_path(service_dict['build'], working_dir=working_dir)
@@ -345,18 +345,18 @@ def env_vars_from_file(filename):
     return env
 
 
-def resolve_host_paths(volumes, working_dir=None):
+def resolve_volume_paths(volumes, working_dir=None):
     if working_dir is None:
-        raise Exception("No working_dir passed to resolve_host_paths()")
+        raise Exception("No working_dir passed to resolve_volume_paths()")
 
-    return [resolve_host_path(v, working_dir) for v in volumes]
+    return [resolve_volume_path(v, working_dir) for v in volumes]
 
 
-def resolve_host_path(volume, working_dir):
+def resolve_volume_path(volume, working_dir):
     container_path, host_path = split_path_mapping(volume)
+    container_path = os.path.expanduser(os.path.expandvars(container_path))
     if host_path is not None:
-        host_path = os.path.expanduser(host_path)
-        host_path = os.path.expandvars(host_path)
+        host_path = os.path.expanduser(os.path.expandvars(host_path))
         return "%s:%s" % (expand_path(working_dir, host_path), container_path)
     else:
         return container_path

+ 19 - 0
tests/unit/config_test.py

@@ -334,6 +334,25 @@ class EnvTest(unittest.TestCase):
             {'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': ''},
         )
 
+    @mock.patch.dict(os.environ)
+    def test_resolve_path(self):
+        os.environ['HOSTENV'] = '/tmp'
+        os.environ['CONTAINERENV'] = '/host/tmp'
+
+        service_dict = config.make_service_dict(
+            'foo',
+            {'volumes': ['$HOSTENV:$CONTAINERENV']},
+            working_dir="tests/fixtures/env"
+        )
+        self.assertEqual(set(service_dict['volumes']), set(['/tmp:/host/tmp']))
+
+        service_dict = config.make_service_dict(
+            'foo',
+            {'volumes': ['/opt${HOSTENV}:/opt${CONTAINERENV}']},
+            working_dir="tests/fixtures/env"
+        )
+        self.assertEqual(set(service_dict['volumes']), set(['/opt/tmp:/opt/host/tmp']))
+
 
 class ExtendsTest(unittest.TestCase):
     def test_extends(self):