Browse Source

Delete data if image is deleted after staging call

Trying to delete image after staging call image gets deleted
from the database, but image data remains in the
staging area.

Deleted image data from the staging area if image_import is
enabled and image is in uploading state while deleting.

Closes-Bug: #1733289
Change-Id: I6ef1c05760a27a0b3620024003b49328c55f19a6
tags/16.0.0.0rc1
Abhishek Kekane 1 year ago
parent
commit
7eb2fcc865

+ 7
- 0
glance/api/v2/images.py View File

@@ -265,6 +265,13 @@ class ImagesController(object):
265 265
         image_repo = self.gateway.get_repo(req.context)
266 266
         try:
267 267
             image = image_repo.get(image_id)
268
+
269
+            # NOTE(abhishekk): If 'image-import' is supported and image status
270
+            # is uploading then delete image data from the staging area.
271
+            if CONF.enable_image_import and image.status == 'uploading':
272
+                file_path = str(CONF.node_staging_uri + '/' + image.image_id)
273
+                self.store_api.delete_from_backend(file_path)
274
+
268 275
             image.delete()
269 276
             image_repo.remove(image)
270 277
         except (glance_store.Forbidden, exception.Forbidden) as e:

+ 3
- 0
glance/tests/unit/utils.py View File

@@ -224,6 +224,9 @@ class FakeStoreAPI(object):
224 224
     def check_location_metadata(self, val, key=''):
225 225
         store.check_location_metadata(val)
226 226
 
227
+    def delete_from_backend(self, uri, context=None):
228
+        pass
229
+
227 230
 
228 231
 class FakePolicyEnforcer(object):
229 232
     def __init__(self, *_args, **kwargs):

+ 18
- 0
glance/tests/unit/v2/test_images_resource.py View File

@@ -2175,6 +2175,24 @@ class TestImagesController(base.IsolatedUnitTest):
2175 2175
         self.assertRaises(webob.exc.HTTPBadRequest, self.controller.delete,
2176 2176
                           request, UUID1)
2177 2177
 
2178
+    def test_delete_uploading_status_image(self):
2179
+        """Ensure status of uploading image is updated (LP bug #1733289)"""
2180
+        self.config(enable_image_import=True)
2181
+        request = unit_test_utils.get_fake_request(is_admin=True)
2182
+        image = self.db.image_create(request.context, {'status': 'uploading'})
2183
+        image_id = image['id']
2184
+        with mock.patch.object(self.store,
2185
+                               'delete_from_backend') as mock_store:
2186
+            self.controller.delete(request, image_id)
2187
+
2188
+        # Ensure delete_from_backend is called
2189
+        self.assertEqual(1, mock_store.call_count)
2190
+
2191
+        image = self.db.image_get(request.context, image_id,
2192
+                                  force_show_deleted=True)
2193
+        self.assertTrue(image['deleted'])
2194
+        self.assertEqual('deleted', image['status'])
2195
+
2178 2196
     def test_index_with_invalid_marker(self):
2179 2197
         fake_uuid = str(uuid.uuid4())
2180 2198
         request = unit_test_utils.get_fake_request()

Loading…
Cancel
Save