fix in config flags features

* fix name clashing
* fix regex
* fix test case

Change-Id: I2dbc4432a4be2b23dd39e66680abbe33da5ae5af
This commit is contained in:
fdesi 2022-07-02 00:06:15 +02:00
parent 18ac668cd7
commit d049eee8f4
2 changed files with 19 additions and 22 deletions

View File

@ -86,22 +86,23 @@ def config_property(f):
@config_property
def config_flags(cls):
"""Decorator to add a custom configuration property with
def user_config_flags(cls):
"""adding custom configuration property with
config-flags to add extra options.
:param cls: Configuration Adapter class
:type cls: charms_openstack.adapters.DefaultConfigurationAdapter
"""
cf_config = {}
config = hookenv.config
if config and 'config-flags' in config:
# match 0 or multiple time any pair of character a=b divide by a coma
config = hookenv.config('config-flags')
if config:
# match 0 or multiple time any pair of character a=b divide by a comma
# zero or one spaces are allows between tokens
parsing_regex = r"^(?:\s{0,1}[^=]\s{0,1}=\s{0,1}[^,]\s{0,1}(?:,|$))*$"
if re.match(parsing_regex, config['config-flags']):
parsing_regex = r"^(?:\s{0,1}[^=\s]+\s{0,1}=\s{0,1}"\
r"[^,\s]+\s{0,1}(?:,|$))*$"
if re.match(parsing_regex, config):
cf_config = dict(map(lambda x: [s.strip() for s in x.split('=')],
config['config-flags']
config
.split(',')))
else:
raise RuntimeError("config-flags string error: {}".format(config))

View File

@ -58,9 +58,9 @@ class TestCustomProperties(unittest.TestCase):
cls_mock = mock.MagicMock()
with mock.patch.object(adapters.hookenv,
'config',
new=cfg):
new=lambda x: cfg[x]):
conf = adapters.config_flags(cls_mock)
conf = adapters.user_config_flags(cls_mock)
self.assertEqual(conf['a'], 'b')
self.assertEqual(conf['c'], 'd')
self.assertEqual(conf['e'], 'f')
@ -72,10 +72,10 @@ class TestCustomProperties(unittest.TestCase):
cls_mock = mock.MagicMock()
with mock.patch.object(adapters.hookenv,
'config',
new=cfg):
new=lambda x: cfg[x]):
with self.assertRaises(RuntimeError):
adapters.config_flags(cls_mock)
adapters.user_config_flags(cls_mock)
def test_user_config_flags_parsing_error_2(self):
cfg = {
@ -84,31 +84,27 @@ class TestCustomProperties(unittest.TestCase):
cls_mock = mock.MagicMock()
with mock.patch.object(adapters.hookenv,
'config',
new=cfg):
new=lambda x: cfg[x]):
with self.assertRaises(RuntimeError):
adapters.config_flags(cls_mock)
adapters.user_config_flags(cls_mock)
def test_user_config_flags_missing(self):
cfg = {
'other-flags': 1
}
cls_mock = mock.MagicMock()
with mock.patch.object(adapters.hookenv,
'config',
new=cfg):
new=lambda x: {}):
conf = adapters.config_flags(cls_mock)
conf = adapters.user_config_flags(cls_mock)
self.assertEqual(conf, {})
def test_user_config_none(self):
cfg = None
cls_mock = mock.MagicMock()
with mock.patch.object(adapters.hookenv,
'config',
new=cfg):
new=lambda x: None):
conf = adapters.config_flags(cls_mock)
conf = adapters.user_config_flags(cls_mock)
self.assertEqual(conf, {})