Fix handling of Novaclient exceptions
If Novaclient returns exception, then Manila wrapper for Novaclient fails with following error: File "/opt/stack/new/manila/manila/compute/nova.py", line 160, in wrapper return res UnboundLocalError: local variable 'res' referenced before assignment So, fix it and cover with unit tests. Change-Id: I5eec4b9e9f3857307daa2ade516f76c044c01631 Closes-Bug: #1530811
This commit is contained in:
parent
f1c096418d
commit
95940a4085
@ -149,15 +149,20 @@ def translate_server_exception(method):
|
|||||||
|
|
||||||
Note: keeps its traceback intact.
|
Note: keeps its traceback intact.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@six.wraps(method)
|
||||||
def wrapper(self, ctx, instance_id, *args, **kwargs):
|
def wrapper(self, ctx, instance_id, *args, **kwargs):
|
||||||
try:
|
try:
|
||||||
res = method(self, ctx, instance_id, *args, **kwargs)
|
res = method(self, ctx, instance_id, *args, **kwargs)
|
||||||
|
return res
|
||||||
except nova_exception.ClientException as e:
|
except nova_exception.ClientException as e:
|
||||||
if isinstance(e, nova_exception.NotFound):
|
if isinstance(e, nova_exception.NotFound):
|
||||||
raise exception.InstanceNotFound(instance_id=instance_id)
|
raise exception.InstanceNotFound(instance_id=instance_id)
|
||||||
elif isinstance(e, nova_exception.BadRequest):
|
elif isinstance(e, nova_exception.BadRequest):
|
||||||
raise exception.InvalidInput(reason=six.text_type(e))
|
raise exception.InvalidInput(reason=six.text_type(e))
|
||||||
return res
|
else:
|
||||||
|
raise exception.ManilaException(e)
|
||||||
|
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
@ -86,6 +86,51 @@ class FakeNovaClient(object):
|
|||||||
self.fixed_ips = self.FixedIPs()
|
self.fixed_ips = self.FixedIPs()
|
||||||
|
|
||||||
|
|
||||||
|
@nova.translate_server_exception
|
||||||
|
def decorated_by_translate_server_exception(self, context, instance_id, exc):
|
||||||
|
if exc:
|
||||||
|
raise exc(instance_id)
|
||||||
|
else:
|
||||||
|
return 'OK'
|
||||||
|
|
||||||
|
|
||||||
|
@ddt.ddt
|
||||||
|
class TranslateServerExceptionTestCase(test.TestCase):
|
||||||
|
|
||||||
|
def test_translate_server_exception(self):
|
||||||
|
result = decorated_by_translate_server_exception(
|
||||||
|
'foo_self', 'foo_ctxt', 'foo_instance_id', None)
|
||||||
|
self.assertEqual('OK', result)
|
||||||
|
|
||||||
|
def test_translate_server_exception_not_found(self):
|
||||||
|
self.assertRaises(
|
||||||
|
exception.InstanceNotFound,
|
||||||
|
decorated_by_translate_server_exception,
|
||||||
|
'foo_self', 'foo_ctxt', 'foo_instance_id', nova_exception.NotFound)
|
||||||
|
|
||||||
|
def test_translate_server_exception_bad_request(self):
|
||||||
|
self.assertRaises(
|
||||||
|
exception.InvalidInput,
|
||||||
|
decorated_by_translate_server_exception,
|
||||||
|
'foo_self', 'foo_ctxt', 'foo_instance_id',
|
||||||
|
nova_exception.BadRequest)
|
||||||
|
|
||||||
|
@ddt.data(
|
||||||
|
nova_exception.HTTPNotImplemented,
|
||||||
|
nova_exception.RetryAfterException,
|
||||||
|
nova_exception.Unauthorized,
|
||||||
|
nova_exception.Forbidden,
|
||||||
|
nova_exception.MethodNotAllowed,
|
||||||
|
nova_exception.OverLimit,
|
||||||
|
nova_exception.RateLimit,
|
||||||
|
)
|
||||||
|
def test_translate_server_exception_other_exception(self, exc):
|
||||||
|
self.assertRaises(
|
||||||
|
exception.ManilaException,
|
||||||
|
decorated_by_translate_server_exception,
|
||||||
|
'foo_self', 'foo_ctxt', 'foo_instance_id', exc)
|
||||||
|
|
||||||
|
|
||||||
@ddt.ddt
|
@ddt.ddt
|
||||||
class NovaApiTestCase(test.TestCase):
|
class NovaApiTestCase(test.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user