summaryrefslogtreecommitdiff
path: root/test/units/plugins/connection
diff options
context:
space:
mode:
authorToshio Kuratomi <a.badger@gmail.com>2017-05-12 09:13:51 -0700
committerGitHub <noreply@github.com>2017-05-12 09:13:51 -0700
commitd834412eadccca6428501544f398d1bb59084e98 (patch)
tree1fd531d4db0ee896eac0955095e06217dd2617a0 /test/units/plugins/connection
parente539726543954c5b6b8067ef3946ab2020bb2a2f (diff)
downloadansible-d834412eadccca6428501544f398d1bb59084e98.tar.gz
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
Diffstat (limited to 'test/units/plugins/connection')
-rw-r--r--test/units/plugins/connection/test_network_cli.py30
1 files changed, 15 insertions, 15 deletions
diff --git a/test/units/plugins/connection/test_network_cli.py b/test/units/plugins/connection/test_network_cli.py
index 818376a2eb..b9e4c8e40e 100644
--- a/test/units/plugins/connection/test_network_cli.py
+++ b/test/units/plugins/connection/test_network_cli.py
@@ -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')