From 83e31cbe456a4a49e90a550796b9ba3f6eeff354 Mon Sep 17 00:00:00 2001 From: Akihiro MOTOKI Date: Mon, 7 Oct 2013 08:38:07 +0900 Subject: [PATCH] Enable Quota DB driver by default Closes-Bug: #1189671 Quota driver is now loaded in lazy mode, i.e. the driver is loaded the first time the driver is accessed. This is to make unit tests work. Some unit tests like extension test cases need to use Config Quota driver (previous default) but QuotaEngine is initialized when quota.py is imported. Thus the unit tests had no chance to specify quota_driver. Change-Id: I9e20961d5a6322361e3c0284b3c2a7ca86755c70 --- etc/neutron.conf | 2 +- neutron/quota.py | 24 +++++++++++-------- neutron/tests/unit/nicira/test_networkgw.py | 5 ++++ .../services/vpn/test_vpnaas_extension.py | 5 ++++ neutron/tests/unit/test_api_v2.py | 9 +++++++ .../unit/test_extension_extended_attribute.py | 5 ++++ neutron/tests/unit/test_extension_pnet.py | 5 ++++ neutron/tests/unit/test_l3_plugin.py | 5 ++++ neutron/tests/unit/test_quota_ext.py | 7 ++++++ 9 files changed, 56 insertions(+), 11 deletions(-) diff --git a/etc/neutron.conf b/etc/neutron.conf index 5abf669175..bfa1353037 100644 --- a/etc/neutron.conf +++ b/etc/neutron.conf @@ -288,7 +288,7 @@ notification_driver = neutron.openstack.common.notifier.rpc_notifier # quota_security_group_rule = 100 # default driver to use for quota checks -# quota_driver = neutron.quota.ConfDriver +# quota_driver = neutron.db.quota_db.DbQuotaDriver [agent] # Use "sudo neutron-rootwrap /etc/neutron/rootwrap.conf" to use the real diff --git a/neutron/quota.py b/neutron/quota.py index 02a7cebd61..908b256f2c 100644 --- a/neutron/quota.py +++ b/neutron/quota.py @@ -47,7 +47,7 @@ quota_opts = [ help=_('Number of ports allowed per tenant, minus for ' 'unlimited')), cfg.StrOpt('quota_driver', - default='neutron.quota.ConfDriver', + default='neutron.db.quota_db.DbQuotaDriver', help=_('Default driver to use for quota checks')), ] # Register the configuration options @@ -209,14 +209,18 @@ class QuotaEngine(object): def __init__(self, quota_driver_class=None): """Initialize a Quota object.""" - if not quota_driver_class: - quota_driver_class = cfg.CONF.QUOTAS.quota_driver - - if isinstance(quota_driver_class, basestring): - quota_driver_class = importutils.import_object(quota_driver_class) - self._resources = {} - self._driver = quota_driver_class + self._driver = None + self._driver_class = quota_driver_class + + def get_driver(self): + if self._driver is None: + _driver_class = (self._driver_class or + cfg.CONF.QUOTAS.quota_driver) + if isinstance(_driver_class, basestring): + _driver_class = importutils.import_object(_driver_class) + self._driver = _driver_class + return self._driver def __contains__(self, resource): return resource in self._resources @@ -281,8 +285,8 @@ class QuotaEngine(object): :param context: The request context, for access checks. """ - return self._driver.limit_check(context, tenant_id, - self._resources, values) + return self.get_driver().limit_check(context, tenant_id, + self._resources, values) @property def resources(self): diff --git a/neutron/tests/unit/nicira/test_networkgw.py b/neutron/tests/unit/nicira/test_networkgw.py index ab19a6c3ce..5a4259e5eb 100644 --- a/neutron/tests/unit/nicira/test_networkgw.py +++ b/neutron/tests/unit/nicira/test_networkgw.py @@ -32,6 +32,7 @@ from neutron.db import db_base_plugin_v2 from neutron import manager from neutron.plugins.nicira.dbexts import nicira_networkgw_db from neutron.plugins.nicira.extensions import nvp_networkgw as networkgw +from neutron import quota from neutron.tests import base from neutron.tests.unit import test_api_v2 from neutron.tests.unit import test_db_plugin @@ -93,6 +94,10 @@ class NetworkGatewayExtensionTestCase(base.BaseTestCase): self.ext_mdw = test_extensions.setup_extensions_middleware(ext_mgr) self.api = webtest.TestApp(self.ext_mdw) + quota.QUOTAS._driver = None + cfg.CONF.set_override('quota_driver', 'neutron.quota.ConfDriver', + group='QUOTAS') + def test_network_gateway_create(self): nw_gw_id = _uuid() data = {self._resource: {'name': 'nw-gw', diff --git a/neutron/tests/unit/services/vpn/test_vpnaas_extension.py b/neutron/tests/unit/services/vpn/test_vpnaas_extension.py index d1f8954534..b3acadf575 100644 --- a/neutron/tests/unit/services/vpn/test_vpnaas_extension.py +++ b/neutron/tests/unit/services/vpn/test_vpnaas_extension.py @@ -31,6 +31,7 @@ from neutron.extensions import vpnaas from neutron import manager from neutron.openstack.common import uuidutils from neutron.plugins.common import constants +from neutron import quota from neutron.tests.unit import test_api_v2 from neutron.tests.unit import test_extensions from neutron.tests.unit import testlib_api @@ -88,6 +89,10 @@ class VpnaasExtensionTestCase(testlib_api.WebTestCase): self.api = webtest.TestApp(self.ext_mdw) super(VpnaasExtensionTestCase, self).setUp() + quota.QUOTAS._driver = None + cfg.CONF.set_override('quota_driver', 'neutron.quota.ConfDriver', + group='QUOTAS') + def tearDown(self): self._plugin_patcher.stop() self.api = None diff --git a/neutron/tests/unit/test_api_v2.py b/neutron/tests/unit/test_api_v2.py index 3268bbac18..61e3eb371a 100644 --- a/neutron/tests/unit/test_api_v2.py +++ b/neutron/tests/unit/test_api_v2.py @@ -36,6 +36,7 @@ from neutron.manager import NeutronManager from neutron.openstack.common.notifier import api as notifer_api from neutron.openstack.common import policy as common_policy from neutron.openstack.common import uuidutils +from neutron import quota from neutron.tests import base from neutron.tests.unit import testlib_api @@ -118,6 +119,10 @@ class APIv2TestBase(base.BaseTestCase): api = router.APIRouter() self.api = webtest.TestApp(api) + quota.QUOTAS._driver = None + cfg.CONF.set_override('quota_driver', 'neutron.quota.ConfDriver', + group='QUOTAS') + class _ArgMatcher(object): """An adapter to assist mock assertions, used to custom compare.""" @@ -1377,6 +1382,10 @@ class ExtensionTestCase(base.BaseTestCase): api = router.APIRouter() self.api = webtest.TestApp(api) + quota.QUOTAS._driver = None + cfg.CONF.set_override('quota_driver', 'neutron.quota.ConfDriver', + group='QUOTAS') + def tearDown(self): super(ExtensionTestCase, self).tearDown() self._plugin_patcher.stop() diff --git a/neutron/tests/unit/test_extension_extended_attribute.py b/neutron/tests/unit/test_extension_extended_attribute.py index 92a0c3cecd..6cc32f0a96 100644 --- a/neutron/tests/unit/test_extension_extended_attribute.py +++ b/neutron/tests/unit/test_extension_extended_attribute.py @@ -29,6 +29,7 @@ from neutron.common import config from neutron import manager from neutron.plugins.common import constants from neutron.plugins.openvswitch import ovs_neutron_plugin +from neutron import quota from neutron.tests import base from neutron.tests.unit.extensions import extendedattribute as extattr from neutron.tests.unit import test_api_v2 @@ -108,6 +109,10 @@ class ExtensionExtendedAttributeTestCase(base.BaseTestCase): self.addCleanup(cfg.CONF.reset) self.addCleanup(self.restore_attribute_map) + quota.QUOTAS._driver = None + cfg.CONF.set_override('quota_driver', 'neutron.quota.ConfDriver', + group='QUOTAS') + def restore_attribute_map(self): # Restore the original RESOURCE_ATTRIBUTE_MAP attributes.RESOURCE_ATTRIBUTE_MAP = self.saved_attr_map diff --git a/neutron/tests/unit/test_extension_pnet.py b/neutron/tests/unit/test_extension_pnet.py index 41080052ed..f69d6b14b1 100644 --- a/neutron/tests/unit/test_extension_pnet.py +++ b/neutron/tests/unit/test_extension_pnet.py @@ -30,6 +30,7 @@ from neutron import context from neutron.extensions import providernet as pnet from neutron.manager import NeutronManager from neutron.openstack.common import uuidutils +from neutron import quota from neutron.tests.unit import test_api_v2 from neutron.tests.unit import test_extensions from neutron.tests.unit import testlib_api @@ -87,6 +88,10 @@ class ProvidernetExtensionTestCase(testlib_api.WebTestCase): self.addCleanup(self._restore_attribute_map) self.api = webtest.TestApp(router.APIRouter()) + quota.QUOTAS._driver = None + cfg.CONF.set_override('quota_driver', 'neutron.quota.ConfDriver', + group='QUOTAS') + def _restore_attribute_map(self): # Restore the global RESOURCE_ATTRIBUTE_MAP attributes.RESOURCE_ATTRIBUTE_MAP = self.saved_attr_map diff --git a/neutron/tests/unit/test_l3_plugin.py b/neutron/tests/unit/test_l3_plugin.py index 0c7aef7002..4f75b576cf 100644 --- a/neutron/tests/unit/test_l3_plugin.py +++ b/neutron/tests/unit/test_l3_plugin.py @@ -46,6 +46,7 @@ from neutron.openstack.common.notifier import api as notifier_api from neutron.openstack.common.notifier import test_notifier from neutron.openstack.common import uuidutils from neutron.plugins.common import constants as service_constants +from neutron import quota from neutron.tests.unit import test_api_v2 from neutron.tests.unit import test_db_plugin from neutron.tests.unit import test_extensions @@ -114,6 +115,10 @@ class L3NatExtensionTestCase(testlib_api.WebTestCase): self.ext_mdw = test_extensions.setup_extensions_middleware(ext_mgr) self.api = webtest.TestApp(self.ext_mdw) + quota.QUOTAS._driver = None + cfg.CONF.set_override('quota_driver', 'neutron.quota.ConfDriver', + group='QUOTAS') + def tearDown(self): self._plugin_patcher.stop() self.api = None diff --git a/neutron/tests/unit/test_quota_ext.py b/neutron/tests/unit/test_quota_ext.py index 9e462edff1..3f572adf6d 100644 --- a/neutron/tests/unit/test_quota_ext.py +++ b/neutron/tests/unit/test_quota_ext.py @@ -327,6 +327,13 @@ class QuotaExtensionDbTestCaseXML(QuotaExtensionDbTestCase): class QuotaExtensionCfgTestCase(QuotaExtensionTestCase): fmt = 'json' + def setUp(self): + cfg.CONF.set_override( + 'quota_driver', + 'neutron.quota.ConfDriver', + group='QUOTAS') + super(QuotaExtensionCfgTestCase, self).setUp() + def test_quotas_default_values(self): tenant_id = 'tenant_id1' env = {'neutron.context': context.Context('', tenant_id)}