From 8b920494327852a70b6b61bfd9a8d29bdf9b63c9 Mon Sep 17 00:00:00 2001 From: Joffrey F Date: Wed, 22 Feb 2017 16:21:23 -0800 Subject: [PATCH] Detect the service that causes the invalid service name error Signed-off-by: Joffrey F --- compose/config/validation.py | 9 ++++++--- tests/unit/config/config_test.py | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/compose/config/validation.py b/compose/config/validation.py index 3f23f0a7a..d4d29565f 100644 --- a/compose/config/validation.py +++ b/compose/config/validation.py @@ -211,9 +211,12 @@ def handle_error_for_schema_with_id(error, path): if is_service_dict_schema(schema_id) and error.validator == 'additionalProperties': return "Invalid service name '{}' - only {} characters are allowed".format( - # The service_name is the key to the json object - list(error.instance)[0], - VALID_NAME_CHARS) + # The service_name is one of the keys in the json object + [i for i in list(error.instance) if not i or any(filter( + lambda c: not re.match(VALID_NAME_CHARS, c), i + ))][0], + VALID_NAME_CHARS + ) if error.validator == 'additionalProperties': if schema_id == '#/definitions/service': diff --git a/tests/unit/config/config_test.py b/tests/unit/config/config_test.py index c26272d9e..e5104f769 100644 --- a/tests/unit/config/config_test.py +++ b/tests/unit/config/config_test.py @@ -554,6 +554,20 @@ class ConfigTest(unittest.TestCase): excinfo.exconly() ) + def test_config_invalid_service_name_raise_validation_error(self): + with pytest.raises(ConfigurationError) as excinfo: + config.load( + build_config_details({ + 'version': '2', + 'services': { + 'test_app': {'build': '.'}, + 'mong\\o': {'image': 'mongo'}, + } + }) + ) + + assert 'Invalid service name \'mong\\o\'' in excinfo.exconly() + def test_load_with_multiple_files_v1(self): base_file = config.ConfigFile( 'base.yaml',