From 22de7f0e72c6c81eb0c15dd6a54da21d22ba5365 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Sat, 8 Dec 2018 16:07:41 +0000 Subject: [PATCH] issue #436: fix string parsing of mitogen_ssh_debug_level It can be a string when specified on the command line. --- ansible_mitogen/connection.py | 15 +++++++++- tests/ansible/tests/connection_test.py | 38 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 tests/ansible/tests/connection_test.py diff --git a/ansible_mitogen/connection.py b/ansible_mitogen/connection.py index 569a5ad8..4b648d4e 100644 --- a/ansible_mitogen/connection.py +++ b/ansible_mitogen/connection.py @@ -64,6 +64,17 @@ def optional_secret(value): return mitogen.core.Secret(value) +def optional_int(value): + """ + Convert `value` to an integer if it is not :data:`None`, otherwise return + :data:`None`. + """ + try: + return int(value) + except (TypeError, ValueError): + return None + + def parse_python_path(s): """ Given the string set for ansible_python_interpeter, parse it using shell @@ -406,7 +417,9 @@ def config_from_play_context(transport, inventory_name, connection): 'mitogen_machinectl_path': connection.get_task_var('mitogen_machinectl_path'), 'mitogen_ssh_debug_level': - connection.get_task_var('mitogen_ssh_debug_level'), + optional_int( + connection.get_task_var('mitogen_ssh_debug_level') + ), 'extra_args': connection.get_extra_args(), } diff --git a/tests/ansible/tests/connection_test.py b/tests/ansible/tests/connection_test.py new file mode 100644 index 00000000..33b60695 --- /dev/null +++ b/tests/ansible/tests/connection_test.py @@ -0,0 +1,38 @@ + +from __future__ import absolute_import +import os.path +import subprocess +import tempfile +import unittest2 + +import mock + +import ansible_mitogen.connection +import testlib + + +LOGGER_NAME = ansible_mitogen.target.LOG.name + + +class OptionalIntTest(unittest2.TestCase): + func = staticmethod(ansible_mitogen.connection.optional_int) + + def test_already_int(self): + self.assertEquals(0, self.func(0)) + self.assertEquals(1, self.func(1)) + self.assertEquals(-1, self.func(-1)) + + def test_is_string(self): + self.assertEquals(0, self.func("0")) + self.assertEquals(1, self.func("1")) + self.assertEquals(-1, self.func("-1")) + + def test_is_none(self): + self.assertEquals(None, self.func(None)) + + def test_is_junk(self): + self.assertEquals(None, self.func({1:2})) + + +if __name__ == '__main__': + unittest2.main()