diff options
author | Mark McLoughlin <markmc@redhat.com> | 2014-06-25 06:53:50 +0100 |
---|---|---|
committer | Mark McLoughlin <markmc@redhat.com> | 2014-06-25 14:00:50 +0100 |
commit | ab7ae99dbde2a9e212b9e86adf1001dad808bf09 (patch) | |
tree | 21debd35c104b9b613c8d0d22063163bb37424df | |
parent | f18797ab7d068663b37f4e25558c624f44e4b099 (diff) | |
download | oslo-config-ab7ae99dbde2a9e212b9e86adf1001dad808bf09.tar.gz |
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
-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') |