diff options
author | Jenkins <jenkins@review.openstack.org> | 2014-07-02 13:15:51 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2014-07-02 13:15:51 +0000 |
commit | 9f26394cb3af205e84cce251c8ad7587ceae573d (patch) | |
tree | 92af8ce495ba615ca6dde979f8b970eb5dd92dc8 | |
parent | 434383bbc14cc59289cc9dac98de4294a7e8b138 (diff) | |
parent | ab7ae99dbde2a9e212b9e86adf1001dad808bf09 (diff) | |
download | oslo-config-9f26394cb3af205e84cce251c8ad7587ceae573d.tar.gz |
Merge "Add CLI option support to config fixture"
-rw-r--r-- | oslo/config/fixture.py | 32 | ||||
-rw-r--r-- | tests/test_fixture.py | 25 |
2 files changed, 54 insertions, 3 deletions
diff --git a/oslo/config/fixture.py b/oslo/config/fixture.py index fd74f3b..2bb6cf8 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 34894b6..def61c1 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') |