From 88e3b0ad984797ec1f856429698949e4781dff3a Mon Sep 17 00:00:00 2001 From: Cyril Roelandt Date: Wed, 21 Sep 2022 19:38:24 +0200 Subject: [PATCH] Boolean options: use strict checking MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Boolean options (such as "--protected" for glance md-namespace-update) should accept a limited amount of valid values, rather than assuming an "invalid" value means "False". The following values (no matter the case) will now be interpreted as True: ‘t’,’true’, ‘on’, ‘y’, ‘yes’, or ‘1’. The following values (no matter the case) will now be interpreted as False: ‘f’, ‘false’, ‘off’, ‘n’, ‘no’, or ‘0’. Change-Id: I0e7942045d883ac398bab4a7a85f2b4ac9b1ed8c Closes-Bug: #1607317 --- doc/source/cli/property-keys.rst | 5 +++++ glanceclient/common/utils.py | 2 +- glanceclient/tests/unit/test_utils.py | 8 ++++++++ ...-properties-strict-checking-bdd624b5da81e723.yaml | 12 ++++++++++++ 4 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/boolean-properties-strict-checking-bdd624b5da81e723.yaml diff --git a/doc/source/cli/property-keys.rst b/doc/source/cli/property-keys.rst index 9e59124e..5c3cee45 100644 --- a/doc/source/cli/property-keys.rst +++ b/doc/source/cli/property-keys.rst @@ -27,3 +27,8 @@ in the Glance Administration Guide. For more information, refer to `Manage images `_ in the Glance Administration Guide. + +.. note:: + + Boolean properties expect one of the following values: '0', '1', 'f', + 'false', 'n', 'no', 'off', 'on', 't', 'true', 'y', 'yes' (case-insensitive). diff --git a/glanceclient/common/utils.py b/glanceclient/common/utils.py index c3f08de8..0de575f5 100644 --- a/glanceclient/common/utils.py +++ b/glanceclient/common/utils.py @@ -103,7 +103,7 @@ def schema_args(schema_getter, omit=None): typemap = { 'string': encodeutils.safe_decode, 'integer': int, - 'boolean': strutils.bool_from_string, + 'boolean': lambda x: strutils.bool_from_string(x, strict=True), 'array': list } diff --git a/glanceclient/tests/unit/test_utils.py b/glanceclient/tests/unit/test_utils.py index 46cefbf6..d10c70de 100644 --- a/glanceclient/tests/unit/test_utils.py +++ b/glanceclient/tests/unit/test_utils.py @@ -229,6 +229,14 @@ class TestUtils(testtools.TestCase): self.assertEqual(encodeutils.safe_decode, opts['type']) self.assertIn('None, opt-1, opt-2', opts['help']) + # Make sure we use strict checking for boolean values. + decorated = utils.schema_args(schema_getter('boolean'))(dummy_func) + arg, opts = decorated.__dict__['arguments'][0] + type_function = opts['type'] + self.assertEqual(type_function('False'), False) + self.assertEqual(type_function('True'), True) + self.assertRaises(ValueError, type_function, 'foo') + def test_iterable_closes(self): # Regression test for bug 1461678. def _iterate(i): diff --git a/releasenotes/notes/boolean-properties-strict-checking-bdd624b5da81e723.yaml b/releasenotes/notes/boolean-properties-strict-checking-bdd624b5da81e723.yaml new file mode 100644 index 00000000..bd0836d5 --- /dev/null +++ b/releasenotes/notes/boolean-properties-strict-checking-bdd624b5da81e723.yaml @@ -0,0 +1,12 @@ +--- +prelude: > +fixes: + - | + * Bug 1607317_: metadata def namespace update CLI is not working as expected for parameter "protected" + + .. _1607317: https://code.launchpad.net/bugs/1607317 +other: + - | + Boolean arguments now expect one of the following values: '0', '1', 'f', + 'false', 'n', 'no', 'off', 'on', 't', 'true', 'y', 'yes' + (case-insensitive). This will not change anything for most users.