| 1 |
__author__ = "Rick Sherman" |
| 2 |
__credits__ = "Jeremy Schulman" |
| 3 |
|
| 4 |
import unittest |
| 5 |
import os |
| 6 |
import yaml |
| 7 |
import json |
| 8 |
import nose2 |
| 9 |
|
| 10 |
from jnpr.junos import Device |
| 11 |
from jnpr.junos.op.phyport import PhyPortStatsTable |
| 12 |
from jnpr.junos.op.ethport import EthPortTable |
| 13 |
from jnpr.junos.factory.factory_loader import FactoryLoader |
| 14 |
from jnpr.junos.factory.optable import generate_sax_parser_input |
| 15 |
|
| 16 |
from ncclient.manager import Manager, make_device_handler |
| 17 |
from ncclient.transport import SSHSession |
| 18 |
from ncclient.operations.rpc import RPCReply |
| 19 |
|
| 20 |
from lxml import etree |
| 21 |
|
| 22 |
from unittest.mock import patch |
| 23 |
|
| 24 |
|
| 25 |
class TestFactoryOpTable(unittest.TestCase): |
| 26 |
@patch("ncclient.manager.connect") |
| 27 |
def setUp(self, mock_connect): |
| 28 |
mock_connect.side_effect = self._mock_manager |
| 29 |
self.dev = Device( |
| 30 |
host="1.1.1.1", user="rick", password="password123", gather_facts=False |
| 31 |
) |
| 32 |
self.dev.open() |
| 33 |
self.ppt = PhyPortStatsTable(self.dev) |
| 34 |
|
| 35 |
@patch("jnpr.junos.Device.execute") |
| 36 |
def test_optable_get(self, mock_execute): |
| 37 |
mock_execute.side_effect = self._mock_manager |
| 38 |
self.ppt.get() |
| 39 |
self.assertEqual(len(self.ppt), 2) |
| 40 |
|
| 41 |
@patch("jnpr.junos.Device.execute") |
| 42 |
def test_optable_get_key(self, mock_execute): |
| 43 |
mock_execute.side_effect = self._mock_manager |
| 44 |
self.ppt.get("ge-0/0/0") |
| 45 |
self.assertEqual(self.ppt.GET_KEY, "interface_name") |
| 46 |
|
| 47 |
def test_optable_path(self): |
| 48 |
fname = "local-get-interface-information.xml" |
| 49 |
path = os.path.join(os.path.dirname(__file__), "rpc-reply", fname) |
| 50 |
lppt = PhyPortStatsTable(path=path) |
| 51 |
lppt.get() |
| 52 |
self.assertEqual(len(lppt), 2) |
| 53 |
|
| 54 |
def test_optable_xml(self): |
| 55 |
fname = "get-interface-information.xml" |
| 56 |
xml = self._read_file(fname) |
| 57 |
lppt = PhyPortStatsTable(xml=xml) |
| 58 |
lppt.get() |
| 59 |
self.assertEqual(len(lppt), 2) |
| 60 |
|
| 61 |
@patch("jnpr.junos.Device.execute") |
| 62 |
def test_optable_view_get(self, mock_execute): |
| 63 |
mock_execute.side_effect = self._mock_manager |
| 64 |
self.ppt.get() |
| 65 |
v = self.ppt["ge-0/0/0"] |
| 66 |
self.assertEqual(v["rx_packets"], 1207) |
| 67 |
|
| 68 |
@patch("jnpr.junos.Device.execute") |
| 69 |
def test_optable_view_get_astype_bool(self, mock_execute): |
| 70 |
mock_execute.side_effect = self._mock_manager |
| 71 |
et = EthPortTable(self.dev) |
| 72 |
et.get() |
| 73 |
v = et["ge-0/0/0"] |
| 74 |
self.assertEqual(v["present"], True) |
| 75 |
|
| 76 |
@patch("jnpr.junos.Device.execute") |
| 77 |
def test_optable_view_get_astype_bool_regex(self, mock_execute): |
| 78 |
mock_execute.side_effect = self._mock_manager |
| 79 |
from jnpr.junos.op.bfd import BfdSessionTable |
| 80 |
|
| 81 |
bfd = BfdSessionTable(self.dev) |
| 82 |
bfd.get() |
| 83 |
v = bfd["10.92.20.4"] |
| 84 |
self.assertEqual(v["no_absorb"], True) |
| 85 |
|
| 86 |
@patch("jnpr.junos.Device.execute") |
| 87 |
def test_optable_view_get_unknown_field(self, mock_execute): |
| 88 |
mock_execute.side_effect = self._mock_manager |
| 89 |
self.ppt.get() |
| 90 |
|
| 91 |
def bad(key): |
| 92 |
v = self.ppt["ge-0/0/0"] |
| 93 |
return v[key] |
| 94 |
|
| 95 |
self.assertRaises(ValueError, bad, "bunk") |
| 96 |
|
| 97 |
def test_generate_sax_parser_fields_with_many_slash(self): |
| 98 |
yaml_data = """ |
| 99 |
--- |
| 100 |
bgpNeighborTable: |
| 101 |
rpc: get-bgp-neighbor-information |
| 102 |
item: bgp-peer |
| 103 |
key: peer-address |
| 104 |
view: bgpNeighborView |
| 105 |
bgpNeighborView: |
| 106 |
fields: |
| 107 |
prefix-count: bgp-option-information/prefix-limit/prefix-count |
| 108 |
prefix-dummy: bgp-option-information/prefix-limit/prefix-dummy |
| 109 |
""" |
| 110 |
globals().update(FactoryLoader().load(yaml.load(yaml_data, Loader=yaml.Loader))) |
| 111 |
tbl = bgpNeighborTable(self.dev) |
| 112 |
data = generate_sax_parser_input(tbl) |
| 113 |
self.assertEqual(data.tag, "bgp-peer") |
| 114 |
self.assertEqual( |
| 115 |
len(etree.tostring(data)), |
| 116 |
len( |
| 117 |
b"<bgp-peer><peer-address/><bgp-option-information><prefix-limit>" |
| 118 |
b"<prefix-count/><prefix-dummy/></prefix-limit>" |
| 119 |
b"</bgp-option-information></bgp-peer>" |
| 120 |
), |
| 121 |
) |
| 122 |
|
| 123 |
def test_generate_sax_parser_fields_with_diff_child_xpaths(self): |
| 124 |
yaml_data = """ |
| 125 |
--- |
| 126 |
twampProbeTable: |
| 127 |
rpc: twamp-get-probe-results |
| 128 |
item: probe-test-results |
| 129 |
key: test-name |
| 130 |
view: probeResultsView |
| 131 |
probeResultsView: |
| 132 |
fields: |
| 133 |
min-delay: probe-test-global-results/probe-test-generic-results/probe-test-rtt/probe-summary-results/min-delay |
| 134 |
max-delay: probe-test-global-results/probe-test-generic-results/probe-test-rtt/probe-summary-results/max-delay |
| 135 |
avg-delay: probe-test-global-results/probe-test-generic-results/probe-test-rtt/probe-summary-results/avg-delay |
| 136 |
positive-rtt-jitter: probe-test-global-results/probe-test-generic-results/probe-test-positive-round-trip-jitter/probe-summary-results/avg-delay |
| 137 |
loss-percentage: probe-test-global-results/probe-test-generic-results/loss-percentage |
| 138 |
current-min-delay: probe-last-test-results/probe-test-generic-results/probe-test-rtt/probe-summary-results/min-delay |
| 139 |
current-max-delay: probe-last-test-results/probe-test-generic-results/probe-test-rtt/probe-summary-results/max-delay |
| 140 |
current-avg-delay: probe-last-test-results/probe-test-generic-results/probe-test-rtt/probe-summary-results/avg-delay |
| 141 |
current-positive-rtt-jitter: probe-last-test-results/probe-test-generic-results/probe-test-positive-round-trip-jitter/probe-summary-results/avg-delay |
| 142 |
current-loss-percentage: probe-last-test-results/probe-test-generic-results/loss-percentage |
| 143 |
""" |
| 144 |
globals().update(FactoryLoader().load(yaml.load(yaml_data, Loader=yaml.Loader))) |
| 145 |
tbl = twampProbeTable(self.dev) |
| 146 |
data = generate_sax_parser_input(tbl) |
| 147 |
self.assertEqual(data.tag, "probe-test-results") |
| 148 |
self.assertEqual( |
| 149 |
len(etree.tostring(data)), |
| 150 |
len( |
| 151 |
b"<probe-test-results><test-name/><probe-last-test-results>" |
| 152 |
b"<probe-test-generic-results><probe-test-rtt><probe-summary" |
| 153 |
b"-results><min-delay/><avg-delay/><max-delay/></probe-summary" |
| 154 |
b"-results></probe-test-rtt><loss-percentage/>" |
| 155 |
b"<probe-test-positive-round-trip-jitter><probe-summary-results>" |
| 156 |
b"<avg-delay/></probe-summary-results></probe-test-positive-round" |
| 157 |
b"-trip-jitter></probe-test-generic-results></probe-last-test-resu" |
| 158 |
b"lts><probe-test-global-results><probe-test-generic-results>" |
| 159 |
b"<probe-test-rtt><probe-summary-results><min-delay/><avg-delay/>" |
| 160 |
b"<max-delay/></probe-summary-results></probe-test-rtt>" |
| 161 |
b"<loss-percentage/><probe-test-positive-round-trip-jitter>" |
| 162 |
b"<probe-summary-results><avg-delay/></probe-summary-results>" |
| 163 |
b"</probe-test-positive-round-trip-jitter></probe-test-generic-" |
| 164 |
b"results></probe-test-global-results></probe-test-results>" |
| 165 |
), |
| 166 |
) |
| 167 |
|
| 168 |
def test_generate_sax_parser_item_with_many_slash(self): |
| 169 |
yaml_data = """ |
| 170 |
--- |
| 171 |
taskmallocdetail: |
| 172 |
rpc: get-task-memory-information |
| 173 |
args: |
| 174 |
level: detail |
| 175 |
item: task-memory-malloc-usage-report/task-malloc-list/task-malloc |
| 176 |
key: tm-name |
| 177 |
view: taskmallocview |
| 178 |
|
| 179 |
taskmallocview: |
| 180 |
fields: |
| 181 |
tmallocs: tm-allocs |
| 182 |
tmallocbytes: tm-alloc-bytes |
| 183 |
tmmaxallocs: tm-max-allocs |
| 184 |
tmmaxallocbytes: tm-max-alloc-bytes |
| 185 |
tmfunctioncalls: tm-function-calls |
| 186 |
""" |
| 187 |
globals().update(FactoryLoader().load(yaml.load(yaml_data, Loader=yaml.Loader))) |
| 188 |
tbl = taskmallocdetail(self.dev) |
| 189 |
data = generate_sax_parser_input(tbl) |
| 190 |
self.assertEqual(data.tag, "task-memory-malloc-usage-report") |
| 191 |
self.assertEqual( |
| 192 |
len(etree.tostring(data)), |
| 193 |
len( |
| 194 |
b"<task-memory-malloc-usage-report><task-malloc-list><task-malloc><tm-name/><t" |
| 195 |
b"m-allocs/><tm-alloc-bytes/><tm-max-allocs/><tm-max-alloc-bytes/><tm-function" |
| 196 |
b"-calls/></task-malloc></task-malloc-list></task-memory-malloc-usage-report>" |
| 197 |
), |
| 198 |
) |
| 199 |
|
| 200 |
def test_generate_sax_parser_same_parents_with_diff_fields(self): |
| 201 |
yaml_data = """ |
| 202 |
--- |
| 203 |
VtepTable: |
| 204 |
rpc: get-interface-information |
| 205 |
args: |
| 206 |
interface-name: "vtep" |
| 207 |
extensive: True |
| 208 |
item: physical-interface |
| 209 |
key: name |
| 210 |
view: VtepView |
| 211 |
|
| 212 |
VtepView: |
| 213 |
fields: |
| 214 |
admin-status: admin-status |
| 215 |
oper-status: oper-status |
| 216 |
link-level-type: link-level-type |
| 217 |
input-bytes: traffic-statistics/input-bytes |
| 218 |
output-bytes: traffic-statistics/output-bytes |
| 219 |
input-errors: input-error-list/input-errors |
| 220 |
output-errors: output-error-list/output-errors |
| 221 |
carrier-transitions: output-error-list/carrier-transitions |
| 222 |
vtep-tunnel-stats: VtepTunnelTable |
| 223 |
|
| 224 |
VtepTunnelTable: |
| 225 |
item: logical-interface |
| 226 |
key: name |
| 227 |
view: VtepTunnelView |
| 228 |
|
| 229 |
VtepTunnelView: |
| 230 |
fields: |
| 231 |
vtep-type: vtep-info/vtep-type |
| 232 |
vtep-address: vtep-info/vtep-address |
| 233 |
tunnel-input-bytes: traffic-statistics/input-bytes |
| 234 |
tunnel-output-bytes: traffic-statistics/output-bytes |
| 235 |
""" |
| 236 |
globals().update(FactoryLoader().load(yaml.load(yaml_data, Loader=yaml.Loader))) |
| 237 |
tbl = VtepTable(self.dev) |
| 238 |
data = generate_sax_parser_input(tbl) |
| 239 |
self.assertEqual(data.tag, "physical-interface") |
| 240 |
self.assertEqual( |
| 241 |
len(etree.tostring(data)), |
| 242 |
len( |
| 243 |
b"<physical-interface><name/><admin-status/><oper-status/>" |
| 244 |
b"<link-level-type/><traffic-statistics><input-bytes/>" |
| 245 |
b"<output-bytes/></traffic-statistics><input-error-list>" |
| 246 |
b"<input-errors/></input-error-list><output-error-list>" |
| 247 |
b"<output-errors/><carrier-transitions/></output-error-list>" |
| 248 |
b"<logical-interface><name/><vtep-info><vtep-type/><vtep-address/>" |
| 249 |
b"</vtep-info><traffic-statistics><input-bytes/><output-bytes/>" |
| 250 |
b"</traffic-statistics></logical-interface></physical-interface>" |
| 251 |
), |
| 252 |
) |
| 253 |
|
| 254 |
@patch("jnpr.junos.Device.execute") |
| 255 |
def test_key_pipe_delim_with_Null(self, mock_execute): |
| 256 |
mock_execute.side_effect = self._mock_manager |
| 257 |
yaml_data = """ |
| 258 |
--- |
| 259 |
UTMStatusTable: |
| 260 |
rpc: show-utmd-status |
| 261 |
item: //utmd-status |
| 262 |
view: UTMStatusView |
| 263 |
key: ../re-name | Null |
| 264 |
|
| 265 |
UTMStatusView: |
| 266 |
fields: |
| 267 |
running: { running: flag } |
| 268 |
""" |
| 269 |
globals().update(FactoryLoader().load(yaml.load(yaml_data, Loader=yaml.Loader))) |
| 270 |
tbl = UTMStatusTable(self.dev) |
| 271 |
data = tbl.get() |
| 272 |
self.assertEqual( |
| 273 |
json.loads(data.to_json()), |
| 274 |
{"node0": {"running": True}, "node1": {"running": True}}, |
| 275 |
) |
| 276 |
|
| 277 |
@patch("jnpr.junos.Device.execute") |
| 278 |
def test_key_pipe_delim_with_Null_use_Null(self, mock_execute): |
| 279 |
mock_execute.side_effect = self._mock_manager |
| 280 |
yaml_data = """ |
| 281 |
--- |
| 282 |
UTMStatusTable: |
| 283 |
rpc: show-utmd-status_use_Null |
| 284 |
item: //utmd-status |
| 285 |
view: UTMStatusView |
| 286 |
key: ../re-name | Null |
| 287 |
|
| 288 |
UTMStatusView: |
| 289 |
fields: |
| 290 |
running: { running: flag } |
| 291 |
""" |
| 292 |
globals().update(FactoryLoader().load(yaml.load(yaml_data, Loader=yaml.Loader))) |
| 293 |
tbl = UTMStatusTable(self.dev) |
| 294 |
data = tbl.get() |
| 295 |
self.assertEqual(json.loads(data.to_json()), {"running": True}) |
| 296 |
|
| 297 |
@patch("jnpr.junos.Device.execute") |
| 298 |
def test_key_and_item_pipe_delim_with_Null_use_Null(self, mock_execute): |
| 299 |
mock_execute.side_effect = self._mock_manager |
| 300 |
yaml_data = """ |
| 301 |
--- |
| 302 |
UTMStatusTable: |
| 303 |
rpc: show-utmd-status_use_Null |
| 304 |
item: //multi-routing-engine-item/utmd-status | //utmd-status |
| 305 |
view: UTMStatusView |
| 306 |
key: |
| 307 |
- ../re-name | Null |
| 308 |
|
| 309 |
UTMStatusView: |
| 310 |
fields: |
| 311 |
running: { running: flag } |
| 312 |
""" |
| 313 |
globals().update(FactoryLoader().load(yaml.load(yaml_data, Loader=yaml.Loader))) |
| 314 |
tbl = UTMStatusTable(self.dev) |
| 315 |
data = tbl.get() |
| 316 |
self.assertEqual(json.loads(data.to_json()), {"running": True}) |
| 317 |
|
| 318 |
def _read_file(self, fname): |
| 319 |
from ncclient.xml_ import NCElement |
| 320 |
|
| 321 |
fpath = os.path.join(os.path.dirname(__file__), "rpc-reply", fname) |
| 322 |
foo = open(fpath).read() |
| 323 |
reply = RPCReply(foo) |
| 324 |
reply.parse() |
| 325 |
rpc_reply = NCElement( |
| 326 |
reply, self.dev._conn._device_handler.transform_reply() |
| 327 |
)._NCElement__doc[0] |
| 328 |
return rpc_reply |
| 329 |
|
| 330 |
def _mock_manager(self, *args, **kwargs): |
| 331 |
if kwargs: |
| 332 |
if args and ("normalize" in kwargs or "filter_xml" in kwargs): |
| 333 |
return self._read_file(args[0].tag + ".xml") |
| 334 |
device_params = kwargs["device_params"] |
| 335 |
device_handler = make_device_handler(device_params) |
| 336 |
session = SSHSession(device_handler) |
| 337 |
return Manager(session, device_handler) |
| 338 |
|
| 339 |
if args: |
| 340 |
return self._read_file(args[0].tag + ".xml") |