Remove extra s from --add-host

linting...
six.string_types
list-of-strings in examples
disallow extra_hosts support for list-of-dicts
A more thorough sets of tests for extra_hosts
Provide better examples
As per @aanand's [comment](https://github.com/docker/compose/pull/1158/files#r28326312)

  I think it'd be better to check `if not isinstance(extra_hosts_line,
  six.string_types)` and raise an error saying `extra_hosts_config must be
  either a list of strings or a string->string mapping`. We shouldn't need
  to do anything special with the list-of-dicts case.
order result to work with assert
use set() instead of sort()

Signed-off-by: CJ <lim@chernjie.com>
This commit is contained in:
CJ 2015-03-24 18:25:09 +08:00
parent 8098b65576
commit 25ee3f0033
3 changed files with 65 additions and 37 deletions

View File

@ -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
)

View File

@ -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

View File

@ -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'