2014-04-25 23:58:21 +02:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
from __future__ import absolute_import
|
2014-08-26 04:16:37 +02:00
|
|
|
|
2014-04-25 23:58:21 +02:00
|
|
|
from .. import unittest
|
2014-08-26 04:16:37 +02:00
|
|
|
import mock
|
|
|
|
|
2014-07-30 22:11:11 +02:00
|
|
|
import docker
|
2014-10-22 16:57:43 +02:00
|
|
|
from requests import Response
|
2014-08-08 18:41:52 +02:00
|
|
|
|
2015-01-12 15:59:05 +01:00
|
|
|
from compose import Service
|
|
|
|
from compose.container import Container
|
|
|
|
from compose.service import (
|
2015-02-14 20:09:55 +01:00
|
|
|
APIError,
|
2014-08-26 04:16:37 +02:00
|
|
|
ConfigError,
|
2014-11-07 02:54:45 +01:00
|
|
|
build_port_bindings,
|
2014-08-26 04:16:37 +02:00
|
|
|
build_volume_binding,
|
2015-05-10 01:38:53 +02:00
|
|
|
get_container_data_volumes,
|
2015-01-16 14:12:29 +01:00
|
|
|
get_container_name,
|
2015-05-10 01:38:53 +02:00
|
|
|
merge_volume_bindings,
|
2014-12-09 01:03:42 +01:00
|
|
|
parse_repository_tag,
|
2015-02-14 20:09:55 +01:00
|
|
|
parse_volume_spec,
|
|
|
|
split_port,
|
2014-08-26 04:16:37 +02:00
|
|
|
)
|
|
|
|
|
2014-04-25 23:58:21 +02:00
|
|
|
|
|
|
|
class ServiceTest(unittest.TestCase):
|
2014-08-24 20:55:38 +02:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.mock_client = mock.create_autospec(docker.Client)
|
|
|
|
|
2014-04-25 23:58:21 +02:00
|
|
|
def test_name_validations(self):
|
|
|
|
self.assertRaises(ConfigError, lambda: Service(name=''))
|
|
|
|
|
|
|
|
self.assertRaises(ConfigError, lambda: Service(name=' '))
|
|
|
|
self.assertRaises(ConfigError, lambda: Service(name='/'))
|
|
|
|
self.assertRaises(ConfigError, lambda: Service(name='!'))
|
|
|
|
self.assertRaises(ConfigError, lambda: Service(name='\xe2'))
|
|
|
|
self.assertRaises(ConfigError, lambda: Service(name='_'))
|
|
|
|
self.assertRaises(ConfigError, lambda: Service(name='____'))
|
|
|
|
self.assertRaises(ConfigError, lambda: Service(name='foo_bar'))
|
|
|
|
self.assertRaises(ConfigError, lambda: Service(name='__foo_bar__'))
|
|
|
|
|
2015-04-15 04:03:02 +02:00
|
|
|
Service('a', image='foo')
|
|
|
|
Service('foo', image='foo')
|
2014-04-25 23:58:21 +02:00
|
|
|
|
|
|
|
def test_project_validation(self):
|
2015-04-15 23:38:24 +02:00
|
|
|
self.assertRaises(ConfigError, lambda: Service('bar'))
|
2015-04-15 04:03:02 +02:00
|
|
|
self.assertRaises(ConfigError, lambda: Service(name='foo', project='_', image='foo'))
|
|
|
|
Service(name='foo', project='bar', image='foo')
|
2014-04-25 23:58:21 +02:00
|
|
|
|
2015-01-16 14:12:29 +01:00
|
|
|
def test_get_container_name(self):
|
|
|
|
self.assertIsNone(get_container_name({}))
|
|
|
|
self.assertEqual(get_container_name({'Name': 'myproject_db_1'}), 'myproject_db_1')
|
|
|
|
self.assertEqual(get_container_name({'Names': ['/myproject_db_1', '/myproject_web_1/db']}), 'myproject_db_1')
|
2015-01-16 17:43:59 +01:00
|
|
|
self.assertEqual(get_container_name({'Names': ['/swarm-host-1/myproject_db_1', '/swarm-host-1/myproject_web_1/db']}), 'myproject_db_1')
|
2015-01-16 14:12:29 +01:00
|
|
|
|
|
|
|
def test_containers(self):
|
2015-04-15 04:03:02 +02:00
|
|
|
service = Service('db', client=self.mock_client, image='foo', project='myproject')
|
2015-01-16 14:12:29 +01:00
|
|
|
|
|
|
|
self.mock_client.containers.return_value = []
|
|
|
|
self.assertEqual(service.containers(), [])
|
|
|
|
|
|
|
|
self.mock_client.containers.return_value = [
|
|
|
|
{'Image': 'busybox', 'Id': 'OUT_1', 'Names': ['/myproject', '/foo/bar']},
|
|
|
|
{'Image': 'busybox', 'Id': 'OUT_2', 'Names': ['/myproject_db']},
|
|
|
|
{'Image': 'busybox', 'Id': 'OUT_3', 'Names': ['/db_1']},
|
|
|
|
{'Image': 'busybox', 'Id': 'IN_1', 'Names': ['/myproject_db_1', '/myproject_web_1/db']},
|
|
|
|
]
|
|
|
|
self.assertEqual([c.id for c in service.containers()], ['IN_1'])
|
|
|
|
|
2015-01-16 17:43:59 +01:00
|
|
|
def test_containers_prefixed(self):
|
2015-04-15 04:03:02 +02:00
|
|
|
service = Service('db', client=self.mock_client, image='foo', project='myproject')
|
2015-01-16 17:43:59 +01:00
|
|
|
|
|
|
|
self.mock_client.containers.return_value = [
|
|
|
|
{'Image': 'busybox', 'Id': 'OUT_1', 'Names': ['/swarm-host-1/myproject', '/swarm-host-1/foo/bar']},
|
|
|
|
{'Image': 'busybox', 'Id': 'OUT_2', 'Names': ['/swarm-host-1/myproject_db']},
|
|
|
|
{'Image': 'busybox', 'Id': 'OUT_3', 'Names': ['/swarm-host-1/db_1']},
|
|
|
|
{'Image': 'busybox', 'Id': 'IN_1', 'Names': ['/swarm-host-1/myproject_db_1', '/swarm-host-1/myproject_web_1/db']},
|
|
|
|
]
|
|
|
|
self.assertEqual([c.id for c in service.containers()], ['IN_1'])
|
|
|
|
|
2014-08-30 17:33:25 +02:00
|
|
|
def test_get_volumes_from_container(self):
|
|
|
|
container_id = 'aabbccddee'
|
|
|
|
service = Service(
|
|
|
|
'test',
|
2015-04-15 04:03:02 +02:00
|
|
|
image='foo',
|
2014-08-30 17:33:25 +02:00
|
|
|
volumes_from=[mock.Mock(id=container_id, spec=Container)])
|
|
|
|
|
|
|
|
self.assertEqual(service._get_volumes_from(), [container_id])
|
|
|
|
|
|
|
|
def test_get_volumes_from_service_container_exists(self):
|
|
|
|
container_ids = ['aabbccddee', '12345']
|
|
|
|
from_service = mock.create_autospec(Service)
|
|
|
|
from_service.containers.return_value = [
|
|
|
|
mock.Mock(id=container_id, spec=Container)
|
|
|
|
for container_id in container_ids
|
|
|
|
]
|
2015-04-15 04:03:02 +02:00
|
|
|
service = Service('test', volumes_from=[from_service], image='foo')
|
2014-08-30 17:33:25 +02:00
|
|
|
|
|
|
|
self.assertEqual(service._get_volumes_from(), container_ids)
|
|
|
|
|
|
|
|
def test_get_volumes_from_service_no_container(self):
|
|
|
|
container_id = 'abababab'
|
|
|
|
from_service = mock.create_autospec(Service)
|
|
|
|
from_service.containers.return_value = []
|
|
|
|
from_service.create_container.return_value = mock.Mock(
|
|
|
|
id=container_id,
|
|
|
|
spec=Container)
|
2015-04-15 04:03:02 +02:00
|
|
|
service = Service('test', image='foo', volumes_from=[from_service])
|
2014-08-30 17:33:25 +02:00
|
|
|
|
|
|
|
self.assertEqual(service._get_volumes_from(), [container_id])
|
|
|
|
from_service.create_container.assert_called_once_with()
|
|
|
|
|
2014-08-26 04:17:33 +02:00
|
|
|
def test_split_port_with_host_ip(self):
|
2014-06-25 12:49:54 +02:00
|
|
|
internal_port, external_port = split_port("127.0.0.1:1000:2000")
|
|
|
|
self.assertEqual(internal_port, "2000")
|
|
|
|
self.assertEqual(external_port, ("127.0.0.1", "1000"))
|
|
|
|
|
2014-08-26 04:17:33 +02:00
|
|
|
def test_split_port_with_protocol(self):
|
2014-07-24 20:48:29 +02:00
|
|
|
internal_port, external_port = split_port("127.0.0.1:1000:2000/udp")
|
|
|
|
self.assertEqual(internal_port, "2000/udp")
|
|
|
|
self.assertEqual(external_port, ("127.0.0.1", "1000"))
|
|
|
|
|
2014-08-26 04:17:33 +02:00
|
|
|
def test_split_port_with_host_ip_no_port(self):
|
2014-06-25 12:49:54 +02:00
|
|
|
internal_port, external_port = split_port("127.0.0.1::2000")
|
|
|
|
self.assertEqual(internal_port, "2000")
|
2014-08-26 04:17:33 +02:00
|
|
|
self.assertEqual(external_port, ("127.0.0.1", None))
|
2014-06-25 12:49:54 +02:00
|
|
|
|
2014-08-26 04:17:33 +02:00
|
|
|
def test_split_port_with_host_port(self):
|
2014-06-25 12:49:54 +02:00
|
|
|
internal_port, external_port = split_port("1000:2000")
|
|
|
|
self.assertEqual(internal_port, "2000")
|
|
|
|
self.assertEqual(external_port, "1000")
|
|
|
|
|
2014-08-26 04:17:33 +02:00
|
|
|
def test_split_port_no_host_port(self):
|
|
|
|
internal_port, external_port = split_port("2000")
|
|
|
|
self.assertEqual(internal_port, "2000")
|
|
|
|
self.assertEqual(external_port, None)
|
|
|
|
|
|
|
|
def test_split_port_invalid(self):
|
|
|
|
with self.assertRaises(ConfigError):
|
|
|
|
split_port("0.0.0.0:1000:2000:tcp")
|
|
|
|
|
2014-11-07 02:54:45 +01:00
|
|
|
def test_build_port_bindings_with_one_port(self):
|
|
|
|
port_bindings = build_port_bindings(["127.0.0.1:1000:1000"])
|
2015-03-26 04:13:01 +01:00
|
|
|
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000")])
|
2014-11-07 02:54:45 +01:00
|
|
|
|
|
|
|
def test_build_port_bindings_with_matching_internal_ports(self):
|
2015-03-26 04:13:01 +01:00
|
|
|
port_bindings = build_port_bindings(["127.0.0.1:1000:1000", "127.0.0.1:2000:1000"])
|
|
|
|
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000"), ("127.0.0.1", "2000")])
|
2014-11-07 02:54:45 +01:00
|
|
|
|
|
|
|
def test_build_port_bindings_with_nonmatching_internal_ports(self):
|
2015-03-26 04:13:01 +01:00
|
|
|
port_bindings = build_port_bindings(["127.0.0.1:1000:1000", "127.0.0.1:2000:2000"])
|
|
|
|
self.assertEqual(port_bindings["1000"], [("127.0.0.1", "1000")])
|
|
|
|
self.assertEqual(port_bindings["2000"], [("127.0.0.1", "2000")])
|
2014-11-07 02:54:45 +01:00
|
|
|
|
2014-07-11 00:56:38 +02:00
|
|
|
def test_split_domainname_none(self):
|
2015-04-15 04:03:02 +02:00
|
|
|
service = Service('foo', image='foo', hostname='name', client=self.mock_client)
|
2014-08-24 20:55:38 +02:00
|
|
|
self.mock_client.containers.return_value = []
|
2014-12-09 01:03:42 +01:00
|
|
|
opts = service._get_container_create_options({'image': 'foo'})
|
2014-07-11 00:56:38 +02:00
|
|
|
self.assertEqual(opts['hostname'], 'name', 'hostname')
|
|
|
|
self.assertFalse('domainname' in opts, 'domainname')
|
2014-06-25 12:49:54 +02:00
|
|
|
|
2014-07-11 00:56:38 +02:00
|
|
|
def test_split_domainname_fqdn(self):
|
2015-03-26 04:13:01 +01:00
|
|
|
service = Service(
|
|
|
|
'foo',
|
|
|
|
hostname='name.domain.tld',
|
2015-04-15 04:03:02 +02:00
|
|
|
image='foo',
|
2015-03-26 04:13:01 +01:00
|
|
|
client=self.mock_client)
|
2014-08-24 20:55:38 +02:00
|
|
|
self.mock_client.containers.return_value = []
|
2014-12-09 01:03:42 +01:00
|
|
|
opts = service._get_container_create_options({'image': 'foo'})
|
2014-07-11 00:56:38 +02:00
|
|
|
self.assertEqual(opts['hostname'], 'name', 'hostname')
|
|
|
|
self.assertEqual(opts['domainname'], 'domain.tld', 'domainname')
|
|
|
|
|
|
|
|
def test_split_domainname_both(self):
|
2015-03-26 04:13:01 +01:00
|
|
|
service = Service(
|
|
|
|
'foo',
|
|
|
|
hostname='name',
|
2015-04-15 04:03:02 +02:00
|
|
|
image='foo',
|
2015-03-26 04:13:01 +01:00
|
|
|
domainname='domain.tld',
|
|
|
|
client=self.mock_client)
|
2014-08-24 20:55:38 +02:00
|
|
|
self.mock_client.containers.return_value = []
|
2014-12-09 01:03:42 +01:00
|
|
|
opts = service._get_container_create_options({'image': 'foo'})
|
2014-07-11 00:56:38 +02:00
|
|
|
self.assertEqual(opts['hostname'], 'name', 'hostname')
|
|
|
|
self.assertEqual(opts['domainname'], 'domain.tld', 'domainname')
|
|
|
|
|
|
|
|
def test_split_domainname_weird(self):
|
2015-03-26 04:13:01 +01:00
|
|
|
service = Service(
|
|
|
|
'foo',
|
|
|
|
hostname='name.sub',
|
|
|
|
domainname='domain.tld',
|
2015-04-15 04:03:02 +02:00
|
|
|
image='foo',
|
2015-03-26 04:13:01 +01:00
|
|
|
client=self.mock_client)
|
2014-08-24 20:55:38 +02:00
|
|
|
self.mock_client.containers.return_value = []
|
2014-12-09 01:03:42 +01:00
|
|
|
opts = service._get_container_create_options({'image': 'foo'})
|
2014-07-11 00:56:38 +02:00
|
|
|
self.assertEqual(opts['hostname'], 'name.sub', 'hostname')
|
|
|
|
self.assertEqual(opts['domainname'], 'domain.tld', 'domainname')
|
2014-08-26 04:16:37 +02:00
|
|
|
|
2014-08-08 18:41:52 +02:00
|
|
|
def test_get_container_not_found(self):
|
2014-12-11 19:08:39 +01:00
|
|
|
self.mock_client.containers.return_value = []
|
2015-04-15 04:03:02 +02:00
|
|
|
service = Service('foo', client=self.mock_client, image='foo')
|
2014-08-08 18:41:52 +02:00
|
|
|
|
|
|
|
self.assertRaises(ValueError, service.get_container)
|
|
|
|
|
2015-01-12 15:59:05 +01:00
|
|
|
@mock.patch('compose.service.Container', autospec=True)
|
2014-08-08 18:41:52 +02:00
|
|
|
def test_get_container(self, mock_container_class):
|
|
|
|
container_dict = dict(Name='default_foo_2')
|
2014-12-11 19:08:39 +01:00
|
|
|
self.mock_client.containers.return_value = [container_dict]
|
2015-04-15 04:03:02 +02:00
|
|
|
service = Service('foo', image='foo', client=self.mock_client)
|
2014-08-08 18:41:52 +02:00
|
|
|
|
|
|
|
container = service.get_container(number=2)
|
|
|
|
self.assertEqual(container, mock_container_class.from_ps.return_value)
|
|
|
|
mock_container_class.from_ps.assert_called_once_with(
|
2014-12-11 19:08:39 +01:00
|
|
|
self.mock_client, container_dict)
|
2014-08-08 18:41:52 +02:00
|
|
|
|
2015-01-12 15:59:05 +01:00
|
|
|
@mock.patch('compose.service.log', autospec=True)
|
2014-10-01 17:09:19 +02:00
|
|
|
def test_pull_image(self, mock_log):
|
|
|
|
service = Service('foo', client=self.mock_client, image='someimage:sometag')
|
|
|
|
service.pull(insecure_registry=True)
|
2015-04-24 17:11:01 +02:00
|
|
|
self.mock_client.pull.assert_called_once_with(
|
|
|
|
'someimage',
|
|
|
|
tag='sometag',
|
|
|
|
insecure_registry=True,
|
|
|
|
stream=True)
|
2014-10-01 17:09:19 +02:00
|
|
|
mock_log.info.assert_called_once_with('Pulling foo (someimage:sometag)...')
|
|
|
|
|
2015-04-24 17:11:01 +02:00
|
|
|
def test_pull_image_no_tag(self):
|
|
|
|
service = Service('foo', client=self.mock_client, image='ababab')
|
|
|
|
service.pull()
|
|
|
|
self.mock_client.pull.assert_called_once_with(
|
|
|
|
'ababab',
|
|
|
|
tag='latest',
|
|
|
|
insecure_registry=False,
|
|
|
|
stream=True)
|
|
|
|
|
2015-01-12 15:59:05 +01:00
|
|
|
@mock.patch('compose.service.Container', autospec=True)
|
|
|
|
@mock.patch('compose.service.log', autospec=True)
|
2014-10-26 18:22:16 +01:00
|
|
|
def test_create_container_from_insecure_registry(
|
|
|
|
self,
|
|
|
|
mock_log,
|
|
|
|
mock_container):
|
2014-10-22 16:57:43 +02:00
|
|
|
service = Service('foo', client=self.mock_client, image='someimage:sometag')
|
|
|
|
mock_response = mock.Mock(Response)
|
|
|
|
mock_response.status_code = 404
|
|
|
|
mock_response.reason = "Not Found"
|
2014-10-26 18:22:16 +01:00
|
|
|
mock_container.create.side_effect = APIError(
|
|
|
|
'Mock error', mock_response, "No such image")
|
|
|
|
|
|
|
|
# We expect the APIError because our service requires a
|
|
|
|
# non-existent image.
|
|
|
|
with self.assertRaises(APIError):
|
2014-10-22 16:57:43 +02:00
|
|
|
service.create_container(insecure_registry=True)
|
2014-10-26 18:22:16 +01:00
|
|
|
|
|
|
|
self.mock_client.pull.assert_called_once_with(
|
2015-04-24 17:11:01 +02:00
|
|
|
'someimage',
|
|
|
|
tag='sometag',
|
2014-10-26 18:22:16 +01:00
|
|
|
insecure_registry=True,
|
|
|
|
stream=True)
|
|
|
|
mock_log.info.assert_called_once_with(
|
2015-04-24 17:11:01 +02:00
|
|
|
'Pulling foo (someimage:sometag)...')
|
2014-10-22 16:57:43 +02:00
|
|
|
|
2015-04-24 20:05:17 +02:00
|
|
|
@mock.patch('compose.service.Container', autospec=True)
|
|
|
|
def test_recreate_container(self, _):
|
|
|
|
mock_container = mock.create_autospec(Container)
|
|
|
|
service = Service('foo', client=self.mock_client, image='someimage')
|
|
|
|
new_container = service.recreate_container(mock_container)
|
|
|
|
|
|
|
|
mock_container.stop.assert_called_once_with()
|
|
|
|
self.mock_client.rename.assert_called_once_with(
|
|
|
|
mock_container.id,
|
|
|
|
'%s_%s' % (mock_container.short_id, mock_container.name))
|
|
|
|
|
|
|
|
new_container.start.assert_called_once_with()
|
|
|
|
mock_container.remove.assert_called_once_with()
|
|
|
|
|
2014-12-09 01:03:42 +01:00
|
|
|
def test_parse_repository_tag(self):
|
|
|
|
self.assertEqual(parse_repository_tag("root"), ("root", ""))
|
|
|
|
self.assertEqual(parse_repository_tag("root:tag"), ("root", "tag"))
|
|
|
|
self.assertEqual(parse_repository_tag("user/repo"), ("user/repo", ""))
|
|
|
|
self.assertEqual(parse_repository_tag("user/repo:tag"), ("user/repo", "tag"))
|
|
|
|
self.assertEqual(parse_repository_tag("url:5000/repo"), ("url:5000/repo", ""))
|
|
|
|
self.assertEqual(parse_repository_tag("url:5000/repo:tag"), ("url:5000/repo", "tag"))
|
|
|
|
|
2015-04-24 17:11:01 +02:00
|
|
|
@mock.patch('compose.service.Container', autospec=True)
|
|
|
|
def test_create_container_latest_is_used_when_no_tag_specified(self, mock_container):
|
|
|
|
mock_container.create.side_effect = APIError(
|
|
|
|
"oops",
|
|
|
|
mock.Mock(status_code=404),
|
|
|
|
"No such image")
|
2014-12-09 01:03:42 +01:00
|
|
|
service = Service('foo', client=self.mock_client, image='someimage')
|
2015-04-24 17:11:01 +02:00
|
|
|
with self.assertRaises(APIError):
|
|
|
|
service.create_container()
|
|
|
|
self.mock_client.pull.assert_called_once_with(
|
|
|
|
'someimage',
|
|
|
|
tag='latest',
|
|
|
|
insecure_registry=False,
|
|
|
|
stream=True)
|
2014-12-09 01:03:42 +01:00
|
|
|
|
2014-10-26 18:23:15 +01:00
|
|
|
def test_create_container_with_build(self):
|
|
|
|
self.mock_client.images.return_value = []
|
|
|
|
service = Service('foo', client=self.mock_client, build='.')
|
|
|
|
service.build = mock.create_autospec(service.build)
|
|
|
|
service.create_container(do_build=True)
|
|
|
|
|
|
|
|
self.mock_client.images.assert_called_once_with(name=service.full_name)
|
|
|
|
service.build.assert_called_once_with()
|
|
|
|
|
|
|
|
def test_create_container_no_build(self):
|
|
|
|
self.mock_client.images.return_value = []
|
|
|
|
service = Service('foo', client=self.mock_client, build='.')
|
|
|
|
service.create_container(do_build=False)
|
|
|
|
|
|
|
|
self.assertFalse(self.mock_client.images.called)
|
|
|
|
self.assertFalse(self.mock_client.build.called)
|
|
|
|
|
2014-08-26 04:16:37 +02:00
|
|
|
|
|
|
|
class ServiceVolumesTest(unittest.TestCase):
|
|
|
|
|
2015-05-10 01:38:53 +02:00
|
|
|
def setUp(self):
|
|
|
|
self.mock_client = mock.create_autospec(docker.Client)
|
|
|
|
|
2014-08-26 04:16:37 +02:00
|
|
|
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'))
|
|
|
|
|
|
|
|
def test_parse_volume_spec_too_many_parts(self):
|
|
|
|
with self.assertRaises(ConfigError):
|
|
|
|
parse_volume_spec('one:two:three:four')
|
|
|
|
|
|
|
|
def test_parse_volume_bad_mode(self):
|
|
|
|
with self.assertRaises(ConfigError):
|
|
|
|
parse_volume_spec('one:two:notrw')
|
|
|
|
|
|
|
|
def test_build_volume_binding(self):
|
|
|
|
binding = build_volume_binding(parse_volume_spec('/outside:/inside'))
|
|
|
|
self.assertEqual(
|
|
|
|
binding,
|
|
|
|
('/outside', dict(bind='/inside', ro=False)))
|
2015-05-10 01:38:53 +02:00
|
|
|
|
|
|
|
def test_get_container_data_volumes(self):
|
|
|
|
options = [
|
|
|
|
'/host/volume:/host/volume:ro',
|
|
|
|
'/new/volume',
|
|
|
|
'/existing/volume',
|
|
|
|
]
|
|
|
|
|
|
|
|
self.mock_client.inspect_image.return_value = {
|
|
|
|
'ContainerConfig': {
|
|
|
|
'Volumes': {
|
|
|
|
'/mnt/image/data': {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
container = Container(self.mock_client, {
|
|
|
|
'Image': 'ababab',
|
|
|
|
'Volumes': {
|
|
|
|
'/host/volume': '/host/volume',
|
|
|
|
'/existing/volume': '/var/lib/docker/aaaaaaaa',
|
|
|
|
'/removed/volume': '/var/lib/docker/bbbbbbbb',
|
|
|
|
'/mnt/image/data': '/var/lib/docker/cccccccc',
|
|
|
|
},
|
|
|
|
}, has_been_inspected=True)
|
|
|
|
|
|
|
|
expected = {
|
|
|
|
'/var/lib/docker/aaaaaaaa': {'bind': '/existing/volume', 'ro': False},
|
|
|
|
'/var/lib/docker/cccccccc': {'bind': '/mnt/image/data', 'ro': False},
|
|
|
|
}
|
|
|
|
|
|
|
|
binds = get_container_data_volumes(container, options)
|
|
|
|
self.assertEqual(binds, expected)
|
|
|
|
|
|
|
|
def test_merge_volume_bindings(self):
|
|
|
|
options = [
|
|
|
|
'/host/volume:/host/volume:ro',
|
|
|
|
'/host/rw/volume:/host/rw/volume',
|
|
|
|
'/new/volume',
|
|
|
|
'/existing/volume',
|
|
|
|
]
|
|
|
|
|
|
|
|
self.mock_client.inspect_image.return_value = {
|
|
|
|
'ContainerConfig': {'Volumes': {}}
|
|
|
|
}
|
|
|
|
|
|
|
|
intermediate_container = Container(self.mock_client, {
|
|
|
|
'Image': 'ababab',
|
|
|
|
'Volumes': {'/existing/volume': '/var/lib/docker/aaaaaaaa'},
|
|
|
|
}, has_been_inspected=True)
|
|
|
|
|
|
|
|
expected = {
|
|
|
|
'/host/volume': {'bind': '/host/volume', 'ro': True},
|
|
|
|
'/host/rw/volume': {'bind': '/host/rw/volume', 'ro': False},
|
|
|
|
'/var/lib/docker/aaaaaaaa': {'bind': '/existing/volume', 'ro': False},
|
|
|
|
}
|
|
|
|
|
|
|
|
binds = merge_volume_bindings(options, intermediate_container)
|
|
|
|
self.assertEqual(binds, expected)
|