Guard against config_dirs not defined on ConfigOpts

Turned out that if the code extracts config_dirs value from ConfigOpts
objects before config files are parsed, then oslo.config will raise
NoSuchOptError exception.

This is not a usual mode of operation for the code, since main()
function of the process using it is expected to parse CLI and config
files before using it, it may nevertheless happen in some test code.

This patch guards against those exceptions, falling back to
/etc/neutron, as we already do when --config-dir is not specified.

Change-Id: I00cf824baa8580b7aa7ec4518a4741e49c998364
Closes-Bug: #1587359
This commit is contained in:
Ihar Hrachyshka 2016-05-31 16:51:30 +02:00
parent 01a9d41ceb
commit a8da782051
2 changed files with 16 additions and 1 deletions

View File

@ -71,7 +71,12 @@ class NeutronModule(object):
if neutron_dir is not None:
neutron_dirs = [neutron_dir]
else:
neutron_dirs = cfg.CONF.config_dirs or ['/etc/neutron']
try:
neutron_dirs = cfg.CONF.config_dirs
except cfg.NoSuchOptError:
neutron_dirs = None
if not neutron_dirs:
neutron_dirs = ['/etc/neutron']
# load configuration from all matching files to reflect oslo.config
# behaviour

View File

@ -269,3 +269,13 @@ class NeutronModuleMultiConfigFileTestCase(base.BaseTestCase):
mod = provconf.NeutronModule('neutron_test')
mod.ini()
self.assertEqual(['zzz', 'foo', 'bar'], mod.service_providers())
class NeutronModuleConfigNotParsedTestCase(base.DietTestCase):
def setup_config(self):
pass
def test_ini_no_crash_if_config_files_not_parsed(self):
mod = provconf.NeutronModule('neutron_test')
mod.ini()