diff --git a/compose/service.py b/compose/service.py index f2dbd746e..e668dc493 100644 --- a/compose/service.py +++ b/compose/service.py @@ -626,20 +626,25 @@ def split_port(port): def build_extra_hosts(extra_hosts_config): - if extra_hosts_config is None: - return None + if not extra_hosts_config: + return {} - if isinstance(extra_hosts_config, basestring): - extra_hosts_config = [extra_hosts_config] - - extra_hosts_dict = {} - for extra_hosts_line in extra_hosts_config: - if isinstance(extra_hosts_line, dict): - # already interpreted as a dict (depends on pyyaml version) - extra_hosts_dict.update(extra_hosts_line) - else: - # not already interpreted as a dict + if isinstance(extra_hosts_config, list): + extra_hosts_dict = {} + for extra_hosts_line in extra_hosts_config: + if not isinstance(extra_hosts_line, six.string_types): + raise ConfigError( + "extra_hosts_config \"%s\" must be either a list of strings or a string->string mapping," % + extra_hosts_config + ) host, ip = extra_hosts_line.split(':') extra_hosts_dict.update({host.strip(): ip.strip()}) + extra_hosts_config = extra_hosts_dict - return extra_hosts_dict + if isinstance(extra_hosts_config, dict): + return extra_hosts_config + + raise ConfigError( + "extra_hosts_config \"%s\" must be either a list of strings or a string->string mapping," % + extra_hosts_config + ) diff --git a/docs/yml.md b/docs/yml.md index 8756b2020..82aeed128 100644 --- a/docs/yml.md +++ b/docs/yml.md @@ -89,19 +89,19 @@ external_links: ### extra_hosts -Add hostname mappings. Use the same values as the docker client `--add-hosts` parameter. +Add hostname mappings. Use the same values as the docker client `--add-host` parameter. ``` extra_hosts: - - docker: 162.242.195.82 - - fig: 50.31.209.229 + - "somehost:162.242.195.82" + - "otherhost:50.31.209.229" ``` An entry with the ip address and hostname will be created in `/etc/hosts` inside containers for this service, e.g: ``` -162.242.195.82 docker -50.31.209.229 fig +162.242.195.82 somehost +50.31.209.229 otherhost ``` ### ports diff --git a/tests/integration/service_test.py b/tests/integration/service_test.py index f71d609da..3fbf546c0 100644 --- a/tests/integration/service_test.py +++ b/tests/integration/service_test.py @@ -5,8 +5,11 @@ from os import path import mock from compose import Service -from compose.service import CannotBeScaledError -from compose.service import build_extra_hosts +from compose.service import ( + CannotBeScaledError, + build_extra_hosts, + ConfigError, +) from compose.container import Container from docker.errors import APIError from .testcases import DockerClientTestCase @@ -110,39 +113,59 @@ class ServiceTest(DockerClientTestCase): def test_build_extra_hosts(self): # string - self.assertEqual(build_extra_hosts("www.example.com: 192.168.0.17"), - {'www.example.com': '192.168.0.17'}) + self.assertRaises(ConfigError, lambda: build_extra_hosts("www.example.com: 192.168.0.17")) # list of strings self.assertEqual(build_extra_hosts( - ["www.example.com: 192.168.0.17"]), - {'www.example.com': '192.168.0.17'}) + ["www.example.com:192.168.0.17"]), + {'www.example.com': '192.168.0.17'}) self.assertEqual(build_extra_hosts( - ["www.example.com: 192.168.0.17", - "api.example.com: 192.168.0.18"]), - {'www.example.com': '192.168.0.17', - 'api.example.com': '192.168.0.18'}) + ["www.example.com: 192.168.0.17"]), + {'www.example.com': '192.168.0.17'}) + self.assertEqual(build_extra_hosts( + ["www.example.com: 192.168.0.17", + "static.example.com:192.168.0.19", + "api.example.com: 192.168.0.18"]), + {'www.example.com': '192.168.0.17', + 'static.example.com': '192.168.0.19', + 'api.example.com': '192.168.0.18'}) + # list of dictionaries + self.assertRaises(ConfigError, lambda: build_extra_hosts( + [{'www.example.com': '192.168.0.17'}, + {'api.example.com': '192.168.0.18'}])) + + # dictionaries self.assertEqual(build_extra_hosts( - [{'www.example.com': '192.168.0.17'}, - {'api.example.com': '192.168.0.18'} - ]), - {'www.example.com': '192.168.0.17', - 'api.example.com': '192.168.0.18'}) + {'www.example.com': '192.168.0.17', + 'api.example.com': '192.168.0.18'}), + {'www.example.com': '192.168.0.17', + 'api.example.com': '192.168.0.18'}) def test_create_container_with_extra_hosts_list(self): - extra_hosts = ['docker:162.242.195.82', 'fig:50.31.209.229'] + extra_hosts = ['somehost:162.242.195.82', 'otherhost:50.31.209.229'] service = self.create_service('db', extra_hosts=extra_hosts) container = service.create_container() service.start_container(container) - self.assertEqual(container.get('HostConfig.ExtraHosts'), extra_hosts) + self.assertEqual(set(container.get('HostConfig.ExtraHosts')), set(extra_hosts)) def test_create_container_with_extra_hosts_string(self): - extra_hosts = 'docker:162.242.195.82' + extra_hosts = 'somehost:162.242.195.82' + service = self.create_service('db', extra_hosts=extra_hosts) + self.assertRaises(ConfigError, lambda: service.create_container()) + + def test_create_container_with_extra_hosts_list_of_dicts(self): + extra_hosts = [{'somehost': '162.242.195.82'}, {'otherhost': '50.31.209.229'}] + service = self.create_service('db', extra_hosts=extra_hosts) + self.assertRaises(ConfigError, lambda: service.create_container()) + + def test_create_container_with_extra_hosts_dicts(self): + extra_hosts = {'somehost': '162.242.195.82', 'otherhost': '50.31.209.229'} + extra_hosts_list = ['somehost:162.242.195.82', 'otherhost:50.31.209.229'] service = self.create_service('db', extra_hosts=extra_hosts) container = service.create_container() service.start_container(container) - self.assertEqual(container.get('HostConfig.ExtraHosts'), [extra_hosts]) + self.assertEqual(set(container.get('HostConfig.ExtraHosts')), set(extra_hosts_list)) def test_create_container_with_specified_volume(self): host_path = '/tmp/host-path'