diff options
author | Roxana Gherle <roxana.gherle@hp.com> | 2015-06-24 16:10:37 -0700 |
---|---|---|
committer | Roxana Gherle <roxana.gherle@hp.com> | 2015-08-18 23:31:08 +0000 |
commit | 90fbc249dcec55dff29ba84c51783a56e2f07960 (patch) | |
tree | 54e2331a033ef825ea62c5a907f35d85ef1d37bb | |
parent | 00665772ecc5424d7d0ccde1b7447ca4a885eeef (diff) | |
download | oslo-config-90fbc249dcec55dff29ba84c51783a56e2f07960.tar.gz |
Add enforce_type option when setting an override
The 'override' value of an option is currently stored and retrieved
with the exact value that is set through set_override method - not
taking into account the type of that option. Sometimes we want the type
of the override value to be the same as the option type and therefore
we will add an optional parameter to the set_override method that will
enforce the option value type.
Closes-Bug: #1461299
Change-Id: I008b76d3292f76d0699f0063930a3b190539740f
(cherry picked from commit 67c3abb048402e4910f18386091a4ccf68448dcf)
-rw-r--r-- | oslo_config/cfg.py | 10 | ||||
-rw-r--r-- | oslo_config/tests/test_cfg.py | 31 |
2 files changed, 39 insertions, 2 deletions
diff --git a/oslo_config/cfg.py b/oslo_config/cfg.py index 77fd44d..174fa35 100644 --- a/oslo_config/cfg.py +++ b/oslo_config/cfg.py @@ -2057,7 +2057,7 @@ class ConfigOpts(collections.Mapping): self._get_group(group) @__clear_cache - def set_override(self, name, override, group=None): + def set_override(self, name, override, group=None, enforce_type=False): """Override an opt value. Override the command line, config file and default values of a @@ -2066,10 +2066,16 @@ class ConfigOpts(collections.Mapping): :param name: the name/dest of the opt :param override: the override value :param group: an option OptGroup object or group name + :param enforce_type: a boolean whether to convert the override + value to the option's type :raises: NoSuchOptError, NoSuchGroupError """ opt_info = self._get_opt_info(name, group) - opt_info['override'] = override + if enforce_type: + opt_info['override'] = self._convert_value(override, + opt_info['opt']) + else: + opt_info['override'] = override @__clear_cache def set_default(self, name, default, group=None): diff --git a/oslo_config/tests/test_cfg.py b/oslo_config/tests/test_cfg.py index 5518f53..af9c1c3 100644 --- a/oslo_config/tests/test_cfg.py +++ b/oslo_config/tests/test_cfg.py @@ -2472,6 +2472,37 @@ class OverridesTestCase(BaseTestCase): self.conf.clear_override('foo') self.assertIsNone(self.conf.foo) + def test_enforce_type_str_override(self): + self.conf.register_opt(cfg.StrOpt('foo')) + self.conf.set_override('foo', True, enforce_type=True) + self.conf([]) + self.assertEqual(self.conf.foo, 'True') + self.conf.clear_override('foo') + self.assertIsNone(self.conf.foo) + + def test_set_override_in_choices(self): + self.conf.register_group(cfg.OptGroup('f')) + self.conf.register_cli_opt(cfg.StrOpt('oo', choices=('a', 'b')), + group='f') + self.conf.set_override('oo', 'b', 'f', enforce_type=True) + self.assertEqual('b', self.conf.f.oo) + + def test_set_override_not_in_choices(self): + self.conf.register_group(cfg.OptGroup('f')) + self.conf.register_cli_opt(cfg.StrOpt('oo', choices=('a', 'b')), + group='f') + self.assertRaises(ValueError, + self.conf.set_override, 'oo', 'c', 'f', + enforce_type=True) + + def test_enforce_type_bool_override(self): + self.conf.register_opt(cfg.BoolOpt('foo')) + self.conf.set_override('foo', 'True', enforce_type=True) + self.conf([]) + self.assertEqual(self.conf.foo, True) + self.conf.clear_override('foo') + self.assertIsNone(self.conf.foo) + class ResetAndClearTestCase(BaseTestCase): |