From e8586114ad3f73172eebd4b923b48eabc25e9cbb Mon Sep 17 00:00:00 2001 From: Nathaniel Case Date: Tue, 27 Aug 2019 14:33:05 -0400 Subject: [PATCH] Move instantiation of terminal plugin earlier (#61261) * Move instantiation of terminal plugin earlier * Move test for invalid network_os to where the failure now lives --- lib/ansible/plugins/connection/network_cli.py | 7 +++---- test/units/plugins/connection/test_network_cli.py | 14 ++++---------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/lib/ansible/plugins/connection/network_cli.py b/lib/ansible/plugins/connection/network_cli.py index aa97e5d204e..6959ec31e60 100644 --- a/lib/ansible/plugins/connection/network_cli.py +++ b/lib/ansible/plugins/connection/network_cli.py @@ -302,6 +302,9 @@ class Connection(NetworkConnectionBase): logging.getLogger('paramiko').setLevel(logging.DEBUG) if self._network_os: + self._terminal = terminal_loader.get(self._network_os, self) + if not self._terminal: + raise AnsibleConnectionFailure('network os %s is not supported' % self._network_os) self.cliconf = cliconf_loader.get(self._network_os, self) if self.cliconf: @@ -391,10 +394,6 @@ class Connection(NetworkConnectionBase): self._ssh_shell = ssh.ssh.invoke_shell() self._ssh_shell.settimeout(self.get_option('persistent_command_timeout')) - self._terminal = terminal_loader.get(self._network_os, self) - if not self._terminal: - raise AnsibleConnectionFailure('network os %s is not supported' % self._network_os) - self.queue_message('vvvv', 'loaded terminal plugin for network_os %s' % self._network_os) terminal_initial_prompt = self.get_option('terminal_initial_prompt') or self._terminal.terminal_initial_prompt diff --git a/test/units/plugins/connection/test_network_cli.py b/test/units/plugins/connection/test_network_cli.py index bbb40676f8e..a625449a72e 100644 --- a/test/units/plugins/connection/test_network_cli.py +++ b/test/units/plugins/connection/test_network_cli.py @@ -34,19 +34,13 @@ from ansible.plugins.loader import connection_loader class TestConnectionClass(unittest.TestCase): - @patch("ansible.plugins.connection.paramiko_ssh.Connection._connect") - def test_network_cli__connect_error(self, mocked_super): + def test_network_cli__invalid_os(self): pc = PlayContext() - pc.network_os = 'ios' - conn = connection_loader.get('network_cli', pc, '/dev/null') + pc.network_os = 'does not exist' - conn.ssh = MagicMock() - conn.receive = MagicMock() - conn._network_os = 'does not exist' - - self.assertRaises(AnsibleConnectionFailure, conn._connect) + self.assertRaises(AnsibleConnectionFailure, connection_loader.get, 'network_cli', pc, '/dev/null') - def test_network_cli__invalid_os(self): + def test_network_cli__no_os(self): pc = PlayContext() pc.network_os = None