Consolidate code for docker conductor tests

Change-Id: If5a92f189ce1b7ab3c968c16fc8bdca2eb47446f
This commit is contained in:
Hongbin Lu 2015-12-05 22:57:37 -05:00
parent 554af35b74
commit 791eeb2bcb
1 changed files with 230 additions and 324 deletions

View File

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