diff --git a/oslo/config/fixture.py b/oslo/config/fixture.py index fd74f3b2..2bb6cf8e 100644 --- a/oslo/config/fixture.py +++ b/oslo/config/fixture.py @@ -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) diff --git a/tests/test_fixture.py b/tests/test_fixture.py index 34894b60..def61c15 100644 --- a/tests/test_fixture.py +++ b/tests/test_fixture.py @@ -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')