From 7ff01fda2a307eb75baf7010ac6374197c5fa880 Mon Sep 17 00:00:00 2001 From: Chris Yeoh Date: Wed, 22 May 2013 13:35:31 +0930 Subject: Adds expected_errors decorator for API v3 Adds an expected_errors decorator for v3 API methods which allows the specification of what exceptions can be raised within the method. If an unexpected error occurs a 500 is instead returned which plus a request to file a bug report Partially implements blueprint v3-api-expected-errors Change-Id: I5723e576b30b47744394517cdb1ff6bab1d8f78b --- .../api/openstack/compute/test_v3_extensions.py | 37 ++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'nova/tests') diff --git a/nova/tests/api/openstack/compute/test_v3_extensions.py b/nova/tests/api/openstack/compute/test_v3_extensions.py index 97429ca45..ec472f38a 100644 --- a/nova/tests/api/openstack/compute/test_v3_extensions.py +++ b/nova/tests/api/openstack/compute/test_v3_extensions.py @@ -16,10 +16,12 @@ from oslo.config import cfg import stevedore +import webob.exc from nova.api import openstack from nova.api.openstack import compute from nova.api.openstack.compute import plugins +from nova.api.openstack import extensions from nova import exception from nova import test @@ -139,3 +141,38 @@ class ExtensionLoadingTestCase(test.TestCase): self.stubs.Set(plugins, 'LoadedExtensionInfo', fake_loaded_extension_info) self.assertRaises(exception.CoreAPIMissing, compute.APIRouterV3) + + def test_extensions_expected_error(self): + @extensions.expected_errors(404) + def fake_func(): + raise webob.exc.HTTPNotFound() + + self.assertRaises(webob.exc.HTTPNotFound, fake_func) + + def test_extensions_expected_error_from_list(self): + @extensions.expected_errors((404, 403)) + def fake_func(): + raise webob.exc.HTTPNotFound() + + self.assertRaises(webob.exc.HTTPNotFound, fake_func) + + def test_extensions_unexpected_error(self): + @extensions.expected_errors(404) + def fake_func(): + raise webob.exc.HTTPConflict() + + self.assertRaises(webob.exc.HTTPInternalServerError, fake_func) + + def test_extensions_unexpected_error_from_list(self): + @extensions.expected_errors((404, 413)) + def fake_func(): + raise webob.exc.HTTPConflict() + + self.assertRaises(webob.exc.HTTPInternalServerError, fake_func) + + def test_extensions_unexpected_policy_not_authorized_error(self): + @extensions.expected_errors(404) + def fake_func(): + raise exception.PolicyNotAuthorized(action="foo") + + self.assertRaises(exception.PolicyNotAuthorized, fake_func) -- cgit