Add tests for docker container
This patch add tests for container start/stop/pause/reboot unpause/delete/create/show Also add ContainerException, as pep8 not allow Exception check which it thinks too board. Partial-Bug: #1439129 Change-Id: I3baeb2c91a7c49cc1871912a58fef23bebf68822
This commit is contained in:
parent
52210e89fa
commit
3a99763261
|
@ -423,3 +423,7 @@ class KeystoneFailure(MagnumException):
|
|||
class CatalogNotFound(MagnumException):
|
||||
message = _("Service type %(service_type)s with endpoint type "
|
||||
"%(endpoint_type)s not found in keystone service catalog.")
|
||||
|
||||
|
||||
class ContainerException(Exception):
|
||||
pass
|
||||
|
|
|
@ -16,6 +16,7 @@ from docker import errors
|
|||
from oslo_config import cfg
|
||||
|
||||
from magnum.common import docker_utils
|
||||
from magnum.common import exception
|
||||
from magnum.conductor.handlers.common import docker_client
|
||||
from magnum.openstack.common import log as logging
|
||||
|
||||
|
@ -91,7 +92,8 @@ class Handler(object):
|
|||
hostname=container_uuid)
|
||||
return container
|
||||
except errors.APIError as api_error:
|
||||
raise Exception("Docker API Error : %s" % str(api_error))
|
||||
raise exception.ContainerException(
|
||||
"Docker API Error : %s" % str(api_error))
|
||||
|
||||
def container_list(self, context):
|
||||
LOG.debug("container_list")
|
||||
|
@ -99,7 +101,8 @@ class Handler(object):
|
|||
container_list = self.docker.containers()
|
||||
return container_list
|
||||
except errors.APIError as api_error:
|
||||
raise Exception("Docker API Error : %s" % str(api_error))
|
||||
raise exception.ContainerException(
|
||||
"Docker API Error : %s" % str(api_error))
|
||||
|
||||
def container_delete(self, context, container_uuid):
|
||||
LOG.debug("container_delete %s" % container_uuid)
|
||||
|
@ -107,7 +110,8 @@ class Handler(object):
|
|||
docker_id = self._find_container_by_name(container_uuid)
|
||||
return self.docker.remove_container(docker_id)
|
||||
except errors.APIError as api_error:
|
||||
raise Exception("Docker API Error : %s" % str(api_error))
|
||||
raise exception.ContainerException(
|
||||
"Docker API Error : %s" % str(api_error))
|
||||
|
||||
def container_show(self, context, container_uuid):
|
||||
LOG.debug("container_show %s" % container_uuid)
|
||||
|
@ -115,7 +119,8 @@ class Handler(object):
|
|||
docker_id = self._find_container_by_name(container_uuid)
|
||||
return self.docker.inspect_container(docker_id)
|
||||
except errors.APIError as api_error:
|
||||
raise Exception("Docker API Error : %s" % str(api_error))
|
||||
raise exception.ContainerException(
|
||||
"Docker API Error : %s" % str(api_error))
|
||||
|
||||
def container_reboot(self, context, container_uuid):
|
||||
LOG.debug("container_reboot %s" % container_uuid)
|
||||
|
@ -123,7 +128,8 @@ class Handler(object):
|
|||
docker_id = self._find_container_by_name(container_uuid)
|
||||
return self.docker.restart(docker_id)
|
||||
except errors.APIError as api_error:
|
||||
raise Exception("Docker API Error : %s" % str(api_error))
|
||||
raise exception.ContainerException(
|
||||
"Docker API Error : %s" % str(api_error))
|
||||
|
||||
def container_stop(self, context, container_uuid):
|
||||
LOG.debug("container_stop %s" % container_uuid)
|
||||
|
@ -131,7 +137,8 @@ class Handler(object):
|
|||
docker_id = self._find_container_by_name(container_uuid)
|
||||
return self.docker.stop(docker_id)
|
||||
except errors.APIError as api_error:
|
||||
raise Exception("Docker API Error : %s" % str(api_error))
|
||||
raise exception.ContainerException(
|
||||
"Docker API Error : %s" % str(api_error))
|
||||
|
||||
def container_start(self, context, container_uuid):
|
||||
LOG.debug("Starting container %s" % container_uuid)
|
||||
|
@ -140,7 +147,8 @@ class Handler(object):
|
|||
LOG.debug("Found Docker container %s" % docker_id)
|
||||
return self.docker.start(docker_id)
|
||||
except errors.APIError as api_error:
|
||||
raise Exception("Docker API Error : %s" % str(api_error))
|
||||
raise exception.ContainerException(
|
||||
"Docker API Error : %s" % str(api_error))
|
||||
|
||||
def container_pause(self, context, container_uuid):
|
||||
LOG.debug("container_pause %s" % container_uuid)
|
||||
|
@ -148,7 +156,8 @@ class Handler(object):
|
|||
docker_id = self._find_container_by_name(container_uuid)
|
||||
return self.docker.pause(docker_id)
|
||||
except errors.APIError as api_error:
|
||||
raise Exception("Docker API Error : %s" % str(api_error))
|
||||
raise exception.ContainerException(
|
||||
"Docker API Error : %s" % str(api_error))
|
||||
|
||||
def container_unpause(self, context, container_uuid):
|
||||
LOG.debug("container_unpause %s" % container_uuid)
|
||||
|
@ -156,7 +165,8 @@ class Handler(object):
|
|||
docker_id = self._find_container_by_name(container_uuid)
|
||||
return self.docker.unpause(docker_id)
|
||||
except errors.APIError as api_error:
|
||||
raise Exception("Docker API Error : %s" % str(api_error))
|
||||
raise exception.ContainerException(
|
||||
"Docker API Error : %s" % str(api_error))
|
||||
|
||||
def container_logs(self, context, container_uuid):
|
||||
LOG.debug("container_logs %s" % container_uuid)
|
||||
|
@ -164,7 +174,8 @@ class Handler(object):
|
|||
docker_id = self._find_container_by_name(container_uuid)
|
||||
return {'output': self.docker.get_container_logs(docker_id)}
|
||||
except errors.APIError as api_error:
|
||||
raise Exception("Docker API Error : %s" % str(api_error))
|
||||
raise exception.ContainerException(
|
||||
"Docker API Error : %s" % str(api_error))
|
||||
|
||||
def container_execute(self, context, container_uuid, command):
|
||||
LOG.debug("container_execute %s command %s" %
|
||||
|
@ -173,4 +184,5 @@ class Handler(object):
|
|||
docker_id = self._find_container_by_name(container_uuid)
|
||||
return {'output': self.docker.execute(docker_id, command)}
|
||||
except errors.APIError as api_error:
|
||||
raise Exception("Docker API Error : %s" % str(api_error))
|
||||
raise exception.ContainerException(
|
||||
"Docker API Error : %s" % str(api_error))
|
||||
|
|
|
@ -11,11 +11,13 @@
|
|||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from docker import errors
|
||||
import mock
|
||||
|
||||
from magnum.common import exception
|
||||
from magnum.conductor.handlers import docker_conductor
|
||||
from magnum.tests import base
|
||||
from mock import patch
|
||||
|
||||
|
||||
class TestDockerConductor(base.BaseTestCase):
|
||||
|
@ -38,6 +40,208 @@ class TestDockerConductor(base.BaseTestCase):
|
|||
self.mock_client.pull.assert_called_once_with('test_image',
|
||||
tag='some_tag')
|
||||
self.mock_client.inspect_image.assert_called_once_with(utf8_image_id)
|
||||
self.mock_client.create_container(mock_container.image_id,
|
||||
self.mock_client.create_container.assert_called_once_with(
|
||||
mock_container.image_id,
|
||||
name='some-name',
|
||||
hostname='some-uduid')
|
||||
hostname='some-uuid')
|
||||
|
||||
def test_container_create_with_failure(self):
|
||||
mock_container = mock.MagicMock()
|
||||
mock_container.image_id = 'test_image:some_tag'
|
||||
with patch.object(errors.APIError, '__str__',
|
||||
return_value='hit error') as mock_init:
|
||||
self.mock_client.pull = mock.Mock(side_effect=
|
||||
errors.APIError('Error', '', ''))
|
||||
|
||||
self.assertRaises(exception.ContainerException,
|
||||
self.conductor.container_create,
|
||||
None, 'some-name', 'some-uuid', mock_container)
|
||||
self.mock_client.pull.assert_called_once_with(
|
||||
'test_image',
|
||||
tag='some_tag')
|
||||
self.assertFalse(self.mock_client.create_container.called)
|
||||
mock_init.assert_called_once_with()
|
||||
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_delete(self, mock_find_container):
|
||||
mock_container_uuid = 'd545a92d-609a-428f-8edb-16b02ad20ca1'
|
||||
mock_docker_id = '2703ef2b705d'
|
||||
mock_find_container.return_value = mock_docker_id
|
||||
self.conductor.container_delete(None, mock_container_uuid)
|
||||
self.mock_client.remove_container.assert_called_once_with(
|
||||
mock_docker_id)
|
||||
mock_find_container.assert_called_once_with(mock_container_uuid)
|
||||
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_delete_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_client.remove_container = mock.Mock(side_effect=
|
||||
errors.APIError('Error', '', ''))
|
||||
self.assertRaises(exception.ContainerException,
|
||||
self.conductor.container_delete,
|
||||
None, mock_container_uuid)
|
||||
self.mock_client.remove_container.assert_called_once_with(
|
||||
mock_docker_id)
|
||||
mock_find_container.assert_called_once_with(mock_container_uuid)
|
||||
mock_init.assert_called_once_with()
|
||||
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_reboot(self, mock_find_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_client.restart.assert_called_once_with(mock_docker_id)
|
||||
mock_find_container.assert_called_once_with(mock_container_uuid)
|
||||
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_reboot_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_client.restart = mock.Mock(side_effect=
|
||||
errors.APIError('Error', '', ''))
|
||||
|
||||
self.assertRaises(exception.ContainerException,
|
||||
self.conductor.container_reboot,
|
||||
None, mock_container_uuid)
|
||||
self.mock_client.restart.assert_called_once_with(mock_docker_id)
|
||||
mock_find_container.assert_called_once_with(mock_container_uuid)
|
||||
mock_init.assert_called_once_with()
|
||||
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_start(self, mock_find_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_client.start.assert_called_once_with(mock_docker_id)
|
||||
mock_find_container.assert_called_once_with(mock_container_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_client.start = mock.Mock(side_effect=
|
||||
errors.APIError('Error', '', ''))
|
||||
|
||||
self.assertRaises(exception.ContainerException,
|
||||
self.conductor.container_start,
|
||||
None, mock_container_uuid)
|
||||
self.mock_client.start.assert_called_once_with(mock_docker_id)
|
||||
mock_find_container.assert_called_once_with(mock_container_uuid)
|
||||
mock_init.assert_called_once_with()
|
||||
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_stop(self, mock_find_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_client.stop.assert_called_once_with(mock_docker_id)
|
||||
mock_find_container.assert_called_once_with(mock_container_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_client.stop = mock.Mock(side_effect=
|
||||
errors.APIError('Error', '', ''))
|
||||
|
||||
self.assertRaises(exception.ContainerException,
|
||||
self.conductor.container_stop,
|
||||
None, mock_container_uuid)
|
||||
self.mock_client.stop.assert_called_once_with(mock_docker_id)
|
||||
mock_find_container.assert_called_once_with(mock_container_uuid)
|
||||
mock_init.assert_called_once_with()
|
||||
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_pause(self, mock_find_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_client.pause.assert_called_once_with(mock_docker_id)
|
||||
mock_find_container.assert_called_once_with(mock_container_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_client.pause = mock.Mock(side_effect=
|
||||
errors.APIError('Error', '', ''))
|
||||
|
||||
self.assertRaises(exception.ContainerException,
|
||||
self.conductor.container_pause,
|
||||
None, mock_container_uuid)
|
||||
self.mock_client.pause.assert_called_once_with(mock_docker_id)
|
||||
mock_find_container.assert_called_once_with(mock_container_uuid)
|
||||
mock_init.assert_called_once_with()
|
||||
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_unpause(self, mock_find_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_client.unpause.assert_called_once_with(mock_docker_id)
|
||||
mock_find_container.assert_called_once_with(mock_container_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_client.unpause = mock.Mock(side_effect=
|
||||
errors.APIError('Error', '', ''))
|
||||
|
||||
self.assertRaises(exception.ContainerException,
|
||||
self.conductor.container_unpause,
|
||||
None, mock_container_uuid)
|
||||
self.mock_client.unpause.assert_called_once_with(mock_docker_id)
|
||||
mock_find_container.assert_called_once_with(mock_container_uuid)
|
||||
mock_init.assert_called_once_with()
|
||||
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_show(self, mock_find_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_client.inspect_container.assert_called_once_with(
|
||||
mock_docker_id)
|
||||
mock_find_container.assert_called_once_with(mock_container_uuid)
|
||||
|
||||
@patch.object(docker_conductor.Handler, '_find_container_by_name')
|
||||
def test_container_show_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_client.inspect_container = mock.Mock(side_effect=
|
||||
errors.APIError('Error', '', ''))
|
||||
self.assertRaises(exception.ContainerException,
|
||||
self.conductor.container_show,
|
||||
None, mock_container_uuid)
|
||||
self.mock_client.inspect_container.assert_called_once_with(
|
||||
mock_docker_id)
|
||||
mock_find_container.assert_called_once_with(mock_container_uuid)
|
||||
mock_init.assert_called_once_with()
|
||||
|
|
Loading…
Reference in New Issue