add back reverted change to network_cli (#18761)

This adds back the change to the network_cli plugin.  Ths change adds
the ensure_connect decorator to the open_shell() method to make sure
the connection is valid before trying to open a shell.

The issue was due to the addition of the decorator that will call
_connect() when there is no connection.  The _connect() method should
have been mocked in the test case.  This commit fixes the test
case as well

Change was originally reverted in c414ded69a
This commit is contained in:
Peter Sprygada 2016-12-05 21:42:09 -05:00 committed by GitHub
parent 14a2757116
commit dc23667cc2
2 changed files with 48 additions and 14 deletions

View file

@ -33,7 +33,6 @@ from ansible.playbook.play_context import PlayContext
from ansible.plugins.connection import network_cli
class TestConnectionClass(unittest.TestCase):
@patch("ansible.plugins.connection.network_cli.terminal_loader")
@ -69,21 +68,27 @@ class TestConnectionClass(unittest.TestCase):
conn.ssh = MagicMock()
conn.receive = MagicMock()
terminal = MagicMock()
conn._terminal = terminal
mock_terminal = MagicMock()
conn._terminal = mock_terminal
mock__connect = MagicMock()
conn._connect = mock__connect
conn.open_shell()
self.assertTrue(terminal.on_open_shell.called)
self.assertFalse(terminal.on_authorize.called)
self.assertTrue(mock__connect.called)
self.assertTrue(mock_terminal.on_open_shell.called)
self.assertFalse(mock_terminal.on_authorize.called)
mock_terminal.reset_mock()
terminal.reset_mock()
conn._play_context.become = True
conn._play_context.become_pass = 'password'
conn.open_shell()
terminal.on_authorize.assert_called_with(passwd='password')
self.assertTrue(mock__connect.called)
mock_terminal.on_authorize.assert_called_with(passwd='password')
def test_network_cli_close_shell(self):
pc = PlayContext()