2015-10-30 21:22:51 +01:00
|
|
|
from __future__ import absolute_import
|
2014-01-06 03:26:32 +01:00
|
|
|
from __future__ import unicode_literals
|
2014-08-02 22:31:08 +02:00
|
|
|
|
2014-07-30 22:11:11 +02:00
|
|
|
import docker
|
2014-08-02 22:31:08 +02:00
|
|
|
|
2014-08-19 23:36:46 +02:00
|
|
|
from .. import mock
|
2015-08-24 21:25:25 +02:00
|
|
|
from .. import unittest
|
2015-01-12 15:59:05 +01:00
|
|
|
from compose.container import Container
|
2015-04-26 23:09:20 +02:00
|
|
|
from compose.container import get_container_name
|
2013-12-18 19:37:48 +01:00
|
|
|
|
2014-08-02 22:31:08 +02:00
|
|
|
|
2014-04-25 23:58:21 +02:00
|
|
|
class ContainerTest(unittest.TestCase):
|
2014-08-08 18:41:52 +02:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.container_dict = {
|
|
|
|
"Id": "abc",
|
|
|
|
"Image": "busybox:latest",
|
2015-05-21 17:19:15 +02:00
|
|
|
"Command": "top",
|
2014-08-08 18:41:52 +02:00
|
|
|
"Created": 1387384730,
|
|
|
|
"Status": "Up 8 seconds",
|
|
|
|
"Ports": None,
|
|
|
|
"SizeRw": 0,
|
|
|
|
"SizeRootFs": 0,
|
2015-01-12 15:59:05 +01:00
|
|
|
"Names": ["/composetest_db_1", "/composetest_web_1/db"],
|
2014-08-08 18:41:52 +02:00
|
|
|
"NetworkSettings": {
|
|
|
|
"Ports": {},
|
|
|
|
},
|
2015-04-26 23:09:20 +02:00
|
|
|
"Config": {
|
|
|
|
"Labels": {
|
|
|
|
"com.docker.compose.project": "composetest",
|
|
|
|
"com.docker.compose.service": "web",
|
2015-05-09 18:53:59 +02:00
|
|
|
"com.docker.compose.container-number": 7,
|
2015-04-26 23:09:20 +02:00
|
|
|
},
|
|
|
|
}
|
2014-08-08 18:41:52 +02:00
|
|
|
}
|
|
|
|
|
2013-12-18 19:37:48 +01:00
|
|
|
def test_from_ps(self):
|
2014-08-08 18:41:52 +02:00
|
|
|
container = Container.from_ps(None,
|
|
|
|
self.container_dict,
|
|
|
|
has_been_inspected=True)
|
2015-03-26 04:13:01 +01:00
|
|
|
self.assertEqual(
|
|
|
|
container.dictionary,
|
|
|
|
{
|
|
|
|
"Id": "abc",
|
|
|
|
"Image": "busybox:latest",
|
|
|
|
"Name": "/composetest_db_1",
|
|
|
|
})
|
2013-12-18 19:37:48 +01:00
|
|
|
|
2015-01-16 18:24:29 +01:00
|
|
|
def test_from_ps_prefixed(self):
|
|
|
|
self.container_dict['Names'] = ['/swarm-host-1' + n for n in self.container_dict['Names']]
|
|
|
|
|
|
|
|
container = Container.from_ps(None,
|
|
|
|
self.container_dict,
|
|
|
|
has_been_inspected=True)
|
|
|
|
self.assertEqual(container.dictionary, {
|
|
|
|
"Id": "abc",
|
2015-03-26 04:13:01 +01:00
|
|
|
"Image": "busybox:latest",
|
2015-01-12 15:59:05 +01:00
|
|
|
"Name": "/composetest_db_1",
|
2015-01-16 18:24:29 +01:00
|
|
|
})
|
|
|
|
|
2013-12-18 19:37:48 +01:00
|
|
|
def test_environment(self):
|
2014-04-25 23:58:21 +02:00
|
|
|
container = Container(None, {
|
2014-06-25 12:47:29 +02:00
|
|
|
'Id': 'abc',
|
2013-12-18 19:37:48 +01:00
|
|
|
'Config': {
|
|
|
|
'Env': [
|
|
|
|
'FOO=BAR',
|
|
|
|
'BAZ=DOGE',
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}, has_been_inspected=True)
|
|
|
|
self.assertEqual(container.environment, {
|
|
|
|
'FOO': 'BAR',
|
|
|
|
'BAZ': 'DOGE',
|
|
|
|
})
|
2014-01-16 19:05:45 +01:00
|
|
|
|
|
|
|
def test_number(self):
|
2015-04-26 23:09:20 +02:00
|
|
|
container = Container(None, self.container_dict, has_been_inspected=True)
|
|
|
|
self.assertEqual(container.number, 7)
|
2014-04-29 10:20:29 +02:00
|
|
|
|
|
|
|
def test_name(self):
|
2014-08-08 18:41:52 +02:00
|
|
|
container = Container.from_ps(None,
|
|
|
|
self.container_dict,
|
|
|
|
has_been_inspected=True)
|
2015-01-12 15:59:05 +01:00
|
|
|
self.assertEqual(container.name, "composetest_db_1")
|
2014-04-29 10:20:29 +02:00
|
|
|
|
|
|
|
def test_name_without_project(self):
|
2015-08-13 00:16:42 +02:00
|
|
|
self.container_dict['Name'] = "/composetest_web_7"
|
2015-04-26 23:09:20 +02:00
|
|
|
container = Container(None, self.container_dict, has_been_inspected=True)
|
|
|
|
self.assertEqual(container.name_without_project, "web_7")
|
2014-08-02 22:31:08 +02:00
|
|
|
|
2015-08-13 00:16:42 +02:00
|
|
|
def test_name_without_project_custom_container_name(self):
|
|
|
|
self.container_dict['Name'] = "/custom_name_of_container"
|
|
|
|
container = Container(None, self.container_dict, has_been_inspected=True)
|
|
|
|
self.assertEqual(container.name_without_project, "custom_name_of_container")
|
|
|
|
|
2014-08-02 22:31:08 +02:00
|
|
|
def test_inspect_if_not_inspected(self):
|
|
|
|
mock_client = mock.create_autospec(docker.Client)
|
|
|
|
container = Container(mock_client, dict(Id="the_id"))
|
|
|
|
|
|
|
|
container.inspect_if_not_inspected()
|
|
|
|
mock_client.inspect_container.assert_called_once_with("the_id")
|
|
|
|
self.assertEqual(container.dictionary,
|
|
|
|
mock_client.inspect_container.return_value)
|
|
|
|
self.assertTrue(container.has_been_inspected)
|
|
|
|
|
|
|
|
container.inspect_if_not_inspected()
|
|
|
|
self.assertEqual(mock_client.inspect_container.call_count, 1)
|
2014-08-08 18:41:52 +02:00
|
|
|
|
|
|
|
def test_human_readable_ports_none(self):
|
|
|
|
container = Container(None, self.container_dict, has_been_inspected=True)
|
|
|
|
self.assertEqual(container.human_readable_ports, '')
|
|
|
|
|
|
|
|
def test_human_readable_ports_public_and_private(self):
|
|
|
|
self.container_dict['NetworkSettings']['Ports'].update({
|
2015-03-26 04:13:01 +01:00
|
|
|
"45454/tcp": [{"HostIp": "0.0.0.0", "HostPort": "49197"}],
|
2014-08-08 18:41:52 +02:00
|
|
|
"45453/tcp": [],
|
|
|
|
})
|
|
|
|
container = Container(None, self.container_dict, has_been_inspected=True)
|
|
|
|
|
|
|
|
expected = "45453/tcp, 0.0.0.0:49197->45454/tcp"
|
|
|
|
self.assertEqual(container.human_readable_ports, expected)
|
|
|
|
|
|
|
|
def test_get_local_port(self):
|
|
|
|
self.container_dict['NetworkSettings']['Ports'].update({
|
2015-03-26 04:13:01 +01:00
|
|
|
"45454/tcp": [{"HostIp": "0.0.0.0", "HostPort": "49197"}],
|
2014-08-08 18:41:52 +02:00
|
|
|
})
|
|
|
|
container = Container(None, self.container_dict, has_been_inspected=True)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
container.get_local_port(45454, protocol='tcp'),
|
|
|
|
'0.0.0.0:49197')
|
2014-08-31 19:04:18 +02:00
|
|
|
|
|
|
|
def test_get(self):
|
|
|
|
container = Container(None, {
|
2015-03-26 04:13:01 +01:00
|
|
|
"Status": "Up 8 seconds",
|
2014-08-31 19:04:18 +02:00
|
|
|
"HostConfig": {
|
2015-03-26 04:13:01 +01:00
|
|
|
"VolumesFrom": ["volume_id"]
|
2014-08-31 19:04:18 +02:00
|
|
|
},
|
|
|
|
}, has_been_inspected=True)
|
|
|
|
|
|
|
|
self.assertEqual(container.get('Status'), "Up 8 seconds")
|
2015-03-26 04:13:01 +01:00
|
|
|
self.assertEqual(container.get('HostConfig.VolumesFrom'), ["volume_id"])
|
2014-08-31 19:04:18 +02:00
|
|
|
self.assertEqual(container.get('Foo.Bar.DoesNotExist'), None)
|
2015-04-26 23:09:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
class GetContainerNameTestCase(unittest.TestCase):
|
|
|
|
|
|
|
|
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-08-26 18:53:11 +02:00
|
|
|
self.assertEqual(
|
|
|
|
get_container_name({
|
|
|
|
'Names': [
|
|
|
|
'/swarm-host-1/myproject_db_1',
|
|
|
|
'/swarm-host-1/myproject_web_1/db'
|
|
|
|
]
|
|
|
|
}),
|
|
|
|
'myproject_db_1'
|
|
|
|
)
|