From a6c98bd660303b51d2125d097c8fc8231ec37b82 Mon Sep 17 00:00:00 2001 From: Wen Liang Date: Wed, 21 Apr 2021 12:55:31 -0400 Subject: [PATCH] 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 --- module_utils/network_lsr/argument_validator.py | 6 +++++- tests/unit/test_network_connections.py | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/module_utils/network_lsr/argument_validator.py b/module_utils/network_lsr/argument_validator.py index 45a9927..b62457b 100644 --- a/module_utils/network_lsr/argument_validator.py +++ b/module_utils/network_lsr/argument_validator.py @@ -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) diff --git a/tests/unit/test_network_connections.py b/tests/unit/test_network_connections.py index f7ec527..0d8bd5d 100644 --- a/tests/unit/test_network_connections.py +++ b/tests/unit/test_network_connections.py @@ -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):