diff options
author | ChangBo Guo(gcb) <eric.guo@easystack.cn> | 2016-06-08 13:14:45 +0800 |
---|---|---|
committer | ChangBo Guo(gcb) <eric.guo@easystack.cn> | 2016-06-08 13:14:45 +0800 |
commit | bba5a89feb232c6aa272a8d993a321765f220685 (patch) | |
tree | 7b54fb92788acffc9a5e346553d4ccc2437f3f2c | |
parent | bb0f7e3880bd83dbb9088f3f8045152ce8e7897b (diff) | |
download | oslo-config-bba5a89feb232c6aa272a8d993a321765f220685.tar.gz |
fixture: Provide parameter enforce_type for method config
This allows fixture to check config option's type and value before
overriding, that ensure testing as same as production enviroment.
Change-Id: I958d04c92cfeacd134aaf2e0a758e0defdb607e2
-rw-r--r-- | oslo_config/fixture.py | 5 | ||||
-rw-r--r-- | oslo_config/tests/test_fixture.py | 17 |
2 files changed, 21 insertions, 1 deletions
diff --git a/oslo_config/fixture.py b/oslo_config/fixture.py index 8e39023..6f047e8 100644 --- a/oslo_config/fixture.py +++ b/oslo_config/fixture.py @@ -58,11 +58,14 @@ class Config(fixtures.Fixture): the specified configuration option group, otherwise the overrides are applied to the ``default`` group. + If a `enforce_type` is supplied, will convert the override + value to the option's type before overriding. """ group = kw.pop('group', None) + enforce_type = kw.pop('enforce_type', False) for k, v in six.iteritems(kw): - self.conf.set_override(k, v, group) + self.conf.set_override(k, v, group, enforce_type=enforce_type) def _unregister_config_opts(self): for group in self._registered_config_opts: diff --git a/oslo_config/tests/test_fixture.py b/oslo_config/tests/test_fixture.py index 1ea3f50..6e2062d 100644 --- a/oslo_config/tests/test_fixture.py +++ b/oslo_config/tests/test_fixture.py @@ -29,6 +29,10 @@ class ConfigTestCase(base.BaseTestCase): config_fixture.setUp() config_fixture.register_opt(cfg.StrOpt( 'testing_option', default='initial_value')) + config_fixture.register_opt(cfg.IntOpt( + 'test2', min=0, default=5)) + config_fixture.register_opt(cfg.StrOpt( + 'test3', choices=['a', 'b'], default='a')) return config_fixture def test_overridden_value(self): @@ -38,6 +42,19 @@ class ConfigTestCase(base.BaseTestCase): self.assertEqual('changed_value', f.conf.get('testing_option')) + def test_overridden_value_with_enforce_type(self): + f = self._make_fixture() + self.assertEqual(5, f.conf.get('test2')) + self.assertEqual('a', f.conf.get('test3')) + # with enforce_type=False + f.config(test2=-1) + self.assertEqual(-1, f.conf.get('test2')) + f.config(test3='c') + self.assertEqual('c', f.conf.get('test3')) + # with enforce_type=True + self.assertRaises(ValueError, f.config, test2=-1, enforce_type=True) + self.assertRaises(ValueError, f.config, test3='c', enforce_type=True) + def test_cleanup(self): f = self._make_fixture() f.config(testing_option='changed_value') |