Connection plugins network_cli and netconf (#32521)

* implements jsonrpc message passing for ansible-connection

* implements more generic mechanism for persistent connections
* starts persistent connection in task_executor if enabled and supported
* supports using network_cli as top level connection plugin
* enhances logging for persistent connection to stdout

* Update action plugins

* Fix Python3 RPC

* Fix Junos bytes<-->str issues

* supports using netconf as top level connection plugin

* Error message when running netconf on an unsupported platform
* Update tests

* Fix `authorize: yes` for `connection: local`

* Handle potentially JSON data in terminal

* Add clarifying detail if possible on ConnectionError
This commit is contained in:
Nathaniel Case 2017-11-09 15:04:40 -05:00 committed by GitHub
parent 897b31f249
commit 9c0275a879
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 722 additions and 798 deletions

View file

@ -35,7 +35,7 @@ from ansible.plugins.connection import network_cli
class TestConnectionClass(unittest.TestCase):
@patch("ansible.plugins.connection.network_cli._Connection._connect")
@patch("ansible.plugins.connection.network_cli.ParamikoSshConnection._connect")
def test_network_cli__connect_error(self, mocked_super):
pc = PlayContext()
new_stdin = StringIO()
@ -47,7 +47,7 @@ class TestConnectionClass(unittest.TestCase):
pc.network_os = None
self.assertRaises(AnsibleConnectionFailure, conn._connect)
@patch("ansible.plugins.connection.network_cli._Connection._connect")
@patch("ansible.plugins.connection.network_cli.ParamikoSshConnection._connect")
def test_network_cli__invalid_os(self, mocked_super):
pc = PlayContext()
new_stdin = StringIO()
@ -60,7 +60,7 @@ class TestConnectionClass(unittest.TestCase):
self.assertRaises(AnsibleConnectionFailure, conn._connect)
@patch("ansible.plugins.connection.network_cli.terminal_loader")
@patch("ansible.plugins.connection.network_cli._Connection._connect")
@patch("ansible.plugins.connection.network_cli.ParamikoSshConnection._connect")
def test_network_cli__connect(self, mocked_super, mocked_terminal_loader):
pc = PlayContext()
new_stdin = StringIO()
@ -70,22 +70,21 @@ class TestConnectionClass(unittest.TestCase):
conn.ssh = MagicMock()
conn.receive = MagicMock()
mock_terminal = MagicMock()
conn._terminal = mock_terminal
conn._terminal = MagicMock()
conn._connect()
self.assertTrue(conn._terminal.on_open_shell.called)
self.assertFalse(conn._terminal.on_authorize.called)
conn._play_context.become = True
conn._play_context.become_method = 'enable'
conn._play_context.become_pass = 'password'
conn._connected = False
conn._connect()
conn._terminal.on_authorize.assert_called_with(passwd='password')
@patch("ansible.plugins.connection.network_cli._Connection.close")
@patch("ansible.plugins.connection.network_cli.ParamikoSshConnection.close")
def test_network_cli_close(self, mocked_super):
pc = PlayContext()
new_stdin = StringIO()
@ -93,20 +92,14 @@ class TestConnectionClass(unittest.TestCase):
terminal = MagicMock(supports_multiplexing=False)
conn._terminal = terminal
conn.close()
conn._shell = MagicMock()
conn._ssh_shell = MagicMock()
conn._connected = True
conn.close()
self.assertTrue(terminal.on_close_shell.called)
self.assertIsNone(conn._ssh_shell)
terminal.supports_multiplexing = True
conn.close()
self.assertIsNone(conn._shell)
@patch("ansible.plugins.connection.network_cli._Connection._connect")
@patch("ansible.plugins.connection.network_cli.ParamikoSshConnection._connect")
def test_network_cli_exec_command(self, mocked_super):
pc = PlayContext()
new_stdin = StringIO()
@ -114,23 +107,17 @@ class TestConnectionClass(unittest.TestCase):
mock_send = MagicMock(return_value=b'command response')
conn.send = mock_send
conn._ssh_shell = MagicMock()
# test sending a single command and converting to dict
rc, out, err = conn.exec_command('command')
out = conn.exec_command('command')
self.assertEqual(out, b'command response')
mock_send.assert_called_with(b'command', None, None, None)
mock_send.assert_called_with(command=b'command')
# test sending a json string
rc, out, err = conn.exec_command(json.dumps({'command': 'command'}))
out = conn.exec_command(json.dumps({'command': 'command'}))
self.assertEqual(out, b'command response')
mock_send.assert_called_with(b'command', None, None, None)
conn._shell = MagicMock()
# test _shell already open
rc, out, err = conn.exec_command('command')
self.assertEqual(out, b'command response')
mock_send.assert_called_with(b'command', None, None, None)
mock_send.assert_called_with(command=b'command')
def test_network_cli_send(self):
pc = PlayContext()
@ -142,7 +129,7 @@ class TestConnectionClass(unittest.TestCase):
conn._terminal = mock__terminal
mock__shell = MagicMock()
conn._shell = mock__shell
conn._ssh_shell = mock__shell
response = b"""device#command
command response
@ -155,7 +142,7 @@ class TestConnectionClass(unittest.TestCase):
output = conn.send(b'command', None, None, None)
mock__shell.sendall.assert_called_with(b'command\r')
self.assertEqual(output, b'command response')
self.assertEqual(output, 'command response')
mock__shell.reset_mock()
mock__shell.recv.return_value = b"ERROR: error message device#"