mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-24 11:21:25 -07:00
Fix for persistent connection plugin on Python3 (#24431)
Fix for persistent connection plugin on Python3. Note that fixes are also needed to each terminal plugin. This PR only fixes the ios terminal (as proof that this approach is workable.) Future PRs can address the other terminal types. * On Python3, pickle needs to work with byte strings, not text strings. * Set the pickle protocol version to 0 because we're using a pty to feed data to the connection plugin. A pty can't have control characters. So we have to send ascii only. That means only using protocol=0 for pickling the data. * ansible-connection isn't being used with py3 in the bug but it needs several changes to work with python3. * In python3, closing the pty too early causes no data to be sent. So leave stdin open until after we finish with the ansible-connection process. * Fix typo using traceback.format_exc() * Cleanup unnecessary StringIO, BytesIO, and to_bytes calls * Modify the network_cli and terminal plugins for py3 compat. Lots of mixing of text and byte strings that needs to be straightened out to be compatible with python3 * Documentation for the bytes<=>text strategy for terminal plugins * Update unittests for more bytes-oriented internals Fixes #24355
This commit is contained in:
parent
e539726543
commit
d834412ead
6 changed files with 146 additions and 112 deletions
|
@ -117,21 +117,21 @@ class TestConnectionClass(unittest.TestCase):
|
|||
mock_open_shell = MagicMock()
|
||||
conn.open_shell = mock_open_shell
|
||||
|
||||
mock_send = MagicMock(return_value='command response')
|
||||
mock_send = MagicMock(return_value=b'command response')
|
||||
conn.send = mock_send
|
||||
|
||||
# test sending a single command and converting to dict
|
||||
rc, out, err = conn.exec_command('command')
|
||||
self.assertEqual(out, 'command response')
|
||||
self.assertEqual(out, b'command response')
|
||||
self.assertTrue(mock_open_shell.called)
|
||||
mock_send.assert_called_with({'command': 'command'})
|
||||
mock_send.assert_called_with({'command': b'command'})
|
||||
|
||||
mock_open_shell.reset_mock()
|
||||
|
||||
# test sending a json string
|
||||
rc, out, err = conn.exec_command(json.dumps({'command': 'command'}))
|
||||
self.assertEqual(out, 'command response')
|
||||
mock_send.assert_called_with({'command': 'command'})
|
||||
self.assertEqual(out, b'command response')
|
||||
mock_send.assert_called_with({'command': b'command'})
|
||||
self.assertTrue(mock_open_shell.called)
|
||||
|
||||
mock_open_shell.reset_mock()
|
||||
|
@ -139,9 +139,9 @@ class TestConnectionClass(unittest.TestCase):
|
|||
|
||||
# test _shell already open
|
||||
rc, out, err = conn.exec_command('command')
|
||||
self.assertEqual(out, 'command response')
|
||||
self.assertEqual(out, b'command response')
|
||||
self.assertFalse(mock_open_shell.called)
|
||||
mock_send.assert_called_with({'command': 'command'})
|
||||
mock_send.assert_called_with({'command': b'command'})
|
||||
|
||||
|
||||
def test_network_cli_send(self):
|
||||
|
@ -150,14 +150,14 @@ class TestConnectionClass(unittest.TestCase):
|
|||
conn = network_cli.Connection(pc, new_stdin)
|
||||
|
||||
mock__terminal = MagicMock()
|
||||
mock__terminal.terminal_stdout_re = [re.compile('device#')]
|
||||
mock__terminal.terminal_stderr_re = [re.compile('^ERROR')]
|
||||
mock__terminal.terminal_stdout_re = [re.compile(b'device#')]
|
||||
mock__terminal.terminal_stderr_re = [re.compile(b'^ERROR')]
|
||||
conn._terminal = mock__terminal
|
||||
|
||||
mock__shell = MagicMock()
|
||||
conn._shell = mock__shell
|
||||
|
||||
response = """device#command
|
||||
response = b"""device#command
|
||||
command response
|
||||
|
||||
device#
|
||||
|
@ -165,15 +165,15 @@ class TestConnectionClass(unittest.TestCase):
|
|||
|
||||
mock__shell.recv.return_value = response
|
||||
|
||||
output = conn.send({'command': 'command'})
|
||||
output = conn.send({'command': b'command'})
|
||||
|
||||
mock__shell.sendall.assert_called_with('command\r')
|
||||
self.assertEqual(output, 'command response')
|
||||
mock__shell.sendall.assert_called_with(b'command\r')
|
||||
self.assertEqual(output, b'command response')
|
||||
|
||||
mock__shell.reset_mock()
|
||||
mock__shell.recv.return_value = "ERROR: error message"
|
||||
mock__shell.recv.return_value = b"ERROR: error message"
|
||||
|
||||
with self.assertRaises(AnsibleConnectionFailure) as exc:
|
||||
conn.send({'command': 'command'})
|
||||
conn.send({'command': b'command'})
|
||||
self.assertEqual(str(exc.exception), 'ERROR: error message')
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue