| 1 |
__author__ = "Rick Sherman" |
| 2 |
|
| 3 |
try: |
| 4 |
import unittest2 as unittest |
| 5 |
except ImportError: |
| 6 |
import unittest |
| 7 |
import nose2 |
| 8 |
from unittest.mock import patch |
| 9 |
import os |
| 10 |
import json |
| 11 |
|
| 12 |
from jnpr.junos import Device |
| 13 |
from jnpr.junos.factory.to_json import ( |
| 14 |
PyEzJSONEncoder, |
| 15 |
TableJSONEncoder, |
| 16 |
TableViewJSONEncoder, |
| 17 |
) |
| 18 |
from jnpr.junos.op.routes import RouteSummaryTable |
| 19 |
from ncclient.manager import Manager, make_device_handler |
| 20 |
from ncclient.transport import SSHSession |
| 21 |
from ncclient.operations.rpc import RPCReply |
| 22 |
|
| 23 |
|
| 24 |
class TestToJson(unittest.TestCase): |
| 25 |
@patch("ncclient.manager.connect") |
| 26 |
def setUp(self, mock_connect): |
| 27 |
mock_connect.side_effect = self._mock_manager |
| 28 |
self.dev = Device( |
| 29 |
host="1.1.1.1", user="rick", password="password123", gather_facts=False |
| 30 |
) |
| 31 |
self.dev.open() |
| 32 |
|
| 33 |
def test_pyez_encoder_default(self): |
| 34 |
with self.assertRaises(TypeError): |
| 35 |
PyEzJSONEncoder.default(PyEzJSONEncoder(), "test") |
| 36 |
|
| 37 |
def test_table_encoder_default(self): |
| 38 |
with self.assertRaises(TypeError): |
| 39 |
TableJSONEncoder.default(TableJSONEncoder(), "test") |
| 40 |
|
| 41 |
def test_view_encoder_default(self): |
| 42 |
with self.assertRaises(TypeError): |
| 43 |
TableViewJSONEncoder.default(TableViewJSONEncoder(), "test") |
| 44 |
|
| 45 |
@patch("jnpr.junos.Device.execute") |
| 46 |
def test_table_json(self, mock_execute): |
| 47 |
mock_execute.side_effect = self._mock_manager |
| 48 |
rst = RouteSummaryTable(self.dev) |
| 49 |
rst.get() |
| 50 |
resp = rst.to_json() |
| 51 |
j = { |
| 52 |
"ISP-1.inet.0": { |
| 53 |
"proto": { |
| 54 |
"Local": {"count": 1, "active": 1}, |
| 55 |
"Direct": {"count": 3, "active": 3}, |
| 56 |
}, |
| 57 |
"dests": 4, |
| 58 |
"holddown": 0, |
| 59 |
"active": 4, |
| 60 |
"hidden": 0, |
| 61 |
"total": 4, |
| 62 |
}, |
| 63 |
"ISP-2.inet.0": { |
| 64 |
"proto": { |
| 65 |
"Local": {"count": 1, "active": 1}, |
| 66 |
"Direct": {"count": 3, "active": 3}, |
| 67 |
}, |
| 68 |
"dests": 4, |
| 69 |
"holddown": 0, |
| 70 |
"active": 4, |
| 71 |
"hidden": 0, |
| 72 |
"total": 4, |
| 73 |
}, |
| 74 |
"inet.0": { |
| 75 |
"proto": { |
| 76 |
"Local": {"count": 4, "active": 4}, |
| 77 |
"Static": {"count": 1, "active": 1}, |
| 78 |
"Direct": {"count": 4, "active": 3}, |
| 79 |
}, |
| 80 |
"dests": 8, |
| 81 |
"holddown": 0, |
| 82 |
"active": 8, |
| 83 |
"hidden": 0, |
| 84 |
"total": 9, |
| 85 |
}, |
| 86 |
} |
| 87 |
self.assertEqual(eval(resp), j) |
| 88 |
|
| 89 |
@patch("jnpr.junos.Device.execute") |
| 90 |
def test_view_json(self, mock_execute): |
| 91 |
mock_execute.side_effect = self._mock_manager |
| 92 |
rst = RouteSummaryTable(self.dev) |
| 93 |
rst.get() |
| 94 |
resp = rst["ISP-1.inet.0"].to_json() |
| 95 |
j = { |
| 96 |
"ISP-1.inet.0": { |
| 97 |
"proto": { |
| 98 |
"Local": {"count": 1, "active": 1}, |
| 99 |
"Direct": {"count": 3, "active": 3}, |
| 100 |
}, |
| 101 |
"dests": 4, |
| 102 |
"holddown": 0, |
| 103 |
"active": 4, |
| 104 |
"hidden": 0, |
| 105 |
"total": 4, |
| 106 |
} |
| 107 |
} |
| 108 |
self.assertEqual(eval(resp), j) |
| 109 |
|
| 110 |
@patch("jnpr.junos.Device.execute") |
| 111 |
def test_json_rpc(self, mock_execute): |
| 112 |
mock_execute.side_effect = self._mock_manager |
| 113 |
resp = self.dev.rpc.get_software_information() |
| 114 |
j = { |
| 115 |
"package-information": { |
| 116 |
"comment": "JUNOS Software Release [12.1X46-D15.3]", |
| 117 |
"name": "junos", |
| 118 |
}, |
| 119 |
"host-name": "firefly", |
| 120 |
"product-model": "firefly-perimeter", |
| 121 |
"product-name": "firefly-perimeter", |
| 122 |
} |
| 123 |
self.assertEqual(eval(json.dumps(resp)), j) |
| 124 |
|
| 125 |
def _read_file(self, fname): |
| 126 |
from ncclient.xml_ import NCElement |
| 127 |
|
| 128 |
fpath = os.path.join(os.path.dirname(__file__), "rpc-reply", fname) |
| 129 |
foo = open(fpath).read() |
| 130 |
reply = RPCReply(foo) |
| 131 |
reply.parse() |
| 132 |
rpc_reply = NCElement( |
| 133 |
reply, self.dev._conn._device_handler.transform_reply() |
| 134 |
)._NCElement__doc[0] |
| 135 |
return rpc_reply |
| 136 |
|
| 137 |
def _mock_manager(self, *args, **kwargs): |
| 138 |
if kwargs: |
| 139 |
if args and ("normalize" in kwargs or "filter_xml" in kwargs): |
| 140 |
return self._read_file(args[0].tag + ".xml") |
| 141 |
device_params = kwargs["device_params"] |
| 142 |
device_handler = make_device_handler(device_params) |
| 143 |
session = SSHSession(device_handler) |
| 144 |
return Manager(session, device_handler) |
| 145 |
|
| 146 |
if args: |
| 147 |
return self._read_file(args[0].tag + ".xml") |