Extract YAML loading and parsing into config module
Signed-off-by: Aanand Prasad <aanand.prasad@gmail.com>
This commit is contained in:
parent
25c70c2af4
commit
4ecf5e01ff
11 changed files with 358 additions and 284 deletions
|
|
@ -1,14 +1,15 @@
|
|||
from __future__ import unicode_literals
|
||||
from compose.project import Project, ConfigurationError
|
||||
from compose import config
|
||||
from compose.project import Project
|
||||
from compose.container import Container
|
||||
from .testcases import DockerClientTestCase
|
||||
|
||||
|
||||
class ProjectTest(DockerClientTestCase):
|
||||
def test_volumes_from_service(self):
|
||||
project = Project.from_config(
|
||||
project = Project.from_dicts(
|
||||
name='composetest',
|
||||
config={
|
||||
service_dicts=config.from_dictionary({
|
||||
'data': {
|
||||
'image': 'busybox:latest',
|
||||
'volumes': ['/var/data'],
|
||||
|
|
@ -17,7 +18,7 @@ class ProjectTest(DockerClientTestCase):
|
|||
'image': 'busybox:latest',
|
||||
'volumes_from': ['data'],
|
||||
},
|
||||
},
|
||||
}),
|
||||
client=self.client,
|
||||
)
|
||||
db = project.get_service('db')
|
||||
|
|
@ -31,14 +32,14 @@ class ProjectTest(DockerClientTestCase):
|
|||
volumes=['/var/data'],
|
||||
name='composetest_data_container',
|
||||
)
|
||||
project = Project.from_config(
|
||||
project = Project.from_dicts(
|
||||
name='composetest',
|
||||
config={
|
||||
service_dicts=config.from_dictionary({
|
||||
'db': {
|
||||
'image': 'busybox:latest',
|
||||
'volumes_from': ['composetest_data_container'],
|
||||
},
|
||||
},
|
||||
}),
|
||||
client=self.client,
|
||||
)
|
||||
db = project.get_service('db')
|
||||
|
|
@ -48,9 +49,9 @@ class ProjectTest(DockerClientTestCase):
|
|||
project.remove_stopped()
|
||||
|
||||
def test_net_from_service(self):
|
||||
project = Project.from_config(
|
||||
project = Project.from_dicts(
|
||||
name='composetest',
|
||||
config={
|
||||
service_dicts=config.from_dictionary({
|
||||
'net': {
|
||||
'image': 'busybox:latest',
|
||||
'command': ["/bin/sleep", "300"]
|
||||
|
|
@ -59,8 +60,8 @@ class ProjectTest(DockerClientTestCase):
|
|||
'image': 'busybox:latest',
|
||||
'net': 'container:net',
|
||||
'command': ["/bin/sleep", "300"]
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
client=self.client,
|
||||
)
|
||||
|
||||
|
|
@ -82,14 +83,14 @@ class ProjectTest(DockerClientTestCase):
|
|||
)
|
||||
net_container.start()
|
||||
|
||||
project = Project.from_config(
|
||||
project = Project.from_dicts(
|
||||
name='composetest',
|
||||
config={
|
||||
service_dicts=config.from_dictionary({
|
||||
'web': {
|
||||
'image': 'busybox:latest',
|
||||
'net': 'container:composetest_net_container'
|
||||
},
|
||||
},
|
||||
}),
|
||||
client=self.client,
|
||||
)
|
||||
|
||||
|
|
@ -257,9 +258,9 @@ class ProjectTest(DockerClientTestCase):
|
|||
project.remove_stopped()
|
||||
|
||||
def test_project_up_starts_depends(self):
|
||||
project = Project.from_config(
|
||||
project = Project.from_dicts(
|
||||
name='composetest',
|
||||
config={
|
||||
service_dicts=config.from_dictionary({
|
||||
'console': {
|
||||
'image': 'busybox:latest',
|
||||
'command': ["/bin/sleep", "300"],
|
||||
|
|
@ -279,7 +280,7 @@ class ProjectTest(DockerClientTestCase):
|
|||
'net': 'container:net',
|
||||
'links': ['app']
|
||||
},
|
||||
},
|
||||
}),
|
||||
client=self.client,
|
||||
)
|
||||
project.start()
|
||||
|
|
@ -296,9 +297,9 @@ class ProjectTest(DockerClientTestCase):
|
|||
project.remove_stopped()
|
||||
|
||||
def test_project_up_with_no_deps(self):
|
||||
project = Project.from_config(
|
||||
project = Project.from_dicts(
|
||||
name='composetest',
|
||||
config={
|
||||
service_dicts=config.from_dictionary({
|
||||
'console': {
|
||||
'image': 'busybox:latest',
|
||||
'command': ["/bin/sleep", "300"],
|
||||
|
|
@ -324,7 +325,7 @@ class ProjectTest(DockerClientTestCase):
|
|||
'links': ['app'],
|
||||
'volumes_from': ['vol']
|
||||
},
|
||||
},
|
||||
}),
|
||||
client=self.client,
|
||||
)
|
||||
project.start()
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ from __future__ import unicode_literals
|
|||
from __future__ import absolute_import
|
||||
import os
|
||||
from os import path
|
||||
import mock
|
||||
|
||||
from compose import Service
|
||||
from compose.service import CannotBeScaledError
|
||||
|
|
@ -481,16 +482,12 @@ class ServiceTest(DockerClientTestCase):
|
|||
for k,v in {'ONE': '1', 'TWO': '2', 'THREE': '3', 'FOO': 'baz', 'DOO': 'dah'}.items():
|
||||
self.assertEqual(env[k], v)
|
||||
|
||||
@mock.patch.dict(os.environ)
|
||||
def test_resolve_env(self):
|
||||
service = self.create_service('web', environment={'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': None, 'NO_DEF': None})
|
||||
os.environ['FILE_DEF'] = 'E1'
|
||||
os.environ['FILE_DEF_EMPTY'] = 'E2'
|
||||
os.environ['ENV_DEF'] = 'E3'
|
||||
try:
|
||||
env = create_and_start_container(service).environment
|
||||
for k,v in {'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': ''}.items():
|
||||
self.assertEqual(env[k], v)
|
||||
finally:
|
||||
del os.environ['FILE_DEF']
|
||||
del os.environ['FILE_DEF_EMPTY']
|
||||
del os.environ['ENV_DEF']
|
||||
service = self.create_service('web', environment={'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': None, 'NO_DEF': None})
|
||||
env = create_and_start_container(service).environment
|
||||
for k,v in {'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': ''}.items():
|
||||
self.assertEqual(env[k], v)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
from __future__ import unicode_literals
|
||||
from __future__ import absolute_import
|
||||
from compose.service import Service
|
||||
from compose.config import make_service_dict
|
||||
from compose.cli.docker_client import docker_client
|
||||
from compose.progress_stream import stream_output
|
||||
from .. import unittest
|
||||
|
|
@ -21,14 +22,15 @@ class DockerClientTestCase(unittest.TestCase):
|
|||
self.client.remove_image(i)
|
||||
|
||||
def create_service(self, name, **kwargs):
|
||||
kwargs['image'] = "busybox:latest"
|
||||
|
||||
if 'command' not in kwargs:
|
||||
kwargs['command'] = ["/bin/sleep", "300"]
|
||||
|
||||
return Service(
|
||||
project='composetest',
|
||||
name=name,
|
||||
client=self.client,
|
||||
image="busybox:latest",
|
||||
**kwargs
|
||||
**make_service_dict(name, kwargs)
|
||||
)
|
||||
|
||||
def check_build(self, *args, **kwargs):
|
||||
|
|
|
|||
134
tests/unit/config_test.py
Normal file
134
tests/unit/config_test.py
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
import os
|
||||
import mock
|
||||
from .. import unittest
|
||||
|
||||
from compose import config
|
||||
|
||||
class ConfigTest(unittest.TestCase):
|
||||
def test_from_dictionary(self):
|
||||
service_dicts = config.from_dictionary({
|
||||
'foo': {'image': 'busybox'},
|
||||
'bar': {'environment': ['FOO=1']},
|
||||
})
|
||||
|
||||
self.assertEqual(
|
||||
sorted(service_dicts, key=lambda d: d['name']),
|
||||
sorted([
|
||||
{
|
||||
'name': 'bar',
|
||||
'environment': {'FOO': '1'},
|
||||
},
|
||||
{
|
||||
'name': 'foo',
|
||||
'image': 'busybox',
|
||||
}
|
||||
])
|
||||
)
|
||||
|
||||
def test_from_dictionary_throws_error_when_not_dict(self):
|
||||
with self.assertRaises(config.ConfigurationError):
|
||||
config.from_dictionary({
|
||||
'web': 'busybox:latest',
|
||||
})
|
||||
|
||||
def test_config_validation(self):
|
||||
self.assertRaises(
|
||||
config.ConfigurationError,
|
||||
lambda: config.make_service_dict('foo', {'port': ['8000']})
|
||||
)
|
||||
config.make_service_dict('foo', {'ports': ['8000']})
|
||||
|
||||
def test_parse_environment_as_list(self):
|
||||
environment =[
|
||||
'NORMAL=F1',
|
||||
'CONTAINS_EQUALS=F=2',
|
||||
'TRAILING_EQUALS=',
|
||||
]
|
||||
self.assertEqual(
|
||||
config.parse_environment(environment),
|
||||
{'NORMAL': 'F1', 'CONTAINS_EQUALS': 'F=2', 'TRAILING_EQUALS': ''},
|
||||
)
|
||||
|
||||
def test_parse_environment_as_dict(self):
|
||||
environment = {
|
||||
'NORMAL': 'F1',
|
||||
'CONTAINS_EQUALS': 'F=2',
|
||||
'TRAILING_EQUALS': None,
|
||||
}
|
||||
self.assertEqual(config.parse_environment(environment), environment)
|
||||
|
||||
def test_parse_environment_invalid(self):
|
||||
with self.assertRaises(config.ConfigurationError):
|
||||
config.parse_environment('a=b')
|
||||
|
||||
def test_parse_environment_empty(self):
|
||||
self.assertEqual(config.parse_environment(None), {})
|
||||
|
||||
@mock.patch.dict(os.environ)
|
||||
def test_resolve_environment(self):
|
||||
os.environ['FILE_DEF'] = 'E1'
|
||||
os.environ['FILE_DEF_EMPTY'] = 'E2'
|
||||
os.environ['ENV_DEF'] = 'E3'
|
||||
|
||||
service_dict = config.make_service_dict(
|
||||
'foo',
|
||||
{
|
||||
'environment': {
|
||||
'FILE_DEF': 'F1',
|
||||
'FILE_DEF_EMPTY': '',
|
||||
'ENV_DEF': None,
|
||||
'NO_DEF': None
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
service_dict['environment'],
|
||||
{'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': ''},
|
||||
)
|
||||
|
||||
def test_env_from_file(self):
|
||||
service_dict = config.make_service_dict(
|
||||
'foo',
|
||||
{'env_file': 'tests/fixtures/env/one.env'},
|
||||
)
|
||||
self.assertEqual(
|
||||
service_dict['environment'],
|
||||
{'ONE': '2', 'TWO': '1', 'THREE': '3', 'FOO': 'bar'},
|
||||
)
|
||||
|
||||
def test_env_from_multiple_files(self):
|
||||
service_dict = config.make_service_dict(
|
||||
'foo',
|
||||
{
|
||||
'env_file': [
|
||||
'tests/fixtures/env/one.env',
|
||||
'tests/fixtures/env/two.env',
|
||||
],
|
||||
},
|
||||
)
|
||||
self.assertEqual(
|
||||
service_dict['environment'],
|
||||
{'ONE': '2', 'TWO': '1', 'THREE': '3', 'FOO': 'baz', 'DOO': 'dah'},
|
||||
)
|
||||
|
||||
def test_env_nonexistent_file(self):
|
||||
options = {'env_file': 'tests/fixtures/env/nonexistent.env'}
|
||||
self.assertRaises(
|
||||
config.ConfigurationError,
|
||||
lambda: config.make_service_dict('foo', options),
|
||||
)
|
||||
|
||||
@mock.patch.dict(os.environ)
|
||||
def test_resolve_environment_from_file(self):
|
||||
os.environ['FILE_DEF'] = 'E1'
|
||||
os.environ['FILE_DEF_EMPTY'] = 'E2'
|
||||
os.environ['ENV_DEF'] = 'E3'
|
||||
service_dict = config.make_service_dict(
|
||||
'foo',
|
||||
{'env_file': 'tests/fixtures/env/resolve.env'},
|
||||
)
|
||||
self.assertEqual(
|
||||
service_dict['environment'],
|
||||
{'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': ''},
|
||||
)
|
||||
|
|
@ -1,8 +1,9 @@
|
|||
from __future__ import unicode_literals
|
||||
from .. import unittest
|
||||
from compose.service import Service
|
||||
from compose.project import Project, ConfigurationError
|
||||
from compose.project import Project
|
||||
from compose.container import Container
|
||||
from compose import config
|
||||
|
||||
import mock
|
||||
import docker
|
||||
|
|
@ -49,26 +50,21 @@ class ProjectTest(unittest.TestCase):
|
|||
self.assertEqual(project.services[2].name, 'web')
|
||||
|
||||
def test_from_config(self):
|
||||
project = Project.from_config('composetest', {
|
||||
dicts = config.from_dictionary({
|
||||
'web': {
|
||||
'image': 'busybox:latest',
|
||||
},
|
||||
'db': {
|
||||
'image': 'busybox:latest',
|
||||
},
|
||||
}, None)
|
||||
})
|
||||
project = Project.from_dicts('composetest', dicts, None)
|
||||
self.assertEqual(len(project.services), 2)
|
||||
self.assertEqual(project.get_service('web').name, 'web')
|
||||
self.assertEqual(project.get_service('web').options['image'], 'busybox:latest')
|
||||
self.assertEqual(project.get_service('db').name, 'db')
|
||||
self.assertEqual(project.get_service('db').options['image'], 'busybox:latest')
|
||||
|
||||
def test_from_config_throws_error_when_not_dict(self):
|
||||
with self.assertRaises(ConfigurationError):
|
||||
project = Project.from_config('composetest', {
|
||||
'web': 'busybox:latest',
|
||||
}, None)
|
||||
|
||||
def test_get_service(self):
|
||||
web = Service(
|
||||
project='composetest',
|
||||
|
|
|
|||
|
|
@ -16,7 +16,6 @@ from compose.service import (
|
|||
build_port_bindings,
|
||||
build_volume_binding,
|
||||
get_container_name,
|
||||
parse_environment,
|
||||
parse_repository_tag,
|
||||
parse_volume_spec,
|
||||
split_port,
|
||||
|
|
@ -47,10 +46,6 @@ class ServiceTest(unittest.TestCase):
|
|||
self.assertRaises(ConfigError, lambda: Service(name='foo', project='_'))
|
||||
Service(name='foo', project='bar')
|
||||
|
||||
def test_config_validation(self):
|
||||
self.assertRaises(ConfigError, lambda: Service(name='foo', port=['8000']))
|
||||
Service(name='foo', ports=['8000'])
|
||||
|
||||
def test_get_container_name(self):
|
||||
self.assertIsNone(get_container_name({}))
|
||||
self.assertEqual(get_container_name({'Name': 'myproject_db_1'}), 'myproject_db_1')
|
||||
|
|
@ -321,98 +316,3 @@ class ServiceVolumesTest(unittest.TestCase):
|
|||
binding,
|
||||
('/home/user', dict(bind='/home/user', ro=False)))
|
||||
|
||||
class ServiceEnvironmentTest(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.mock_client = mock.create_autospec(docker.Client)
|
||||
self.mock_client.containers.return_value = []
|
||||
|
||||
def test_parse_environment_as_list(self):
|
||||
environment =[
|
||||
'NORMAL=F1',
|
||||
'CONTAINS_EQUALS=F=2',
|
||||
'TRAILING_EQUALS='
|
||||
]
|
||||
self.assertEqual(
|
||||
parse_environment(environment),
|
||||
{'NORMAL': 'F1', 'CONTAINS_EQUALS': 'F=2', 'TRAILING_EQUALS': ''})
|
||||
|
||||
def test_parse_environment_as_dict(self):
|
||||
environment = {
|
||||
'NORMAL': 'F1',
|
||||
'CONTAINS_EQUALS': 'F=2',
|
||||
'TRAILING_EQUALS': None,
|
||||
}
|
||||
self.assertEqual(parse_environment(environment), environment)
|
||||
|
||||
def test_parse_environment_invalid(self):
|
||||
with self.assertRaises(ConfigError):
|
||||
parse_environment('a=b')
|
||||
|
||||
def test_parse_environment_empty(self):
|
||||
self.assertEqual(parse_environment(None), {})
|
||||
|
||||
@mock.patch.dict(os.environ)
|
||||
def test_resolve_environment(self):
|
||||
os.environ['FILE_DEF'] = 'E1'
|
||||
os.environ['FILE_DEF_EMPTY'] = 'E2'
|
||||
os.environ['ENV_DEF'] = 'E3'
|
||||
service = Service(
|
||||
'foo',
|
||||
environment={
|
||||
'FILE_DEF': 'F1',
|
||||
'FILE_DEF_EMPTY': '',
|
||||
'ENV_DEF': None,
|
||||
'NO_DEF': None
|
||||
},
|
||||
client=self.mock_client,
|
||||
image='image_name',
|
||||
)
|
||||
options = service._get_container_create_options({})
|
||||
self.assertEqual(
|
||||
options['environment'],
|
||||
{'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': ''}
|
||||
)
|
||||
|
||||
def test_env_from_file(self):
|
||||
service = Service('foo',
|
||||
env_file='tests/fixtures/env/one.env',
|
||||
client=self.mock_client,
|
||||
image='image_name',
|
||||
)
|
||||
options = service._get_container_create_options({})
|
||||
self.assertEqual(
|
||||
options['environment'],
|
||||
{'ONE': '2', 'TWO': '1', 'THREE': '3', 'FOO': 'bar'}
|
||||
)
|
||||
|
||||
def test_env_from_multiple_files(self):
|
||||
service = Service('foo',
|
||||
env_file=['tests/fixtures/env/one.env', 'tests/fixtures/env/two.env'],
|
||||
client=self.mock_client,
|
||||
image='image_name',
|
||||
)
|
||||
options = service._get_container_create_options({})
|
||||
self.assertEqual(
|
||||
options['environment'],
|
||||
{'ONE': '2', 'TWO': '1', 'THREE': '3', 'FOO': 'baz', 'DOO': 'dah'}
|
||||
)
|
||||
|
||||
def test_env_nonexistent_file(self):
|
||||
self.assertRaises(ConfigError, lambda: Service('foo', env_file='tests/fixtures/env/nonexistent.env'))
|
||||
|
||||
@mock.patch.dict(os.environ)
|
||||
def test_resolve_environment_from_file(self):
|
||||
os.environ['FILE_DEF'] = 'E1'
|
||||
os.environ['FILE_DEF_EMPTY'] = 'E2'
|
||||
os.environ['ENV_DEF'] = 'E3'
|
||||
service = Service('foo',
|
||||
env_file=['tests/fixtures/env/resolve.env'],
|
||||
client=self.mock_client,
|
||||
image='image_name',
|
||||
)
|
||||
options = service._get_container_create_options({})
|
||||
self.assertEqual(
|
||||
options['environment'],
|
||||
{'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': ''}
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue