From 67c3abb048402e4910f18386091a4ccf68448dcf Mon Sep 17 00:00:00 2001 From: Roxana Gherle Date: Wed, 24 Jun 2015 16:10:37 -0700 Subject: [PATCH] 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 --- oslo_config/cfg.py | 10 ++++++++-- 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 e4064318..0cf1c50f 100644 --- a/oslo_config/cfg.py +++ b/oslo_config/cfg.py @@ -2089,7 +2089,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 @@ -2098,10 +2098,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 080ee336..f4cdb002 100644 --- a/oslo_config/tests/test_cfg.py +++ b/oslo_config/tests/test_cfg.py @@ -2513,6 +2513,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):