From 554b92e458f5c9887f25275ecb1d0340870060af Mon Sep 17 00:00:00 2001 From: Valeriy Ponomaryov Date: Mon, 3 Aug 2015 15:35:48 +0300 Subject: [PATCH] Fix exceptions handling for py34 compatibility Fix following cases: "except SomeException, e:" "raise exc_value, msg, exc_trace" "e.message" Partially-Implements: bp py3-compatibility Change-Id: I8c20ae725000895b6b7b7c0da7d4bfba3e4849f0 --- manila/api/openstack/wsgi.py | 2 +- manila/compute/nova.py | 15 ++++++------- manila/share/api.py | 4 ++-- manila/share/drivers/glusterfs.py | 2 +- manila/share/drivers/glusterfs_native.py | 2 +- manila/tests/cmd/test_manage.py | 11 +++++----- manila/tests/compute/test_nova.py | 16 ++++++++++---- manila/tests/volume/test_cinder.py | 16 ++++++++++---- manila/volume/cinder.py | 27 ++++++++++-------------- 9 files changed, 52 insertions(+), 43 deletions(-) diff --git a/manila/api/openstack/wsgi.py b/manila/api/openstack/wsgi.py index e28d32cf..cda78424 100644 --- a/manila/api/openstack/wsgi.py +++ b/manila/api/openstack/wsgi.py @@ -769,7 +769,7 @@ class Resource(wsgi.Application): try: msg_dict = dict(url=request.url, status=response.status_int) msg = _("%(url)s returned with HTTP %(status)d") % msg_dict - except AttributeError, e: + except AttributeError as e: msg_dict = dict(url=request.url, e=e) msg = _("%(url)s returned a fault: %(e)s") % msg_dict diff --git a/manila/compute/nova.py b/manila/compute/nova.py index fb70b5c5..5d016999 100644 --- a/manila/compute/nova.py +++ b/manila/compute/nova.py @@ -16,8 +16,6 @@ Handles all requests to Nova. """ -import sys - from novaclient import exceptions as nova_exception from novaclient import service_catalog from novaclient import utils @@ -26,6 +24,7 @@ from novaclient.v2.contrib import assisted_volume_snapshots from novaclient.v2 import servers as nova_servers from oslo_config import cfg from oslo_log import log +import six from manila.db import base from manila import exception @@ -152,13 +151,11 @@ def translate_server_exception(method): def wrapper(self, ctx, instance_id, *args, **kwargs): try: res = method(self, ctx, instance_id, *args, **kwargs) - except nova_exception.ClientException: - exc_type, exc_value, exc_trace = sys.exc_info() - if isinstance(exc_value, nova_exception.NotFound): - exc_value = exception.InstanceNotFound(instance_id=instance_id) - elif isinstance(exc_value, nova_exception.BadRequest): - exc_value = exception.InvalidInput(reason=exc_value.message) - raise exc_value, None, exc_trace + except nova_exception.ClientException as e: + if isinstance(e, nova_exception.NotFound): + raise exception.InstanceNotFound(instance_id=instance_id) + elif isinstance(e, nova_exception.BadRequest): + raise exception.InvalidInput(reason=six.text_type(e)) return res return wrapper diff --git a/manila/share/api.py b/manila/share/api.py index 078da300..9aacf9e6 100644 --- a/manila/share/api.py +++ b/manila/share/api.py @@ -166,7 +166,7 @@ class API(base.Base): try: is_public = strutils.bool_from_string(is_public, strict=True) except ValueError as e: - raise exception.InvalidParameterValue(e.message) + raise exception.InvalidParameterValue(six.text_type(e)) options = {'size': size, 'user_id': context.user_id, @@ -432,7 +432,7 @@ class API(base.Base): fields['is_public'] = strutils.bool_from_string( fields['is_public'], strict=True) except ValueError as e: - raise exception.InvalidParameterValue(e.message) + raise exception.InvalidParameterValue(six.text_type(e)) return self.db.share_update(context, share['id'], fields) @policy.wrap_check_policy('share') diff --git a/manila/share/drivers/glusterfs.py b/manila/share/drivers/glusterfs.py index a933e977..19ad16e4 100644 --- a/manila/share/drivers/glusterfs.py +++ b/manila/share/drivers/glusterfs.py @@ -178,7 +178,7 @@ class GlusterManager(object): raise exception.GlusterfsException( _("'gluster version' failed on server " "%(server)s: %(message)s") % - {'server': self.host, 'message': exc.message}) + {'server': self.host, 'message': six.text_type(exc)}) try: owords = out.split() if owords[0] != 'glusterfs': diff --git a/manila/share/drivers/glusterfs_native.py b/manila/share/drivers/glusterfs_native.py index 2aeea992..7e66269c 100644 --- a/manila/share/drivers/glusterfs_native.py +++ b/manila/share/drivers/glusterfs_native.py @@ -166,7 +166,7 @@ class GlusterfsNativeShareDriver(driver.ExecuteMixin, driver.ShareDriver): try: glusterfs_versions[srvaddr] = gluster_mgr.get_gluster_version() except exception.GlusterfsException as exc: - exceptions[srvaddr] = exc.message + exceptions[srvaddr] = six.text_type(exc) if exceptions: for srvaddr, excmsg in six.iteritems(exceptions): LOG.error(_LE("'gluster version' failed on server " diff --git a/manila/tests/cmd/test_manage.py b/manila/tests/cmd/test_manage.py index 7f1a5216..ed07853a 100644 --- a/manila/tests/cmd/test_manage.py +++ b/manila/tests/cmd/test_manage.py @@ -21,6 +21,7 @@ import sys import ddt import mock from oslo_config import cfg +import six from manila.cmd import manage as manila_manage from manila import context @@ -82,7 +83,7 @@ class ManilaCmdManageTestCase(test.TestCase): try: import bpython except ImportError as e: - self.skipTest(e.message) + self.skipTest(six.text_type(e)) self.mock_object(bpython, 'embed') self.shell_commands.run(**kwargs) bpython.embed.assert_called_once_with() @@ -92,7 +93,7 @@ class ManilaCmdManageTestCase(test.TestCase): import bpython import IPython except ImportError as e: - self.skipTest(e.message) + self.skipTest(six.text_type(e)) self.mock_object(bpython, 'embed', mock.Mock(side_effect=ImportError())) self.mock_object(IPython, 'embed') @@ -105,7 +106,7 @@ class ManilaCmdManageTestCase(test.TestCase): try: import bpython except ImportError as e: - self.skipTest(e.message) + self.skipTest(six.text_type(e)) self.mock_object(bpython, 'embed') self.shell_commands.run() @@ -116,7 +117,7 @@ class ManilaCmdManageTestCase(test.TestCase): try: import IPython except ImportError as e: - self.skipTest(e.message) + self.skipTest(six.text_type(e)) self.mock_object(IPython, 'embed') self.shell_commands.run(shell='ipython') @@ -131,7 +132,7 @@ class ManilaCmdManageTestCase(test.TestCase): setattr(IPython.Shell, 'IPShell', mock.Mock(side_effect=ImportError())) except ImportError as e: - self.skipTest(e.message) + self.skipTest(six.text_type(e)) self.mock_object(IPython, 'embed', mock.Mock(side_effect=ImportError())) self.mock_object(readline, 'parse_and_bind') diff --git a/manila/tests/compute/test_nova.py b/manila/tests/compute/test_nova.py index a54046de..c2373001 100644 --- a/manila/tests/compute/test_nova.py +++ b/manila/tests/compute/test_nova.py @@ -12,6 +12,7 @@ # License for the specific language governing permissions and limitations # under the License. +import ddt import mock from novaclient import exceptions as nova_exception from novaclient import utils @@ -85,6 +86,7 @@ class FakeNovaClient(object): self.fixed_ips = self.FixedIPs() +@ddt.ddt class NovaApiTestCase(test.TestCase): def setUp(self): super(NovaApiTestCase, self).setUp() @@ -123,11 +125,17 @@ class NovaApiTestCase(test.TestCase): self.assertEqual(instance_id, result['id']) utils.find_resource.assert_called_once_with(mock.ANY, instance_id) - def test_server_get_failed(self): - nova.novaclient.side_effect = nova_exception.NotFound(404) + @ddt.data( + {'nova_e': nova_exception.NotFound(404), + 'manila_e': exception.InstanceNotFound}, + {'nova_e': nova_exception.BadRequest(400), + 'manila_e': exception.InvalidInput}, + ) + @ddt.unpack + def test_server_get_failed(self, nova_e, manila_e): + nova.novaclient.side_effect = nova_e instance_id = 'instance_id' - self.assertRaises(exception.InstanceNotFound, - self.api.server_get, self.ctx, instance_id) + self.assertRaises(manila_e, self.api.server_get, self.ctx, instance_id) def test_server_list(self): self.assertEqual([{'id': 'id1'}, {'id': 'id2'}], diff --git a/manila/tests/volume/test_cinder.py b/manila/tests/volume/test_cinder.py index 908fc3a2..1c36ec20 100644 --- a/manila/tests/volume/test_cinder.py +++ b/manila/tests/volume/test_cinder.py @@ -13,6 +13,7 @@ # under the License. from cinderclient import exceptions as cinder_exception +import ddt import mock from manila import context @@ -40,6 +41,7 @@ class FakeCinderClient(object): self.volume_snapshots = self.volumes +@ddt.ddt class CinderApiTestCase(test.TestCase): def setUp(self): super(CinderApiTestCase, self).setUp() @@ -59,11 +61,17 @@ class CinderApiTestCase(test.TestCase): result = self.api.get(self.ctx, volume_id) self.assertEqual(result['id'], volume_id) - def test_get_failed(self): - cinder.cinderclient.side_effect = cinder_exception.NotFound(404) + @ddt.data( + {'cinder_e': cinder_exception.NotFound(404), + 'manila_e': exception.VolumeNotFound}, + {'cinder_e': cinder_exception.BadRequest(400), + 'manila_e': exception.InvalidInput}, + ) + @ddt.unpack + def test_get_failed(self, cinder_e, manila_e): + cinder.cinderclient.side_effect = cinder_e volume_id = 'volume_id' - self.assertRaises(exception.VolumeNotFound, - self.api.get, self.ctx, volume_id) + self.assertRaises(manila_e, self.api.get, self.ctx, volume_id) def test_create(self): result = self.api.create(self.ctx, 1, '', '') diff --git a/manila/volume/cinder.py b/manila/volume/cinder.py index 22b23c10..f8341343 100644 --- a/manila/volume/cinder.py +++ b/manila/volume/cinder.py @@ -18,13 +18,13 @@ Handles all requests relating to volumes + cinder. """ import copy -import sys from cinderclient import exceptions as cinder_exception from cinderclient import service_catalog from cinderclient.v2 import client as cinder_client from oslo_config import cfg from oslo_log import log +import six import manila.context as ctxt from manila.db import base @@ -176,13 +176,11 @@ def translate_volume_exception(method): def wrapper(self, ctx, volume_id, *args, **kwargs): try: res = method(self, ctx, volume_id, *args, **kwargs) - except cinder_exception.ClientException: - exc_type, exc_value, exc_trace = sys.exc_info() - if isinstance(exc_value, cinder_exception.NotFound): - exc_value = exception.VolumeNotFound(volume_id=volume_id) - elif isinstance(exc_value, cinder_exception.BadRequest): - exc_value = exception.InvalidInput(reason=exc_value.message) - raise exc_value, None, exc_trace + except cinder_exception.ClientException as e: + if isinstance(e, cinder_exception.NotFound): + raise exception.VolumeNotFound(volume_id=volume_id) + elif isinstance(e, cinder_exception.BadRequest): + raise exception.InvalidInput(reason=six.text_type(e)) return res return wrapper @@ -195,12 +193,9 @@ def translate_snapshot_exception(method): def wrapper(self, ctx, snapshot_id, *args, **kwargs): try: res = method(self, ctx, snapshot_id, *args, **kwargs) - except cinder_exception.ClientException: - exc_type, exc_value, exc_trace = sys.exc_info() - if isinstance(exc_value, cinder_exception.NotFound): - exc_value = ( - exception.VolumeSnapshotNotFound(snapshot_id=snapshot_id)) - raise exc_value, None, exc_trace + except cinder_exception.ClientException as e: + if isinstance(e, cinder_exception.NotFound): + raise exception.VolumeSnapshotNotFound(snapshot_id=snapshot_id) return res return wrapper @@ -303,14 +298,14 @@ class API(base.Base): item = cinderclient(context).volumes.create(size, **kwargs) return _untranslate_volume_summary_view(context, item) except cinder_exception.BadRequest as e: - raise exception.InvalidInput(reason=e.message) + raise exception.InvalidInput(reason=six.text_type(e)) except cinder_exception.NotFound: raise exception.NotFound( _("Error in creating cinder " "volume. Cinder volume type %s not exist. Check parameter " "cinder_volume_type in configuration file.") % volume_type) except Exception as e: - raise exception.ManilaException(e.message) + raise exception.ManilaException(e) @translate_volume_exception def extend(self, context, volume_id, new_size):