diff --git a/compose/config/config.py b/compose/config/config.py index b5646a479..44c401d4b 100644 --- a/compose/config/config.py +++ b/compose/config/config.py @@ -186,8 +186,16 @@ class ServiceLoader(object): already_seen=other_already_seen, ) + base_service = extends_options['service'] other_config = load_yaml(other_config_path) - other_service_dict = other_config[extends_options['service']] + + if base_service not in other_config: + msg = ( + "Cannot extend service '%s' in %s: Service not found" + ) % (base_service, other_config_path) + raise ConfigurationError(msg) + + other_service_dict = other_config[base_service] other_loader.detect_cycle(extends_options['service']) other_service_dict = other_loader.make_service_dict( service_dict['name'], diff --git a/tests/fixtures/extends/nonexistent-service.yml b/tests/fixtures/extends/nonexistent-service.yml new file mode 100644 index 000000000..e9e17f1bd --- /dev/null +++ b/tests/fixtures/extends/nonexistent-service.yml @@ -0,0 +1,4 @@ +web: + image: busybox + extends: + service: foo diff --git a/tests/unit/config_test.py b/tests/unit/config_test.py index 861d36bda..3e3e9e34a 100644 --- a/tests/unit/config_test.py +++ b/tests/unit/config_test.py @@ -917,6 +917,11 @@ class ExtendsTest(unittest.TestCase): }, ]) + def test_load_throws_error_when_base_service_does_not_exist(self): + err_msg = r'''Cannot extend service 'foo' in .*: Service not found''' + with self.assertRaisesRegexp(ConfigurationError, err_msg): + load_from_filename('tests/fixtures/extends/nonexistent-service.yml') + class BuildPathTest(unittest.TestCase): def setUp(self):