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:
vponomaryov 2016-01-04 12:46:03 +02:00
parent f1c096418d
commit 95940a4085
2 changed files with 51 additions and 1 deletions

View File

@ -149,15 +149,20 @@ def translate_server_exception(method):
Note: keeps its traceback intact.
"""
@six.wraps(method)
def wrapper(self, ctx, instance_id, *args, **kwargs):
try:
res = method(self, ctx, instance_id, *args, **kwargs)
return res
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
else:
raise exception.ManilaException(e)
return wrapper

View File

@ -86,6 +86,51 @@ class FakeNovaClient(object):
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
class NovaApiTestCase(test.TestCase):
def setUp(self):