Extracts logic for finding domain configs

The logic for finding configs shouldn't be mixed up with the logic for
procssing those configs. This refactoring also allows the logic to be
more easily mocked.

Change-Id: Ie5ec034bca72d61e0b4de63e480be06959ee8583
Related-Bug: 1472059
This commit is contained in:
David Stanek 2016-02-11 17:49:31 +00:00
parent ed2ff17290
commit 61706dc818
2 changed files with 50 additions and 16 deletions

View File

@ -500,11 +500,33 @@ DOMAIN_CONF_FHEAD = 'keystone.'
DOMAIN_CONF_FTAIL = '.conf'
def _domain_config_finder(conf_dir):
"""Return a generator of all domain config files found in a directory.
Donmain configs match the filename pattern of
'keystone.<domain_name>.conf'.
:returns: generator yeilding (filename, domain_name) tuples
"""
for r, d, f in os.walk(conf_dir):
for fname in f:
if (fname.startswith(DOMAIN_CONF_FHEAD) and
fname.endswith(DOMAIN_CONF_FTAIL)):
if fname.count('.') >= 2:
domain_name = fname[len(DOMAIN_CONF_FHEAD):
-len(DOMAIN_CONF_FTAIL)]
yield (os.path.join(r, fname), domain_name)
else:
LOG.warning(_LW('Ignoring file (%s) while scanning '
'domain config directory'), fname)
class DomainConfigUploadFiles(object):
def __init__(self):
def __init__(self, domain_config_finder=_domain_config_finder):
super(DomainConfigUploadFiles, self).__init__()
self.load_backends()
self._domain_config_finder = domain_config_finder
def load_backends(self):
drivers = backends.load_backends()
@ -638,21 +660,8 @@ class DomainConfigUploadFiles(object):
os.path.join(conf_dir, fname), domain_name)
return
# Request is to transfer all config files, so let's read all the
# files in the config directory, and transfer those that match the
# filename pattern of 'keystone.<domain_name>.conf'
for r, d, f in os.walk(conf_dir):
for fname in f:
if (fname.startswith(DOMAIN_CONF_FHEAD) and
fname.endswith(DOMAIN_CONF_FTAIL)):
if fname.count('.') >= 2:
self.upload_configs_to_database(
os.path.join(r, fname),
fname[len(DOMAIN_CONF_FHEAD):
-len(DOMAIN_CONF_FTAIL)])
else:
LOG.warning(_LW('Ignoring file (%s) while scanning '
'domain config directory'), fname)
for filename, domain_name in self._domain_config_finder(conf_dir):
self.upload_configs_to_database(filename, domain_name)
def run(self):
# First off, let's just check we can talk to the domain database

View File

@ -19,6 +19,7 @@ import fixtures
import mock
from oslo_config import cfg
from six.moves import range
from testtools import matchers
from keystone.cmd import cli
from keystone.common import dependency
@ -363,3 +364,27 @@ class CliDomainConfigInvalidDomainTestCase(CliDomainConfigAllTestCase):
'file': os.path.join(CONF.identity.domain_config_dir,
file_name)})
mock_print.assert_has_calls([mock.call(error_msg)])
class TestDomainConfigFinder(unit.BaseTestCase):
def setUp(self):
super(TestDomainConfigFinder, self).setUp()
self.logging = self.useFixture(fixtures.LoggerFixture())
@mock.patch('os.walk')
def test_finder_ignores_files(self, mock_walk):
mock_walk.return_value = [
['.', [], ['file.txt', 'keystone.conf', 'keystone.domain0.conf']],
]
domain_configs = list(cli._domain_config_finder('.'))
expected_domain_configs = [('./keystone.domain0.conf', 'domain0')]
self.assertThat(domain_configs,
matchers.Equals(expected_domain_configs))
expected_msg = ('Ignoring file (keystone.conf) while scanning domain '
'config directory')
self.assertThat(self.logging.output,
matchers.Contains(expected_msg))