Add CLI option support to config fixture

oslo.config.fixture.Config has register_opt() and register_opts() but
not register_cli_opt() and register_cli_opts().

Change-Id: I8a1582dc0deb0230eb02b0894a49398f333375fe
This commit is contained in:
Mark McLoughlin 2014-06-25 06:53:50 +01:00
parent f18797ab7d
commit ab7ae99dbd
2 changed files with 54 additions and 3 deletions

View File

@ -84,3 +84,35 @@ class Config(fixtures.Fixture):
"""
for opt in opts:
self.register_opt(opt, group=group)
def register_cli_opt(self, opt, group=None):
"""Register a single CLI option for the test run.
Options registered in this manner will automatically be unregistered
during cleanup.
If a `group` argument is supplied, it will register the new option
to that group, otherwise the option is registered to the ``default``
group.
CLI options must be registered before the command line and config files
are parsed. This is to ensure that all CLI options are shown in --help
and option validation works as expected.
"""
self.conf.register_cli_opt(opt, group=group)
self._registered_config_opts.setdefault(group, set()).add(opt)
def register_cli_opts(self, opts, group=None):
"""Register multiple CLI options for the test run.
This works in the same manner as register_opt() but takes a list of
options as the first argument. All arguments will be registered to the
same group if the ``group`` argument is supplied, otherwise all options
will be registered to the ``default`` group.
CLI options must be registered before the command line and config files
are parsed. This is to ensure that all CLI options are shown in --help
and option validation works as expected.
"""
for opt in opts:
self.register_cli_opt(opt, group=group)

View File

@ -28,8 +28,6 @@ class ConfigTestCase(base.BaseTestCase):
super(ConfigTestCase, self).setUp()
self.config_fixture = self.useFixture(config.Config(conf))
self.config = self.config_fixture.config
self.register_opt = self.config_fixture.register_opt
self.register_opts = self.config_fixture.register_opts
self.config_fixture.register_opt(cfg.StrOpt(
'testing_option', default='initial_value'))
@ -55,7 +53,7 @@ class ConfigTestCase(base.BaseTestCase):
def test_register_options(self):
opt1 = cfg.StrOpt('first_test_opt', default='initial_value_1')
opt2 = cfg.StrOpt('second_test_opt', default='initial_value_2')
self.register_opts([opt1, opt2])
self.config_fixture.register_opts([opt1, opt2])
self.assertEqual(conf.get('first_test_opt'), opt1.default)
self.assertEqual(conf.get('second_test_opt'), opt2.default)
@ -66,3 +64,24 @@ class ConfigTestCase(base.BaseTestCase):
opt.default)
self.config_fixture.cleanUp()
self.assertRaises(cfg.NoSuchOptError, conf.get, 'new_test_opt')
def test_register_cli_option(self):
opt = cfg.StrOpt('new_test_opt', default='initial_value')
self.config_fixture.register_cli_opt(opt)
self.assertEqual(conf.get('new_test_opt'),
opt.default)
def test_register_cli_options(self):
opt1 = cfg.StrOpt('first_test_opt', default='initial_value_1')
opt2 = cfg.StrOpt('second_test_opt', default='initial_value_2')
self.config_fixture.register_cli_opts([opt1, opt2])
self.assertEqual(conf.get('first_test_opt'), opt1.default)
self.assertEqual(conf.get('second_test_opt'), opt2.default)
def test_cleanup_unregister_cli_option(self):
opt = cfg.StrOpt('new_test_opt', default='initial_value')
self.config_fixture.register_cli_opt(opt)
self.assertEqual(conf.get('new_test_opt'),
opt.default)
self.config_fixture.cleanUp()
self.assertRaises(cfg.NoSuchOptError, conf.get, 'new_test_opt')