From 54233724f1a5c485c64486c9ec59fd32a3ef3349 Mon Sep 17 00:00:00 2001 From: He Jie Xu Date: Thu, 24 Jan 2013 15:26:55 +0800 Subject: [PATCH] Fixes per tenant quota doesn't work Fixes bug 1101331 Change-Id: I9c4e88b437506ed5818ee56ff6fd70588cb0f965 --- quantum/api/v2/base.py | 14 +++---- quantum/db/quota_db.py | 2 +- quantum/quota.py | 13 ++++--- .../tests/unit/test_quota_per_tenant_ext.py | 38 ++++++++++++++++++- 4 files changed, 51 insertions(+), 16 deletions(-) diff --git a/quantum/api/v2/base.py b/quantum/api/v2/base.py index b2a7b185e6..4dd79b6b46 100644 --- a/quantum/api/v2/base.py +++ b/quantum/api/v2/base.py @@ -40,8 +40,6 @@ FAULT_MAP = {exceptions.NotFound: webob.exc.HTTPNotFound, ValueError: webob.exc.HTTPBadRequest, } -QUOTAS = quota.QUOTAS - def _fields(request): """ @@ -284,9 +282,9 @@ class Controller(object): plugin=self._plugin) try: tenant_id = item[self._resource]['tenant_id'] - count = QUOTAS.count(request.context, self._resource, - self._plugin, self._collection, - tenant_id) + count = quota.QUOTAS.count(request.context, self._resource, + self._plugin, self._collection, + tenant_id) if bulk: delta = deltas.get(tenant_id, 0) + 1 deltas[tenant_id] = delta @@ -297,9 +295,9 @@ class Controller(object): # We don't want to quota this resource LOG.debug(e) else: - QUOTAS.limit_check(request.context, - item[self._resource]['tenant_id'], - **kwargs) + quota.QUOTAS.limit_check(request.context, + item[self._resource]['tenant_id'], + **kwargs) def notify(create_result): notifier_api.notify(request.context, diff --git a/quantum/db/quota_db.py b/quantum/db/quota_db.py index 4e73fb6596..c72f9f3295 100644 --- a/quantum/db/quota_db.py +++ b/quantum/db/quota_db.py @@ -145,7 +145,7 @@ class DbQuotaDriver(object): quotas = DbQuotaDriver.get_tenant_quotas( context, sub_resources, context.tenant_id) - return dict((k, v['limit']) for k, v in quotas.items()) + return dict((k, v) for k, v in quotas.items()) def limit_check(self, context, tenant_id, resources, values): """Check simple quota limits. diff --git a/quantum/quota.py b/quantum/quota.py index 21cea51af4..ec08effcb6 100644 --- a/quantum/quota.py +++ b/quantum/quota.py @@ -282,9 +282,12 @@ def _count_resource(context, plugin, resources, tenant_id): return len(obj_list) if obj_list else 0 -resources = [] -for resource_item in cfg.CONF.QUOTAS.quota_items: - resources.append(CountableResource(resource_item, _count_resource, - 'quota_' + resource_item)) +def register_resources_from_config(): + resources = [] + for resource_item in cfg.CONF.QUOTAS.quota_items: + resources.append(CountableResource(resource_item, _count_resource, + 'quota_' + resource_item)) + QUOTAS.register_resources(resources) -QUOTAS.register_resources(resources) + +register_resources_from_config() diff --git a/quantum/tests/unit/test_quota_per_tenant_ext.py b/quantum/tests/unit/test_quota_per_tenant_ext.py index 440cca8a28..8505136e88 100644 --- a/quantum/tests/unit/test_quota_per_tenant_ext.py +++ b/quantum/tests/unit/test_quota_per_tenant_ext.py @@ -1,4 +1,4 @@ -import unittest +import unittest2 as unittest import webtest import mock @@ -6,6 +6,7 @@ import mock from quantum.api import extensions from quantum.api.v2 import attributes from quantum.common import config +from quantum.common import exceptions from quantum import context from quantum.db import api as db from quantum import manager @@ -53,7 +54,8 @@ class QuotaExtensionTestCase(unittest.TestCase): 'quota_items', ['network', 'subnet', 'port', 'extra1'], group='QUOTAS') - + quota.QUOTAS = quota.QuotaEngine() + quota.register_resources_from_config() self._plugin_patcher = mock.patch(TARGET_PLUGIN, autospec=True) self.plugin = self._plugin_patcher.start() self.plugin.return_value.supported_extension_aliases = ['quotas'] @@ -153,3 +155,35 @@ class QuotaExtensionTestCase(unittest.TestCase): self.assertEqual(404, res.status_int) except Exception: pass + + def test_quotas_limit_check(self): + tenant_id = 'tenant_id1' + env = {'quantum.context': context.Context('', tenant_id, + is_admin=True)} + quotas = {'quota': {'network': 5}} + res = self.api.put_json(_get_path('quotas', id=tenant_id, fmt='json'), + quotas, extra_environ=env) + self.assertEqual(200, res.status_int) + quota.QUOTAS.limit_check(context.Context('', tenant_id), + tenant_id, + network=4) + + def test_quotas_limit_check_with_over_quota(self): + tenant_id = 'tenant_id1' + env = {'quantum.context': context.Context('', tenant_id, + is_admin=True)} + quotas = {'quota': {'network': 5}} + res = self.api.put_json(_get_path('quotas', id=tenant_id, fmt='json'), + quotas, extra_environ=env) + self.assertEqual(200, res.status_int) + with self.assertRaises(exceptions.OverQuota): + quota.QUOTAS.limit_check(context.Context('', tenant_id), + tenant_id, + network=6) + + def test_quotas_limit_check_with_invalid_quota_value(self): + tenant_id = 'tenant_id1' + with self.assertRaises(exceptions.InvalidQuotaValue): + quota.QUOTAS.limit_check(context.Context('', tenant_id), + tenant_id, + network=-1)