diff options
author | Julien Danjou <julien@danjou.info> | 2013-07-22 16:12:51 +0200 |
---|---|---|
committer | Julien Danjou <julien@danjou.info> | 2013-10-09 10:30:52 +0200 |
commit | a289afed93819ad3028731d91b299ef3122d02fa (patch) | |
tree | 9fb5e07b1455ac818639a372e8fa8073f0593fb3 /tests/test_cfg.py | |
parent | b4252b921773906f17e52dec14cf3912daca9c1a (diff) | |
download | oslo-config-a289afed93819ad3028731d91b299ef3122d02fa.tar.gz |
Add the ability to validate default options value
Currently, the default options value provided are not validated, which
can trigger fun things like a IntOpt having a "foobar" as default value,
with code failing later when the option is being used.
This patch introduces a _validate_value method that can be used to
validate the default value to the expected type.
ConfigOpts.__call__ gains a validate_default_values arguments that can
trigger default value validation. The argument is False by default in
order to not break existing code.
Ultimately, we could in a later version validate the default at Opt
object creation time, which would be much better, but that may break
existing code, so we'll see.
Change-Id: I6c1998a3be3f1579139317cd85d56b42cabf89db
Diffstat (limited to 'tests/test_cfg.py')
-rw-r--r-- | tests/test_cfg.py | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/tests/test_cfg.py b/tests/test_cfg.py index b5a8c60..c074a1d 100644 --- a/tests/test_cfg.py +++ b/tests/test_cfg.py @@ -88,7 +88,8 @@ class BaseTestCase(utils.BaseTestCase): prog='test', version='1.0', usage='%(prog)s FOO BAR', - default_config_files=default_config_files) + default_config_files=default_config_files, + validate_default_values=True) def setUp(self): super(BaseTestCase, self).setUp() @@ -790,6 +791,18 @@ class ConfigFileOptsTestCase(BaseTestCase): self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(self.conf.foo, 666) + def test_conf_file_int_wrong_default(self): + self.conf.register_opt(cfg.IntOpt('foo', default='t666')) + + paths = self.create_tempfiles([('test', + '[DEFAULT]\n')]) + + self.conf(['--config-file', paths[0]]) + self.assertRaises(AttributeError, + getattr, + self.conf, + 'foo') + def test_conf_file_int_value(self): self.conf.register_opt(cfg.IntOpt('foo')) @@ -851,6 +864,18 @@ class ConfigFileOptsTestCase(BaseTestCase): self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(self.conf.foo, 6.66) + def test_conf_file_float_default_wrong_type(self): + self.conf.register_opt(cfg.FloatOpt('foo', default='foobar6.66')) + + paths = self.create_tempfiles([('test', + '[DEFAULT]\n')]) + + self.conf(['--config-file', paths[0]]) + self.assertRaises(AttributeError, + getattr, + self.conf, + 'foo') + def test_conf_file_float_value(self): self.conf.register_opt(cfg.FloatOpt('foo')) @@ -2909,6 +2934,16 @@ class ChoicesTestCase(BaseTestCase): self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(self.conf.foo, 'baaar') + def test_conf_file_choice_bad_default(self): + self.conf.register_cli_opt(cfg.StrOpt('foo', + choices=['baar', 'baaar'], + default='foobaz')) + self.conf([]) + self.assertRaises(AttributeError, + getattr, + self.conf, + 'foobaz') + class PrintHelpTestCase(utils.BaseTestCase): |