Allow loading custom config files into Config Fixture.
This patch adds a set_config_files option that allows a tester to force load a specific list of configuration files. It will override the existing set of files, and trigger a reload of those configuration files. Note: This _does_ trigger the autodetection mechanism, as the present state of oslo_config makes it so __call__ is the only thing that sets self._args. After triggering, however, it immediately overrides the detected file list and replaces it with the provided one. Tests provided. Change-Id: I0bcea8be9d2527c61bd8d150e9ed606fabdd290e
This commit is contained in:
parent
e30131a1ef
commit
ba3aebe39b
@ -36,10 +36,18 @@ class Config(fixtures.Fixture):
|
||||
# NOTE(morganfainberg): unregister must be added to cleanup before
|
||||
# reset is because cleanup works in reverse order of registered items,
|
||||
# and a reset must occur before unregistering options can occur.
|
||||
self.addCleanup(self._reset_default_config_files)
|
||||
self.addCleanup(self._unregister_config_opts)
|
||||
self.addCleanup(self.conf.reset)
|
||||
self._registered_config_opts = {}
|
||||
|
||||
# Grab an old copy of the default config files - if it exists - for
|
||||
# subsequent cleanup.
|
||||
if hasattr(self.conf, 'default_config_files'):
|
||||
self._default_config_files = self.conf.default_config_files
|
||||
else:
|
||||
self._default_config_files = None
|
||||
|
||||
def config(self, **kw):
|
||||
"""Override configuration values.
|
||||
|
||||
@ -61,6 +69,17 @@ class Config(fixtures.Fixture):
|
||||
self.conf.unregister_opts(self._registered_config_opts[group],
|
||||
group=group)
|
||||
|
||||
def _reset_default_config_files(self):
|
||||
if not hasattr(self.conf, 'default_config_files'):
|
||||
return
|
||||
|
||||
if self._default_config_files:
|
||||
self.conf.default_config_files = self._default_config_files
|
||||
else:
|
||||
# Delete, because we could conceivably begin with the property
|
||||
# being unset.
|
||||
self.conf.default_config_files = None
|
||||
|
||||
def register_opt(self, opt, group=None):
|
||||
"""Register a single option for the test run.
|
||||
|
||||
@ -140,3 +159,21 @@ class Config(fixtures.Fixture):
|
||||
raw_config[group][key] = [str(value)]
|
||||
|
||||
self.conf._namespace._add_parsed_config_file(raw_config, raw_config)
|
||||
|
||||
def set_config_files(self, config_files):
|
||||
"""Specify a list of config files to read.
|
||||
|
||||
This method allows you to predefine the list of configuration files
|
||||
that are loaded by oslo_config. It will ensure that your tests do not
|
||||
attempt to autodetect, and accidentally pick up config files from
|
||||
locally installed services.
|
||||
"""
|
||||
if not isinstance(config_files, list):
|
||||
raise AttributeError("Please pass a list() to set_config_files()")
|
||||
|
||||
# Make sure the namespace exists for our tests.
|
||||
if not self.conf._namespace:
|
||||
self.conf.__call__(args=[])
|
||||
|
||||
self.conf.default_config_files = config_files
|
||||
self.conf.reload_config_files()
|
||||
|
3
oslo_config/tests/test_fixture.conf
Normal file
3
oslo_config/tests/test_fixture.conf
Normal file
@ -0,0 +1,3 @@
|
||||
[DEFAULT]
|
||||
first_test_opt=loaded_value_1
|
||||
second_test_opt=loaded_value_2
|
@ -115,3 +115,29 @@ class ConfigTestCase(base.BaseTestCase):
|
||||
self.config_fixture.register_opt(opt2)
|
||||
self.assertEqual(conf.first_test_opt, 'initial_value_1')
|
||||
self.assertEqual(conf.second_test_opt, 'initial_value_2')
|
||||
|
||||
def test_assert_default_files_cleanup(self):
|
||||
"""Assert that using the fixture forces a clean list."""
|
||||
self.assertNotIn('default_config_files', conf)
|
||||
|
||||
config_files = ['./test_fixture.conf']
|
||||
self.config_fixture.set_config_files(config_files)
|
||||
|
||||
self.assertEqual(conf.default_config_files, config_files)
|
||||
self.config_fixture.cleanUp()
|
||||
|
||||
self.assertNotIn('default_config_files', conf)
|
||||
|
||||
def test_load_custom_files(self):
|
||||
self.assertNotIn('default_config_files', conf)
|
||||
config_files = ['./oslo_config/tests/test_fixture.conf']
|
||||
self.config_fixture.set_config_files(config_files)
|
||||
|
||||
opt1 = cfg.StrOpt('first_test_opt', default='initial_value_1')
|
||||
opt2 = cfg.StrOpt('second_test_opt', default='initial_value_2')
|
||||
|
||||
self.config_fixture.register_opt(opt1)
|
||||
self.config_fixture.register_opt(opt2)
|
||||
|
||||
self.assertEqual('loaded_value_1', conf.get('first_test_opt'))
|
||||
self.assertEqual('loaded_value_2', conf.get('second_test_opt'))
|
||||
|
Loading…
Reference in New Issue
Block a user