| 1 |
try: |
| 2 |
import unittest2 as unittest |
| 3 |
except ImportError: |
| 4 |
import unittest |
| 5 |
import nose2 |
| 6 |
from unittest.mock import MagicMock, patch |
| 7 |
|
| 8 |
from jnpr.junos.transport.tty_netconf import tty_netconf |
| 9 |
|
| 10 |
import six |
| 11 |
import os |
| 12 |
import select |
| 13 |
import socket |
| 14 |
from ncclient.operations import RPCError |
| 15 |
|
| 16 |
|
| 17 |
class TestTTYNetconf(unittest.TestCase): |
| 18 |
def setUp(self): |
| 19 |
self.tty_net = tty_netconf(MagicMock()) |
| 20 |
self.tty_net._tty.port = "/dev/tty" |
| 21 |
|
| 22 |
@patch("jnpr.junos.transport.tty_netconf.tty_netconf._receive") |
| 23 |
def test_open_at_shell_true(self, mock_rcv): |
| 24 |
mock_rcv.return_value = ( |
| 25 |
b"" |
| 26 |
b"<!-- user lab, class j-superuser -->" |
| 27 |
b'<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">' |
| 28 |
b"<capabilities>" |
| 29 |
b"<capability>urn:ietf:params:netconf:base:1.0</capability>" |
| 30 |
b"<capability>urn:ietf:params:xml:ns:netconf:capability:validate:1.0</capability>" |
| 31 |
b"<capability>urn:ietf:params:xml:ns:netconf:capability:url:1.0?scheme=http,ftp,file</capability>" |
| 32 |
b"<capability>urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring</capability>" |
| 33 |
b"<capability>http://xml.juniper.net/netconf/junos/1.0</capability>" |
| 34 |
b"<capability>http://xml.juniper.net/dmi/system/1.0</capability>" |
| 35 |
b"</capabilities>" |
| 36 |
b"<session-id>82697</session-id>" |
| 37 |
b"</hello>" |
| 38 |
) |
| 39 |
self.tty_net.open(True) |
| 40 |
self.tty_net._tty.write.assert_called_with("xml-mode netconf need-trailer") |
| 41 |
|
| 42 |
@patch("jnpr.junos.transport.tty_netconf.tty_netconf._receive") |
| 43 |
@patch("jnpr.junos.transport.tty_netconf.timedelta") |
| 44 |
def test_open_RuntimeError(self, mock_delta, mock_rcv): |
| 45 |
mock_rcv.return_value = "]]>]]>" |
| 46 |
self.tty_net._tty.read.return_value = six.b("testing") |
| 47 |
from datetime import timedelta |
| 48 |
|
| 49 |
mock_delta.return_value = timedelta(seconds=0.5) |
| 50 |
self.assertRaises(RuntimeError, self.tty_net.open, False) |
| 51 |
self.tty_net._tty.write.assert_called_with("junoscript netconf need-trailer") |
| 52 |
|
| 53 |
@patch("ncclient.operations.rpc.RPCReply.parse") |
| 54 |
@patch("jnpr.junos.transport.tty_netconf.tty_netconf._receive") |
| 55 |
def test_rpc(self, mock_rcv, mock_parse): |
| 56 |
mock_rcv.return_value = "]]>]]>" |
| 57 |
self.tty_net.rpc("get-interface-information") |
| 58 |
self.tty_net._tty.rawwrite.assert_called_with( |
| 59 |
six.b("<rpc><get-interface-information/></rpc>") |
| 60 |
) |
| 61 |
|
| 62 |
@patch("jnpr.junos.transport.tty_netconf.tty_netconf._receive") |
| 63 |
def test_tty_netconf_single_rpc_error(self, mock_rcv): |
| 64 |
mock_rcv.return_value = """ |
| 65 |
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> |
| 66 |
<rpc-error><error-type>protocol</error-type> |
| 67 |
<error-tag>operation-failed</error-tag> |
| 68 |
<error-severity>error</error-severity> |
| 69 |
<error-message>interface-ranges expansion failed |
| 70 |
</error-message></rpc-error></rpc-reply>""" |
| 71 |
self.assertRaises(RPCError, self.tty_net.rpc, "commit-configuration") |
| 72 |
|
| 73 |
@patch("jnpr.junos.transport.tty_netconf.tty_netconf._receive") |
| 74 |
def test_tty_netconf_multi_rpc_error(self, mock_rcv): |
| 75 |
mock_rcv.return_value = self._read_file("commit-configuration.xml") |
| 76 |
self.assertRaises(RPCError, self.tty_net.rpc, "commit-configuration") |
| 77 |
|
| 78 |
@patch("jnpr.junos.transport.tty_netconf.tty_netconf.rpc") |
| 79 |
def test_close_force_true(self, mock_rpc): |
| 80 |
self.tty_net.close(True) |
| 81 |
mock_rpc.assert_called_with("close-session") |
| 82 |
|
| 83 |
@patch("jnpr.junos.transport.tty_netconf.tty_netconf.rpc") |
| 84 |
def test_close_force_false(self, mock_rpc): |
| 85 |
self.tty_net.close(False) |
| 86 |
self.assertTrue("close-session" not in mock_rpc.call_args_list) |
| 87 |
|
| 88 |
@patch("jnpr.junos.transport.tty_netconf.tty_netconf.rpc") |
| 89 |
def test_zeroize_exception(self, mock_rpc): |
| 90 |
mock_rpc.side_effect = ValueError |
| 91 |
self.assertTrue(not self.tty_net.zeroize()) |
| 92 |
|
| 93 |
@patch("jnpr.junos.transport.tty_netconf.select.select") |
| 94 |
def test_tty_netconf_receive_select_error(self, mock_select): |
| 95 |
mock_select.side_effect = select.error |
| 96 |
self.assertRaises(select.error, self.tty_net._receive) |
| 97 |
|
| 98 |
@patch("jnpr.junos.transport.tty_netconf.select.select") |
| 99 |
def test_tty_netconf_receive_socket_error(self, mock_select): |
| 100 |
mock_select.side_effect = socket.error |
| 101 |
self.assertRaises(socket.error, self.tty_net._receive) |
| 102 |
|
| 103 |
@patch("jnpr.junos.transport.tty_netconf.select.select") |
| 104 |
def test_tty_netconf_receive_empty_line(self, mock_select): |
| 105 |
rx = MagicMock() |
| 106 |
rx.read_until.side_effect = iter([six.b(""), six.b("]]>]]>")]) |
| 107 |
mock_select.return_value = ([rx], [], []) |
| 108 |
self.assertEqual(self.tty_net._receive().tag, "error-in-receive") |
| 109 |
|
| 110 |
@patch("jnpr.junos.transport.tty_netconf.select.select") |
| 111 |
def test_tty_netconf_receive_splited_eom(self, mock_select): |
| 112 |
rx = MagicMock() |
| 113 |
rx.read_until.side_effect = iter(["testing]", "]>", "]]>"]) |
| 114 |
mock_select.return_value = ([rx], [], []) |
| 115 |
self.assertEqual(self.tty_net._receive().tag, "error-in-receive") |
| 116 |
|
| 117 |
@patch("jnpr.junos.transport.tty_netconf.select.select") |
| 118 |
def test_tty_netconf_receive_splited_eom(self, mock_select): |
| 119 |
rx = MagicMock() |
| 120 |
rx.read_until.side_effect = iter([six.b(i) for i in ["testing]", "]>", "]]>"]]) |
| 121 |
mock_select.return_value = ([rx], [], []) |
| 122 |
self.assertEqual(self.tty_net._receive().tag, "error-in-receive") |
| 123 |
|
| 124 |
@patch("jnpr.junos.transport.tty_netconf.select.select") |
| 125 |
def test_tty_netconf_receive_XMLSyntaxError(self, mock_select): |
| 126 |
rx = MagicMock() |
| 127 |
|
| 128 |
rx.read_until.side_effect = iter( |
| 129 |
[six.b("<rpc-reply>ok<dummy></rpc-reply>"), six.b("\n]]>]]>")] |
| 130 |
) |
| 131 |
mock_select.return_value = ([rx], [], []) |
| 132 |
self.assertEqual( |
| 133 |
self.tty_net._receive(), six.b("<rpc-reply>ok<dummy/></rpc-reply>") |
| 134 |
) |
| 135 |
|
| 136 |
@patch("jnpr.junos.transport.tty_netconf.select.select") |
| 137 |
def test_tty_netconf_receive_XMLSyntaxError_eom_in_center(self, mock_select): |
| 138 |
rx = MagicMock() |
| 139 |
rx.read_until.side_effect = iter( |
| 140 |
[six.b("<rpc-reply>ok</rpc-reply>"), six.b("]]>]]>\ndummy")] |
| 141 |
) |
| 142 |
mock_select.return_value = ([rx], [], []) |
| 143 |
self.assertEqual(self.tty_net._receive(), six.b("<rpc-reply>ok</rpc-reply>")) |
| 144 |
|
| 145 |
@patch("jnpr.junos.transport.tty_netconf.select.select") |
| 146 |
def test_tty_netconf_receive_xmn_error(self, mock_select): |
| 147 |
rx = MagicMock() |
| 148 |
rx.read_until.side_effect = iter( |
| 149 |
[ |
| 150 |
six.b("<message>ok</message>"), |
| 151 |
six.b("\n</xnm:error>\n"), |
| 152 |
six.b("]]>]]>\ndummy"), |
| 153 |
] |
| 154 |
) |
| 155 |
mock_select.return_value = ([rx], [], []) |
| 156 |
self.assertEqual(self.tty_net._receive().tag, "error-in-receive") |
| 157 |
|
| 158 |
def _read_file(self, fname): |
| 159 |
fpath = os.path.join(os.path.dirname(__file__), "rpc-reply", fname) |
| 160 |
foo = open(fpath).read() |
| 161 |
|
| 162 |
# specific to multi rpc error |
| 163 |
if fname == "commit-configuration.xml": |
| 164 |
return foo |