1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Python 2: accept both long and int for type=int (module options) (#53289)

* Added unit test
This commit is contained in:
Felix Fontein 2019-03-05 15:39:03 +01:00 committed by Sam Doran
parent 1112e1d0da
commit 07fcb60d55
3 changed files with 13 additions and 5 deletions

View file

@ -0,0 +1,3 @@
bugfixes:
- "If large integers are passed as options to modules under Python 2, module argument
parsing will reject them as they are of type ``long`` and not of type ``int``."

View file

@ -1864,7 +1864,7 @@ class AnsibleModule(object):
raise TypeError('%s cannot be converted to a bool' % type(value)) raise TypeError('%s cannot be converted to a bool' % type(value))
def _check_type_int(self, value): def _check_type_int(self, value):
if isinstance(value, int): if isinstance(value, integer_types):
return value return value
if isinstance(value, string_types): if isinstance(value, string_types):

View file

@ -14,7 +14,7 @@ import pytest
from units.compat.mock import MagicMock, patch from units.compat.mock import MagicMock, patch
from ansible.module_utils import basic from ansible.module_utils import basic
from ansible.module_utils.six import string_types from ansible.module_utils.six import string_types, integer_types
from ansible.module_utils.six.moves import builtins from ansible.module_utils.six.moves import builtins
from units.mock.procenv import ModuleTestCase, swap_stdin_and_argv from units.mock.procenv import ModuleTestCase, swap_stdin_and_argv
@ -24,6 +24,8 @@ MOCK_VALIDATOR_FAIL = MagicMock(side_effect=TypeError("bad conversion"))
VALID_SPECS = ( VALID_SPECS = (
# Simple type=int # Simple type=int
({'arg': {'type': 'int'}}, {'arg': 42}, 42), ({'arg': {'type': 'int'}}, {'arg': 42}, 42),
# Simple type=int with a large value (will be of type long under Python 2)
({'arg': {'type': 'int'}}, {'arg': 18765432109876543210}, 18765432109876543210),
# Simple type=list, elements=int # Simple type=list, elements=int
({'arg': {'type': 'list', 'elements': 'int'}}, {'arg': [42, 32]}, [42, 32]), ({'arg': {'type': 'list', 'elements': 'int'}}, {'arg': [42, 32]}, [42, 32]),
# Type=int with conversion from string # Type=int with conversion from string
@ -183,6 +185,9 @@ def test_validator_basic_types(argspec, expected, stdin):
am = basic.AnsibleModule(argspec) am = basic.AnsibleModule(argspec)
if 'type' in argspec['arg']: if 'type' in argspec['arg']:
if argspec['arg']['type'] == 'int':
type_ = integer_types
else:
type_ = getattr(builtins, argspec['arg']['type']) type_ = getattr(builtins, argspec['arg']['type'])
else: else:
type_ = str type_ = str
@ -191,14 +196,14 @@ def test_validator_basic_types(argspec, expected, stdin):
assert am.params['arg'] == expected assert am.params['arg'] == expected
@pytest.mark.parametrize('stdin', [{'arg': 42}], indirect=['stdin']) @pytest.mark.parametrize('stdin', [{'arg': 42}, {'arg': 18765432109876543210}], indirect=['stdin'])
def test_validator_function(mocker, stdin): def test_validator_function(mocker, stdin):
# Type is a callable # Type is a callable
MOCK_VALIDATOR_SUCCESS = mocker.MagicMock(return_value=27) MOCK_VALIDATOR_SUCCESS = mocker.MagicMock(return_value=27)
argspec = {'arg': {'type': MOCK_VALIDATOR_SUCCESS}} argspec = {'arg': {'type': MOCK_VALIDATOR_SUCCESS}}
am = basic.AnsibleModule(argspec) am = basic.AnsibleModule(argspec)
assert isinstance(am.params['arg'], int) assert isinstance(am.params['arg'], integer_types)
assert am.params['arg'] == 27 assert am.params['arg'] == 27