From 921eec91abee56ea69e1b8b2bff59379b68bff87 Mon Sep 17 00:00:00 2001 From: Unmesh Gurjar Date: Tue, 11 Dec 2012 03:30:31 -0800 Subject: Fixed deleting instance booted from invalid vol 1. Translated the exception (NotFound) raised from Cinder client to Nova's native exception (VolumeNotFound). 2. Added unit test coverage. Fixes LP: #1087214. Change-Id: I3cee31a9fd068b65a4097f3f3054aebb69d9ba61 --- nova/tests/test_cinder.py | 9 +++++++++ nova/volume/cinder.py | 21 +++++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/nova/tests/test_cinder.py b/nova/tests/test_cinder.py index 3302aedb8..11d29a3ff 100644 --- a/nova/tests/test_cinder.py +++ b/nova/tests/test_cinder.py @@ -17,7 +17,9 @@ import httplib2 import urlparse +from cinderclient import exceptions as cinder_exception from nova import context +from nova import exception from nova.volume import cinder from nova import test @@ -77,6 +79,9 @@ class FakeHTTPClient(cinder.cinder_client.client.HTTPClient): volume = {'volume': _stub_volume(id='1234')} return (200, volume) + def get_volumes_nonexisting(self, **kw): + raise cinder_exception.NotFound(code=404, message='Resource not found') + class FakeCinderClient(cinder.cinder_client.Client): @@ -146,3 +151,7 @@ class CinderTestCase(test.TestCase): self.assertEquals( self.fake_client_factory.client.client.management_url, 'http://other_host:8776/v1/project_id') + + def test_get_non_existing_volume(self): + self.assertRaises(exception.VolumeNotFound, self.api.get, self.context, + 'nonexisting') diff --git a/nova/volume/cinder.py b/nova/volume/cinder.py index ae7b76e26..1b8305505 100644 --- a/nova/volume/cinder.py +++ b/nova/volume/cinder.py @@ -20,7 +20,9 @@ Handles all requests relating to volumes + cinder. """ +import sys +from cinderclient import exceptions as cinder_exception from cinderclient import service_catalog from cinderclient.v1 import client as cinder_client @@ -139,9 +141,24 @@ def _untranslate_snapshot_summary_view(context, snapshot): class API(base.Base): """API for interacting with the volume manager.""" + def _reraise_translated_volume_exception(self, volume_id): + """Transform the exception for the volume but keep its traceback + intact.""" + exc_type, exc_value, exc_trace = sys.exc_info() + new_exc = self._translate_volume_exception(volume_id, exc_value) + raise new_exc, None, exc_trace + + def _translate_volume_exception(self, volume_id, exc_value): + if isinstance(exc_value, cinder_exception.NotFound): + return exception.VolumeNotFound(volume_id=volume_id) + return exc_value + def get(self, context, volume_id): - item = cinderclient(context).volumes.get(volume_id) - return _untranslate_volume_summary_view(context, item) + try: + item = cinderclient(context).volumes.get(volume_id) + return _untranslate_volume_summary_view(context, item) + except Exception: + self._reraise_translated_volume_exception(volume_id) def get_all(self, context, search_opts={}): items = cinderclient(context).volumes.list(detailed=True) -- cgit