arg_validator: reject bool arguments from ArgValidatorNum

`ArgValidatorNum` would normalize boolean into int when
`self.numeric_type` is int, then `self.numeric_type(False)` is 0 and
`self.numeric_type(True)` is 1. Therefore, we need to fix
`ArgValidatorNum()` to reject boolean values when integer values are
expected for the setting. This bug fix potentially breaks previously
"working" playbooks (but realistically, they were not working, because
setting 0 or 1 was unlikely intended).

Signed-off-by: Wen Liang <liangwen12year@gmail.com>
This commit is contained in:
Wen Liang 2021-04-21 12:55:31 -04:00 committed by Gris Ge
parent dfacbf72f7
commit a6c98bd660
2 changed files with 7 additions and 1 deletions

View file

@ -271,7 +271,11 @@ class ArgValidatorNum(ArgValidator):
def _validate_impl(self, value, name):
v = None
try:
if isinstance(value, self.numeric_type):
if isinstance(value, bool):
# bool can (probably) be converted to self.numeric_type,
# but here we don't want to accept a boolean value.
pass
elif isinstance(value, self.numeric_type):
# ArgValidatorNum should normalize the input values to be of type
# self.numeric_type, except the default_value
v = self.numeric_type(value)

View file

@ -384,6 +384,8 @@ class TestValidator(unittest.TestCase):
v = network_lsr.argument_validator.ArgValidatorNum("state", required=True)
self.assertValidationError(v, None)
self.assertValidationError(v, False)
self.assertValidationError(v, True)
def test_validate_bool(self):