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
This commit is contained in:
Nathaniel Case 2019-08-27 14:33:05 -04:00 committed by GitHub
parent 36f4af203c
commit e8586114ad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 16 deletions

View file

@ -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

View file

@ -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):
pc = PlayContext()
pc.network_os = 'ios'
conn = connection_loader.get('network_cli', pc, '/dev/null')
conn.ssh = MagicMock()
conn.receive = MagicMock()
conn._network_os = 'does not exist'
self.assertRaises(AnsibleConnectionFailure, conn._connect)
def test_network_cli__invalid_os(self):
pc = PlayContext()
pc.network_os = 'does not exist'
self.assertRaises(AnsibleConnectionFailure, connection_loader.get, 'network_cli', pc, '/dev/null')
def test_network_cli__no_os(self):
pc = PlayContext()
pc.network_os = None