issue #436: fix string parsing of mitogen_ssh_debug_level

It can be a string when specified on the command line.
This commit is contained in:
David Wilson 2018-12-08 16:07:41 +00:00
parent 9074ddf69f
commit 22de7f0e72
2 changed files with 52 additions and 1 deletions

View File

@ -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(),
}

View File

@ -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()