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
This commit is contained in:
parent
a7d4277fb3
commit
67c3abb048
oslo_config
@ -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):
|
||||
|
@ -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):
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user