Merge pull request #4590 from shin-/3474-self-referencing-ext-links

Prevent service to create a container if it is referencing itself in an external link
This commit is contained in:
Joffrey F 2017-03-09 15:36:48 -08:00 committed by GitHub
commit 4e92b5bb85
2 changed files with 21 additions and 1 deletions

View File

@ -22,6 +22,7 @@ from . import const
from . import progress_stream
from .config import DOCKER_CONFIG_KEYS
from .config import merge_environment
from .config.errors import DependencyError
from .config.types import ServicePort
from .config.types import VolumeSpec
from .const import DEFAULT_TIMEOUT
@ -872,7 +873,17 @@ class Service(object):
if self.custom_container_name and not one_off:
return self.custom_container_name
return build_container_name(self.project, self.name, number, one_off)
container_name = build_container_name(
self.project, self.name, number, one_off,
)
ext_links_origins = [l.split(':')[0] for l in self.options.get('external_links', [])]
if container_name in ext_links_origins:
raise DependencyError(
'Service {0} has a self-referential external link: {1}'.format(
self.name, container_name
)
)
return container_name
def remove_image(self, image_type):
if not image_type or image_type == ImageType.none:

View File

@ -7,6 +7,7 @@ from docker.errors import APIError
from .. import mock
from .. import unittest
from compose.config.errors import DependencyError
from compose.config.types import ServicePort
from compose.config.types import VolumeFromSpec
from compose.config.types import VolumeSpec
@ -170,6 +171,14 @@ class ServiceTest(unittest.TestCase):
2000000000
)
def test_self_reference_external_link(self):
service = Service(
name='foo',
external_links=['default_foo_1']
)
with self.assertRaises(DependencyError):
service.get_container_name(1)
def test_mem_reservation(self):
self.mock_client.create_host_config.return_value = {}