2014-04-25 23:58:21 +02:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
from .. import unittest
|
2015-01-12 15:59:05 +01:00
|
|
|
from compose.service import Service
|
2015-02-27 12:54:57 +01:00
|
|
|
from compose.project import Project
|
2015-03-06 22:30:56 +01:00
|
|
|
from compose.container import Container
|
2015-08-10 17:00:45 +02:00
|
|
|
from compose.const import LABEL_SERVICE
|
2015-03-06 22:30:56 +01:00
|
|
|
|
|
|
|
import mock
|
|
|
|
import docker
|
2014-04-25 23:58:21 +02:00
|
|
|
|
2015-03-26 04:13:01 +01:00
|
|
|
|
2014-04-25 23:58:21 +02:00
|
|
|
class ProjectTest(unittest.TestCase):
|
2015-07-10 15:18:27 +02:00
|
|
|
def setUp(self):
|
|
|
|
self.mock_client = mock.create_autospec(docker.Client)
|
|
|
|
|
2014-04-25 23:58:21 +02:00
|
|
|
def test_from_dict(self):
|
2015-01-12 15:59:05 +01:00
|
|
|
project = Project.from_dicts('composetest', [
|
2014-04-25 23:58:21 +02:00
|
|
|
{
|
|
|
|
'name': 'web',
|
2014-06-11 12:25:50 +02:00
|
|
|
'image': 'busybox:latest'
|
2014-04-25 23:58:21 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'db',
|
2014-06-11 12:25:50 +02:00
|
|
|
'image': 'busybox:latest'
|
2014-06-07 15:03:53 +02:00
|
|
|
},
|
2014-04-25 23:58:21 +02:00
|
|
|
], None)
|
|
|
|
self.assertEqual(len(project.services), 2)
|
|
|
|
self.assertEqual(project.get_service('web').name, 'web')
|
2014-06-11 12:25:50 +02:00
|
|
|
self.assertEqual(project.get_service('web').options['image'], 'busybox:latest')
|
2014-04-25 23:58:21 +02:00
|
|
|
self.assertEqual(project.get_service('db').name, 'db')
|
2014-06-11 12:25:50 +02:00
|
|
|
self.assertEqual(project.get_service('db').options['image'], 'busybox:latest')
|
2014-04-25 23:58:21 +02:00
|
|
|
|
|
|
|
def test_from_dict_sorts_in_dependency_order(self):
|
2015-01-12 15:59:05 +01:00
|
|
|
project = Project.from_dicts('composetest', [
|
2014-04-25 23:58:21 +02:00
|
|
|
{
|
|
|
|
'name': 'web',
|
2014-06-11 12:25:50 +02:00
|
|
|
'image': 'busybox:latest',
|
2014-04-25 23:58:21 +02:00
|
|
|
'links': ['db'],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'db',
|
2014-05-29 09:40:11 +02:00
|
|
|
'image': 'busybox:latest',
|
|
|
|
'volumes_from': ['volume']
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'volume',
|
|
|
|
'image': 'busybox:latest',
|
|
|
|
'volumes': ['/tmp'],
|
2014-04-25 23:58:21 +02:00
|
|
|
}
|
|
|
|
], None)
|
|
|
|
|
2014-05-29 09:40:11 +02:00
|
|
|
self.assertEqual(project.services[0].name, 'volume')
|
|
|
|
self.assertEqual(project.services[1].name, 'db')
|
|
|
|
self.assertEqual(project.services[2].name, 'web')
|
2014-04-25 23:58:21 +02:00
|
|
|
|
|
|
|
def test_from_config(self):
|
2015-05-27 04:09:26 +02:00
|
|
|
dicts = [
|
|
|
|
{
|
|
|
|
'name': 'web',
|
2014-06-11 12:25:50 +02:00
|
|
|
'image': 'busybox:latest',
|
2014-04-25 23:58:21 +02:00
|
|
|
},
|
2015-05-27 04:09:26 +02:00
|
|
|
{
|
|
|
|
'name': 'db',
|
2014-06-11 12:25:50 +02:00
|
|
|
'image': 'busybox:latest',
|
2014-04-25 23:58:21 +02:00
|
|
|
},
|
2015-05-27 04:09:26 +02:00
|
|
|
]
|
2015-02-27 12:54:57 +01:00
|
|
|
project = Project.from_dicts('composetest', dicts, None)
|
2014-04-25 23:58:21 +02:00
|
|
|
self.assertEqual(len(project.services), 2)
|
|
|
|
self.assertEqual(project.get_service('web').name, 'web')
|
2014-06-11 12:25:50 +02:00
|
|
|
self.assertEqual(project.get_service('web').options['image'], 'busybox:latest')
|
2014-04-25 23:58:21 +02:00
|
|
|
self.assertEqual(project.get_service('db').name, 'db')
|
2014-06-11 12:25:50 +02:00
|
|
|
self.assertEqual(project.get_service('db').options['image'], 'busybox:latest')
|
2014-04-25 23:58:21 +02:00
|
|
|
|
|
|
|
def test_get_service(self):
|
|
|
|
web = Service(
|
2015-01-12 15:59:05 +01:00
|
|
|
project='composetest',
|
2014-04-25 23:58:21 +02:00
|
|
|
name='web',
|
|
|
|
client=None,
|
2014-06-11 12:25:50 +02:00
|
|
|
image="busybox:latest",
|
2014-04-25 23:58:21 +02:00
|
|
|
)
|
|
|
|
project = Project('test', [web], None)
|
|
|
|
self.assertEqual(project.get_service('web'), web)
|
2014-06-07 15:29:28 +02:00
|
|
|
|
2014-06-21 12:39:36 +02:00
|
|
|
def test_get_services_returns_all_services_without_args(self):
|
2014-06-07 15:29:28 +02:00
|
|
|
web = Service(
|
2015-01-12 15:59:05 +01:00
|
|
|
project='composetest',
|
2014-06-07 15:29:28 +02:00
|
|
|
name='web',
|
2015-04-15 04:03:02 +02:00
|
|
|
image='foo',
|
2014-06-07 15:29:28 +02:00
|
|
|
)
|
|
|
|
console = Service(
|
2015-01-12 15:59:05 +01:00
|
|
|
project='composetest',
|
2014-06-07 15:29:28 +02:00
|
|
|
name='console',
|
2015-04-15 04:03:02 +02:00
|
|
|
image='foo',
|
2014-06-07 15:29:28 +02:00
|
|
|
)
|
|
|
|
project = Project('test', [web, console], None)
|
2014-06-21 12:39:36 +02:00
|
|
|
self.assertEqual(project.get_services(), [web, console])
|
2014-06-07 15:29:28 +02:00
|
|
|
|
|
|
|
def test_get_services_returns_listed_services_with_args(self):
|
|
|
|
web = Service(
|
2015-01-12 15:59:05 +01:00
|
|
|
project='composetest',
|
2014-06-07 15:29:28 +02:00
|
|
|
name='web',
|
2015-04-15 04:03:02 +02:00
|
|
|
image='foo',
|
2014-06-07 15:29:28 +02:00
|
|
|
)
|
|
|
|
console = Service(
|
2015-01-12 15:59:05 +01:00
|
|
|
project='composetest',
|
2014-06-07 15:29:28 +02:00
|
|
|
name='console',
|
2015-04-15 04:03:02 +02:00
|
|
|
image='foo',
|
2014-06-07 15:29:28 +02:00
|
|
|
)
|
|
|
|
project = Project('test', [web, console], None)
|
|
|
|
self.assertEqual(project.get_services(['console']), [console])
|
2014-06-08 12:58:13 +02:00
|
|
|
|
|
|
|
def test_get_services_with_include_links(self):
|
|
|
|
db = Service(
|
2015-01-12 15:59:05 +01:00
|
|
|
project='composetest',
|
2014-06-08 12:58:13 +02:00
|
|
|
name='db',
|
2015-04-15 04:03:02 +02:00
|
|
|
image='foo',
|
2014-06-08 12:58:13 +02:00
|
|
|
)
|
|
|
|
web = Service(
|
2015-01-12 15:59:05 +01:00
|
|
|
project='composetest',
|
2014-06-08 12:58:13 +02:00
|
|
|
name='web',
|
2015-04-15 04:03:02 +02:00
|
|
|
image='foo',
|
2014-06-08 12:58:13 +02:00
|
|
|
links=[(db, 'database')]
|
|
|
|
)
|
|
|
|
cache = Service(
|
2015-01-12 15:59:05 +01:00
|
|
|
project='composetest',
|
2015-04-15 04:03:02 +02:00
|
|
|
name='cache',
|
|
|
|
image='foo'
|
2014-06-08 12:58:13 +02:00
|
|
|
)
|
|
|
|
console = Service(
|
2015-01-12 15:59:05 +01:00
|
|
|
project='composetest',
|
2014-06-08 12:58:13 +02:00
|
|
|
name='console',
|
2015-04-15 04:03:02 +02:00
|
|
|
image='foo',
|
2014-06-08 12:58:13 +02:00
|
|
|
links=[(web, 'web')]
|
|
|
|
)
|
|
|
|
project = Project('test', [web, db, cache, console], None)
|
|
|
|
self.assertEqual(
|
2015-03-06 22:30:56 +01:00
|
|
|
project.get_services(['console'], include_deps=True),
|
2014-06-08 12:58:13 +02:00
|
|
|
[db, web, console]
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_get_services_removes_duplicates_following_links(self):
|
|
|
|
db = Service(
|
2015-01-12 15:59:05 +01:00
|
|
|
project='composetest',
|
2014-06-08 12:58:13 +02:00
|
|
|
name='db',
|
2015-04-15 04:03:02 +02:00
|
|
|
image='foo',
|
2014-06-08 12:58:13 +02:00
|
|
|
)
|
|
|
|
web = Service(
|
2015-01-12 15:59:05 +01:00
|
|
|
project='composetest',
|
2014-06-08 12:58:13 +02:00
|
|
|
name='web',
|
2015-04-15 04:03:02 +02:00
|
|
|
image='foo',
|
2014-06-08 12:58:13 +02:00
|
|
|
links=[(db, 'database')]
|
|
|
|
)
|
|
|
|
project = Project('test', [web, db], None)
|
|
|
|
self.assertEqual(
|
2015-03-06 22:30:56 +01:00
|
|
|
project.get_services(['web', 'db'], include_deps=True),
|
2014-06-08 12:58:13 +02:00
|
|
|
[db, web]
|
|
|
|
)
|
2015-03-06 22:30:56 +01:00
|
|
|
|
|
|
|
def test_use_volumes_from_container(self):
|
|
|
|
container_id = 'aabbccddee'
|
|
|
|
container_dict = dict(Name='aaa', Id=container_id)
|
2015-07-10 15:18:27 +02:00
|
|
|
self.mock_client.inspect_container.return_value = container_dict
|
2015-03-06 22:30:56 +01:00
|
|
|
project = Project.from_dicts('test', [
|
|
|
|
{
|
|
|
|
'name': 'test',
|
|
|
|
'image': 'busybox:latest',
|
|
|
|
'volumes_from': ['aaa']
|
|
|
|
}
|
2015-07-10 15:18:27 +02:00
|
|
|
], self.mock_client)
|
2015-03-06 22:30:56 +01:00
|
|
|
self.assertEqual(project.get_service('test')._get_volumes_from(), [container_id])
|
|
|
|
|
|
|
|
def test_use_volumes_from_service_no_container(self):
|
|
|
|
container_name = 'test_vol_1'
|
2015-07-10 15:18:27 +02:00
|
|
|
self.mock_client.containers.return_value = [
|
2015-03-06 22:30:56 +01:00
|
|
|
{
|
|
|
|
"Name": container_name,
|
|
|
|
"Names": [container_name],
|
|
|
|
"Id": container_name,
|
|
|
|
"Image": 'busybox:latest'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
project = Project.from_dicts('test', [
|
|
|
|
{
|
|
|
|
'name': 'vol',
|
|
|
|
'image': 'busybox:latest'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'test',
|
|
|
|
'image': 'busybox:latest',
|
|
|
|
'volumes_from': ['vol']
|
|
|
|
}
|
2015-07-10 15:18:27 +02:00
|
|
|
], self.mock_client)
|
2015-03-06 22:30:56 +01:00
|
|
|
self.assertEqual(project.get_service('test')._get_volumes_from(), [container_name])
|
|
|
|
|
|
|
|
@mock.patch.object(Service, 'containers')
|
|
|
|
def test_use_volumes_from_service_container(self, mock_return):
|
|
|
|
container_ids = ['aabbccddee', '12345']
|
|
|
|
mock_return.return_value = [
|
|
|
|
mock.Mock(id=container_id, spec=Container)
|
|
|
|
for container_id in container_ids]
|
|
|
|
|
|
|
|
project = Project.from_dicts('test', [
|
|
|
|
{
|
|
|
|
'name': 'vol',
|
|
|
|
'image': 'busybox:latest'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'test',
|
|
|
|
'image': 'busybox:latest',
|
|
|
|
'volumes_from': ['vol']
|
|
|
|
}
|
|
|
|
], None)
|
|
|
|
self.assertEqual(project.get_service('test')._get_volumes_from(), container_ids)
|
|
|
|
|
2015-06-20 00:22:13 +02:00
|
|
|
def test_net_unset(self):
|
|
|
|
project = Project.from_dicts('test', [
|
|
|
|
{
|
|
|
|
'name': 'test',
|
|
|
|
'image': 'busybox:latest',
|
|
|
|
}
|
2015-07-10 15:18:27 +02:00
|
|
|
], self.mock_client)
|
2015-06-20 00:22:13 +02:00
|
|
|
service = project.get_service('test')
|
|
|
|
self.assertEqual(service._get_net(), None)
|
|
|
|
self.assertNotIn('NetworkMode', service._get_container_host_config({}))
|
|
|
|
|
2015-03-06 22:30:56 +01:00
|
|
|
def test_use_net_from_container(self):
|
|
|
|
container_id = 'aabbccddee'
|
|
|
|
container_dict = dict(Name='aaa', Id=container_id)
|
2015-07-10 15:18:27 +02:00
|
|
|
self.mock_client.inspect_container.return_value = container_dict
|
2015-03-06 22:30:56 +01:00
|
|
|
project = Project.from_dicts('test', [
|
|
|
|
{
|
|
|
|
'name': 'test',
|
|
|
|
'image': 'busybox:latest',
|
|
|
|
'net': 'container:aaa'
|
|
|
|
}
|
2015-07-10 15:18:27 +02:00
|
|
|
], self.mock_client)
|
2015-03-06 22:30:56 +01:00
|
|
|
service = project.get_service('test')
|
2015-03-26 04:13:01 +01:00
|
|
|
self.assertEqual(service._get_net(), 'container:' + container_id)
|
2015-03-06 22:30:56 +01:00
|
|
|
|
|
|
|
def test_use_net_from_service(self):
|
|
|
|
container_name = 'test_aaa_1'
|
2015-07-10 15:18:27 +02:00
|
|
|
self.mock_client.containers.return_value = [
|
2015-03-06 22:30:56 +01:00
|
|
|
{
|
|
|
|
"Name": container_name,
|
|
|
|
"Names": [container_name],
|
|
|
|
"Id": container_name,
|
|
|
|
"Image": 'busybox:latest'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
project = Project.from_dicts('test', [
|
|
|
|
{
|
|
|
|
'name': 'aaa',
|
|
|
|
'image': 'busybox:latest'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'test',
|
|
|
|
'image': 'busybox:latest',
|
|
|
|
'net': 'container:aaa'
|
|
|
|
}
|
2015-07-10 15:18:27 +02:00
|
|
|
], self.mock_client)
|
2015-03-06 22:30:56 +01:00
|
|
|
|
|
|
|
service = project.get_service('test')
|
2015-03-26 04:13:01 +01:00
|
|
|
self.assertEqual(service._get_net(), 'container:' + container_name)
|
2015-08-10 17:00:45 +02:00
|
|
|
|
|
|
|
def test_container_without_name(self):
|
|
|
|
self.mock_client.containers.return_value = [
|
|
|
|
{'Image': 'busybox:latest', 'Id': '1', 'Name': '1'},
|
|
|
|
{'Image': 'busybox:latest', 'Id': '2', 'Name': None},
|
|
|
|
{'Image': 'busybox:latest', 'Id': '3'},
|
|
|
|
]
|
|
|
|
self.mock_client.inspect_container.return_value = {
|
|
|
|
'Id': '1',
|
|
|
|
'Config': {
|
|
|
|
'Labels': {
|
|
|
|
LABEL_SERVICE: 'web',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
project = Project.from_dicts(
|
|
|
|
'test',
|
|
|
|
[{
|
|
|
|
'name': 'web',
|
|
|
|
'image': 'busybox:latest',
|
|
|
|
}],
|
|
|
|
self.mock_client,
|
|
|
|
)
|
|
|
|
self.assertEqual([c.id for c in project.containers()], ['1'])
|