Skip to content

Commit a467a8a

Browse files
bfirshaanand
authored andcommitted
Merge pull request #1261 from aanand/fix-vars-in-volume-paths
Fix vars in volume paths (cherry picked from commit 4926f8a) Signed-off-by: Aanand Prasad <aanand.prasad@gmail.com> Conflicts: tests/unit/service_test.py
1 parent 78227c3 commit a467a8a

5 files changed

Lines changed: 35 additions & 19 deletions

File tree

compose/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,8 @@ def resolve_host_paths(volumes, working_dir=None):
328328
def resolve_host_path(volume, working_dir):
329329
container_path, host_path = split_volume(volume)
330330
if host_path is not None:
331+
host_path = os.path.expanduser(host_path)
332+
host_path = os.path.expandvars(host_path)
331333
return "%s:%s" % (expand_path(working_dir, host_path), container_path)
332334
else:
333335
return container_path

compose/service.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from collections import namedtuple
44
import logging
55
import re
6-
import os
76
from operator import attrgetter
87
import sys
98
import six
@@ -586,8 +585,7 @@ def parse_repository_tag(s):
586585

587586
def build_volume_binding(volume_spec):
588587
internal = {'bind': volume_spec.internal, 'ro': volume_spec.mode == 'ro'}
589-
external = os.path.expanduser(volume_spec.external)
590-
return os.path.abspath(os.path.expandvars(external)), internal
588+
return volume_spec.external, internal
591589

592590

593591
def build_port_bindings(ports):

tests/integration/service_test.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,24 @@ def test_create_container_with_specified_volume(self):
123123
self.assertTrue(path.basename(actual_host_path) == path.basename(host_path),
124124
msg=("Last component differs: %s, %s" % (actual_host_path, host_path)))
125125

126+
@mock.patch.dict(os.environ)
127+
def test_create_container_with_home_and_env_var_in_volume_path(self):
128+
os.environ['VOLUME_NAME'] = 'my-volume'
129+
os.environ['HOME'] = '/tmp/home-dir'
130+
expected_host_path = os.path.join(os.environ['HOME'], os.environ['VOLUME_NAME'])
131+
132+
host_path = '~/${VOLUME_NAME}'
133+
container_path = '/container-path'
134+
135+
service = self.create_service('db', volumes=['%s:%s' % (host_path, container_path)])
136+
container = service.create_container()
137+
service.start_container(container)
138+
139+
actual_host_path = container.get('Volumes')[container_path]
140+
components = actual_host_path.split('/')
141+
self.assertTrue(components[-2:] == ['home-dir', 'my-volume'],
142+
msg="Last two components differ: %s, %s" % (actual_host_path, expected_host_path))
143+
126144
def test_create_container_with_volumes_from(self):
127145
volume_service = self.create_service('data')
128146
volume_container_1 = volume_service.create_container()

tests/unit/config_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ def test_config_validation(self):
3939
config.make_service_dict('foo', {'ports': ['8000']})
4040

4141

42+
class VolumePathTest(unittest.TestCase):
43+
@mock.patch.dict(os.environ)
44+
def test_volume_binding_with_environ(self):
45+
os.environ['VOLUME_PATH'] = '/host/path'
46+
d = config.make_service_dict('foo', {'volumes': ['${VOLUME_PATH}:/container/path']}, working_dir='.')
47+
self.assertEqual(d['volumes'], ['/host/path:/container/path'])
48+
49+
@mock.patch.dict(os.environ)
50+
def test_volume_binding_with_home(self):
51+
os.environ['HOME'] = '/home/user'
52+
d = config.make_service_dict('foo', {'volumes': ['~:/container/path']}, working_dir='.')
53+
self.assertEqual(d['volumes'], ['/home/user:/container/path'])
54+
55+
4256
class MergeVolumesTest(unittest.TestCase):
4357
def test_empty(self):
4458
service_dict = config.merge_service_dicts({}, {})

tests/unit/service_test.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import unicode_literals
22
from __future__ import absolute_import
3-
import os
43

54
from .. import unittest
65
import mock
@@ -301,18 +300,3 @@ def test_build_volume_binding(self):
301300
self.assertEqual(
302301
binding,
303302
('/outside', dict(bind='/inside', ro=False)))
304-
305-
@mock.patch.dict(os.environ)
306-
def test_build_volume_binding_with_environ(self):
307-
os.environ['VOLUME_PATH'] = '/opt'
308-
binding = build_volume_binding(parse_volume_spec('${VOLUME_PATH}:/opt'))
309-
self.assertEqual(binding, ('/opt', dict(bind='/opt', ro=False)))
310-
311-
@mock.patch.dict(os.environ)
312-
def test_building_volume_binding_with_home(self):
313-
os.environ['HOME'] = '/home/user'
314-
binding = build_volume_binding(parse_volume_spec('~:/home/user'))
315-
self.assertEqual(
316-
binding,
317-
('/home/user', dict(bind='/home/user', ro=False)))
318-

0 commit comments

Comments
 (0)