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:
parent
8098b65576
commit
25ee3f0033
3 changed files with 65 additions and 37 deletions
|
|
@ -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'
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue