Fixes #2008 - re-use list_or_dict schema for all the types
At the same time, moves extra_hosts validation to the config module. Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
parent
12b82a20ff
commit
efec2aae6c
8 changed files with 90 additions and 88 deletions
|
|
@ -22,8 +22,6 @@ from compose.const import LABEL_PROJECT
|
|||
from compose.const import LABEL_SERVICE
|
||||
from compose.const import LABEL_VERSION
|
||||
from compose.container import Container
|
||||
from compose.service import build_extra_hosts
|
||||
from compose.service import ConfigError
|
||||
from compose.service import ConvergencePlan
|
||||
from compose.service import ConvergenceStrategy
|
||||
from compose.service import Net
|
||||
|
|
@ -139,37 +137,6 @@ class ServiceTest(DockerClientTestCase):
|
|||
container.start()
|
||||
self.assertEqual(container.get('HostConfig.CpuShares'), 73)
|
||||
|
||||
def test_build_extra_hosts(self):
|
||||
# string
|
||||
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'})
|
||||
self.assertEqual(build_extra_hosts(
|
||||
["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'})
|
||||
|
||||
def test_create_container_with_extra_hosts_list(self):
|
||||
extra_hosts = ['somehost:162.242.195.82', 'otherhost:50.31.209.229']
|
||||
service = self.create_service('db', extra_hosts=extra_hosts)
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ def service_sort(services):
|
|||
return sorted(services, key=itemgetter('name'))
|
||||
|
||||
|
||||
def build_config_details(contents, working_dir, filename):
|
||||
def build_config_details(contents, working_dir='working_dir', filename='filename.yml'):
|
||||
return config.ConfigDetails(
|
||||
working_dir,
|
||||
[config.ConfigFile(filename, contents)])
|
||||
|
|
@ -512,6 +512,29 @@ class ConfigTest(unittest.TestCase):
|
|||
|
||||
assert 'line 3, column 32' in exc.exconly()
|
||||
|
||||
def test_validate_extra_hosts_invalid(self):
|
||||
with pytest.raises(ConfigurationError) as exc:
|
||||
config.load(build_config_details({
|
||||
'web': {
|
||||
'image': 'alpine',
|
||||
'extra_hosts': "www.example.com: 192.168.0.17",
|
||||
}
|
||||
}))
|
||||
assert "'extra_hosts' contains an invalid type" in exc.exconly()
|
||||
|
||||
def test_validate_extra_hosts_invalid_list(self):
|
||||
with pytest.raises(ConfigurationError) as exc:
|
||||
config.load(build_config_details({
|
||||
'web': {
|
||||
'image': 'alpine',
|
||||
'extra_hosts': [
|
||||
{'www.example.com': '192.168.0.17'},
|
||||
{'api.example.com': '192.168.0.18'}
|
||||
],
|
||||
}
|
||||
}))
|
||||
assert "which is an invalid type" in exc.exconly()
|
||||
|
||||
|
||||
class InterpolationTest(unittest.TestCase):
|
||||
@mock.patch.dict(os.environ)
|
||||
|
|
|
|||
29
tests/unit/config/types_test.py
Normal file
29
tests/unit/config/types_test.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
from compose.config.types import parse_extra_hosts
|
||||
|
||||
|
||||
def test_parse_extra_hosts_list():
|
||||
expected = {'www.example.com': '192.168.0.17'}
|
||||
assert parse_extra_hosts(["www.example.com:192.168.0.17"]) == expected
|
||||
|
||||
expected = {'www.example.com': '192.168.0.17'}
|
||||
assert parse_extra_hosts(["www.example.com: 192.168.0.17"]) == expected
|
||||
|
||||
assert parse_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'
|
||||
}
|
||||
|
||||
|
||||
def test_parse_extra_hosts_dict():
|
||||
assert parse_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'
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue