From 51ea4093371ebbf96f391d6dee62356793f201f4 Mon Sep 17 00:00:00 2001 From: Steve Leon Date: Tue, 11 Jun 2013 17:21:31 -0700 Subject: [PATCH] Fix quota bug where it raises exception when resources are deleted - Added an extra check to make sure that if the delta is negative, it should not raise an over quota fixed bug 1178285 Change-Id: I23cdc7bfa0a58ee87ae085d51814f73bc9790754 --- reddwarf/quota/quota.py | 5 ++-- reddwarf/tests/unittests/quota/test_quota.py | 30 ++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/reddwarf/quota/quota.py b/reddwarf/quota/quota.py index edd0c568a4..cbf6bab81c 100644 --- a/reddwarf/quota/quota.py +++ b/reddwarf/quota/quota.py @@ -149,9 +149,10 @@ class DbQuotaDriver(object): deltas.keys()) overs = [resource for resource in deltas - if (quota_usages[resource].in_use + + if (int(deltas[resource]) > 0 and + (quota_usages[resource].in_use + quota_usages[resource].reserved + - int(deltas[resource])) > quotas[resource].hard_limit] + int(deltas[resource])) > quotas[resource].hard_limit)] if overs: raise exception.QuotaExceeded(overs=sorted(overs)) diff --git a/reddwarf/tests/unittests/quota/test_quota.py b/reddwarf/tests/unittests/quota/test_quota.py index 3168633bd0..9893872717 100644 --- a/reddwarf/tests/unittests/quota/test_quota.py +++ b/reddwarf/tests/unittests/quota/test_quota.py @@ -486,6 +486,36 @@ class DbQuotaDriverTest(testtools.TestCase): resources, delta) + def test_reserve_over_quota_but_can_apply_negative_deltas(self): + + FAKE_QUOTAS = [QuotaUsage(id=1, + tenant_id=FAKE_TENANT1, + resource=Resource.INSTANCES, + in_use=10, + reserved=0), + QuotaUsage(id=2, + tenant_id=FAKE_TENANT1, + resource=Resource.VOLUMES, + in_use=50, + reserved=0)] + + self.mock_quota_result.all = Mock(return_value=[]) + self.mock_usage_result.all = Mock(return_value=FAKE_QUOTAS) + + QuotaUsage.save = Mock() + Reservation.create = Mock() + + delta = {'instances': -1, 'volumes': -3} + self.driver.reserve(FAKE_TENANT1, resources, delta) + _, kw = Reservation.create.call_args_list[0] + self.assertEquals(1, kw['usage_id']) + self.assertEquals(-1, kw['delta']) + self.assertEquals(Reservation.Statuses.RESERVED, kw['status']) + _, kw = Reservation.create.call_args_list[1] + self.assertEquals(2, kw['usage_id']) + self.assertEquals(-3, kw['delta']) + self.assertEquals(Reservation.Statuses.RESERVED, kw['status']) + def test_commit(self): Reservation.save = Mock()