Catch expected exceptions in remote console controller

Remote console controllers (both V2 & V2.1), does not catch
all the expected exceptions which results to raise HTTPInternalServerError.

This patch catches all the expected exceptions in those controllers.
Also share serial_console tests among V2 & V2.1

Change-Id: Iea706eb45b0f2f6cdfd7e8d6061cad03ab43c6ee
This commit is contained in:
ghanshyam
2014-12-19 16:52:47 +09:00
parent 28902c4f3a
commit dec4ad9b4b
3 changed files with 46 additions and 26 deletions

View File

@@ -48,7 +48,10 @@ class ConsolesController(wsgi.Controller):
except exception.InstanceNotReady as e:
raise webob.exc.HTTPConflict(
explanation=_('Instance not yet ready'))
except exception.ConsoleTypeUnavailable as e:
except exception.InstanceNotFound as e:
raise webob.exc.HTTPNotFound(explanation=e.format_message())
except (exception.ConsoleTypeUnavailable,
exception.ConsoleTypeInvalid) as e:
raise webob.exc.HTTPBadRequest(explanation=e.format_message())
except NotImplementedError:
msg = _("Unable to get vnc console, functionality not implemented")
@@ -71,8 +74,11 @@ class ConsolesController(wsgi.Controller):
output = self.compute_api.get_spice_console(context,
instance,
console_type)
except exception.ConsoleTypeUnavailable as e:
except (exception.ConsoleTypeUnavailable,
exception.ConsoleTypeInvalid) as e:
raise webob.exc.HTTPBadRequest(explanation=e.format_message())
except exception.InstanceNotFound as e:
raise webob.exc.HTTPNotFound(explanation=e.format_message())
except exception.InstanceNotReady as e:
raise webob.exc.HTTPConflict(explanation=e.format_message())
except NotImplementedError:
@@ -97,8 +103,11 @@ class ConsolesController(wsgi.Controller):
output = self.compute_api.get_rdp_console(context,
instance,
console_type)
except exception.ConsoleTypeUnavailable as e:
except (exception.ConsoleTypeUnavailable,
exception.ConsoleTypeInvalid) as e:
raise webob.exc.HTTPBadRequest(explanation=e.format_message())
except exception.InstanceNotFound as e:
raise webob.exc.HTTPNotFound(explanation=e.format_message())
except exception.InstanceNotReady as e:
raise webob.exc.HTTPConflict(explanation=e.format_message())
except NotImplementedError:
@@ -123,8 +132,16 @@ class ConsolesController(wsgi.Controller):
console_type)
except exception.InstanceNotFound as e:
raise webob.exc.HTTPNotFound(explanation=e.format_message())
except exception.InstanceNotFound as e:
raise webob.exc.HTTPNotFound(explanation=e.format_message())
except exception.InstanceNotReady as e:
raise webob.exc.HTTPConflict(explanation=e.format_message())
except (exception.ConsoleTypeUnavailable,
exception.ConsoleTypeInvalid,
exception.ImageSerialPortNumberInvalid,
exception.ImageSerialPortNumberExceedFlavorValue,
exception.SocketPortRangeExhaustedException) as e:
raise webob.exc.HTTPBadRequest(explanation=e.format_message())
except NotImplementedError:
msg = _("Unable to get serial console, "
"functionality not implemented")

View File

@@ -123,7 +123,7 @@ class RemoteConsolesController(wsgi.Controller):
return {'console': {'type': console_type, 'url': output['url']}}
@extensions.expected_errors((404, 409, 501))
@extensions.expected_errors((400, 404, 409, 501))
@wsgi.action('os-getSerialConsole')
@validation.schema(remote_consoles.get_serial_console)
def get_serial_console(self, req, id, body):
@@ -142,6 +142,11 @@ class RemoteConsolesController(wsgi.Controller):
raise webob.exc.HTTPNotFound(explanation=e.format_message())
except exception.InstanceNotReady as e:
raise webob.exc.HTTPConflict(explanation=e.format_message())
except (exception.ConsoleTypeUnavailable,
exception.ImageSerialPortNumberInvalid,
exception.ImageSerialPortNumberExceedFlavorValue,
exception.SocketPortRangeExhaustedException) as e:
raise webob.exc.HTTPBadRequest(explanation=e.format_message())
except NotImplementedError:
msg = _("Unable to get serial console, "
"functionality not implemented")

View File

@@ -431,25 +431,6 @@ class ConsolesExtensionTestV21(test.NoDBTestCase):
res = req.get_response(self.app)
self.assertEqual(400, res.status_int)
class ConsolesExtensionTestV2(ConsolesExtensionTestV21):
def _setup_wsgi(self):
self.flags(
osapi_compute_extension=[
'nova.api.openstack.compute.contrib.select_extensions'],
osapi_compute_ext_list=['Consoles'])
self.app = fakes.wsgi_app(init_only=('servers',))
def test_get_vnc_console_with_undefined_param(self):
pass
def test_get_spice_console_with_undefined_param(self):
pass
def test_get_rdp_console_with_undefined_param(self):
pass
def test_get_serial_console(self):
body = {'os-getSerialConsole': {'type': 'serial'}}
req = webob.Request.blank(self.url)
@@ -492,7 +473,6 @@ class ConsolesExtensionTestV2(ConsolesExtensionTestV21):
res = req.get_response(self.app)
self.assertEqual(res.status_int, 400)
self.assertTrue(get_serial_console.called)
@mock.patch.object(compute_api.API, 'get_serial_console')
def test_get_serial_console_no_type(self, get_serial_console):
@@ -507,7 +487,6 @@ class ConsolesExtensionTestV2(ConsolesExtensionTestV21):
res = req.get_response(self.app)
self.assertEqual(res.status_int, 400)
self.assertTrue(get_serial_console.called)
@mock.patch.object(compute_api.API, 'get_serial_console')
def test_get_serial_console_no_instance(self, get_serial_console):
@@ -552,7 +531,7 @@ class ConsolesExtensionTestV2(ConsolesExtensionTestV21):
req.headers["content-type"] = "application/json"
res = req.get_response(self.app)
self.assertEqual(res.status_int, 500)
self.assertEqual(res.status_int, 400)
self.assertTrue(get_serial_console.called)
@mock.patch.object(compute_api.API, 'get_serial_console')
@@ -585,3 +564,22 @@ class ConsolesExtensionTestV2(ConsolesExtensionTestV21):
res = req.get_response(self.app)
self.assertEqual(res.status_int, 400)
self.assertTrue(get_serial_console.called)
class ConsolesExtensionTestV2(ConsolesExtensionTestV21):
def _setup_wsgi(self):
self.flags(
osapi_compute_extension=[
'nova.api.openstack.compute.contrib.select_extensions'],
osapi_compute_ext_list=['Consoles'])
self.app = fakes.wsgi_app(init_only=('servers',))
def test_get_vnc_console_with_undefined_param(self):
pass
def test_get_spice_console_with_undefined_param(self):
pass
def test_get_rdp_console_with_undefined_param(self):
pass