Merge pull request #2712 from jzvelc/fix_v2_service_extend

Fix `extends` when using v2 config format
This commit is contained in:
Aanand Prasad 2016-01-21 17:06:21 +00:00
commit 7a4fdfd034
2 changed files with 32 additions and 4 deletions

View File

@ -136,6 +136,9 @@ class ConfigFile(namedtuple('_ConfigFile', 'filename config')):
return 1
return version
def get_service(self, name):
return self.get_service_dicts()[name]
def get_service_dicts(self):
return self.config if self.version == 1 else self.config.get('services', {})
@ -354,19 +357,19 @@ def process_config_file(config_file, service_name=None):
if config_file.version == 2:
processed_config = dict(config_file.config)
processed_config['services'] = interpolated_config
processed_config['services'] = services = interpolated_config
processed_config['volumes'] = interpolate_environment_variables(
config_file.get_volumes(), 'volume')
processed_config['networks'] = interpolate_environment_variables(
config_file.get_networks(), 'network')
if config_file.version == 1:
processed_config = interpolated_config
processed_config = services = interpolated_config
config_file = config_file._replace(config=processed_config)
validate_against_fields_schema(config_file)
if service_name and service_name not in processed_config:
if service_name and service_name not in services:
raise ConfigurationError(
"Cannot extend service '{}' in {}: Service not found".format(
service_name, config_file.filename))
@ -411,7 +414,8 @@ class ServiceExtendsResolver(object):
extended_file = process_config_file(
extends_file,
service_name=service_name)
service_config = extended_file.config[service_name]
service_config = extended_file.get_service(service_name)
return config_path, service_config, service_name
def resolve_extends(self, extended_config_path, service_dict, service_name):

View File

@ -1937,6 +1937,30 @@ class ExtendsTest(unittest.TestCase):
load_from_filename(str(tmpdir.join('docker-compose.yml')))
assert 'Version mismatch' in exc.exconly()
def test_extends_with_defined_version_passes(self):
tmpdir = py.test.ensuretemp('test_extends_with_defined_version')
self.addCleanup(tmpdir.remove)
tmpdir.join('docker-compose.yml').write("""
version: 2
services:
web:
extends:
file: base.yml
service: base
image: busybox
""")
tmpdir.join('base.yml').write("""
version: 2
services:
base:
volumes: ['/foo']
ports: ['3000:3000']
command: top
""")
service = load_from_filename(str(tmpdir.join('docker-compose.yml')))
self.assertEquals(service[0]['command'], "top")
@pytest.mark.xfail(IS_WINDOWS_PLATFORM, reason='paths use slash')
class ExpandPathTest(unittest.TestCase):