diff --git a/manila/compute/nova.py b/manila/compute/nova.py index e79c0fab..26eec2aa 100644 --- a/manila/compute/nova.py +++ b/manila/compute/nova.py @@ -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 diff --git a/manila/tests/compute/test_nova.py b/manila/tests/compute/test_nova.py index 6059f93a..b2f1a4fa 100644 --- a/manila/tests/compute/test_nova.py +++ b/manila/tests/compute/test_nova.py @@ -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):