From 149ea050cc58f39eaf9b4660bb8f0271b99d03da Mon Sep 17 00:00:00 2001 From: Abhishek Kekane Date: Fri, 6 Dec 2019 10:35:55 +0000 Subject: [PATCH] Staging area not cleared if image is deleted while importing If multiple stores configured in glance and Image is deleted while import operation is in progress then image data stays in staging area (filesystem backend) and there is no other way than clearing it manually. Modified delete method to delete the data from staging area if image is deleted while import operation is in progress. Conflicts: glance/api/v2/images.py glance/async_/flows/api_image_import.py glance/common/store_utils.py NOTE: Conflicts occured due to reserved store capability is not available in Stein. Change-Id: Ib58accd6514e589dccde57fe063815b1ab1ce496 Closes-Bug: #1855417 (cherry picked from commit 5d15f073718328380f2d847e4abfb9307536d810) --- glance/api/v2/images.py | 38 ++++++++++---------- glance/tests/unit/v2/test_images_resource.py | 32 +++++++++++++++++ 2 files changed, 50 insertions(+), 20 deletions(-) diff --git a/glance/api/v2/images.py b/glance/api/v2/images.py index f4a57e0f71..22b4df8c99 100644 --- a/glance/api/v2/images.py +++ b/glance/api/v2/images.py @@ -335,27 +335,25 @@ class ImagesController(object): image_repo = self.gateway.get_repo(req.context) try: image = image_repo.get(image_id) - - if image.status == 'uploading': - file_path = str( - CONF.node_staging_uri + '/' + image.image_id)[7:] - if os.path.exists(file_path): - try: - LOG.debug( - "After upload to the backend, deleting staged " - "image data from %(fn)s", {'fn': file_path}) - os.unlink(file_path) - except OSError as e: - LOG.error( - "After upload to backend, deletion of staged " - "image data from %(fn)s has failed because " - "[Errno %(en)d]", {'fn': file_path, - 'en': e.errno}) - else: - LOG.warning(_( + file_path = str( + CONF.node_staging_uri + '/' + image_id)[7:] + if os.path.exists(file_path): + try: + LOG.debug( + "After upload to the backend, deleting staged " + "image data from %(fn)s", {'fn': file_path}) + os.unlink(file_path) + except OSError as e: + LOG.error( "After upload to backend, deletion of staged " - "image data has failed because " - "it cannot be found at %(fn)s"), {'fn': file_path}) + "image data from %(fn)s has failed because " + "[Errno %(en)d]", {'fn': file_path, + 'en': e.errno}) + else: + LOG.warning(_( + "After upload to backend, deletion of staged " + "image data has failed because " + "it cannot be found at %(fn)s"), {'fn': file_path}) image.delete() image_repo.remove(image) diff --git a/glance/tests/unit/v2/test_images_resource.py b/glance/tests/unit/v2/test_images_resource.py index a82e86143a..c0df32bcc3 100644 --- a/glance/tests/unit/v2/test_images_resource.py +++ b/glance/tests/unit/v2/test_images_resource.py @@ -2493,6 +2493,38 @@ class TestImagesController(base.IsolatedUnitTest): self.assertEqual('deleted', deleted_img['status']) self.assertNotIn('%s/%s' % (BASE_URI, UUID1), self.store.data) + def test_verify_staging_data_deleted_on_image_delete(self): + self.config(enabled_backends={'fake-store': 'file'}) + self.config(node_staging_uri='file:///tmp/staging') + image_id = str(uuid.uuid4()) + self.images = [ + _db_fixture(image_id, owner=TENANT1, + name='1', + disk_format='raw', + container_format='bare', + status='importing', + checksum=None, + os_hash_algo=None, + os_hash_value=None), + ] + self.db.image_create(None, self.images[0]) + request = unit_test_utils.get_fake_request() + try: + with mock.patch.object(os.path, 'exists') as mock_exists: + mock_exists.return_value = True + with mock.patch.object(os, "unlink") as mock_unlik: + self.controller.delete(request, image_id) + + self.assertEqual(1, mock_exists.call_count) + self.assertEqual(1, mock_unlik.call_count) + except Exception as e: + self.fail("Delete raised exception: %s" % e) + + deleted_img = self.db.image_get(request.context, image_id, + force_show_deleted=True) + self.assertTrue(deleted_img['deleted']) + self.assertEqual('deleted', deleted_img['status']) + def test_delete_with_tags(self): request = unit_test_utils.get_fake_request() changes = [