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
|
|
@ -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