Interpolate environment variables
Signed-off-by: Aanand Prasad <aanand.prasad@gmail.com>
This commit is contained in:
parent
31ac3ce22a
commit
8b5bd945d0
9 changed files with 235 additions and 35 deletions
17
tests/fixtures/environment-interpolation/docker-compose.yml
vendored
Normal file
17
tests/fixtures/environment-interpolation/docker-compose.yml
vendored
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
web:
|
||||
# unbracketed name
|
||||
image: $IMAGE
|
||||
|
||||
# array element
|
||||
ports:
|
||||
- "${HOST_PORT}:8000"
|
||||
|
||||
# dictionary item value
|
||||
labels:
|
||||
mylabel: "${LABEL_VALUE}"
|
||||
|
||||
# unset value
|
||||
hostname: "host-${UNSET_VALUE}"
|
||||
|
||||
# escaped interpolation
|
||||
command: "$${ESCAPED}"
|
||||
5
tests/fixtures/volume-path-interpolation/docker-compose.yml
vendored
Normal file
5
tests/fixtures/volume-path-interpolation/docker-compose.yml
vendored
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
test:
|
||||
image: busybox
|
||||
command: top
|
||||
volumes:
|
||||
- "~/${VOLUME_NAME}:/container-path"
|
||||
|
|
@ -488,6 +488,21 @@ class CLITestCase(DockerClientTestCase):
|
|||
self.assertEqual(len(containers), 1)
|
||||
self.assertIn("FOO=1", containers[0].get('Config.Env'))
|
||||
|
||||
@patch.dict(os.environ)
|
||||
def test_home_and_env_var_in_volume_path(self):
|
||||
os.environ['VOLUME_NAME'] = 'my-volume'
|
||||
os.environ['HOME'] = '/tmp/home-dir'
|
||||
expected_host_path = os.path.join(os.environ['HOME'], os.environ['VOLUME_NAME'])
|
||||
|
||||
self.command.base_dir = 'tests/fixtures/volume-path-interpolation'
|
||||
self.command.dispatch(['up', '-d'], None)
|
||||
|
||||
container = self.project.containers(stopped=True)[0]
|
||||
actual_host_path = container.get('Volumes')['/container-path']
|
||||
components = actual_host_path.split('/')
|
||||
self.assertTrue(components[-2:] == ['home-dir', 'my-volume'],
|
||||
msg="Last two components differ: %s, %s" % (actual_host_path, expected_host_path))
|
||||
|
||||
def test_up_with_extends(self):
|
||||
self.command.base_dir = 'tests/fixtures/extends'
|
||||
self.command.dispatch(['up', '-d'], None)
|
||||
|
|
|
|||
|
|
@ -273,24 +273,6 @@ class ServiceTest(DockerClientTestCase):
|
|||
|
||||
self.assertEqual(service.containers(stopped=False), [new_container])
|
||||
|
||||
@patch.dict(os.environ)
|
||||
def test_create_container_with_home_and_env_var_in_volume_path(self):
|
||||
os.environ['VOLUME_NAME'] = 'my-volume'
|
||||
os.environ['HOME'] = '/tmp/home-dir'
|
||||
expected_host_path = os.path.join(os.environ['HOME'], os.environ['VOLUME_NAME'])
|
||||
|
||||
host_path = '~/${VOLUME_NAME}'
|
||||
container_path = '/container-path'
|
||||
|
||||
service = self.create_service('db', volumes=['%s:%s' % (host_path, container_path)])
|
||||
container = service.create_container()
|
||||
service.start_container(container)
|
||||
|
||||
actual_host_path = container.get('Volumes')[container_path]
|
||||
components = actual_host_path.split('/')
|
||||
self.assertTrue(components[-2:] == ['home-dir', 'my-volume'],
|
||||
msg="Last two components differ: %s, %s" % (actual_host_path, expected_host_path))
|
||||
|
||||
def test_create_container_with_volumes_from(self):
|
||||
volume_service = self.create_service('data')
|
||||
volume_container_1 = volume_service.create_container()
|
||||
|
|
|
|||
|
|
@ -59,11 +59,56 @@ class ConfigTest(unittest.TestCase):
|
|||
make_service_dict('foo', {'ports': ['8000']}, 'tests/')
|
||||
|
||||
|
||||
class VolumePathTest(unittest.TestCase):
|
||||
class InterpolationTest(unittest.TestCase):
|
||||
@mock.patch.dict(os.environ)
|
||||
def test_volume_binding_with_environ(self):
|
||||
def test_config_file_with_environment_variable(self):
|
||||
os.environ.update(
|
||||
IMAGE="busybox",
|
||||
HOST_PORT="80",
|
||||
LABEL_VALUE="myvalue",
|
||||
)
|
||||
|
||||
service_dicts = config.load(
|
||||
config.find('tests/fixtures/environment-interpolation', None),
|
||||
)
|
||||
|
||||
self.assertEqual(service_dicts, [
|
||||
{
|
||||
'name': 'web',
|
||||
'image': 'busybox',
|
||||
'ports': ['80:8000'],
|
||||
'labels': {'mylabel': 'myvalue'},
|
||||
'hostname': 'host-',
|
||||
'command': '${ESCAPED}',
|
||||
}
|
||||
])
|
||||
|
||||
@mock.patch.dict(os.environ)
|
||||
def test_invalid_interpolation(self):
|
||||
with self.assertRaises(config.ConfigurationError) as cm:
|
||||
config.load(
|
||||
config.ConfigDetails(
|
||||
{'web': {'image': '${'}},
|
||||
'working_dir',
|
||||
'filename.yml'
|
||||
)
|
||||
)
|
||||
|
||||
self.assertIn('Invalid', cm.exception.msg)
|
||||
self.assertIn('for "image" option', cm.exception.msg)
|
||||
self.assertIn('in service "web"', cm.exception.msg)
|
||||
self.assertIn('"${"', cm.exception.msg)
|
||||
|
||||
@mock.patch.dict(os.environ)
|
||||
def test_volume_binding_with_environment_variable(self):
|
||||
os.environ['VOLUME_PATH'] = '/host/path'
|
||||
d = make_service_dict('foo', {'volumes': ['${VOLUME_PATH}:/container/path']}, working_dir='.')
|
||||
d = config.load(
|
||||
config.ConfigDetails(
|
||||
config={'foo': {'volumes': ['${VOLUME_PATH}:/container/path']}},
|
||||
working_dir='.',
|
||||
filename=None,
|
||||
)
|
||||
)[0]
|
||||
self.assertEqual(d['volumes'], ['/host/path:/container/path'])
|
||||
|
||||
@mock.patch.dict(os.environ)
|
||||
|
|
@ -400,18 +445,22 @@ class EnvTest(unittest.TestCase):
|
|||
os.environ['HOSTENV'] = '/tmp'
|
||||
os.environ['CONTAINERENV'] = '/host/tmp'
|
||||
|
||||
service_dict = make_service_dict(
|
||||
'foo',
|
||||
{'volumes': ['$HOSTENV:$CONTAINERENV']},
|
||||
working_dir="tests/fixtures/env"
|
||||
)
|
||||
service_dict = config.load(
|
||||
config.ConfigDetails(
|
||||
config={'foo': {'volumes': ['$HOSTENV:$CONTAINERENV']}},
|
||||
working_dir="tests/fixtures/env",
|
||||
filename=None,
|
||||
)
|
||||
)[0]
|
||||
self.assertEqual(set(service_dict['volumes']), set(['/tmp:/host/tmp']))
|
||||
|
||||
service_dict = make_service_dict(
|
||||
'foo',
|
||||
{'volumes': ['/opt${HOSTENV}:/opt${CONTAINERENV}']},
|
||||
working_dir="tests/fixtures/env"
|
||||
)
|
||||
service_dict = config.load(
|
||||
config.ConfigDetails(
|
||||
config={'foo': {'volumes': ['/opt${HOSTENV}:/opt${CONTAINERENV}']}},
|
||||
working_dir="tests/fixtures/env",
|
||||
filename=None,
|
||||
)
|
||||
)[0]
|
||||
self.assertEqual(set(service_dict['volumes']), set(['/opt/tmp:/opt/host/tmp']))
|
||||
|
||||
|
||||
|
|
|
|||
31
tests/unit/interpolation_test.py
Normal file
31
tests/unit/interpolation_test.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import unittest
|
||||
|
||||
from compose.config.interpolation import interpolate, InvalidInterpolation
|
||||
|
||||
|
||||
class InterpolationTest(unittest.TestCase):
|
||||
def test_valid_interpolations(self):
|
||||
self.assertEqual(interpolate('$foo', dict(foo='hi')), 'hi')
|
||||
self.assertEqual(interpolate('${foo}', dict(foo='hi')), 'hi')
|
||||
|
||||
self.assertEqual(interpolate('${subject} love you', dict(subject='i')), 'i love you')
|
||||
self.assertEqual(interpolate('i ${verb} you', dict(verb='love')), 'i love you')
|
||||
self.assertEqual(interpolate('i love ${object}', dict(object='you')), 'i love you')
|
||||
|
||||
def test_empty_value(self):
|
||||
self.assertEqual(interpolate('${foo}', dict(foo='')), '')
|
||||
|
||||
def test_unset_value(self):
|
||||
self.assertEqual(interpolate('${foo}', dict()), '')
|
||||
|
||||
def test_escaped_interpolation(self):
|
||||
self.assertEqual(interpolate('$${foo}', dict(foo='hi')), '${foo}')
|
||||
|
||||
def test_invalid_strings(self):
|
||||
self.assertRaises(InvalidInterpolation, lambda: interpolate('${', dict()))
|
||||
self.assertRaises(InvalidInterpolation, lambda: interpolate('$}', dict()))
|
||||
self.assertRaises(InvalidInterpolation, lambda: interpolate('${}', dict()))
|
||||
self.assertRaises(InvalidInterpolation, lambda: interpolate('${ }', dict()))
|
||||
self.assertRaises(InvalidInterpolation, lambda: interpolate('${ foo}', dict()))
|
||||
self.assertRaises(InvalidInterpolation, lambda: interpolate('${foo }', dict()))
|
||||
self.assertRaises(InvalidInterpolation, lambda: interpolate('${foo!}', dict()))
|
||||
Loading…
Add table
Add a link
Reference in a new issue