Test OptionsCache initialisation

As a precursor to reading options via stevedore extensions and thereby
not having to assume the global cfg.CONF is being used.

Change-Id: Ie3be4ceadf336fd9494fe48ec09cb26b3d0de43f
This commit is contained in:
Alexis Lee 2016-12-15 16:24:42 +00:00
parent 68ca7ae76f
commit 184a3a90e0
2 changed files with 84 additions and 19 deletions

View File

@ -564,7 +564,7 @@ def update_flagmappings(package_name, options, verbose=0):
print(line)
def main():
def parse_args():
parser = argparse.ArgumentParser(
description='Manage flag files, to aid in updating documentation.',
usage='%(prog)s <cmd> <package> [options]')
@ -594,30 +594,38 @@ def main():
'Defaults to stdout for "dump"',
required=False,
type=str,)
args = parser.parse_args()
if args.repos is None:
args.repos = ['./sources/%s/%s' % args.package]
for repository in args.repos:
def import_module(conf, package_name, base_path):
sys.path.insert(0, base_path)
try:
__import__(package_name)
except ImportError as e:
if conf.verbose >= 1:
print(str(e))
print("Failed to import: %s (%s)" % (package_name, e))
import_modules(base_path, package_name, verbose=conf.verbose)
sys.path.pop(0)
def load_options(conf):
if conf.repos is None:
conf.repos = ['./sources/%s/%s' % conf.package]
for repository in conf.repos:
package_name = os.path.basename(repository)
base_path = os.path.dirname(repository)
sys.path.insert(0, base_path)
try:
__import__(package_name)
except ImportError as e:
if args.verbose >= 1:
print(str(e))
print("Failed to import: %s (%s)" % (package_name, e))
import_modules(base_path, package_name, verbose=args.verbose)
sys.path.pop(0)
import_module(conf, package_name, os.path.dirname(repository))
overrides = _get_overrides(package_name)
options = OptionsCache(overrides, verbose=args.verbose)
options.maybe_load_extensions(args.repos)
options = OptionsCache(overrides, verbose=conf.verbose)
options.maybe_load_extensions(conf.repos)
return options
def main():
args = parse_args()
options = load_options(args)
if args.verbose > 0:
print("%s options imported from package %s." % (len(options),
str(package_name)))

View File

@ -0,0 +1,57 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock
import unittest
from oslo_config import cfg
from autogenerate_config_docs import autohelp
class TestAutohelp(unittest.TestCase):
@mock.patch.object(autohelp, "import_modules")
def test_load_options(self, mock_import_modules):
main_conf = cfg.ConfigOpts()
main_conf([])
main_conf.repos = ["sources/repo1/pkg1", "sources/repo2/pkg2"]
main_conf.verbose = False
conf = cfg.CONF
conf([])
def side(main_conf, pkg, base):
group = cfg.OptGroup(name=pkg)
conf.register_group(group)
conf.register_opts([cfg.StrOpt("%s_opt" % pkg)], group=group)
# I had a lot of trouble trying to mock __import__, wrapping up that
# code into the new import_module function helped
@mock.patch.object(autohelp.OptionsCache, "maybe_load_extensions")
@mock.patch.object(autohelp, "import_module")
def run_test(mock_import_module, mock_maybe):
mock_import_module.side_effect = side
opts = autohelp.load_options(main_conf)
mock_import_module.assert_has_calls([
mock.call(main_conf, 'pkg1', "sources/repo1"),
mock.call(main_conf, 'pkg2', "sources/repo2")])
mock_maybe.assert_called_once_with(main_conf.repos)
return opts
opts = run_test()
opts_by_name = opts._opts_by_name
for (e_grp, e_opt) in [('pkg1', 'pkg1_opt'),
('pkg2', 'pkg2_opt')]:
(grp, opt) = opts_by_name.get('%s/%s' % (e_grp, e_opt))
self.assertEqual(e_grp, grp)
self.assertEqual(e_opt, opt.name)