| 1 |
import socket |
| 2 |
import sys |
| 3 |
|
| 4 |
try: |
| 5 |
import unittest2 as unittest |
| 6 |
except ImportError: |
| 7 |
import unittest |
| 8 |
import nose2 |
| 9 |
from unittest.mock import MagicMock, patch |
| 10 |
from jnpr.junos.transport.tty_ssh import SSH |
| 11 |
|
| 12 |
|
| 13 |
class TestTTYSSH(unittest.TestCase): |
| 14 |
@patch("jnpr.junos.transport.tty_ssh.paramiko") |
| 15 |
def setUp(self, mock_paramiko): |
| 16 |
self.ssh_conn = SSH( |
| 17 |
host="1.1.1.1", user="test", password="password123", port=3007, timeout=30 |
| 18 |
) |
| 19 |
self._ssh = mock_paramiko.invoke_shell |
| 20 |
|
| 21 |
def test_open(self): |
| 22 |
self.ssh_conn._tty_open() |
| 23 |
self.ssh_conn._ssh_pre.connect.assert_called() |
| 24 |
|
| 25 |
def test_open_exception(self): |
| 26 |
self.ssh_conn._ssh_pre.connect.side_effect = socket.error |
| 27 |
SSH.RETRY_OPEN = 1 |
| 28 |
SSH.RETRY_BACKOFF = 0.1 |
| 29 |
self.assertRaises(RuntimeError, self.ssh_conn._tty_open) |
| 30 |
# reset |
| 31 |
SSH.RETRY_OPEN = 3 |
| 32 |
SSH.RETRY_BACKOFF = 2 |
| 33 |
|
| 34 |
def test_read(self): |
| 35 |
self.ssh_conn._tty_open() |
| 36 |
self.assertRaises(ValueError, self.ssh_conn.read) |
| 37 |
|
| 38 |
def test_close(self): |
| 39 |
self.assertEqual(self.ssh_conn._tty_close(), None) |
| 40 |
|
| 41 |
@patch("jnpr.junos.transport.tty_ssh.paramiko") |
| 42 |
def test_tty_ssh_baud(self, mock_paramiko): |
| 43 |
self.ssh_conn = SSH( |
| 44 |
host="1.1.1.1", |
| 45 |
user="test", |
| 46 |
password="password123", |
| 47 |
port=3007, |
| 48 |
timeout=30, |
| 49 |
baud=0, |
| 50 |
) |
| 51 |
|
| 52 |
self.ssh_conn._tty_open() |
| 53 |
self.ssh_conn.rawwrite("<rpc>") |
| 54 |
self.ssh_conn._ssh.sendall.assert_called_with("<rpc>") |
| 55 |
|
| 56 |
def test_tty_ssh_rawwrite_sys_py3(self): |
| 57 |
with patch.object(sys.modules["sys"], "version", "3.x") as mock_sys: |
| 58 |
self.ssh_conn._tty_open() |
| 59 |
content = MagicMock() |
| 60 |
self.ssh_conn.rawwrite(content) |
| 61 |
content.decode.assert_called_with("utf-8") |