Move volume parsing to config.types module

This removes the last of the old service.ConfigError

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2015-11-13 19:40:10 -05:00
commit 8572d50903
12 changed files with 186 additions and 196 deletions

View file

@ -11,6 +11,7 @@ import pytest
from compose.config import config
from compose.config.errors import ConfigurationError
from compose.config.types import VolumeSpec
from compose.const import IS_WINDOWS_PLATFORM
from tests import mock
from tests import unittest
@ -147,7 +148,7 @@ class ConfigTest(unittest.TestCase):
'name': 'web',
'build': '/',
'links': ['db'],
'volumes': ['/home/user/project:/code'],
'volumes': [VolumeSpec.parse('/home/user/project:/code')],
},
{
'name': 'db',
@ -211,7 +212,7 @@ class ConfigTest(unittest.TestCase):
{
'name': 'web',
'image': 'example/web',
'volumes': ['/home/user/project:/code'],
'volumes': [VolumeSpec.parse('/home/user/project:/code')],
'labels': {'label': 'one'},
},
]
@ -626,14 +627,11 @@ class VolumeConfigTest(unittest.TestCase):
@mock.patch.dict(os.environ)
def test_volume_binding_with_environment_variable(self):
os.environ['VOLUME_PATH'] = '/host/path'
d = config.load(
build_config_details(
{'foo': {'build': '.', 'volumes': ['${VOLUME_PATH}:/container/path']}},
'.',
None,
)
)[0]
self.assertEqual(d['volumes'], ['/host/path:/container/path'])
d = config.load(build_config_details(
{'foo': {'build': '.', 'volumes': ['${VOLUME_PATH}:/container/path']}},
'.',
))[0]
self.assertEqual(d['volumes'], [VolumeSpec.parse('/host/path:/container/path')])
@pytest.mark.skipif(IS_WINDOWS_PLATFORM, reason='posix paths')
@mock.patch.dict(os.environ)
@ -1031,19 +1029,21 @@ class EnvTest(unittest.TestCase):
build_config_details(
{'foo': {'build': '.', 'volumes': ['$HOSTENV:$CONTAINERENV']}},
"tests/fixtures/env",
None,
)
)[0]
self.assertEqual(set(service_dict['volumes']), set(['/tmp:/host/tmp']))
self.assertEqual(
set(service_dict['volumes']),
set([VolumeSpec.parse('/tmp:/host/tmp')]))
service_dict = config.load(
build_config_details(
{'foo': {'build': '.', 'volumes': ['/opt${HOSTENV}:/opt${CONTAINERENV}']}},
"tests/fixtures/env",
None,
)
)[0]
self.assertEqual(set(service_dict['volumes']), set(['/opt/tmp:/opt/host/tmp']))
self.assertEqual(
set(service_dict['volumes']),
set([VolumeSpec.parse('/opt/tmp:/opt/host/tmp')]))
def load_from_filename(filename):
@ -1290,8 +1290,14 @@ class ExtendsTest(unittest.TestCase):
dicts = load_from_filename('tests/fixtures/volume-path/docker-compose.yml')
paths = [
'%s:/foo' % os.path.abspath('tests/fixtures/volume-path/common/foo'),
'%s:/bar' % os.path.abspath('tests/fixtures/volume-path/bar'),
VolumeSpec(
os.path.abspath('tests/fixtures/volume-path/common/foo'),
'/foo',
'rw'),
VolumeSpec(
os.path.abspath('tests/fixtures/volume-path/bar'),
'/bar',
'rw')
]
self.assertEqual(set(dicts[0]['volumes']), set(paths))

View file

@ -1,4 +1,9 @@
import pytest
from compose.config.errors import ConfigurationError
from compose.config.types import parse_extra_hosts
from compose.config.types import VolumeSpec
from compose.const import IS_WINDOWS_PLATFORM
def test_parse_extra_hosts_list():
@ -27,3 +32,35 @@ def test_parse_extra_hosts_dict():
'www.example.com': '192.168.0.17',
'api.example.com': '192.168.0.18'
}
class TestVolumeSpec(object):
def test_parse_volume_spec_only_one_path(self):
spec = VolumeSpec.parse('/the/volume')
assert spec == (None, '/the/volume', 'rw')
def test_parse_volume_spec_internal_and_external(self):
spec = VolumeSpec.parse('external:interval')
assert spec == ('external', 'interval', 'rw')
def test_parse_volume_spec_with_mode(self):
spec = VolumeSpec.parse('external:interval:ro')
assert spec == ('external', 'interval', 'ro')
spec = VolumeSpec.parse('external:interval:z')
assert spec == ('external', 'interval', 'z')
def test_parse_volume_spec_too_many_parts(self):
with pytest.raises(ConfigurationError) as exc:
VolumeSpec.parse('one:two:three:four')
assert 'has incorrect format' in exc.exconly()
@pytest.mark.xfail((not IS_WINDOWS_PLATFORM), reason='does not have a drive')
def test_parse_volume_windows_absolute_path(self):
windows_path = "c:\\Users\\me\\Documents\\shiny\\config:\\opt\\shiny\\config:ro"
assert VolumeSpec.parse(windows_path) == (
"/c/Users/me/Documents/shiny/config",
"/opt/shiny/config",
"ro"
)

View file

