Consolidate code for docker conductor tests
Change-Id: If5a92f189ce1b7ab3c968c16fc8bdca2eb47446f
This commit is contained in:
parent
554af35b74
commit
791eeb2bcb
|
@ -40,103 +40,95 @@ class TestDockerHandler(base.BaseTestCase):
|
|||
self.dfc_context_manager.__enter__.return_value = self.mock_docker
|
||||
self.addCleanup(dfc_patcher.stop)
|
||||
|
||||
def test_container_create(self):
|
||||
mock_container = mock.MagicMock()
|
||||
mock_container.name = 'some-name'
|
||||
mock_container.uuid = 'some-uuid'
|
||||
mock_container.image = 'test_image:some_tag'
|
||||
mock_container.command = None
|
||||
mock_container.memory = None
|
||||
mock_container.environment = None
|
||||
def _test_container_create(
|
||||
self, container_dict, expected_kwargs, expected_image='test_image',
|
||||
expected_tag='some_tag'):
|
||||
name = container_dict.pop('name')
|
||||
mock_container = mock.MagicMock(**container_dict)
|
||||
type(mock_container).name = mock.PropertyMock(return_value=name)
|
||||
|
||||
container = self.conductor.container_create(
|
||||
None, mock_container)
|
||||
|
||||
utf8_image = self.conductor._encode_utf8(mock_container.image)
|
||||
self.mock_docker.pull.assert_called_once_with('test_image',
|
||||
tag='some_tag')
|
||||
self.mock_docker.inspect_image.assert_called_once_with(utf8_image)
|
||||
self.mock_docker.pull.assert_called_once_with(expected_image,
|
||||
tag=expected_tag)
|
||||
self.mock_docker.create_container.assert_called_once_with(
|
||||
mock_container.image,
|
||||
name='some-name',
|
||||
hostname='some-uuid',
|
||||
command=None,
|
||||
mem_limit=None,
|
||||
environment=None)
|
||||
mock_container.image, **expected_kwargs)
|
||||
self.assertEqual(fields.ContainerStatus.STOPPED, container.status)
|
||||
|
||||
def test_container_create(self):
|
||||
container_dict = {
|
||||
'name': 'some-name',
|
||||
'uuid': 'some-uuid',
|
||||
'image': 'test_image:some_tag',
|
||||
'command': None,
|
||||
'memory': None,
|
||||
'environment': None,
|
||||
}
|
||||
expected_kwargs = {
|
||||
'name': 'some-name',
|
||||
'hostname': 'some-uuid',
|
||||
'command': None,
|
||||
'mem_limit': None,
|
||||
'environment': None,
|
||||
}
|
||||
self._test_container_create(container_dict, expected_kwargs)
|
||||
|
||||
def test_container_create_with_command(self):
|
||||
mock_container = mock.MagicMock()
|
||||
mock_container.name = 'some-name'
|
||||
mock_container.uuid = 'some-uuid'
|
||||
mock_container.image = 'test_image:some_tag'
|
||||
mock_container.command = 'env'
|
||||
mock_container.memory = None
|
||||
mock_container.environment = None
|
||||
|
||||
container = self.conductor.container_create(
|
||||
None, mock_container)
|
||||
|
||||
utf8_image = self.conductor._encode_utf8(mock_container.image)
|
||||
self.mock_docker.pull.assert_called_once_with('test_image',
|
||||
tag='some_tag')
|
||||
self.mock_docker.inspect_image.assert_called_once_with(utf8_image)
|
||||
self.mock_docker.create_container.assert_called_once_with(
|
||||
mock_container.image,
|
||||
name='some-name',
|
||||
hostname='some-uuid',
|
||||
command='env',
|
||||
mem_limit=None,
|
||||
environment=None)
|
||||
self.assertEqual(fields.ContainerStatus.STOPPED, container.status)
|
||||
container_dict = {
|
||||
'name': 'some-name',
|
||||
'uuid': 'some-uuid',
|
||||
'image': 'test_image:some_tag',
|
||||
'command': 'env',
|
||||
'memory': None,
|
||||
'environment': None,
|
||||
}
|
||||
expected_kwargs = {
|
||||
'name': 'some-name',
|
||||
'hostname': 'some-uuid',
|
||||
'command': 'env',
|
||||
'mem_limit': None,
|
||||
'environment': None,
|
||||
}
|
||||
self._test_container_create(container_dict, expected_kwargs)
|
||||
|
||||
def test_container_create_with_memory(self):
|
||||
mock_container = mock.MagicMock()
|
||||
mock_container.name = 'some-name'
|
||||
mock_container.uuid = 'some-uuid'
|
||||
mock_container.image = 'test_image:some_tag'
|
||||
mock_container.command = None
|
||||
mock_container.memory = '512m'
|
||||
mock_container.environment = None
|
||||
container = self.conductor.container_create(
|
||||
None, mock_container)
|
||||
|
||||
utf8_image = self.conductor._encode_utf8(mock_container.image)
|
||||
self.mock_docker.pull.assert_called_once_with('test_image',
|
||||
tag='some_tag')
|
||||
self.mock_docker.inspect_image.assert_called_once_with(utf8_image)
|
||||
self.mock_docker.create_container.assert_called_once_with(
|
||||
mock_container.image,
|
||||
name='some-name',
|
||||
hostname='some-uuid',
|
||||
command=None,
|
||||
mem_limit='512m',
|
||||
environment=None)
|
||||
self.assertEqual(fields.ContainerStatus.STOPPED, container.status)
|
||||
container_dict = {
|
||||
'name': 'some-name',
|
||||
'uuid': 'some-uuid',
|
||||
'image': 'test_image:some_tag',
|
||||
'command': None,
|
||||
'memory': '512m',
|
||||
'environment': None,
|
||||
}
|
||||
expected_kwargs = {
|
||||
'name': 'some-name',
|
||||
'hostname': 'some-uuid',
|
||||
'command': None,
|
||||
'mem_limit': '512m',
|
||||
'environment': None,
|
||||
}
|
||||
self._test_container_create(container_dict, expected_kwargs)
|
||||
|
||||
def test_container_create_with_environment(self):
|
||||
mock_container = mock.MagicMock()
|
||||
mock_container.name = 'some-name'
|
||||
mock_container.uuid = 'some-uuid'
|
||||
mock_container.image = 'test_image:some_tag'
|
||||
mock_container.command = None
|
||||
mock_container.memory = '512m'
|
||||
mock_container.environment = {'key1': 'val1', 'key2': 'val2'}
|
||||
container = self.conductor.container_create(
|
||||
None, mock_container)
|
||||
|
||||
utf8_image = self.conductor._encode_utf8(mock_container.image)
|
||||
self.mock_docker.pull.assert_called_once_with('test_image',
|
||||
tag='some_tag')
|
||||
self.mock_docker.inspect_image.assert_called_once_with(utf8_image)
|
||||
self.mock_docker.create_container.assert_called_once_with(
|
||||
mock_container.image,
|
||||
name='some-name',
|
||||
hostname='some-uuid',
|
||||
command=None,
|
||||
mem_limit='512m',
|
||||
environment={'key1': 'val1', 'key2': 'val2'})
|
||||
self.assertEqual(fields.ContainerStatus.STOPPED, container.status)
|
||||
container_dict = {
|
||||
'name': 'some-name',
|
||||
'uuid': 'some-uuid',
|
||||
'image': 'test_image:some_tag',
|
||||
'command': None,
|
||||
'memory': '512m',
|
||||
'environment': {'key1': 'val1', 'key2': 'val2'},
|
||||
}
|
||||
expected_kwargs = {
|
||||
'name': 'some-name',
|
||||
'hostname': 'some-uuid',
|
||||
'command': None,
|
||||
'mem_limit': '512m',
|
||||
'environment': {'key1': 'val1', 'key2': 'val2'},
|
||||
}
|
||||
self._test_container_create(container_dict, expected_kwargs)
|
||||
|
||||
def test_encode_utf8_unicode(self):
|
||||
image = 'some_image:some_tag'
|
||||
|
@ -226,371 +218,285 @@ class TestDockerHandler(base.BaseTestCase):
|
|||
'fake-status', 'fake-func')
|
||||
self.assertEqual('fake-status', mock_container.status)
|
||||
|
||||
@mock.patch.object(objects.Container, 'get_by_uuid')
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_reboot(self, mock_find_container, mock_get_by_uuid):
|
||||
def _test_container(self, action, docker_func_name, expected_status,
|
||||
mock_find_container, mock_get_by_uuid):
|
||||
mock_container = mock.MagicMock()
|
||||
mock_get_by_uuid.return_value = mock_container
|
||||
mock_container_uuid = 'd545a92d-609a-428f-8edb-16b02ad20ca1'
|
||||
mock_docker_id = '2703ef2b705d'
|
||||
mock_find_container.return_value = mock_docker_id
|
||||
self.conductor.container_reboot(None, mock_container_uuid)
|
||||
self.mock_docker.restart.assert_called_once_with(mock_docker_id)
|
||||
action_func = getattr(self.conductor, action)
|
||||
action_func(None, mock_container_uuid)
|
||||
docker_func = getattr(self.mock_docker, docker_func_name)
|
||||
docker_func.assert_called_once_with(mock_docker_id)
|
||||
mock_find_container.assert_called_once_with(self.mock_docker,
|
||||
mock_container_uuid)
|
||||
self.assertEqual(fields.ContainerStatus.RUNNING, mock_container.status)
|
||||
self.assertEqual(expected_status, mock_container.status)
|
||||
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_reboot_with_failure(self, mock_find_container):
|
||||
def _test_container_with_failure(
|
||||
self, action, docker_func_name, mock_find_container):
|
||||
mock_container_uuid = 'd545a92d-609a-428f-8edb-16b02ad20ca1'
|
||||
mock_docker_id = '2703ef2b705d'
|
||||
mock_find_container.return_value = mock_docker_id
|
||||
with patch.object(errors.APIError, '__str__',
|
||||
return_value='hit error') as mock_init:
|
||||
self.mock_docker.restart = mock.Mock(
|
||||
side_effect=errors.APIError('Error', '', ''))
|
||||
|
||||
setattr(self.mock_docker, docker_func_name, mock.Mock(
|
||||
side_effect=errors.APIError('Error', '', '')))
|
||||
self.assertRaises(exception.ContainerException,
|
||||
self.conductor.container_reboot,
|
||||
getattr(self.conductor, action),
|
||||
None, mock_container_uuid)
|
||||
self.mock_docker.restart.assert_called_once_with(mock_docker_id)
|
||||
docker_func = getattr(self.mock_docker, docker_func_name)
|
||||
docker_func.assert_called_once_with(mock_docker_id)
|
||||
mock_find_container.assert_called_once_with(self.mock_docker,
|
||||
mock_container_uuid)
|
||||
mock_init.assert_called_once_with()
|
||||
|
||||
@mock.patch.object(objects.Container, 'get_by_uuid')
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_reboot(self, mock_find_container, mock_get_by_uuid):
|
||||
self._test_container(
|
||||
'container_reboot', 'restart', fields.ContainerStatus.RUNNING,
|
||||
mock_find_container, mock_get_by_uuid)
|
||||
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_reboot_with_failure(self, mock_find_container):
|
||||
self._test_container_with_failure(
|
||||
'container_reboot', 'restart', mock_find_container)
|
||||
|
||||
@mock.patch.object(objects.Container, 'get_by_uuid')
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_start(self, mock_find_container, mock_get_by_uuid):
|
||||
mock_container = mock.MagicMock()
|
||||
mock_get_by_uuid.return_value = mock_container
|
||||
mock_container_uuid = 'd545a92d-609a-428f-8edb-16b02ad20ca1'
|
||||
mock_docker_id = '2703ef2b705d'
|
||||
mock_find_container.return_value = mock_docker_id
|
||||
self.conductor.container_start(None, mock_container_uuid)
|
||||
self.mock_docker.start.assert_called_once_with(mock_docker_id)
|
||||
mock_find_container.assert_called_once_with(self.mock_docker,
|
||||
mock_container_uuid)
|
||||
self.assertEqual(fields.ContainerStatus.RUNNING, mock_container.status)
|
||||
self._test_container(
|
||||
'container_start', 'start', fields.ContainerStatus.RUNNING,
|
||||
mock_find_container, mock_get_by_uuid)
|
||||
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_start_with_failure(self, mock_find_container):
|
||||
mock_container_uuid = 'd545a92d-609a-428f-8edb-16b02ad20ca1'
|
||||
mock_docker_id = '2703ef2b705d'
|
||||
mock_find_container.return_value = mock_docker_id
|
||||
with patch.object(errors.APIError, '__str__',
|
||||
return_value='hit error') as mock_init:
|
||||
self.mock_docker.start = mock.Mock(
|
||||
side_effect=errors.APIError('Error', '', ''))
|
||||
|
||||
self.assertRaises(exception.ContainerException,
|
||||
self.conductor.container_start,
|
||||
None, mock_container_uuid)
|
||||
self.mock_docker.start.assert_called_once_with(mock_docker_id)
|
||||
mock_find_container.assert_called_once_with(self.mock_docker,
|
||||
mock_container_uuid)
|
||||
mock_init.assert_called_once_with()
|
||||
self._test_container_with_failure(
|
||||
'container_start', 'start', mock_find_container)
|
||||
|
||||
@mock.patch.object(objects.Container, 'get_by_uuid')
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_stop(self, mock_find_container, mock_get_by_uuid):
|
||||
mock_container = mock.MagicMock()
|
||||
mock_get_by_uuid.return_value = mock_container
|
||||
mock_container_uuid = 'd545a92d-609a-428f-8edb-16b02ad20ca1'
|
||||
mock_docker_id = '2703ef2b705d'
|
||||
mock_find_container.return_value = mock_docker_id
|
||||
self.conductor.container_stop(None, mock_container_uuid)
|
||||
self.mock_docker.stop.assert_called_once_with(mock_docker_id)
|
||||
mock_find_container.assert_called_once_with(self.mock_docker,
|
||||
mock_container_uuid)
|
||||
self.assertEqual(fields.ContainerStatus.STOPPED, mock_container.status)
|
||||
self._test_container(
|
||||
'container_stop', 'stop', fields.ContainerStatus.STOPPED,
|
||||
mock_find_container, mock_get_by_uuid)
|
||||
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_stop_with_failure(self, mock_find_container):
|
||||
mock_container_uuid = 'd545a92d-609a-428f-8edb-16b02ad20ca1'
|
||||
mock_docker_id = '2703ef2b705d'
|
||||
mock_find_container.return_value = mock_docker_id
|
||||
with patch.object(errors.APIError, '__str__',
|
||||
return_value='hit error') as mock_init:
|
||||
self.mock_docker.stop = mock.Mock(
|
||||
side_effect=errors.APIError('Error', '', ''))
|
||||
|
||||
self.assertRaises(exception.ContainerException,
|
||||
self.conductor.container_stop,
|
||||
None, mock_container_uuid)
|
||||
self.mock_docker.stop.assert_called_once_with(mock_docker_id)
|
||||
mock_find_container.assert_called_once_with(self.mock_docker,
|
||||
mock_container_uuid)
|
||||
mock_init.assert_called_once_with()
|
||||
self._test_container_with_failure(
|
||||
'container_stop', 'stop', mock_find_container)
|
||||
|
||||
@mock.patch.object(objects.Container, 'get_by_uuid')
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_pause(self, mock_find_container, mock_get_by_uuid):
|
||||
mock_container = mock.MagicMock()
|
||||
mock_get_by_uuid.return_value = mock_container
|
||||
mock_container_uuid = 'd545a92d-609a-428f-8edb-16b02ad20ca1'
|
||||
mock_docker_id = '2703ef2b705d'
|
||||
mock_find_container.return_value = mock_docker_id
|
||||
self.conductor.container_pause(None, mock_container_uuid)
|
||||
self.mock_docker.pause.assert_called_once_with(mock_docker_id)
|
||||
mock_find_container.assert_called_once_with(self.mock_docker,
|
||||
mock_container_uuid)
|
||||
self.assertEqual(fields.ContainerStatus.PAUSED, mock_container.status)
|
||||
self._test_container(
|
||||
'container_pause', 'pause', fields.ContainerStatus.PAUSED,
|
||||
mock_find_container, mock_get_by_uuid)
|
||||
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_pause_with_failure(self, mock_find_container):
|
||||
mock_container_uuid = 'd545a92d-609a-428f-8edb-16b02ad20ca1'
|
||||
mock_docker_id = '2703ef2b705d'
|
||||
mock_find_container.return_value = mock_docker_id
|
||||
with patch.object(errors.APIError, '__str__',
|
||||
return_value='hit error') as mock_init:
|
||||
self.mock_docker.pause = mock.Mock(
|
||||
side_effect=errors.APIError('Error', '', ''))
|
||||
|
||||
self.assertRaises(exception.ContainerException,
|
||||
self.conductor.container_pause,
|
||||
None, mock_container_uuid)
|
||||
self.mock_docker.pause.assert_called_once_with(mock_docker_id)
|
||||
mock_find_container.assert_called_once_with(self.mock_docker,
|
||||
mock_container_uuid)
|
||||
mock_init.assert_called_once_with()
|
||||
self._test_container_with_failure(
|
||||
'container_pause', 'pause', mock_find_container)
|
||||
|
||||
@mock.patch.object(objects.Container, 'get_by_uuid')
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_unpause(self, mock_find_container, mock_get_by_uuid):
|
||||
mock_container = mock.MagicMock()
|
||||
mock_get_by_uuid.return_value = mock_container
|
||||
mock_container_uuid = 'd545a92d-609a-428f-8edb-16b02ad20ca1'
|
||||
mock_docker_id = '2703ef2b705d'
|
||||
mock_find_container.return_value = mock_docker_id
|
||||
self.conductor.container_unpause(None, mock_container_uuid)
|
||||
self.mock_docker.unpause.assert_called_once_with(mock_docker_id)
|
||||
mock_find_container.assert_called_once_with(self.mock_docker,
|
||||
mock_container_uuid)
|
||||
self.assertEqual(fields.ContainerStatus.RUNNING, mock_container.status)
|
||||
self._test_container(
|
||||
'container_unpause', 'unpause', fields.ContainerStatus.RUNNING,
|
||||
mock_find_container, mock_get_by_uuid)
|
||||
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_unpause_with_failure(self, mock_find_container):
|
||||
mock_container_uuid = 'd545a92d-609a-428f-8edb-16b02ad20ca1'
|
||||
mock_docker_id = '2703ef2b705d'
|
||||
mock_find_container.return_value = mock_docker_id
|
||||
with patch.object(errors.APIError, '__str__',
|
||||
return_value='hit error') as mock_init:
|
||||
self.mock_docker.unpause = mock.Mock(
|
||||
side_effect=errors.APIError('Error', '', ''))
|
||||
self._test_container_with_failure(
|
||||
'container_unpause', 'unpause', mock_find_container)
|
||||
|
||||
self.assertRaises(exception.ContainerException,
|
||||
self.conductor.container_unpause,
|
||||
None, mock_container_uuid)
|
||||
self.mock_docker.unpause.assert_called_once_with(mock_docker_id)
|
||||
mock_find_container.assert_called_once_with(self.mock_docker,
|
||||
mock_container_uuid)
|
||||
mock_init.assert_called_once_with()
|
||||
def _test_container_show(
|
||||
self, mock_find_container, mock_get_by_uuid, container_detail=None,
|
||||
expected_status=None, mock_docker_id='2703ef2b705d'):
|
||||
mock_container = mock.MagicMock()
|
||||
mock_get_by_uuid.return_value = mock_container
|
||||
mock_container_uuid = 'd545a92d-609a-428f-8edb-16b02ad20ca1'
|
||||
mock_find_container.return_value = mock_docker_id
|
||||
if container_detail is not None:
|
||||
self.mock_docker.inspect_container.return_value = container_detail
|
||||
|
||||
self.conductor.container_show(None, mock_container_uuid)
|
||||
|
||||
if mock_docker_id:
|
||||
self.mock_docker.inspect_container.assert_called_once_with(
|
||||
mock_docker_id)
|
||||
mock_find_container.assert_called_once_with(self.mock_docker,
|
||||
mock_container_uuid)
|
||||
if expected_status is not None:
|
||||
self.assertEqual(expected_status, mock_container.status)
|
||||
|
||||
@mock.patch.object(objects.Container, 'get_by_uuid')
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_show(self, mock_find_container, mock_get_by_uuid):
|
||||
mock_container = mock.MagicMock()
|
||||
mock_get_by_uuid.return_value = mock_container
|
||||
mock_container_uuid = 'd545a92d-609a-428f-8edb-16b02ad20ca1'
|
||||
mock_docker_id = '2703ef2b705d'
|
||||
mock_find_container.return_value = mock_docker_id
|
||||
self.conductor.container_show(None, mock_container_uuid)
|
||||
self.mock_docker.inspect_container.assert_called_once_with(
|
||||
mock_docker_id)
|
||||
mock_find_container.assert_called_once_with(self.mock_docker,
|
||||
mock_container_uuid)
|
||||
self._test_container_show(mock_find_container, mock_get_by_uuid)
|
||||
|
||||
@mock.patch.object(objects.Container, 'get_by_uuid')
|
||||
@mock.patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_show_with_running_state(self, mock_find_container,
|
||||
mock_get_by_uuid):
|
||||
mock_container = mock.MagicMock()
|
||||
mock_get_by_uuid.return_value = mock_container
|
||||
mock_container_uuid = 'd545a92d-609a-428f-8edb-16b02ad20ca1'
|
||||
mock_docker_id = '2703ef2b705d'
|
||||
mock_find_container.return_value = mock_docker_id
|
||||
mock_container_detail = {'State': {'Error': '',
|
||||
'Running': True,
|
||||
'Paused': False}}
|
||||
self.mock_docker.inspect_container.return_value = mock_container_detail
|
||||
self.conductor.container_show(None, mock_container_uuid)
|
||||
self.assertEqual(fields.ContainerStatus.RUNNING, mock_container.status)
|
||||
self._test_container_show(
|
||||
mock_find_container, mock_get_by_uuid, mock_container_detail,
|
||||
fields.ContainerStatus.RUNNING)
|
||||
|
||||
@mock.patch.object(objects.Container, 'get_by_uuid')
|
||||
@mock.patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_show_with_stop_state(self, mock_find_container,
|
||||
mock_get_by_uuid):
|
||||
mock_container = mock.MagicMock()
|
||||
mock_get_by_uuid.return_value = mock_container
|
||||
mock_container_uuid = 'd545a92d-609a-428f-8edb-16b02ad20ca1'
|
||||
mock_docker_id = '2703ef2b705d'
|
||||
mock_find_container.return_value = mock_docker_id
|
||||
mock_container_detail = {'State': {'Error': '',
|
||||
'Running': False,
|
||||
'Paused': False}}
|
||||
self.mock_docker.inspect_container.return_value = mock_container_detail
|
||||
self.conductor.container_show(None, mock_container_uuid)
|
||||
self.assertEqual(fields.ContainerStatus.STOPPED, mock_container.status)
|
||||
self._test_container_show(
|
||||
mock_find_container, mock_get_by_uuid, mock_container_detail,
|
||||
fields.ContainerStatus.STOPPED)
|
||||
|
||||
@mock.patch.object(objects.Container, 'get_by_uuid')
|
||||
@mock.patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_show_with_pause_state(self, mock_find_container,
|
||||
mock_get_by_uuid):
|
||||
mock_container = mock.MagicMock()
|
||||
mock_get_by_uuid.return_value = mock_container
|
||||
mock_container_uuid = 'd545a92d-609a-428f-8edb-16b02ad20ca1'
|
||||
mock_docker_id = '2703ef2b705d'
|
||||
mock_find_container.return_value = mock_docker_id
|
||||
mock_container_detail = {'State': {'Error': '',
|
||||
'Running': True,
|
||||
'Paused': True}}
|
||||
self.mock_docker.inspect_container.return_value = mock_container_detail
|
||||
self.conductor.container_show(None, mock_container_uuid)
|
||||
self.assertEqual(fields.ContainerStatus.PAUSED, mock_container.status)
|
||||
self._test_container_show(
|
||||
mock_find_container, mock_get_by_uuid, mock_container_detail,
|
||||
fields.ContainerStatus.PAUSED)
|
||||
|
||||
@mock.patch.object(objects.Container, 'get_by_uuid')
|
||||
@mock.patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_show_with_error_status(self, mock_find_container,
|
||||
mock_get_by_uuid):
|
||||
mock_container = mock.MagicMock()
|
||||
mock_get_by_uuid.return_value = mock_container
|
||||
mock_container_uuid = 'd545a92d-609a-428f-8edb-16b02ad20ca1'
|
||||
mock_docker_id = '2703ef2b705d'
|
||||
mock_find_container.return_value = mock_docker_id
|
||||
mock_container_detail = {'State': {'Error': True,
|
||||
'Running': False,
|
||||
'Paused': False}}
|
||||
self.mock_docker.inspect_container.return_value = mock_container_detail
|
||||
self.conductor.container_show(None, mock_container_uuid)
|
||||
self.assertEqual(fields.ContainerStatus.ERROR, mock_container.status)
|
||||
self._test_container_show(
|
||||
mock_find_container, mock_get_by_uuid, mock_container_detail,
|
||||
fields.ContainerStatus.ERROR)
|
||||
|
||||
@mock.patch.object(objects.Container, 'get_by_uuid')
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_show_with_failure(self, mock_find_container,
|
||||
mock_get_by_uuid):
|
||||
mock_get_by_uuid.return_value = mock.MagicMock()
|
||||
def _test_container_show_with_failure(
|
||||
self, mock_find_container, mock_get_by_uuid, error,
|
||||
assert_raise=True, expected_status=None):
|
||||
mock_container = mock.MagicMock()
|
||||
mock_get_by_uuid.return_value = mock_container
|
||||
mock_container_uuid = 'd545a92d-609a-428f-8edb-1d6b02ad20ca1'
|
||||
mock_docker_id = '2703ef2b705d'
|
||||
mock_find_container.return_value = mock_docker_id
|
||||
with patch.object(errors.APIError, '__str__',
|
||||
return_value='hit error') as mock_init:
|
||||
return_value=error) as mock_init:
|
||||
self.mock_docker.inspect_container = mock.Mock(
|
||||
side_effect=errors.APIError('Error', '', ''))
|
||||
self.assertRaises(exception.ContainerException,
|
||||
self.conductor.container_show,
|
||||
None, mock_container_uuid)
|
||||
|
||||
if assert_raise:
|
||||
self.assertRaises(exception.ContainerException,
|
||||
self.conductor.container_show,
|
||||
None, mock_container_uuid)
|
||||
else:
|
||||
self.conductor.container_show(None, mock_container_uuid)
|
||||
|
||||
self.mock_docker.inspect_container.assert_called_once_with(
|
||||
mock_docker_id)
|
||||
mock_find_container.assert_called_once_with(self.mock_docker,
|
||||
mock_container_uuid)
|
||||
mock_init.assert_called_with()
|
||||
if expected_status is not None:
|
||||
self.assertEqual(expected_status, mock_container.status)
|
||||
|
||||
@mock.patch.object(objects.Container, 'get_by_uuid')
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_show_with_failure(self, mock_find_container,
|
||||
mock_get_by_uuid):
|
||||
self._test_container_show_with_failure(
|
||||
mock_find_container, mock_get_by_uuid, error='hit error')
|
||||
|
||||
@mock.patch.object(objects.Container, 'get_by_uuid')
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_show_with_not_found(self, mock_find_container,
|
||||
mock_get_by_uuid):
|
||||
mock_container = mock.MagicMock()
|
||||
mock_get_by_uuid.return_value = mock_container
|
||||
mock_container_uuid = 'd545a92d-609a-428f-8edb-1d6b02ad20ca1'
|
||||
mock_docker_id = '2703ef2b705d'
|
||||
mock_find_container.return_value = mock_docker_id
|
||||
with patch.object(errors.APIError, '__str__',
|
||||
return_value='404 error') as mock_init:
|
||||
self.mock_docker.inspect_container = mock.Mock(
|
||||
side_effect=errors.APIError('Error', '', ''))
|
||||
self.conductor.container_show(None, mock_container_uuid)
|
||||
self.mock_docker.inspect_container.assert_called_once_with(
|
||||
mock_docker_id)
|
||||
mock_find_container.assert_called_once_with(self.mock_docker,
|
||||
mock_container_uuid)
|
||||
mock_init.assert_called_once_with()
|
||||
self.assertEqual(fields.ContainerStatus.ERROR,
|
||||
mock_container.status)
|
||||
self._test_container_show_with_failure(
|
||||
mock_find_container, mock_get_by_uuid, error='404 error',
|
||||
assert_raise=False, expected_status=fields.ContainerStatus.ERROR)
|
||||
|
||||
@mock.patch.object(objects.Container, 'get_by_uuid')
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_show_with_not_found_from_docker(self,
|
||||
mock_find_container,
|
||||
mock_get_by_uuid):
|
||||
mock_container = mock.MagicMock()
|
||||
mock_get_by_uuid.return_value = mock_container
|
||||
mock_container_uuid = 'd545a92d-609a-428f-8edb-1d6b02ad20ca1'
|
||||
mock_docker_id = {}
|
||||
self._test_container_show(
|
||||
mock_find_container, mock_get_by_uuid, mock_docker_id={},
|
||||
expected_status=fields.ContainerStatus.ERROR)
|
||||
|
||||
def _test_container_exec(self, mock_find_container, docker_version='1.2.2',
|
||||
deprecated=False):
|
||||
mock_container_uuid = 'd545a92d-609a-428f-8edb-16b02ad20ca1'
|
||||
mock_docker_id = '2703ef2b705d'
|
||||
docker.version = docker_version
|
||||
mock_find_container.return_value = mock_docker_id
|
||||
self.conductor.container_show(None, mock_container_uuid)
|
||||
mock_create_res = mock.MagicMock()
|
||||
self.mock_docker.exec_create.return_value = mock_create_res
|
||||
self.conductor.container_exec(None, mock_container_uuid, 'ls')
|
||||
if deprecated:
|
||||
self.mock_docker.execute.assert_called_once_with(
|
||||
mock_docker_id, 'ls')
|
||||
else:
|
||||
self.mock_docker.exec_create.assert_called_once_with(
|
||||
mock_docker_id, 'ls', True, True, False)
|
||||
self. mock_docker.exec_start.assert_called_once_with(
|
||||
mock_create_res, False, False, False)
|
||||
mock_find_container.assert_called_once_with(self.mock_docker,
|
||||
mock_container_uuid)
|
||||
self.assertEqual(fields.ContainerStatus.ERROR, mock_container.status)
|
||||
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_exec(self, mock_find_container):
|
||||
mock_container_uuid = 'd545a92d-609a-428f-8edb-16b02ad20ca1'
|
||||
mock_docker_id = '2703ef2b705d'
|
||||
docker.version = '1.2.2'
|
||||
mock_find_container.return_value = mock_docker_id
|
||||
mock_create_res = mock.MagicMock()
|
||||
self.mock_docker.exec_create.return_value = mock_create_res
|
||||
self.conductor.container_exec(None, mock_container_uuid, 'ls')
|
||||
self.mock_docker.exec_create.assert_called_once_with(mock_docker_id,
|
||||
'ls',
|
||||
True, True, False)
|
||||
|
||||
self. mock_docker.exec_start.assert_called_once_with(mock_create_res,
|
||||
False, False,
|
||||
False)
|
||||
mock_find_container.assert_called_once_with(self.mock_docker,
|
||||
mock_container_uuid)
|
||||
self._test_container_exec(mock_find_container)
|
||||
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_exec_deprecated(self, mock_find_container):
|
||||
self._test_container_exec(
|
||||
mock_find_container, docker_version='0.7.0', deprecated=True)
|
||||
|
||||
def _test_container_exec_with_failure(
|
||||
self, mock_find_container, docker_version='1.2.2',
|
||||
deprecated=False):
|
||||
mock_container_uuid = 'd545a92d-609a-428f-8edb-16b02ad20ca1'
|
||||
mock_docker_id = '2703ef2b705d'
|
||||
docker.version = '0.7.0'
|
||||
docker.version = docker_version
|
||||
mock_find_container.return_value = mock_docker_id
|
||||
mock_create_res = mock.MagicMock()
|
||||
self.mock_docker.exec_create.return_value = mock_create_res
|
||||
self.conductor.container_exec(None, mock_container_uuid, 'ls')
|
||||
self.mock_docker.execute.assert_called_once_with(mock_docker_id, 'ls')
|
||||
mock_find_container.assert_called_once_with(self.mock_docker,
|
||||
mock_container_uuid)
|
||||
with patch.object(errors.APIError, '__str__',
|
||||
return_value='hit error') as mock_init:
|
||||
if deprecated:
|
||||
self.mock_docker.execute = mock.Mock(
|
||||
side_effect=errors.APIError('Error', '', ''))
|
||||
else:
|
||||
self.mock_docker.exec_create = mock.Mock(
|
||||
side_effect=errors.APIError('Error', '', ''))
|
||||
self.assertRaises(exception.ContainerException,
|
||||
self.conductor.container_exec,
|
||||
None, mock_container_uuid, 'ls')
|
||||
if deprecated:
|
||||
self.mock_docker.execute.assert_called_once_with(
|
||||
mock_docker_id, 'ls')
|
||||
else:
|
||||
self.mock_docker.exec_create.assert_called_once_with(
|
||||
mock_docker_id, 'ls', True, True, False)
|
||||
mock_find_container.assert_called_once_with(self.mock_docker,
|
||||
mock_container_uuid)
|
||||
mock_init.assert_called_once_with()
|
||||
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_exec_with_failure(self, mock_find_container):
|
||||
mock_container_uuid = 'd545a92d-609a-428f-8edb-16b02ad20ca1'
|
||||
mock_docker_id = '2703ef2b705d'
|
||||
docker.version = '1.2.2'
|
||||
mock_find_container.return_value = mock_docker_id
|
||||
with patch.object(errors.APIError, '__str__',
|
||||
return_value='hit error') as mock_init:
|
||||
self.mock_docker.exec_create = mock.Mock(
|
||||
side_effect=errors.APIError('Error', '', ''))
|
||||
self.assertRaises(exception.ContainerException,
|
||||
self.conductor.container_exec,
|
||||
None, mock_container_uuid, 'ls')
|
||||
self.mock_docker.exec_create.assert_called_once_with(
|
||||
mock_docker_id, 'ls', True, True, False)
|
||||
mock_find_container.assert_called_once_with(self.mock_docker,
|
||||
mock_container_uuid)
|
||||
mock_init.assert_called_once_with()
|
||||
self._test_container_exec_with_failure(mock_find_container)
|
||||
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_exec_deprecated_with_failure(self, mock_find_container):
|
||||
mock_container_uuid = 'd545a92d-609a-428f-8edb-16b02ad20ca1'
|
||||
mock_docker_id = '2703ef2b705d'
|
||||
docker.version = '0.7.0'
|
||||
mock_find_container.return_value = mock_docker_id
|
||||
with patch.object(errors.APIError, '__str__',
|
||||
return_value='hit error') as mock_init:
|
||||
self.mock_docker.execute = mock.Mock(
|
||||
side_effect=errors.APIError('Error', '', ''))
|
||||
self.assertRaises(exception.ContainerException,
|
||||
self.conductor.container_exec,
|
||||
None, mock_container_uuid, 'ls')
|
||||
self.mock_docker.execute.assert_called_once_with(mock_docker_id,
|
||||
'ls')
|
||||
mock_find_container.assert_called_once_with(self.mock_docker,
|
||||
mock_container_uuid)
|
||||
mock_init.assert_called_once_with()
|
||||
self._test_container_exec_with_failure(
|
||||
mock_find_container, docker_version='0.7.0', deprecated=True)
|
||||
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_logs(self, mock_find_container):
|
||||
|
|
Loading…
Reference in New Issue