Merge "Filter out deleted images from storage usage" into stable/havana

This commit is contained in:
Jenkins 2014-02-10 21:38:44 +00:00 committed by Gerrit Code Review
commit 1982ca25ec
4 changed files with 53 additions and 3 deletions

View File

@ -513,6 +513,12 @@ def image_destroy(context, image_id):
DATA['images'][image_id]['deleted'] = True
DATA['images'][image_id]['deleted_at'] = timeutils.utcnow()
# NOTE(flaper87): Move the image to one of the deleted statuses
# if it hasn't been done yet.
if (DATA['images'][image_id]['status'] not in
['deleted', 'pending_delete']):
DATA['images'][image_id]['status'] = 'deleted'
_image_locations_set(image_id, [])
for prop in DATA['images'][image_id]['properties']:
@ -654,6 +660,11 @@ def user_get_storage_usage(context, owner_id, image_id=None, session=None):
images = image_get_all(context, filters={'owner': owner_id})
total = 0
for image in images:
if image['status'] in ['killed', 'pending_delete', 'deleted']:
continue
if image['id'] != image_id:
total = total + (image['size'] * len(image['locations']))
locations = [l for l in image['locations']
if not l.get('deleted', False)]
total += (image['size'] * len(locations))
return total

View File

@ -694,8 +694,15 @@ def _image_get_disk_usage_by_owner(owner, session, image_id=None):
if image_id is not None:
query = query.filter(models.Image.id != image_id)
query = query.filter(models.Image.size > 0)
query = query.filter(~models.Image.status.in_(['killed',
'pending_delete',
'deleted']))
images = query.all()
total = sum([i.size * len(i.locations) for i in images])
total = 0
for i in images:
locations = [l for l in i.locations if not l['deleted']]
total += (i.size * len(locations))
return total

View File

@ -1097,7 +1097,8 @@ class DriverQuotaTests(test_utils.BaseTestCase):
super(DriverQuotaTests, self).setUp()
self.owner_id1 = uuidutils.generate_uuid()
self.context1 = context.RequestContext(
is_admin=False, auth_tok='user:user:user', user=self.owner_id1)
is_admin=False, user=self.owner_id1, tenant=self.owner_id1,
auth_tok='%s:%s:user' % (self.owner_id1, self.owner_id1))
self.db_api = db_tests.get_db(self.config)
db_tests.reset_db(self.db_api)
self.addCleanup(timeutils.clear_time_override)
@ -1163,6 +1164,29 @@ class DriverQuotaTests(test_utils.BaseTestCase):
x = self.db_api.user_get_storage_usage(self.context1, self.owner_id1)
self.assertEqual(total, x)
def test_storage_quota_deleted_image(self):
# NOTE(flaper87): This needs to be tested for
# soft deleted images as well. Currently there's no
# good way to delete locations.
dt1 = timeutils.utcnow()
sz = 53
new_fixture_dict = {'id': 'SOMEID', 'created_at': dt1,
'updated_at': dt1, 'size': sz,
'owner': self.owner_id1}
new_fixture = build_image_fixture(**new_fixture_dict)
new_fixture['locations'].append({'url': 'file:///some/path/file',
'metadata': {}})
self.db_api.image_create(self.context1, new_fixture)
total = reduce(lambda x, y: x + y,
[f['size'] for f in self.owner1_fixtures])
x = self.db_api.user_get_storage_usage(self.context1, self.owner_id1)
self.assertEqual(total + (sz * 2), x)
self.db_api.image_destroy(self.context1, 'SOMEID')
x = self.db_api.user_get_storage_usage(self.context1, self.owner_id1)
self.assertEqual(total, x)
class TestVisibility(test_utils.BaseTestCase):
def setUp(self):

View File

@ -96,3 +96,11 @@ class TestSqlAlchemyDBDataIntegrity(base.TestDriver):
fake_paginate_query)
images = self.db_api.image_get_all(self.context,
sort_key='name')
class TestSqlAlchemyQuota(base.DriverQuotaTests):
def setUp(self):
db_tests.load(get_db, reset_db)
super(TestSqlAlchemyQuota, self).setUp()
self.addCleanup(db_tests.reset)