@ -2,12 +2,11 @@ from __future__ import absolute_import
from __future__ import unicode_literals
import docker
import pytest
from .. import mock
from .. import unittest
from compose.config.types import VolumeFromSpec
from compose.const import IS_WINDOWS_PLATFORM
from compose.config.types import VolumeSpec
from compose.const import LABEL_CONFIG_HASH
from compose.const import LABEL_ONE_OFF
from compose.const import LABEL_PROJECT
@ -15,7 +14,6 @@ from compose.const import LABEL_SERVICE
from compose.container import Container
from compose.service import build_ulimits
from compose.service import build_volume_binding
from compose.service import ConfigError
from compose.service import ContainerNet
from compose.service import get_container_data_volumes
from compose.service import merge_volume_bindings
@ -23,7 +21,6 @@ from compose.service import NeedsBuildError
from compose.service import Net
from compose.service import NoSuchImageError
from compose.service import parse_repository_tag
from compose.service import parse_volume_spec
from compose.service import Service
from compose.service import ServiceNet
from compose.service import VolumeFromSpec
@ -585,46 +582,12 @@ class ServiceVolumesTest(unittest.TestCase):
def setUp(self):
self.mock_client = mock.create_autospec(docker.Client)
def test_parse_volume_spec_only_one_path(self):
spec = parse_volume_spec('/the/volume')
self.assertEqual(spec, (None, '/the/volume', 'rw'))
def test_parse_volume_spec_internal_and_external(self):
spec = parse_volume_spec('external:interval')
self.assertEqual(spec, ('external', 'interval', 'rw'))
def test_parse_volume_spec_with_mode(self):
spec = parse_volume_spec('external:interval:ro')
self.assertEqual(spec, ('external', 'interval', 'ro'))
spec = parse_volume_spec('external:interval:z')
self.assertEqual(spec, ('external', 'interval', 'z'))
def test_parse_volume_spec_too_many_parts(self):
with self.assertRaises(ConfigError):
parse_volume_spec('one:two:three:four')
@pytest.mark.xfail((not IS_WINDOWS_PLATFORM), reason='does not have a drive')
def test_parse_volume_windows_absolute_path(self):
windows_absolute_path = "c:\\Users\\me\\Documents\\shiny\\config:\\opt\\shiny\\config:ro"
spec = parse_volume_spec(windows_absolute_path)
self.assertEqual(
spec,
(
"/c/Users/me/Documents/shiny/config",
"/opt/shiny/config",
"ro"
)
)
def test_build_volume_binding(self):
binding = build_volume_binding(parse_volume_spec('/outside:/inside'))
self.assertEqual(binding, ('/inside', '/outside:/inside:rw'))
binding = build_volume_binding(VolumeSpec.parse('/outside:/inside'))
assert binding == ('/inside', '/outside:/inside:rw')
def test_get_container_data_volumes(self):
options = [parse_volume_spec(v) for v in [
options = [VolumeSpec.parse(v) for v in [
'/host/volume:/host/volume:ro',
'/new/volume',
'/existing/volume',
@ -648,19 +611,19 @@ class ServiceVolumesTest(unittest.TestCase):
}, has_been_inspected=True)
expected = [
parse_volume_spec('/var/lib/docker/aaaaaaaa:/existing/volume:rw'),
parse_volume_spec('/var/lib/docker/cccccccc:/mnt/image/data:rw'),
VolumeSpec.parse('/var/lib/docker/aaaaaaaa:/existing/volume:rw'),
VolumeSpec.parse('/var/lib/docker/cccccccc:/mnt/image/data:rw'),
]
volumes = get_container_data_volumes(container, options)
self.assertEqual(sorted(volumes), sorted(expected))
assert sorted(volumes) == sorted(expected)
def test_merge_volume_bindings(self):
options = [
'/host/volume:/host/volume:ro',
'/host/rw/volume:/host/rw/volume',
'/new/volume',
'/existing/volume',
VolumeSpec.parse('/host/volume:/host/volume:ro'),
VolumeSpec.parse('/host/rw/volume:/host/rw/volume'),
VolumeSpec.parse('/new/volume'),
VolumeSpec.parse('/existing/volume'),
]
self.mock_client.inspect_image.return_value = {
@ -686,8 +649,8 @@ class ServiceVolumesTest(unittest.TestCase):
'web',
image='busybox',
volumes=[
'/host/path:/data1',
'/host/path:/data2',
VolumeSpec.parse('/host/path:/data1'),
VolumeSpec.parse('/host/path:/data2'),
],
client=self.mock_client,
)
@ -716,7 +679,7 @@ class ServiceVolumesTest(unittest.TestCase):
service = Service(
'web',
image='busybox',
volumes=['/host/path:/data'],
volumes=[VolumeSpec.parse('/host/path:/data')],
client=self.mock_client,
)
@ -784,22 +747,17 @@ class ServiceVolumesTest(unittest.TestCase):
def test_create_with_special_volume_mode(self):
self.mock_client.inspect_image.return_value = {'Id': 'imageid'}
create_calls = []
def create_container(*args, **kwargs):
create_calls.append((args, kwargs))
return {'Id': 'containerid'}
self.mock_client.create_container = create_container
volumes = ['/tmp:/foo:z']
self.mock_client.create_container.return_value = {'Id': 'containerid'}
volume = '/tmp:/foo:z'
Service(
'web',
client=self.mock_client,
image='busybox',
volumes=volumes,
volumes=[VolumeSpec.parse(volume)],
).create_container()
self.assertEqual(len(create_calls), 1)
self.assertEqual(self.mock_client.create_host_config.call_args[1]['binds'], volumes)
assert self.mock_client.create_container.call_count == 1
self.assertEqual(
self.mock_client.create_host_config.call_args[1]['binds'],
[volume])