| 1 |
try: |
| 2 |
import unittest2 as unittest |
| 3 |
except ImportError: |
| 4 |
import unittest |
| 5 |
from jnpr.junos.utils.config import Config |
| 6 |
import nose2 |
| 7 |
from unittest.mock import patch, MagicMock, call |
| 8 |
import re |
| 9 |
import sys |
| 10 |
import os |
| 11 |
from lxml import etree |
| 12 |
import six |
| 13 |
import socket |
| 14 |
|
| 15 |
from jnpr.junos.console import Console |
| 16 |
from jnpr.junos.transport.tty_netconf import tty_netconf |
| 17 |
from jnpr.junos.transport.tty_telnet import Terminal |
| 18 |
|
| 19 |
|
| 20 |
if sys.version < "3": |
| 21 |
builtin_string = "__builtin__" |
| 22 |
else: |
| 23 |
builtin_string = "builtins" |
| 24 |
|
| 25 |
|
| 26 |
class TestConsole(unittest.TestCase): |
| 27 |
@classmethod |
| 28 |
def setUpClass(cls): |
| 29 |
# Save object state |
| 30 |
cls.open = tty_netconf.open |
| 31 |
|
| 32 |
@classmethod |
| 33 |
def tearDownClass(cls): |
| 34 |
# Revert object state |
| 35 |
tty_netconf.open = cls.open |
| 36 |
|
| 37 |
@patch("jnpr.junos.transport.tty_telnet.Telnet._tty_open") |
| 38 |
@patch("jnpr.junos.transport.tty_telnet.telnetlib.Telnet.expect") |
| 39 |
@patch("jnpr.junos.transport.tty_telnet.Telnet.write") |
| 40 |
def setUp(self, mock_write, mock_expect, mock_open): |
| 41 |
tty_netconf.open = MagicMock() |
| 42 |
mock_expect.side_effect = [ |
| 43 |
(1, re.search(r"(?P<login>ogin:\s*$)", "login: "), six.b("\r\r\n ogin:")), |
| 44 |
( |
| 45 |
2, |
| 46 |
re.search(r"(?P<passwd>assword:\s*$)", "password: "), |
| 47 |
six.b("\r\r\n password:"), |
| 48 |
), |
| 49 |
( |
| 50 |
3, |
| 51 |
re.search(r"(?P<shell>%|#\s*$)", "junos % "), |
| 52 |
six.b("\r\r\nroot@device:~ # "), |
| 53 |
), |
| 54 |
] |
| 55 |
self.dev = Console(host="1.1.1.1", user="lab", password="lab123", mode="Telnet") |
| 56 |
self.dev.open() |
| 57 |
|
| 58 |
@patch("jnpr.junos.console.Console._tty_logout") |
| 59 |
def tearDown(self, mock_tty_logout): |
| 60 |
self.dev.close() |
| 61 |
|
| 62 |
def test_telnet_host_none(self): |
| 63 |
self.dev = Console(host=None, user="lab", password="lab123", mode="Telnet") |
| 64 |
self.assertTrue(self.dev.open()["failed"]) |
| 65 |
|
| 66 |
@patch("jnpr.junos.console.warnings") |
| 67 |
def test_telnet_old_fact_warning(self, mock_warn): |
| 68 |
self.dev = Console( |
| 69 |
host="1.1.1.1", |
| 70 |
user="lab", |
| 71 |
password="lab123", |
| 72 |
mode="Telnet", |
| 73 |
fact_style="old", |
| 74 |
) |
| 75 |
mock_warn.assert_has_calls( |
| 76 |
[ |
| 77 |
call.warn( |
| 78 |
"fact-style old will be removed in a future release.", |
| 79 |
RuntimeWarning, |
| 80 |
) |
| 81 |
] |
| 82 |
) |
| 83 |
|
| 84 |
@patch("jnpr.junos.transport.tty_telnet.Telnet._tty_open") |
| 85 |
@patch("jnpr.junos.transport.tty_telnet.telnetlib.Telnet.expect") |
| 86 |
@patch("jnpr.junos.transport.tty_telnet.Telnet.write") |
| 87 |
def test_login_bad_password(self, mock_write, mock_expect, mock_open): |
| 88 |
tty_netconf.open = MagicMock() |
| 89 |
mock_expect.side_effect = [ |
| 90 |
(1, re.search(r"(?P<login>ogin:\s*$)", "login: "), six.b("\r\r\n ogin:")), |
| 91 |
( |
| 92 |
2, |
| 93 |
re.search(r"(?P<passwd>assword:\s*$)", "password: "), |
| 94 |
six.b("\r\r\n password:"), |
| 95 |
), |
| 96 |
( |
| 97 |
3, |
| 98 |
re.search("(?P<badpasswd>ogin incorrect)", "login incorrect"), |
| 99 |
six.b("\r\r\nlogin incorrect"), |
| 100 |
), |
| 101 |
] |
| 102 |
self.dev = Console(host="1.1.1.1", user="lab", password="lab123", mode="Telnet") |
| 103 |
self.assertRaises(StopIteration, self.dev.open) |
| 104 |
|
| 105 |
@patch("jnpr.junos.console.Console._tty_logout") |
| 106 |
@patch("jnpr.junos.transport.tty_telnet.Telnet._tty_open") |
| 107 |
@patch("jnpr.junos.transport.tty_telnet.telnetlib.Telnet.expect") |
| 108 |
@patch("jnpr.junos.transport.tty_telnet.Telnet.write") |
| 109 |
def test_with_context(self, mock_write, mock_expect, mock_open, mock_logout): |
| 110 |
tty_netconf.open = MagicMock() |
| 111 |
|
| 112 |
mock_expect.side_effect = [ |
| 113 |
(1, re.search(r"(?P<login>ogin:\s*$)", "login: "), six.b("\r\r\n ogin:")), |
| 114 |
( |
| 115 |
2, |
| 116 |
re.search(r"(?P<passwd>assword:\s*$)", "password: "), |
| 117 |
six.b("\r\r\n password:"), |
| 118 |
), |
| 119 |
( |
| 120 |
3, |
| 121 |
re.search(r"(?P<shell>%|#\s*$)", "junos % "), |
| 122 |
six.b("\r\r\nroot@device:~ # "), |
| 123 |
), |
| 124 |
] |
| 125 |
with Console( |
| 126 |
host="1.1.1.1", user="lab", password="lab123", mode="Telnet" |
| 127 |
) as dev: |
| 128 |
self.assertTrue(isinstance(self.dev, Console)) |
| 129 |
|
| 130 |
@patch("jnpr.junos.console.Console._tty_login") |
| 131 |
def test_console_open_error(self, mock_tty_login): |
| 132 |
mock_tty_login.side_effect = RuntimeError |
| 133 |
self.assertRaises(RuntimeError, self.dev.open) |
| 134 |
|
| 135 |
def test_console_connected(self): |
| 136 |
self.assertTrue(self.dev.connected) |
| 137 |
self.assertFalse(self.dev._gather_facts) |
| 138 |
|
| 139 |
@patch("jnpr.junos.console.Console._tty_logout") |
| 140 |
def test_console_close_error(self, mock_logout): |
| 141 |
mock_logout.side_effect = RuntimeError |
| 142 |
self.assertRaises(RuntimeError, self.dev.close) |
| 143 |
|
| 144 |
@patch("jnpr.junos.console.Console._tty_logout") |
| 145 |
def test_console_close_socket_error(self, mock_logout): |
| 146 |
mock_logout.side_effect = socket.error |
| 147 |
self.assertRaises(socket.error, self.dev.close) |
| 148 |
|
| 149 |
@patch("jnpr.junos.console.Console._tty_logout") |
| 150 |
def test_console_close_socket_conn_reset(self, mock_logout): |
| 151 |
mock_logout.side_effect = socket.error("Connection reset by peer") |
| 152 |
self.dev.close() |
| 153 |
self.assertFalse(self.dev.connected) |
| 154 |
|
| 155 |
@patch("jnpr.junos.console.Console._tty_logout") |
| 156 |
def test_console_close_telnet_conn_closed(self, mock_logout): |
| 157 |
mock_logout.side_effect = EOFError("telnet connection closed") |
| 158 |
self.dev.close() |
| 159 |
self.assertFalse(self.dev.connected) |
| 160 |
|
| 161 |
@patch("jnpr.junos.transport.tty_telnet.Telnet") |
| 162 |
@patch("jnpr.junos.console.Console._tty_login") |
| 163 |
def test_console_tty_open_err(self, mock_login, mock_telnet): |
| 164 |
with patch( |
| 165 |
"jnpr.junos.transport.tty_telnet." "telnetlib.Telnet.open" |
| 166 |
) as mock_open: |
| 167 |
mock_telnet.RETRY_OPEN = 1 |
| 168 |
mock_login.side_effect = ValueError |
| 169 |
self.dev._tty.LOGIN_RETRY = self.dev._tty.RETRY_OPEN = 1 |
| 170 |
self.assertRaises(ValueError, self.dev.open) |
| 171 |
|
| 172 |
@patch("jnpr.junos.transport.tty_serial.Serial._tty_open") |
| 173 |
@patch("jnpr.junos.transport.tty_serial.serial.Serial.readline") |
| 174 |
@patch("jnpr.junos.transport.tty_serial.Serial.write") |
| 175 |
def test_console_serial(self, mock_write, mock_expect, mock_open): |
| 176 |
tty_netconf.open = MagicMock() |
| 177 |
mock_expect.side_effect = [ |
| 178 |
six.b("\r\r\n Login:"), |
| 179 |
six.b("\r\r\n password:"), |
| 180 |
six.b("\r\r\nroot@device:~ # "), |
| 181 |
] |
| 182 |
self.dev = Console(host="1.1.1.1", user="lab", password="lab123", mode="serial") |
| 183 |
self.dev.open() |
| 184 |
self.assertTrue(self.dev.connected) |
| 185 |
self.assertFalse(self.dev._gather_facts) |
| 186 |
|
| 187 |
def test_wrong_mode(self): |
| 188 |
dev = Console(host="1.1.1.1", user="lab", password="lab123", mode="testing") |
| 189 |
self.assertRaises(AttributeError, dev.open) |
| 190 |
|
| 191 |
@patch("jnpr.junos.transport.tty_telnet.Telnet._tty_close") |
| 192 |
def test_console_close_error_skip_logout(self, mock_close): |
| 193 |
mock_close.side_effect = RuntimeError |
| 194 |
self.assertRaises(RuntimeError, self.dev.close, skip_logout=True) |
| 195 |
|
| 196 |
@patch("jnpr.junos.transport.tty_netconf.tty_netconf.rpc") |
| 197 |
def test_console_zeroize(self, mock_zeroize): |
| 198 |
self.dev.zeroize() |
| 199 |
self.assertTrue(mock_zeroize.called) |
| 200 |
|
| 201 |
@patch("jnpr.junos.transport.tty_netconf.tty_netconf.rpc") |
| 202 |
@patch("jnpr.junos.console.FACT_LIST") |
| 203 |
@patch("jnpr.junos.device.warnings") |
| 204 |
def test_console_gather_facts(self, mock_warnings, mock_fact_list, mock_rpc): |
| 205 |
self.dev._fact_style = "old" |
| 206 |
from jnpr.junos.ofacts.session import facts_session |
| 207 |
|
| 208 |
mock_fact_list.__iter__.return_value = [facts_session] |
| 209 |
self.dev.facts_refresh() |
| 210 |
self.assertEqual(mock_rpc.call_count, 8) |
| 211 |
|
| 212 |
@patch("jnpr.junos.console.Console._tty_login") |
| 213 |
@patch("jnpr.junos.console.FACT_LIST") |
| 214 |
@patch("jnpr.junos.device.warnings") |
| 215 |
def test_console_gather_facts_true(self, mock_warnings, mock_fact_list, tty_login): |
| 216 |
self.dev._fact_style = "old" |
| 217 |
self.dev.facts = self.dev.ofacts |
| 218 |
from jnpr.junos.ofacts.session import facts_session |
| 219 |
|
| 220 |
mock_fact_list.__iter__.return_value = [facts_session] |
| 221 |
self.dev._gather_facts = True |
| 222 |
self.dev.open() |
| 223 |
self.assertEqual( |
| 224 |
self.dev.facts, |
| 225 |
{ |
| 226 |
"2RE": False, |
| 227 |
"RE_hw_mi": False, |
| 228 |
"ifd_style": "CLASSIC", |
| 229 |
"serialnumber": "UNKNOWN", |
| 230 |
"model": "UNKNOWN", |
| 231 |
"vc_capable": False, |
| 232 |
"switch_style": "NONE", |
| 233 |
"personality": "UNKNOWN", |
| 234 |
}, |
| 235 |
) |
| 236 |
|
| 237 |
@patch("jnpr.junos.transport.tty.sleep") |
| 238 |
@patch("ncclient.operations.rpc.RPCReply.parse") |
| 239 |
@patch("jnpr.junos.transport.tty_telnet.telnetlib.Telnet.write") |
| 240 |
@patch("jnpr.junos.transport.tty_netconf.select.select") |
| 241 |
@patch("jnpr.junos.transport.tty_telnet.telnetlib.Telnet.read_until") |
| 242 |
def test_load_console( |
| 243 |
self, mock_read_until, mock_select, mock_write, mock_parse, mock_sleep |
| 244 |
): |
| 245 |
mock_select.return_value = ([self.dev._tty._rx], [], []) |
| 246 |
xml = """<policy-options> |
| 247 |
<policy-statement> |
| 248 |
<name>F5-in</name> |
| 249 |
<term> |
| 250 |
<name>testing</name> |
| 251 |
<then> |
| 252 |
<accept/> |
| 253 |
</then> |
| 254 |
</term> |
| 255 |
<from> |
| 256 |
<protocol>mpls</protocol> |
| 257 |
</from> |
| 258 |
</policy-statement> |
| 259 |
</policy-options>""" |
| 260 |
|
| 261 |
mock_read_until.return_value = six.b( |
| 262 |
""" |
| 263 |
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/15.2I0/junos"> |
| 264 |
<load-configuration-results> |
| 265 |
<ok/> |
| 266 |
</load-configuration-results> |
| 267 |
</rpc-reply> |
| 268 |
]]>]]>""" |
| 269 |
) |
| 270 |
cu = Config(self.dev) |
| 271 |
op = cu.load(xml, format="xml") |
| 272 |
cu.commit() |
| 273 |
|
| 274 |
@patch("ncclient.operations.rpc.RPCReply.parse") |
| 275 |
@patch("jnpr.junos.transport.tty_netconf.tty_netconf._receive") |
| 276 |
@patch("jnpr.junos.transport.tty_telnet.Telnet.rawwrite") |
| 277 |
def test_console_rpc_call(self, mock_write, mock_rcv, mock_parse): |
| 278 |
self.dev._tty.nc.rpc = MagicMock(side_effect=self._mock_manager) |
| 279 |
op = self.dev.rpc.get_chassis_inventory() |
| 280 |
self.assertEqual(op.tag, "chassis-inventory") |
| 281 |
|
| 282 |
@patch("ncclient.operations.rpc.RPCReply.parse") |
| 283 |
@patch("jnpr.junos.transport.tty_netconf.tty_netconf._receive") |
| 284 |
@patch("jnpr.junos.transport.tty_telnet.Telnet.rawwrite") |
| 285 |
def test_console_rpc_call_exception(self, mock_write, mock_rcv, mock_parse): |
| 286 |
mock_rcv.return_value = "<output>testing</output>" |
| 287 |
op = self.dev.rpc.get_chassis_inventory() |
| 288 |
self.assertEqual(op.tag, "output") |
| 289 |
|
| 290 |
def test_timeout_getter_setter(self): |
| 291 |
self.dev.timeout = 1 |
| 292 |
self.assertEqual(1, self.dev.timeout) |
| 293 |
|
| 294 |
# below 2 function will be used in future. |
| 295 |
def _mock_manager(self, *args, **kwargs): |
| 296 |
if args: |
| 297 |
return self._read_file(etree.XML(args[0]).tag + ".xml") |
| 298 |
|
| 299 |
def _read_file(self, fname): |
| 300 |
from ncclient.xml_ import NCElement |
| 301 |
|
| 302 |
fpath = os.path.join(os.path.dirname(__file__), "rpc-reply", fname) |
| 303 |
with open(fpath) as fp: |
| 304 |
return fp.read() |