issue #436: fix string parsing of mitogen_ssh_debug_level

It can be a string when specified on the command line.
issue510
David Wilson 6 years ago
parent 9074ddf69f
commit 22de7f0e72

@ -64,6 +64,17 @@ def optional_secret(value):
return mitogen.core.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): def parse_python_path(s):
""" """
Given the string set for ansible_python_interpeter, parse it using shell 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': 'mitogen_machinectl_path':
connection.get_task_var('mitogen_machinectl_path'), connection.get_task_var('mitogen_machinectl_path'),
'mitogen_ssh_debug_level': 'mitogen_ssh_debug_level':
connection.get_task_var('mitogen_ssh_debug_level'), optional_int(
connection.get_task_var('mitogen_ssh_debug_level')
),
'extra_args': 'extra_args':
connection.get_extra_args(), connection.get_extra_args(),
} }

@ -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()
Loading…
Cancel
Save