diff --git a/neutron/quota/resource_registry.py b/neutron/quota/resource_registry.py index 21df72285c1..a917eacc06c 100644 --- a/neutron/quota/resource_registry.py +++ b/neutron/quota/resource_registry.py @@ -36,6 +36,12 @@ def get_all_resources(): return ResourceRegistry.get_instance().resources +def unregister_all_resources(): + if not ResourceRegistry._instance: + return + return ResourceRegistry.get_instance().unregister_resources() + + def get_resource(resource_name): return ResourceRegistry.get_instance().get_resource(resource_name) @@ -183,6 +189,10 @@ class ResourceRegistry(object): if not cfg.CONF.QUOTAS.track_quota_usage: return + if isinstance(self._resources.get(resource_name), + resource.CountableResource): + raise RuntimeError("Resource %s is already registered as a " + "countable resource." % resource_name) current_model_class = self._tracked_resource_mappings.setdefault( resource_name, model_class) diff --git a/neutron/tests/base.py b/neutron/tests/base.py index 49f712b11e1..b5400bce7b0 100644 --- a/neutron/tests/base.py +++ b/neutron/tests/base.py @@ -46,6 +46,7 @@ from neutron.common import utils from neutron.db import agentschedulers_db from neutron import manager from neutron import policy +from neutron.quota import resource_registry from neutron.tests import fake_notifier from neutron.tests import post_mortem_debug from neutron.tests import tools @@ -302,6 +303,7 @@ class BaseTestCase(DietTestCase): policy.init() self.addCleanup(policy.reset) + self.addCleanup(resource_registry.unregister_all_resources) self.addCleanup(rpc_consumer_reg.clear) def get_new_temp_dir(self): diff --git a/neutron/tests/unit/quota/test_resource_registry.py b/neutron/tests/unit/quota/test_resource_registry.py index 5f265a8833d..52ead4efced 100644 --- a/neutron/tests/unit/quota/test_resource_registry.py +++ b/neutron/tests/unit/quota/test_resource_registry.py @@ -14,6 +14,7 @@ import mock from oslo_config import cfg +import testtools from neutron import context from neutron.quota import resource @@ -64,6 +65,13 @@ class TestResourceRegistry(base.DietTestCase): def test_register_resource_by_name_not_tracked(self): self._test_register_resource_by_name('meh', resource.CountableResource) + def test_tracked_resource_error_if_already_registered_as_untracked(self): + self.registry.register_resource_by_name('meh') + with testtools.ExpectedException(RuntimeError): + self.registry.set_tracked_resource('meh', test_quota.MehModel) + # ensure unregister works + self.registry.unregister_resources() + def test_register_resource_by_name_with_tracking_disabled_by_config(self): cfg.CONF.set_override('track_quota_usage', False, group='QUOTAS')