mirror of
https://github.com/docker/compose.git
synced 2025-07-22 05:04:27 +02:00
Merge pull request #1825 from aanand/dedupe-unset-env-warnings
Avoid duplicate warnings if an unset env variable is used multiple times
This commit is contained in:
commit
c443e95f07
@ -10,13 +10,15 @@ log = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
def interpolate_environment_variables(config):
|
def interpolate_environment_variables(config):
|
||||||
|
mapping = BlankDefaultDict(os.environ)
|
||||||
|
|
||||||
return dict(
|
return dict(
|
||||||
(service_name, process_service(service_name, service_dict))
|
(service_name, process_service(service_name, service_dict, mapping))
|
||||||
for (service_name, service_dict) in config.items()
|
for (service_name, service_dict) in config.items()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def process_service(service_name, service_dict):
|
def process_service(service_name, service_dict, mapping):
|
||||||
if not isinstance(service_dict, dict):
|
if not isinstance(service_dict, dict):
|
||||||
raise ConfigurationError(
|
raise ConfigurationError(
|
||||||
'Service "%s" doesn\'t have any configuration options. '
|
'Service "%s" doesn\'t have any configuration options. '
|
||||||
@ -25,14 +27,14 @@ def process_service(service_name, service_dict):
|
|||||||
)
|
)
|
||||||
|
|
||||||
return dict(
|
return dict(
|
||||||
(key, interpolate_value(service_name, key, val))
|
(key, interpolate_value(service_name, key, val, mapping))
|
||||||
for (key, val) in service_dict.items()
|
for (key, val) in service_dict.items()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def interpolate_value(service_name, config_key, value):
|
def interpolate_value(service_name, config_key, value, mapping):
|
||||||
try:
|
try:
|
||||||
return recursive_interpolate(value)
|
return recursive_interpolate(value, mapping)
|
||||||
except InvalidInterpolation as e:
|
except InvalidInterpolation as e:
|
||||||
raise ConfigurationError(
|
raise ConfigurationError(
|
||||||
'Invalid interpolation format for "{config_key}" option '
|
'Invalid interpolation format for "{config_key}" option '
|
||||||
@ -45,39 +47,43 @@ def interpolate_value(service_name, config_key, value):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def recursive_interpolate(obj):
|
def recursive_interpolate(obj, mapping):
|
||||||
if isinstance(obj, six.string_types):
|
if isinstance(obj, six.string_types):
|
||||||
return interpolate(obj, os.environ)
|
return interpolate(obj, mapping)
|
||||||
elif isinstance(obj, dict):
|
elif isinstance(obj, dict):
|
||||||
return dict(
|
return dict(
|
||||||
(key, recursive_interpolate(val))
|
(key, recursive_interpolate(val, mapping))
|
||||||
for (key, val) in obj.items()
|
for (key, val) in obj.items()
|
||||||
)
|
)
|
||||||
elif isinstance(obj, list):
|
elif isinstance(obj, list):
|
||||||
return map(recursive_interpolate, obj)
|
return [recursive_interpolate(val, mapping) for val in obj]
|
||||||
else:
|
else:
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
|
|
||||||
def interpolate(string, mapping):
|
def interpolate(string, mapping):
|
||||||
try:
|
try:
|
||||||
return Template(string).substitute(BlankDefaultDict(mapping))
|
return Template(string).substitute(mapping)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise InvalidInterpolation(string)
|
raise InvalidInterpolation(string)
|
||||||
|
|
||||||
|
|
||||||
class BlankDefaultDict(dict):
|
class BlankDefaultDict(dict):
|
||||||
def __init__(self, mapping):
|
def __init__(self, *args, **kwargs):
|
||||||
super(BlankDefaultDict, self).__init__(mapping)
|
super(BlankDefaultDict, self).__init__(*args, **kwargs)
|
||||||
|
self.missing_keys = []
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
try:
|
try:
|
||||||
return super(BlankDefaultDict, self).__getitem__(key)
|
return super(BlankDefaultDict, self).__getitem__(key)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
log.warn(
|
if key not in self.missing_keys:
|
||||||
"The {} variable is not set. Substituting a blank string."
|
log.warn(
|
||||||
.format(key)
|
"The {} variable is not set. Substituting a blank string."
|
||||||
)
|
.format(key)
|
||||||
|
)
|
||||||
|
self.missing_keys.append(key)
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
@ -198,6 +198,30 @@ class InterpolationTest(unittest.TestCase):
|
|||||||
}
|
}
|
||||||
])
|
])
|
||||||
|
|
||||||
|
@mock.patch.dict(os.environ)
|
||||||
|
def test_unset_variable_produces_warning(self):
|
||||||
|
os.environ.pop('FOO', None)
|
||||||
|
os.environ.pop('BAR', None)
|
||||||
|
config_details = config.ConfigDetails(
|
||||||
|
config={
|
||||||
|
'web': {
|
||||||
|
'image': '${FOO}',
|
||||||
|
'command': '${BAR}',
|
||||||
|
'entrypoint': '${BAR}',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
working_dir='.',
|
||||||
|
filename=None,
|
||||||
|
)
|
||||||
|
|
||||||
|
with mock.patch('compose.config.interpolation.log') as log:
|
||||||
|
config.load(config_details)
|
||||||
|
|
||||||
|
self.assertEqual(2, log.warn.call_count)
|
||||||
|
warnings = sorted(args[0][0] for args in log.warn.call_args_list)
|
||||||
|
self.assertIn('BAR', warnings[0])
|
||||||
|
self.assertIn('FOO', warnings[1])
|
||||||
|
|
||||||
@mock.patch.dict(os.environ)
|
@mock.patch.dict(os.environ)
|
||||||
def test_invalid_interpolation(self):
|
def test_invalid_interpolation(self):
|
||||||
with self.assertRaises(config.ConfigurationError) as cm:
|
with self.assertRaises(config.ConfigurationError) as cm:
|
||||||
|
@ -1,31 +1,32 @@
|
|||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from compose.config.interpolation import interpolate, InvalidInterpolation
|
from compose.config.interpolation import interpolate, InvalidInterpolation
|
||||||
|
from compose.config.interpolation import BlankDefaultDict as bddict
|
||||||
|
|
||||||
|
|
||||||
class InterpolationTest(unittest.TestCase):
|
class InterpolationTest(unittest.TestCase):
|
||||||
def test_valid_interpolations(self):
|
def test_valid_interpolations(self):
|
||||||
self.assertEqual(interpolate('$foo', dict(foo='hi')), 'hi')
|
self.assertEqual(interpolate('$foo', bddict(foo='hi')), 'hi')
|
||||||
self.assertEqual(interpolate('${foo}', dict(foo='hi')), 'hi')
|
self.assertEqual(interpolate('${foo}', bddict(foo='hi')), 'hi')
|
||||||
|
|
||||||
self.assertEqual(interpolate('${subject} love you', dict(subject='i')), 'i love you')
|
self.assertEqual(interpolate('${subject} love you', bddict(subject='i')), 'i love you')
|
||||||
self.assertEqual(interpolate('i ${verb} you', dict(verb='love')), 'i love you')
|
self.assertEqual(interpolate('i ${verb} you', bddict(verb='love')), 'i love you')
|
||||||
self.assertEqual(interpolate('i love ${object}', dict(object='you')), 'i love you')
|
self.assertEqual(interpolate('i love ${object}', bddict(object='you')), 'i love you')
|
||||||
|
|
||||||
def test_empty_value(self):
|
def test_empty_value(self):
|
||||||
self.assertEqual(interpolate('${foo}', dict(foo='')), '')
|
self.assertEqual(interpolate('${foo}', bddict(foo='')), '')
|
||||||
|
|
||||||
def test_unset_value(self):
|
def test_unset_value(self):
|
||||||
self.assertEqual(interpolate('${foo}', dict()), '')
|
self.assertEqual(interpolate('${foo}', bddict()), '')
|
||||||
|
|
||||||
def test_escaped_interpolation(self):
|
def test_escaped_interpolation(self):
|
||||||
self.assertEqual(interpolate('$${foo}', dict(foo='hi')), '${foo}')
|
self.assertEqual(interpolate('$${foo}', bddict(foo='hi')), '${foo}')
|
||||||
|
|
||||||
def test_invalid_strings(self):
|
def test_invalid_strings(self):
|
||||||
self.assertRaises(InvalidInterpolation, lambda: interpolate('${', dict()))
|
self.assertRaises(InvalidInterpolation, lambda: interpolate('${', bddict()))
|
||||||
self.assertRaises(InvalidInterpolation, lambda: interpolate('$}', dict()))
|
self.assertRaises(InvalidInterpolation, lambda: interpolate('$}', bddict()))
|
||||||
self.assertRaises(InvalidInterpolation, lambda: interpolate('${}', dict()))
|
self.assertRaises(InvalidInterpolation, lambda: interpolate('${}', bddict()))
|
||||||
self.assertRaises(InvalidInterpolation, lambda: interpolate('${ }', dict()))
|
self.assertRaises(InvalidInterpolation, lambda: interpolate('${ }', bddict()))
|
||||||
self.assertRaises(InvalidInterpolation, lambda: interpolate('${ foo}', dict()))
|
self.assertRaises(InvalidInterpolation, lambda: interpolate('${ foo}', bddict()))
|
||||||
self.assertRaises(InvalidInterpolation, lambda: interpolate('${foo }', dict()))
|
self.assertRaises(InvalidInterpolation, lambda: interpolate('${foo }', bddict()))
|
||||||
self.assertRaises(InvalidInterpolation, lambda: interpolate('${foo!}', dict()))
|
self.assertRaises(InvalidInterpolation, lambda: interpolate('${foo!}', bddict()))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user