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
This commit is contained in:
Steve Leon 2013-06-11 17:21:31 -07:00
parent 79b5b01e28
commit 51ea409337
2 changed files with 33 additions and 2 deletions

View File

@ -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))

View File

@ -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()