From 8bd848df43ea96b7431802748a1e8567a8e89203 Mon Sep 17 00:00:00 2001 From: Jun Hong Li Date: Thu, 5 Feb 2015 17:20:42 +0800 Subject: [PATCH] Throw NotFound exception when template is gone When using vsphere backend store, if the template is deleted in vCenter datastore by some reason, it fails to delete the image from glance due to the generic Exception. The bug is fixed by catching oslo.vmware.FileNotFoundException, and throw NotFound exception. So that the behavior of deleting image will be identical with file system backend. Change-Id: If22f71feadec025eaf9a38c4cb15eb7f669820f8 Closes-Bug: 1418396 --- glance_store/_drivers/vmware_datastore.py | 5 +++++ tests/unit/test_vmware_store.py | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/glance_store/_drivers/vmware_datastore.py b/glance_store/_drivers/vmware_datastore.py index 22e70931..8197107b 100644 --- a/glance_store/_drivers/vmware_datastore.py +++ b/glance_store/_drivers/vmware_datastore.py @@ -26,6 +26,7 @@ from oslo_utils import excutils from oslo_utils import units from oslo_vmware import api from oslo_vmware import constants +import oslo_vmware.exceptions as vexc from oslo_vmware.objects import datacenter as oslo_datacenter from oslo_vmware.objects import datastore as oslo_datastore from oslo_vmware import vim_util @@ -572,6 +573,10 @@ class Store(glance_store.Store): datacenter=dc_obj.ref) try: self.session.wait_for_task(delete_task) + except vexc.FileNotFoundException: + msg = _('Image file %s not found') % file_path + LOG.warn(msg) + raise exceptions.NotFound(message=msg) except Exception: with excutils.save_and_reraise_exception(): LOG.exception(_LE('Failed to delete image %(image)s ' diff --git a/tests/unit/test_vmware_store.py b/tests/unit/test_vmware_store.py index 22c3ac9a..7b2175f4 100644 --- a/tests/unit/test_vmware_store.py +++ b/tests/unit/test_vmware_store.py @@ -21,6 +21,7 @@ import uuid import mock from oslo_utils import units from oslo_vmware import api +from oslo_vmware.exceptions import FileNotFoundException from oslo_vmware.objects import datacenter as oslo_datacenter from oslo_vmware.objects import datastore as oslo_datastore import six @@ -224,6 +225,18 @@ class TestStore(base.StoreBaseTest, HttpConn.return_value = FakeHTTPConnection(status=404) self.assertRaises(exceptions.NotFound, self.store.get, loc) + def test_delete_non_existing(self): + """ + Test that trying to delete an image that doesn't exist raises an error + """ + loc = location.get_location_from_uri( + "vsphere://127.0.0.1/folder/openstack_glance/%s?" + "dsName=ds1&dcPath=dc1" % FAKE_UUID, conf=self.conf) + with mock.patch.object(self.store.session, + 'wait_for_task') as mock_task: + mock_task.side_effect = FileNotFoundException + self.assertRaises(exceptions.NotFound, self.store.delete, loc) + @mock.patch('oslo_vmware.api.VMwareAPISession') def test_get_size(self, mock_api_session): """