Initialize config in DietTestCase class

Now, after [1] was merged all tests where neutron_lib.context.Context is
used needs to have configured config because every time
instance of the neutron_lib.context.Context class is created it tries to
initialize policies and check if it's service_role or not.

Until now this was done only in the BaseTestCase class so all tests
which inherits from the DietTestCase or SqlTestCaseLight classes did not
have it and those tests were failing.

Additionally this patch removes adding CONF.reset() to the cleanUp in
some tests as it's not needed anymore because it is done in the setUp of
the DietTestCase class.

Closes-Bug: #2025753

[1] https://review.opendev.org/c/openstack/neutron-lib/+/887191

Change-Id: I52597ab066c3d7a2d835278431e00b63c8f55c46
This commit is contained in:
Slawek Kaplonski 2023-07-19 12:58:58 +02:00
parent 154ec11e69
commit 5db57734aa
4 changed files with 20 additions and 26 deletions

View File

@ -266,6 +266,24 @@ class DietTestCase(base.BaseTestCase, metaclass=_CatchTimeoutMetaclass):
config.register_common_config_options()
self.addCleanup(CONF.reset)
self.setup_config()
@staticmethod
def config_parse(conf=None, args=None):
"""Create the default configurations."""
if args is None:
args = []
args += ['--config-file', etcdir('neutron.conf')]
if conf is None:
config.init(args=args)
else:
conf(args)
def setup_config(self, args=None):
"""Tests that need a non-default config can override this method."""
self.config_parse(args=args)
def addOnException(self, handler):
def safe_handler(*args, **kwargs):
@ -352,17 +370,6 @@ class ProcessMonitorFixture(fixtures.Fixture):
class BaseTestCase(DietTestCase):
@staticmethod
def config_parse(conf=None, args=None):
"""Create the default configurations."""
if args is None:
args = []
args += ['--config-file', etcdir('neutron.conf')]
if conf is None:
config.init(args=args)
else:
conf(args)
def setUp(self):
super(BaseTestCase, self).setUp()
@ -371,7 +378,6 @@ class BaseTestCase(DietTestCase):
cfg.CONF.set_override('state_path', self.get_default_temp_dir().path)
self.addCleanup(CONF.reset)
self.useFixture(ProcessMonitorFixture())
self.useFixture(fixtures.MonkeyPatch(
@ -384,8 +390,6 @@ class BaseTestCase(DietTestCase):
self.useFixture(fixture.RPCFixture())
self.setup_config()
self._callback_manager = registry_manager.CallbacksManager()
self.useFixture(fixture.CallbackRegistryFixture(
callback_manager=self._callback_manager))
@ -436,10 +440,6 @@ class BaseTestCase(DietTestCase):
root = root or self.get_default_temp_dir()
return root.join(filename)
def setup_config(self, args=None):
"""Tests that need a non-default config can override this method."""
self.config_parse(args=args)
def config(self, **kw):
"""Override some configuration values.

View File

@ -31,8 +31,8 @@ from neutron.tests.unit.db import test_db_base_plugin_v2 as test_plugin
class NetworkRbacTestcase(test_plugin.NeutronDbPluginV2TestCase):
def setUp(self):
self.context = context.get_admin_context()
super(NetworkRbacTestcase, self).setUp(plugin='ml2')
self.context = context.get_admin_context()
def _make_networkrbac(self, network, target,
action=rbac_db_models.ACCESS_SHARED):

View File

@ -40,11 +40,11 @@ class BaseTestTrackedResources(test_plugin.Ml2PluginV2TestCase,
SgTestCaseWrapper):
def setUp(self):
self.ctx = context.get_admin_context()
self.addCleanup(self._cleanup)
test_db_base_plugin_v2.NeutronDbPluginV2TestCase.quota_db_driver = (
'neutron.db.quota.driver.DbQuotaDriver')
super(BaseTestTrackedResources, self).setUp()
self.ctx = context.get_admin_context()
self._project_id = uuidutils.generate_uuid()
# TODO(ralonsoh): "tenant_id" reference should be removed.
self._tenant_id = self._project_id

View File

@ -76,8 +76,6 @@ class TestResourceRegistry(base.DietTestCase):
def test_register_resource_by_name_with_tracking_disabled_by_config(self):
cfg.CONF.set_override('track_quota_usage', False,
group='QUOTAS')
# DietTestCase does not automatically cleans configuration overrides
self.addCleanup(cfg.CONF.reset)
self.registry.set_tracked_resource('meh', test_quota.MehModel)
self.assertNotIn(
'meh', self.registry._tracked_resource_mappings)
@ -95,8 +93,6 @@ class TestAuxiliaryFunctions(base.DietTestCase):
def test_resync_tracking_disabled(self):
cfg.CONF.set_override('track_quota_usage', False,
group='QUOTAS')
# DietTestCase does not automatically cleans configuration overrides
self.addCleanup(cfg.CONF.reset)
with mock.patch('neutron.quota.resource.'
'TrackedResource.resync') as mock_resync:
self.registry.set_tracked_resource('meh', test_quota.MehModel)
@ -122,8 +118,6 @@ class TestAuxiliaryFunctions(base.DietTestCase):
def test_set_resources_dirty_invoked_with_tracking_disabled(self):
cfg.CONF.set_override('track_quota_usage', False,
group='QUOTAS')
# DietTestCase does not automatically cleans configuration overrides
self.addCleanup(cfg.CONF.reset)
with mock.patch('neutron.quota.resource.'
'TrackedResource.mark_dirty') as mock_mark_dirty:
self.registry.set_tracked_resource('meh', test_quota.MehModel)