2016-12-04 02:38:41 +01:00
|
|
|
#
|
|
|
|
# (c) 2016 Red Hat Inc.
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# Make coding more python3-ish
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
import re
|
2018-10-22 17:35:15 +02:00
|
|
|
import time
|
2016-12-04 02:38:41 +01:00
|
|
|
import json
|
|
|
|
|
|
|
|
from io import StringIO
|
|
|
|
|
|
|
|
from ansible.compat.tests import unittest
|
|
|
|
from ansible.compat.tests.mock import patch, MagicMock
|
|
|
|
|
2018-10-22 17:35:15 +02:00
|
|
|
from ansible.module_utils._text import to_text
|
2016-12-04 02:38:41 +01:00
|
|
|
from ansible.errors import AnsibleConnectionFailure
|
|
|
|
from ansible.playbook.play_context import PlayContext
|
|
|
|
from ansible.plugins.connection import network_cli
|
2018-05-16 14:59:01 +02:00
|
|
|
from ansible.plugins.loader import connection_loader
|
2016-12-04 02:38:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestConnectionClass(unittest.TestCase):
|
|
|
|
|
2017-11-16 19:49:57 +01:00
|
|
|
@patch("ansible.plugins.connection.paramiko_ssh.Connection._connect")
|
2017-06-06 10:26:25 +02:00
|
|
|
def test_network_cli__connect_error(self, mocked_super):
|
2016-12-04 02:38:41 +01:00
|
|
|
pc = PlayContext()
|
|
|
|
new_stdin = StringIO()
|
|
|
|
|
2018-05-16 14:59:01 +02:00
|
|
|
conn = connection_loader.get('network_cli', pc, '/dev/null')
|
2017-06-06 10:26:25 +02:00
|
|
|
conn.ssh = MagicMock()
|
|
|
|
conn.receive = MagicMock()
|
|
|
|
conn._terminal = MagicMock()
|
|
|
|
pc.network_os = None
|
2016-12-04 02:38:41 +01:00
|
|
|
self.assertRaises(AnsibleConnectionFailure, conn._connect)
|
|
|
|
|
2017-11-16 19:49:57 +01:00
|
|
|
@patch("ansible.plugins.connection.paramiko_ssh.Connection._connect")
|
2017-06-06 10:26:25 +02:00
|
|
|
def test_network_cli__invalid_os(self, mocked_super):
|
|
|
|
pc = PlayContext()
|
|
|
|
new_stdin = StringIO()
|
2016-12-04 02:38:41 +01:00
|
|
|
|
2018-05-16 14:59:01 +02:00
|
|
|
conn = connection_loader.get('network_cli', pc, '/dev/null')
|
2017-06-06 10:26:25 +02:00
|
|
|
conn.ssh = MagicMock()
|
|
|
|
conn.receive = MagicMock()
|
|
|
|
conn._terminal = MagicMock()
|
|
|
|
pc.network_os = None
|
2016-12-04 02:38:41 +01:00
|
|
|
self.assertRaises(AnsibleConnectionFailure, conn._connect)
|
|
|
|
|
2017-06-06 10:26:25 +02:00
|
|
|
@patch("ansible.plugins.connection.network_cli.terminal_loader")
|
2017-11-16 19:49:57 +01:00
|
|
|
@patch("ansible.plugins.connection.paramiko_ssh.Connection._connect")
|
2017-06-06 10:26:25 +02:00
|
|
|
def test_network_cli__connect(self, mocked_super, mocked_terminal_loader):
|
2016-12-04 02:38:41 +01:00
|
|
|
pc = PlayContext()
|
2018-07-02 14:41:00 +02:00
|
|
|
pc.network_os = 'ios'
|
2016-12-04 02:38:41 +01:00
|
|
|
new_stdin = StringIO()
|
2017-06-06 10:26:25 +02:00
|
|
|
|
2018-05-16 14:59:01 +02:00
|
|
|
conn = connection_loader.get('network_cli', pc, '/dev/null')
|
2016-12-04 02:38:41 +01:00
|
|
|
|
|
|
|
conn.ssh = MagicMock()
|
|
|
|
conn.receive = MagicMock()
|
2017-11-09 21:04:40 +01:00
|
|
|
conn._terminal = MagicMock()
|
2016-12-06 03:42:09 +01:00
|
|
|
|
2017-06-06 10:26:25 +02:00
|
|
|
conn._connect()
|
|
|
|
self.assertTrue(conn._terminal.on_open_shell.called)
|
2017-11-28 22:28:58 +01:00
|
|
|
self.assertFalse(conn._terminal.on_become.called)
|
2016-12-04 02:38:41 +01:00
|
|
|
|
|
|
|
conn._play_context.become = True
|
2017-11-09 21:04:40 +01:00
|
|
|
conn._play_context.become_method = 'enable'
|
2016-12-04 02:38:41 +01:00
|
|
|
conn._play_context.become_pass = 'password'
|
2017-11-09 21:04:40 +01:00
|
|
|
conn._connected = False
|
2016-12-04 02:38:41 +01:00
|
|
|
|
2017-06-06 10:26:25 +02:00
|
|
|
conn._connect()
|
2017-11-28 22:28:58 +01:00
|
|
|
conn._terminal.on_become.assert_called_with(passwd='password')
|
2016-12-04 02:38:41 +01:00
|
|
|
|
2017-11-16 19:49:57 +01:00
|
|
|
@patch("ansible.plugins.connection.paramiko_ssh.Connection.close")
|
2017-06-06 10:26:25 +02:00
|
|
|
def test_network_cli_close(self, mocked_super):
|
2016-12-04 02:38:41 +01:00
|
|
|
pc = PlayContext()
|
|
|
|
new_stdin = StringIO()
|
|
|
|
conn = network_cli.Connection(pc, new_stdin)
|
|
|
|
|
|
|
|
terminal = MagicMock(supports_multiplexing=False)
|
|
|
|
conn._terminal = terminal
|
2017-11-09 21:04:40 +01:00
|
|
|
conn._ssh_shell = MagicMock()
|
2018-03-16 18:28:04 +01:00
|
|
|
conn.paramiko_conn = MagicMock()
|
2017-11-09 21:04:40 +01:00
|
|
|
conn._connected = True
|
2016-12-04 02:38:41 +01:00
|
|
|
|
2017-06-06 10:26:25 +02:00
|
|
|
conn.close()
|
2016-12-04 02:38:41 +01:00
|
|
|
self.assertTrue(terminal.on_close_shell.called)
|
2017-11-09 21:04:40 +01:00
|
|
|
self.assertIsNone(conn._ssh_shell)
|
2018-03-16 18:28:04 +01:00
|
|
|
self.assertIsNone(conn.paramiko_conn)
|
2016-12-04 02:38:41 +01:00
|
|
|
|
2017-11-16 19:49:57 +01:00
|
|
|
@patch("ansible.plugins.connection.paramiko_ssh.Connection._connect")
|
2017-06-06 10:26:25 +02:00
|
|
|
def test_network_cli_exec_command(self, mocked_super):
|
2016-12-04 02:38:41 +01:00
|
|
|
pc = PlayContext()
|
|
|
|
new_stdin = StringIO()
|
|
|
|
conn = network_cli.Connection(pc, new_stdin)
|
|
|
|
|
2017-05-12 18:13:51 +02:00
|
|
|
mock_send = MagicMock(return_value=b'command response')
|
2016-12-04 02:38:41 +01:00
|
|
|
conn.send = mock_send
|
2017-11-09 21:04:40 +01:00
|
|
|
conn._ssh_shell = MagicMock()
|
2016-12-04 02:38:41 +01:00
|
|
|
|
|
|
|
# test sending a single command and converting to dict
|
2017-11-09 21:04:40 +01:00
|
|
|
out = conn.exec_command('command')
|
2017-05-12 18:13:51 +02:00
|
|
|
self.assertEqual(out, b'command response')
|
2017-11-09 21:04:40 +01:00
|
|
|
mock_send.assert_called_with(command=b'command')
|
2016-12-04 02:38:41 +01:00
|
|
|
|
|
|
|
# test sending a json string
|
2017-11-09 21:04:40 +01:00
|
|
|
out = conn.exec_command(json.dumps({'command': 'command'}))
|
2017-05-12 18:13:51 +02:00
|
|
|
self.assertEqual(out, b'command response')
|
2017-11-09 21:04:40 +01:00
|
|
|
mock_send.assert_called_with(command=b'command')
|
2016-12-04 02:38:41 +01:00
|
|
|
|
|
|
|
def test_network_cli_send(self):
|
|
|
|
pc = PlayContext()
|
|
|
|
new_stdin = StringIO()
|
|
|
|
conn = network_cli.Connection(pc, new_stdin)
|
|
|
|
mock__terminal = MagicMock()
|
2017-05-12 18:13:51 +02:00
|
|
|
mock__terminal.terminal_stdout_re = [re.compile(b'device#')]
|
|
|
|
mock__terminal.terminal_stderr_re = [re.compile(b'^ERROR')]
|
2016-12-04 02:38:41 +01:00
|
|
|
conn._terminal = mock__terminal
|
|
|
|
|
|
|
|
mock__shell = MagicMock()
|
2017-11-09 21:04:40 +01:00
|
|
|
conn._ssh_shell = mock__shell
|
2016-12-04 02:38:41 +01:00
|
|
|
|
2017-05-12 18:13:51 +02:00
|
|
|
response = b"""device#command
|
2016-12-04 02:38:41 +01:00
|
|
|
command response
|
|
|
|
|
|
|
|
device#
|
|
|
|
"""
|
|
|
|
|
2018-10-22 17:35:15 +02:00
|
|
|
mock__shell.recv.side_effect = [response, None]
|
|
|
|
|
|
|
|
conn.get_option = MagicMock()
|
|
|
|
conn.get_option.return_value = 1
|
2018-10-23 03:01:56 +02:00
|
|
|
|
2017-06-06 10:26:25 +02:00
|
|
|
output = conn.send(b'command', None, None, None)
|
2016-12-04 02:38:41 +01:00
|
|
|
|
2017-05-12 18:13:51 +02:00
|
|
|
mock__shell.sendall.assert_called_with(b'command\r')
|
2018-10-22 17:35:15 +02:00
|
|
|
self.assertEqual(to_text(conn._command_response), 'command response')
|
2016-12-04 02:38:41 +01:00
|
|
|
|
|
|
|
mock__shell.reset_mock()
|
2018-10-22 17:35:15 +02:00
|
|
|
mock__shell.recv.side_effect = [b"ERROR: error message device#"]
|
2016-12-04 02:38:41 +01:00
|
|
|
|
|
|
|
with self.assertRaises(AnsibleConnectionFailure) as exc:
|
2017-06-06 10:26:25 +02:00
|
|
|
conn.send(b'command', None, None, None)
|
2017-05-24 16:10:38 +02:00
|
|
|
self.assertEqual(str(exc.exception), 'ERROR: error message device#')
|