Debian Codegraph @

tests/unit/factory/test_cmdtable.py

raw
1 __author__ = "Nitin Kumar"
2 __credits__ = "Jeremy Schulman"
3
4 import unittest
5 import os
6 import nose2
7
8 from jnpr.junos import Device
9 from jnpr.junos.exception import RpcError
10
11 from ncclient.manager import Manager, make_device_handler
12 from ncclient.transport import SSHSession
13 from unittest.mock import MagicMock, patch
14 import yamlordereddictloader
15 from jnpr.junos.factory.factory_loader import FactoryLoader
16 import yaml
17 import json
18
19
20 class TestFactoryCMDTable(unittest.TestCase):
21 @patch("ncclient.manager.connect")
22 def setUp(self, mock_connect):
23 mock_connect.side_effect = self._mock_manager
24 self.dev = Device(
25 host="1.1.1.1", user="rick", password="password123", gather_facts=False
26 )
27 self.dev.open()
28
29 @patch("jnpr.junos.Device.execute")
30 def test_unstructured_cmerror(self, mock_execute):
31 mock_execute.side_effect = self._mock_manager
32 yaml_data = """
33 ---
34 CMErrorTable:
35 command: show cmerror module brief
36 target: Null
37 key:
38 - module
39 view: CMErrorView
40
41 CMErrorView:
42 columns:
43 module: Module
44 name: Name
45 errors: Active Errors
46 filters:
47 - errors
48 - name
49 """
50 globals().update(
51 FactoryLoader().load(
52 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
53 )
54 )
55 stats = CMErrorTable(self.dev)
56 stats = stats.get(target="fpc1")
57 self.assertEqual(
58 dict(stats),
59 {
60 1: {"errors": 0, "name": "PQ3 Chip"},
61 2: {"errors": 0, "name": "Host Loopback"},
62 3: {"errors": 0, "name": "CM[0]"},
63 4: {"errors": 0, "name": "CM[1]"},
64 5: {"errors": 0, "name": "LUCHIP(0)"},
65 6: {"errors": 0, "name": "TOE-LU-0:0:0"},
66 },
67 )
68 self.assertEqual(repr(stats), "CMErrorTable:1.1.1.1: 6 items")
69 self.assertEqual(len(stats), 6)
70
71 @patch("jnpr.junos.Device.execute")
72 def test_unstructured_sysctl_oneline_op(self, mock_execute):
73 mock_execute.side_effect = self._mock_manager
74 yaml_data = """
75 ---
76 sysctlVeriexecTable:
77 command: request routing-engine execute command sysctl dummy
78 view: sysctlView
79
80 sysctlView:
81 regex:
82 veriexec-name: '(.*):'
83 veriexec-state: '(.*)'
84 """
85 globals().update(
86 FactoryLoader().load(
87 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
88 )
89 )
90 stats = sysctlVeriexecTable(self.dev)
91 stats = stats.get()
92 self.assertEqual(
93 dict(stats),
94 {
95 "veriexec-name": "security.mac.veriexec.state",
96 "veriexec-state": "loaded active enforce",
97 },
98 )
99 self.assertEqual(repr(stats), "sysctlVeriexecTable:1.1.1.1: 2 items")
100 self.assertEqual(len(stats), 2)
101
102 @patch("jnpr.junos.Device.execute")
103 def test_unstructured_cmerror_multiline_header(self, mock_execute):
104 mock_execute.side_effect = self._mock_manager
105 yaml_data = """
106 ---
107 CMErrorTable:
108 command: show cmerror module brief dummy multiline
109 target: Null
110 key: module
111 view: CMErrorView
112
113 CMErrorView:
114 columns:
115 module: Module
116 name: Name
117 errors: Active Errors
118 pfe:
119 - PFE
120 - Specific
121 callback:
122 - Callback
123 - Function
124 data: ModuleData
125 """
126 globals().update(
127 FactoryLoader().load(
128 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
129 )
130 )
131 stats = CMErrorTable(self.dev)
132 stats = stats.get(target="fpc1")
133 self.assertEqual(
134 dict(stats),
135 {
136 1: {
137 "errors": 0,
138 "pfe": "Yes",
139 "name": "PQ3 Chip",
140 "module": 1,
141 "callback": "0x00000000",
142 "data": "0x00000000",
143 },
144 2: {
145 "errors": 0,
146 "pfe": "No",
147 "name": "Host Loopback",
148 "module": 2,
149 "callback": "0x00000000",
150 "data": "0x464295b0",
151 },
152 3: {
153 "errors": 0,
154 "pfe": "No",
155 "name": "CM[0]",
156 "module": 3,
157 "callback": "0x41f550f0",
158 "data": "0x462f767c",
159 },
160 4: {
161 "errors": 0,
162 "pfe": "No",
163 "name": "LUCHIP(0)",
164 "module": 4,
165 "callback": "0x00000000",
166 "data": "0x481b84d8",
167 },
168 },
169 )
170 self.assertEqual(repr(stats), "CMErrorTable:1.1.1.1: 4 items")
171 self.assertEqual(len(stats), 4)
172
173 def test_view_variable(self):
174 yaml_data = """
175 ---
176 CMErrorTable:
177 command: show cmerror module brief
178 target: Null
179 key: module
180 view: CMErrorView
181
182 CMErrorView:
183 columns:
184 module: Module
185 name: Name
186 errors: Active Errors
187 """
188 globals().update(
189 FactoryLoader().load(
190 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
191 )
192 )
193 stats = CMErrorTable(self.dev)
194 stats.view = globals()["CMErrorView"]
195 self.assertEqual(stats.view, globals()["CMErrorView"])
196
197 def test_view_setter_raise_exception(self):
198 yaml_data = """
199 ---
200 CMErrorTable:
201 command: show cmerror module brief
202 target: Null
203 key: module
204 view: CMErrorView
205
206 CMErrorView:
207 columns:
208 module: Module
209 name: Name
210 errors: Active Errors
211 """
212 globals().update(
213 FactoryLoader().load(
214 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
215 )
216 )
217 stats = CMErrorTable(self.dev)
218
219 with self.assertRaises(ValueError):
220 stats.view = "dummy"
221
222 @patch("paramiko.SSHClient")
223 @patch("jnpr.junos.utils.start_shell.StartShell.wait_for")
224 @patch("jnpr.junos.Device.execute")
225 def test_request_pfe_rpc_not_avialable(self, mock_execute, mock_ss, mock_ssh_conn):
226 mock_execute.side_effect = RpcError(rsp="ok")
227 yaml_data = """
228 ---
229 CMErrorTable:
230 command: show cmerror module brief
231 target: fpc1
232 key: module
233 view: CMErrorView
234
235 CMErrorView:
236 columns:
237 module: Module
238 name: Name
239 """
240 globals().update(
241 FactoryLoader().load(
242 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
243 )
244 )
245 stats = CMErrorTable(self.dev)
246 with patch("jnpr.junos.utils.start_shell.StartShell.run") as ss_run:
247 stats.get()
248 ss_run.assert_called_with('cprod -A fpc1 -c "show cmerror module brief"')
249
250 @patch("jnpr.junos.Device.execute")
251 def test_get_api_params(self, mock_execute):
252 mock_execute.side_effect = self._mock_manager
253 yaml_data = """
254 ---
255 CMErrorTable:
256 command: show cmerror module brief
257 target: fpc1
258 key: name
259 view: CMErrorView
260
261 CMErrorView:
262 columns:
263 module: Module
264 name: Name
265 errors: Active Errors
266 """
267 globals().update(
268 FactoryLoader().load(
269 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
270 )
271 )
272 stats = CMErrorTable(self.dev)
273 stats.get(key="module", key_items=[1], filters=["errors"])
274 self.assertEqual(dict(stats), {1: {"errors": 0}})
275
276 @patch("jnpr.junos.Device.execute")
277 def test_cmdview_properties(self, mock_execute):
278 mock_execute.side_effect = self._mock_manager
279 yaml_data = """
280 ---
281 CMErrorTable:
282 command: show cmerror module brief
283 target: fpc1
284 key: name
285 view: CMErrorView
286
287 CMErrorView:
288 columns:
289 module: Module
290 name: Name
291 errors: Active Errors
292 """
293 globals().update(
294 FactoryLoader().load(
295 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
296 )
297 )
298 stats = CMErrorTable(self.dev).get()
299 self.assertEqual(stats.VIEW.T.__class__.__name__, "property")
300 self.assertEqual(stats.VIEW.xml.__class__.__name__, "property")
301 expected = [
302 "Host Loopback",
303 "CM[1]",
304 "CM[0]",
305 "LUCHIP(0)",
306 "TOE-LU-0:0:0",
307 "PQ3 Chip",
308 ]
309 expected.sort()
310 got_keys = list(stats.keys())
311 got_keys.sort()
312 self.assertListEqual(got_keys, expected)
313 got_values = list(stats.values())
314 got_items = list(stats.items())
315 self.assertEqual(repr(stats), "CMErrorTable:1.1.1.1: 6 items")
316
317 @patch("jnpr.junos.Device.execute")
318 def test_cmdtable_iter(self, mock_execute):
319 mock_execute.side_effect = self._mock_manager
320 yaml_data = """
321 ---
322 FPCLinkStatTable:
323 command: show link stats
324 target: Null
325 delimiter: ":"
326 """
327 globals().update(
328 FactoryLoader().load(
329 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
330 )
331 )
332 stats = FPCLinkStatTable(self.dev)
333 stats = stats.get(target="fpc1")
334 self.assertEqual(
335 {k: v for k, v in stats},
336 {
337 "PPP LCP/NCP": 0,
338 "ISIS": 0,
339 "BFD": 15,
340 "OAM": 0,
341 "ETHOAM": 0,
342 "LACP": 0,
343 "LMI": 0,
344 "UBFD": 0,
345 "HDLC keepalives": 0,
346 "OSPF Hello": 539156,
347 "RSVP": 0,
348 },
349 )
350
351 @patch("jnpr.junos.Device.execute")
352 def test_title_in_view(self, mock_execute):
353 mock_execute.side_effect = self._mock_manager
354 yaml_data = r"""
355 ---
356 ShowLuchipTable:
357 command: show luchip {{ lu_instance }}
358 target: Null
359 args:
360 lu_instance: 0
361 key: ppe
362 view: ShowLuchipView
363
364 ShowLuchipView:
365 title: PPE Zone Enable masks and (active)
366 regex:
367 ppe: 'PPE\[(\d+)\]'
368 zone_enable_mask: 'Zone Enable Mask (0x[a-z0-9]+)'
369 active_zones: '\((0x[a-z0-9]+)\)'
370 """
371 globals().update(
372 FactoryLoader().load(
373 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
374 )
375 )
376 stats = ShowLuchipTable(self.dev)
377 stats = stats.get(target="fpc1")
378 self.assertEqual(
379 dict(stats),
380 {
381 0: {
382 "active_zones": "0x00000000",
383 "ppe": 0,
384 "zone_enable_mask": "0xfcff0ffe",
385 },
386 1: {
387 "active_zones": "0x00000000",
388 "ppe": 1,
389 "zone_enable_mask": "0xfcff0fff",
390 },
391 2: {
392 "active_zones": "0x00000000",
393 "ppe": 2,
394 "zone_enable_mask": "0xfcff0fff",
395 },
396 3: {
397 "active_zones": "0x00000000",
398 "ppe": 3,
399 "zone_enable_mask": "0xfcff0fff",
400 },
401 4: {
402 "active_zones": "0x00000000",
403 "ppe": 4,
404 "zone_enable_mask": "0xfcff0fff",
405 },
406 5: {
407 "active_zones": "0x00000000",
408 "ppe": 5,
409 "zone_enable_mask": "0xfcff0fff",
410 },
411 6: {
412 "active_zones": "0x00000000",
413 "ppe": 6,
414 "zone_enable_mask": "0xfcff0fff",
415 },
416 7: {
417 "active_zones": "0x00000000",
418 "ppe": 7,
419 "zone_enable_mask": "0xfcff0fff",
420 },
421 8: {
422 "active_zones": "0x00000000",
423 "ppe": 8,
424 "zone_enable_mask": "0xfcff0fff",
425 },
426 9: {
427 "active_zones": "0x00000000",
428 "ppe": 9,
429 "zone_enable_mask": "0xfcff0fff",
430 },
431 10: {
432 "active_zones": "0x00000100",
433 "ppe": 10,
434 "zone_enable_mask": "0xfcff0fff",
435 },
436 11: {
437 "active_zones": "0x00000000",
438 "ppe": 11,
439 "zone_enable_mask": "0xfcff0fff",
440 },
441 12: {
442 "active_zones": "0x00000000",
443 "ppe": 12,
444 "zone_enable_mask": "0xfcff0fff",
445 },
446 13: {
447 "active_zones": "0x00000000",
448 "ppe": 13,
449 "zone_enable_mask": "0xfcff0fff",
450 },
451 14: {
452 "active_zones": "0x00000000",
453 "ppe": 14,
454 "zone_enable_mask": "0xfcff0fff",
455 },
456 15: {
457 "active_zones": "0x00000000",
458 "ppe": 15,
459 "zone_enable_mask": "0xfcff0fff",
460 },
461 },
462 )
463
464 @patch("jnpr.junos.Device.execute")
465 def test_unstructured_linkstats(self, mock_execute):
466 mock_execute.side_effect = self._mock_manager
467 yaml_data = """
468 ---
469 FPCLinkStatTable:
470 command: show link stats
471 target: Null
472 delimiter: ":"
473 """
474 globals().update(
475 FactoryLoader().load(
476 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
477 )
478 )
479 stats = FPCLinkStatTable(self.dev)
480 stats = stats.get(target="fpc1")
481 self.assertEqual(
482 dict(stats),
483 {
484 "PPP LCP/NCP": 0,
485 "ISIS": 0,
486 "BFD": 15,
487 "OAM": 0,
488 "ETHOAM": 0,
489 "LACP": 0,
490 "LMI": 0,
491 "UBFD": 0,
492 "HDLC keepalives": 0,
493 "OSPF Hello": 539156,
494 "RSVP": 0,
495 },
496 )
497
498 @patch("jnpr.junos.Device.execute")
499 def test_field_eval(self, mock_execute):
500 mock_execute.side_effect = self._mock_manager
501 yaml_data = r"""
502 ---
503 XMChipStatsTable:
504 command: show xmchip {{ instance }} pt stats
505 args:
506 instance: -1
507 target: fpc2
508 item: '*'
509 view: XMChipStatsView
510
511 XMChipStatsView:
512 regex:
513 pct_wi_1: 'PCT entries used by all WI-1 streams\s+:\s?(\d+)'
514 pct_wi_0: 'PCT entries used by all WI-0 streams\s+:\s?(\d+)'
515 eval:
516 total_pct: '{{ pct_wi_1 }} + {{ pct_wi_0 }}'
517 """
518 globals().update(
519 FactoryLoader().load(
520 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
521 )
522 )
523 stats = XMChipStatsTable(self.dev)
524 stats = stats.get(target="fpc1", args={"instance": 0})
525 self.assertEqual(dict(stats), {"pct_wi_1": 0, "pct_wi_0": 0, "total_pct": 0})
526
527 @patch("jnpr.junos.Device.execute")
528 def test__contains__(self, mock_execute):
529 mock_execute.side_effect = self._mock_manager
530 yaml_data = """
531 ---
532 FPCLinkStatTable:
533 command: show link stats
534 target: Null
535 delimiter: ":"
536 """
537 globals().update(
538 FactoryLoader().load(
539 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
540 )
541 )
542 stats = FPCLinkStatTable(self.dev)
543 stats = stats.get(target="fpc1")
544 self.assertTrue("OSPF Hello" in stats)
545
546 @patch("jnpr.junos.Device.execute")
547 def test_unstructured_ttpstatistics(self, mock_execute):
548 mock_execute.side_effect = self._mock_manager
549 yaml_data = """
550 ---
551 FPCTTPStatsTable:
552 command: show ttp statistics
553 target: Null
554 view: FPCTTPStatsView
555
556 FPCTTPStatsView:
557 fields:
558 TTPStatistics: _FPCTTPStatisticsTable
559 TTPTransmitStatistics: _FPCTTPTransmitStatisticsTable
560 TTPReceiveStatistics: _FPCTTPReceiveStatisticsTable
561 TTPQueueSizes: _FPCTTPQueueSizesTable
562 TTPQueueSizes2: _FPCTTPQueueSizesTable2
563
564 _FPCTTPStatisticsTable:
565 title: TTP Statistics
566 view: _FPCTTPStatisticsView
567
568 _FPCTTPStatisticsView:
569 columns:
570 rcvd: Receive
571 tras: Transmit
572
573 _FPCTTPTransmitStatisticsTable:
574 title: TTP Transmit Statistics
575 view: _FPCTTPTransmitStatisticsView
576
577 _FPCTTPTransmitStatisticsView:
578 columns:
579 queue0: Queue 0
580 queue1: Queue 1
581 queue2: Queue 2
582 queue3: Queue 3
583 filters:
584 - queue2
585
586 _FPCTTPReceiveStatisticsTable:
587 title: TTP Receive Statistics
588 key: name
589 key_items:
590 - Coalesce
591 view: _FPCTTPReceiveStatisticsView
592
593 _FPCTTPReceiveStatisticsView:
594 columns:
595 control: Control
596 high: High
597 medium: Medium
598 low: Low
599 discard: Discard
600
601 _FPCTTPQueueSizesTable:
602 title: TTP Receive Queue Sizes
603 delimiter: ":"
604 key_items:
605 - High
606 - Low
607 # view: _FPCTTPQueueSizesView
608
609 _FPCTTPQueueSizesTable2:
610 title: TTP Receive Queue Sizes
611 delimiter: ":"
612 key_items:
613 - High
614 - Low
615 # view: _FPCTTPQueueSizesView
616
617 _FPCTTPQueueSizesView:
618 fields:
619 high: High
620
621 TTPReceiveStatsTable:
622 command: show ttp statistics
623 target: Null
624 title: TTP Receive Statistics
625 key: name
626 view: FPCTTPReceiveStatsView
627
628 FPCTTPReceiveStatsView:
629 columns:
630 control: Control
631 high: High
632 medium: Medium
633 low: Low
634 discard: Discard
635 """
636 globals().update(
637 FactoryLoader().load(
638 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
639 )
640 )
641 stats = FPCTTPStatsTable(self.dev)
642 stats = stats.get(target="fpc2")
643 self.assertEqual(
644 dict(stats),
645 {
646 "TTPQueueSizes": {"High": "0 (max is 4473)", "Low": "0 (max is 2236)"},
647 "TTPQueueSizes2": {"High": "0 (max is 4473)", "Low": "0 (max is 2236)"},
648 "TTPReceiveStatistics": {
649 "Coalesce": {
650 "control": 0,
651 "discard": 0,
652 "high": 0,
653 "low": 0,
654 "medium": 0,
655 "name": "Coalesce",
656 }
657 },
658 "TTPStatistics": {
659 "Coalesce": {"name": "Coalesce", "rcvd": 0, "tras": 0},
660 "Coalesce Fail": {"name": "Coalesce Fail", "rcvd": 0, "tras": 0},
661 "Drops": {"name": "Drops", "rcvd": 0, "tras": 0},
662 "L2 Packets": {"name": "L2 Packets", "rcvd": 4292, "tras": 1093544},
663 "L3 Packets": {"name": "L3 Packets", "rcvd": 542638, "tras": 0},
664 "Netwk Fail": {"name": "Netwk Fail", "rcvd": 0, "tras": 0},
665 "Queue Drops": {"name": "Queue Drops", "rcvd": 0, "tras": 0},
666 "Unknown": {"name": "Unknown", "rcvd": 0, "tras": 0},
667 },
668 "TTPTransmitStatistics": {
669 "L2 Packets": {"queue2": 0},
670 "L3 Packets": {"queue2": 0},
671 },
672 },
673 )
674
675 @patch("jnpr.junos.Device.execute")
676 def test_unstructured_mtip_cge_regex(self, mock_execute):
677 mock_execute.side_effect = self._mock_manager
678 yaml_data = r"""
679 ---
680 MtipCgeSummaryTable:
681 command: show mtip-cge summary
682 target: Null
683 key: id
684 view: MtipCgeSummaryView
685
686 MtipCgeSummaryView:
687 regex:
688 id: numbers
689 name: "[a-z0-9_.]+"
690 fpc: numbers
691 pic: numbers
692 ifd: '(\w+-\d(/\d){2})'
693 ptr: "[a-f0-9]+"
694
695 MtipCgeStatisticsTable:
696 command: show mtip-cge {{ mtip_id }} statistics
697 args:
698 mtip_id: 11
699 target: Null
700 delimiter: ":"
701 title: Statistics
702 key_items:
703 - aFrameCheckSequenceErrors
704 - aAlignmentErrors
705 - aPAUSEMACCtrlFramesTransmitted
706 - aPAUSEMACCtrlFramesReceived
707 - aFrameTooLongErrors
708 - aInRangeLengthErrors
709 """
710 globals().update(
711 FactoryLoader().load(
712 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
713 )
714 )
715 stats = MtipCgeSummaryTable(self.dev)
716 stats = stats.get(target="fpc2")
717 self.assertEqual(
718 dict(stats),
719 {
720 2: {
721 "fpc": 1,
722 "id": 2,
723 "ifd": "et-1/0/0",
724 "name": "mtip_cge.1.0.0",
725 "pic": 0,
726 "ptr": "4f119fb8",
727 },
728 4: {
729 "fpc": 1,
730 "id": 4,
731 "ifd": "et-1/2/0",
732 "name": "mtip_cge.1.2.0",
733 "pic": 2,
734 "ptr": "4f119c98",
735 },
736 5: {
737 "fpc": 1,
738 "id": 5,
739 "ifd": "et-1/2/1",
740 "name": "mtip_cge.1.2.1",
741 "pic": 2,
742 "ptr": "4f119bf8",
743 },
744 },
745 )
746
747 @patch("jnpr.junos.Device.execute")
748 def test_unstructured_icmpstats_nested(self, mock_execute):
749 mock_execute.side_effect = self._mock_manager
750 yaml_data = """
751 ---
752 ICMPStatsTable:
753 command: show icmp statistics
754 target: Null
755 view: ICMPStatsView
756
757 ICMPStatsView:
758 fields:
759 statistics: _ICMPStatisticsTable
760 discards: _ICMPDiscardsTable
761 errors: _ICMPErrorsTable
762 rate: _ICMPRateTable
763
764 _ICMPStatisticsTable:
765 title: ICMP Statistics
766 key: name
767 view: _ICMPStatisticsView
768
769 _ICMPStatisticsView:
770 regex:
771 value: numbers
772 name: words
773
774 _ICMPDiscardsTable:
775 title: ICMP Discards
776 key: name
777 view: _ICMPDiscardsView
778
779 _ICMPDiscardsView:
780 regex:
781 value: numbers
782 name: words
783
784 _ICMPErrorsTable:
785 title: ICMP Errors
786 key: name
787 view: _ICMPErrorsView
788
789 _ICMPErrorsView:
790 regex:
791 error: numbers
792 name: words
793
794 _ICMPRateTable:
795 title: ICMP Rate Limit Settings
796 key: name
797 view: _ICMPRateView
798
799 _ICMPRateView:
800 regex:
801 rate: numbers
802 name: words
803 """
804 globals().update(
805 FactoryLoader().load(
806 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
807 )
808 )
809 stats = ICMPStatsTable(self.dev)
810 stats = stats.get(target="fpc2")
811 self.assertEqual(
812 dict(stats),
813 {
814 "discards": {
815 "bad source addresses": {
816 "name": "bad source addresses",
817 "value": 0,
818 },
819 "unknown originators": {"name": "unknown originators", "value": 0},
820 "bad dest addresses": {"name": "bad dest addresses", "value": 0},
821 "multicasts": {"name": "multicasts", "value": 0},
822 "IP fragments": {"name": "IP fragments", "value": 0},
823 "ICMP errors": {"name": "ICMP errors", "value": 0},
824 },
825 "rate": {
826 "pps total": {"rate": 1000, "name": "pps total"},
827 "pps per iff": {"rate": 500, "name": "pps per iff"},
828 },
829 "statistics": {
830 "other unreachables": {"name": "other unreachables", "value": 0},
831 "redirects": {"name": "redirects", "value": 0},
832 "igmp v1 handoffs": {"name": "igmp v1 handoffs", "value": 0},
833 "parameter problems": {"name": "parameter problems", "value": 0},
834 "throttled": {"name": "throttled", "value": 0},
835 "tag te requests": {"name": "tag te requests", "value": 0},
836 "network unreachables": {
837 "name": "network unreachables",
838 "value": 0,
839 },
840 "source route denials": {
841 "name": "source route denials",
842 "value": 0,
843 },
844 "filter prohibited": {"name": "filter prohibited", "value": 0},
845 "ttl captured": {"name": "ttl captured", "value": 0},
846 "mtu exceeded": {"name": "mtu exceeded", "value": 0},
847 "requests": {"name": "requests", "value": 0},
848 "icmp": {"name": "icmp", "value": 0},
849 "ttl expired": {"name": "ttl expired", "value": 0},
850 "tag te to RE": {"name": "tag te to RE", "value": 0},
851 },
852 "errors": {
853 "bad nh lookup": {"name": "bad nh lookup", "error": 0},
854 "invalid ICMP type": {"name": "invalid ICMP type", "error": 0},
855 "unprocessed redirects": {
856 "name": "unprocessed redirects",
857 "error": 0,
858 },
859 "bad cf mtu": {"name": "bad cf mtu", "error": 0},
860 "bad route lookup": {"name": "bad route lookup", "error": 0},
861 "invalid protocol": {"name": "invalid protocol", "error": 0},
862 "runts": {"name": "runts", "error": 0},
863 "bad input interface": {"name": "bad input interface", "error": 0},
864 "unsupported ICMP type": {
865 "name": "unsupported ICMP type",
866 "error": 0,
867 },
868 "unknown unreachables": {
869 "name": "unknown unreachables",
870 "error": 0,
871 },
872 },
873 },
874 )
875 self.assertTrue(stats.D.__class__.__name__ == "Device")
876
877 @patch("jnpr.junos.Device.execute")
878 def test_unstructured_ithrottle_key_args(self, mock_execute):
879 mock_execute.side_effect = self._mock_manager
880 yaml_data = r"""
881 IthrottleIDTable:
882 command: show ithrottle id {{ id }}
883 args:
884 id: 0
885 item: '*'
886 target: Null
887 view: IthrottleIDView
888
889 IthrottleIDView:
890 regex:
891 min_usage: 'Min Usage Perc: (\d+\.\d+)'
892 max_usage: 'Max Usage Perc: (\d+\.\d+)'
893 usg_enable: 'AdjustUsageEnable: (\d)'
894 fields:
895 throttle_stats: _ThrottleStatsTable
896
897 _ThrottleStatsTable:
898 title: Throttle Stats
899 delimiter: ":"
900 key_items:
901 - Disables
902 - AdjDown
903 - Enables
904 """
905 globals().update(
906 FactoryLoader().load(
907 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
908 )
909 )
910 stats = IthrottleIDTable(self.dev).get(target="fpc2")
911 self.assertEqual(
912 dict(stats),
913 {
914 "usg_enable": 1,
915 "min_usage": 25.0,
916 "throttle_stats": {"Disables": 0, "AdjDown": 4, "Enables": 0},
917 "max_usage": 50.0,
918 },
919 )
920
921 @patch("jnpr.junos.Device.execute")
922 def test_pci_errs_multi_key_regex(self, mock_execute):
923 mock_execute.side_effect = self._mock_manager
924 yaml_data = r"""
925 ---
926 ShowPciErrorsTable:
927 command: show pci errors {{ pci_controller_number }}
928 target: fpc2
929 args:
930 pci_controller_number: 2
931 key:
932 - pci_addr
933 - status_type
934
935 view: ShowPciErrorsView
936
937 ShowPciErrorsView:
938 regex:
939 pci_addr: 'PCI ERROR: (\d+:\d+:\d+:\d+) \(0x[a-z0-9]+\)'
940 status_type: '(Slot|Link) status :'
941 status: '0x[a-z0-9]+'
942 filters:
943 - pci_addr
944 - status
945 """
946 globals().update(
947 FactoryLoader().load(
948 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
949 )
950 )
951 stats = ShowPciErrorsTable(self.dev).get()
952 self.assertEqual(
953 dict(stats),
954 {
955 ("2:2:9:0", "Link"): {"status": "0x00000001", "pci_addr": "2:2:9:0"},
956 ("2:2:9:0", "Slot"): {"status": "0x0000004c", "pci_addr": "2:2:9:0"},
957 },
958 )
959
960 @patch("jnpr.junos.Device.execute")
961 def test_fpcmemory_multi_key_columns(self, mock_execute):
962 mock_execute.side_effect = self._mock_manager
963 yaml_data = """
964 ---
965 FPCMemory:
966 command: show memory
967 target: fpc1
968 key:
969 - id
970 - base
971 view: FPCMemoryView
972
973 FPCMemoryView:
974 columns:
975 id: ID
976 base: Base
977 total: Total(b)
978 free: Free(b)
979 used: Used(b)
980 perc: "%"
981 name: Name
982 filters:
983 - total
984 - free
985 - id
986 - base
987 """
988 globals().update(
989 FactoryLoader().load(
990 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
991 )
992 )
993 stats = FPCMemory(self.dev).get()
994 self.assertEqual(
995 dict(stats),
996 {
997 (2, "bcdfffe0"): {
998 "base": "bcdfffe0",
999 "total": 52428784,
1000 "id": 2,
1001 "free": 52428784,
1002 },
1003 (0, "4d9ad8e8"): {
1004 "base": "4d9ad8e8",
1005 "total": 1726292636,
1006 "id": 0,
1007 "free": 1514622708,
1008 },
1009 (1, "b47ffb88"): {
1010 "base": "b47ffb88",
1011 "total": 67108860,
1012 "id": 1,
1013 "free": 53057404,
1014 },
1015 (3, "b87ffb88"): {
1016 "base": "b87ffb88",
1017 "total": 73400316,
1018 "id": 3,
1019 "free": 73400316,
1020 },
1021 },
1022 )
1023
1024 @patch("jnpr.junos.Device.execute")
1025 def test_item_regex_pq3_pci(self, mock_execute):
1026 mock_execute.side_effect = self._mock_manager
1027 yaml_data = r"""
1028 ---
1029 PQ3PCITable:
1030 command: show pq3 pci
1031 target: Null
1032 item: PCI
1033 key:
1034 - pci
1035 view: PQ3PCI
1036
1037 PQ3PCI:
1038 regex:
1039 pci: 'PCI (\d+(:\d+){3})'
1040 rto: 'rto (\d+)'
1041 rnr: 'rnr (\d+)'
1042 rxe: 'rxe (\d+)'
1043 bdllp: 'bdllp (\d+)'
1044 btlp: 'btlp (\d+)'
1045 """
1046 globals().update(
1047 FactoryLoader().load(
1048 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
1049 )
1050 )
1051 stats = PQ3PCITable(self.dev)
1052 stats = stats.get(target="fpc2")
1053 self.assertEqual(
1054 dict(stats),
1055 {
1056 "0:0:0:0": {
1057 "bdllp": 0,
1058 "btlp": 0,
1059 "pci": "0:0:0:0",
1060 "rnr": 0,
1061 "rto": 0,
1062 "rxe": 0,
1063 },
1064 "2:0:0:0": {
1065 "bdllp": 0,
1066 "btlp": 0,
1067 "pci": "2:0:0:0",
1068 "rnr": 0,
1069 "rto": 0,
1070 "rxe": 0,
1071 },
1072 "2:1:0:0": {
1073 "bdllp": 0,
1074 "btlp": 0,
1075 "pci": "2:1:0:0",
1076 "rnr": 0,
1077 "rto": 0,
1078 "rxe": 0,
1079 },
1080 "2:2:1:0": {
1081 "bdllp": 0,
1082 "btlp": 0,
1083 "pci": "2:2:1:0",
1084 "rnr": 0,
1085 "rto": 0,
1086 "rxe": 0,
1087 },
1088 "2:2:2:0": {
1089 "bdllp": 0,
1090 "btlp": 0,
1091 "pci": "2:2:2:0",
1092 "rnr": 0,
1093 "rto": 0,
1094 "rxe": 0,
1095 },
1096 "2:2:4:0": {
1097 "bdllp": 0,
1098 "btlp": 0,
1099 "pci": "2:2:4:0",
1100 "rnr": 0,
1101 "rto": 0,
1102 "rxe": 0,
1103 },
1104 "2:2:5:0": {
1105 "bdllp": 0,
1106 "btlp": 0,
1107 "pci": "2:2:5:0",
1108 "rnr": 0,
1109 "rto": 0,
1110 "rxe": 0,
1111 },
1112 "2:2:6:0": {
1113 "bdllp": 0,
1114 "btlp": 0,
1115 "pci": "2:2:6:0",
1116 "rnr": 0,
1117 "rto": 0,
1118 "rxe": 0,
1119 },
1120 "2:2:7:0": {
1121 "bdllp": 0,
1122 "btlp": 0,
1123 "pci": "2:2:7:0",
1124 "rnr": 0,
1125 "rto": 0,
1126 "rxe": 0,
1127 },
1128 "2:2:8:0": {
1129 "bdllp": 0,
1130 "btlp": 0,
1131 "pci": "2:2:8:0",
1132 "rnr": 0,
1133 "rto": 0,
1134 "rxe": 0,
1135 },
1136 "2:2:9:0": {
1137 "bdllp": 0,
1138 "btlp": 0,
1139 "pci": "2:2:9:0",
1140 "rnr": 0,
1141 "rto": 0,
1142 "rxe": 0,
1143 },
1144 },
1145 )
1146
1147 @patch("jnpr.junos.Device.execute")
1148 def test_regex_with_fields(self, mock_execute):
1149 mock_execute.side_effect = self._mock_manager
1150 yaml_data = r"""
1151 ---
1152 SchedulerTable:
1153 command: show sched
1154 target: Null
1155 key: name
1156 view: SchedulerView
1157
1158 SchedulerView:
1159 fields:
1160 thread: _TopThreadTable
1161 columns:
1162 cpu: CPU
1163 name: Name
1164 time_ms: Time(ms)
1165 regex:
1166 interrupt_time: 'Total interrupt time (\d+)'
1167
1168 _TopThreadTable:
1169 title: Top Thread
1170 delimiter: "="
1171 """
1172 globals().update(
1173 FactoryLoader().load(
1174 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
1175 )
1176 )
1177 stats = SchedulerTable(self.dev)
1178 stats = stats.get(target="fpc1")
1179 self.assertEqual(
1180 dict(stats),
1181 {
1182 "interrupt_time": 16786614,
1183 "Idle": {"time_ms": 7397672498, "cpu": "85%", "name": "Idle"},
1184 "Level 3": {"time_ms": 1, "cpu": "0%", "name": "Level 3"},
1185 "thread": {
1186 "cpu": "4%",
1187 "pid": 99,
1188 "name": "LU Background Service",
1189 "time": "410844018 ms",
1190 },
1191 },
1192 )
1193
1194 @patch("jnpr.junos.Device.execute")
1195 def test_exists(self, mock_execute):
1196 mock_execute.side_effect = self._mock_manager
1197 yaml_data = """
1198 ---
1199 HostlbStatusSummaryTable:
1200 command: show host_loopback status-summary
1201 target: Null
1202 view: HostlbStatusSummaryView
1203
1204 HostlbStatusSummaryView:
1205 exists:
1206 no_detected_wedges: No detected wedges
1207 no_toolkit_errors: No toolkit errors
1208 """
1209 globals().update(
1210 FactoryLoader().load(
1211 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
1212 )
1213 )
1214 stats = HostlbStatusSummaryTable(self.dev)
1215 stats = stats.get(target="fpc3")
1216 self.assertEqual(
1217 dict(stats), {"no_detected_wedges": True, "no_toolkit_errors": True}
1218 )
1219
1220 @patch("jnpr.junos.Device.execute")
1221 def test_table_path_option(self, mock_execute):
1222 mock_execute.side_effect = self._mock_manager
1223 yaml_data = """
1224 ---
1225 HostlbStatusSummaryTable:
1226 command: show host_loopback status-summary
1227 target: Null
1228 view: HostlbStatusSummaryView
1229
1230 HostlbStatusSummaryView:
1231 exists:
1232 no_detected_wedges: No detected wedges
1233 no_toolkit_errors: No toolkit errors
1234 """
1235 globals().update(
1236 FactoryLoader().load(
1237 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
1238 )
1239 )
1240 stats = HostlbStatusSummaryTable(
1241 path=os.path.join(
1242 os.path.dirname(__file__),
1243 "rpc-reply",
1244 "show_host_loopback_status-summary.xml",
1245 )
1246 )
1247 stats = stats.get()
1248 self.assertEqual(
1249 dict(stats), {"no_detected_wedges": True, "no_toolkit_errors": True}
1250 )
1251
1252 @patch("jnpr.junos.Device.execute")
1253 def test_table_with_item_regex(self, mock_execute):
1254 mock_execute.side_effect = self._mock_manager
1255 yaml_data = r"""
1256 ---
1257 DevicesLocalTable:
1258 command: show devices local
1259 target: Null
1260 item: 'TSEC Ethernet Device Driver: (\.?\w+),'
1261 key: name
1262 view: DevicesLocalView
1263
1264 DevicesLocalView:
1265 fields:
1266 TSEC_status_counters: _TSECStatusCountersTable
1267 receive_counters: _ReceiveTable
1268 transmit_per_queue: _TransmitQueueTable
1269
1270 _ReceiveTable:
1271 item: '*'
1272 title: 'Receive:'
1273 view: _ReceiveView
1274
1275 _ReceiveView:
1276 regex:
1277 bytes: '(\d+) bytes'
1278 packets: '(\d+) packets'
1279 FCS_errors: '(\d+) FCS errors'
1280 broadcast_packets: '(\d+) broadcast packets'
1281
1282 _TSECStatusCountersTable:
1283 item: '*'
1284 title: 'TSEC status counters:'
1285 view: _TSECStatusCountersView
1286
1287 _TSECStatusCountersView:
1288 regex:
1289 kernel_dropped: 'kernel_dropped:(\d+)'
1290 rx_large: 'rx_large:(\d+)'
1291
1292 _TransmitQueueTable:
1293 item: '\[(\d+)\]'
1294 title: 'Transmit per queue:'
1295 view: _TransmitQueueView
1296
1297 _TransmitQueueView:
1298 fields:
1299 queue: _TransmitPerQueueTable
1300
1301 _TransmitPerQueueTable:
1302 title: 'Transmit per queue:'
1303 item: '*'
1304 view: _TransmitPerQueueView
1305
1306 _TransmitPerQueueView:
1307 regex:
1308 bytes: '(\d+) bytes'
1309 packets: '(\d+) packets'
1310 """
1311 globals().update(
1312 FactoryLoader().load(
1313 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
1314 )
1315 )
1316 stats = DevicesLocalTable(self.dev)
1317 stats = stats.get(target="fpc1")
1318 self.assertEqual(
1319 dict(stats),
1320 {
1321 ".le1": {
1322 "TSEC_status_counters": {"kernel_dropped": 0, "rx_large": 0},
1323 "receive_counters": {
1324 "FCS_errors": 0,
1325 "broadcast_packets": 107271,
1326 "bytes": 185584608,
1327 "packets": 2250212,
1328 },
1329 "transmit_per_queue": {
1330 0: {"queue": {"bytes": 10300254, "packets": 72537}},
1331 1: {"queue": {"bytes": 4474302, "packets": 106531}},
1332 2: {"queue": {"bytes": 260203538, "packets": 1857137}},
1333 3: {"queue": {"bytes": 199334, "packets": 2179}},
1334 },
1335 },
1336 ".le3": {
1337 "TSEC_status_counters": {"kernel_dropped": 0, "rx_large": 0},
1338 "receive_counters": {
1339 "FCS_errors": 0,
1340 "broadcast_packets": 0,
1341 "bytes": 0,
1342 "packets": 0,
1343 },
1344 "transmit_per_queue": {
1345 0: {"queue": {"bytes": 0, "packets": 0}},
1346 1: {"queue": {"bytes": 4474302, "packets": 106531}},
1347 2: {"queue": {"bytes": 0, "packets": 0}},
1348 3: {"queue": {"bytes": 0, "packets": 0}},
1349 },
1350 },
1351 },
1352 )
1353
1354 @patch("jnpr.junos.Device.execute")
1355 def test_table_item_group_key_mismatch(self, mock_execute):
1356 mock_execute.side_effect = self._mock_manager
1357 yaml_data = r"""
1358 ---
1359 DevicesLocalTable:
1360 command: show devices local
1361 target: fpc1
1362 item: '(TSEC) Ethernet Device Driver: (\.?\w+),'
1363 key: name
1364 view: DevicesLocalView
1365
1366 DevicesLocalView:
1367 fields:
1368 receive_counters: _ReceiveTable
1369
1370 _ReceiveTable:
1371 item: '*'
1372 title: 'Receive:'
1373 view: _ReceiveView
1374
1375 _ReceiveView:
1376 regex:
1377 bytes: '(\d+) bytes'
1378 packets: '(\d+) packets'
1379 FCS_errors: '(\d+) FCS errors'
1380 broadcast_packets: '(\d+) broadcast packets'
1381 """
1382 globals().update(
1383 FactoryLoader().load(
1384 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
1385 )
1386 )
1387 stats = DevicesLocalTable(self.dev)
1388 self.assertRaises(KeyError, stats.get)
1389
1390 @patch("jnpr.junos.Device.execute")
1391 def test_table_with_item_without_view(self, mock_execute):
1392 mock_execute.side_effect = self._mock_manager
1393 yaml_data = r"""
1394 ---
1395 EthernetSwitchStatisticsIterTable:
1396 command: show chassis ethernet-switch statistics
1397 item: Statistics for port (\d) connected to device (FPC\d)
1398 key:
1399 - port
1400 - fpc
1401 """
1402 globals().update(
1403 FactoryLoader().load(
1404 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
1405 )
1406 )
1407 stats = EthernetSwitchStatisticsIterTable(self.dev)
1408 stats = stats.get()
1409 self.assertEqual(
1410 dict(stats),
1411 {
1412 (1, "FPC1"): {
1413 "RX Broadcast Packets": 9206817,
1414 "RX Byte Counter": 320925468,
1415 "RX Packets 1024-1518 Octets": 3200039,
1416 "RX Packets 512-1023 Octets": 4428661,
1417 "TX Packets 128-255 Octets": 6738243,
1418 "TX Packets 64 Octets": 142545489,
1419 "TX Packets 65-127 Octets": 83498558,
1420 },
1421 (2, "FPC2"): {
1422 "RX Byte Counter": 133138157,
1423 "RX Packets 1024-1518 Octets": 7994417,
1424 "RX Packets 128-255 Octets": 5848046,
1425 "RX Packets 256-511 Octets": 14518495,
1426 "RX Packets 512-1023 Octets": 33598800,
1427 "RX Packets 64 Octets": 69281885,
1428 "RX Packets 65-127 Octets": 98793558,
1429 "TX Broadcast Packets": 46011330,
1430 "TX Byte Counter": 12099160,
1431 "TX Packets 128-255 Octets": 11245893,
1432 "TX Packets 256-511 Octets": 55746,
1433 "TX Packets 512-1023 Octets": 3669,
1434 "TX Packets 64 Octets": 140666613,
1435 "TX Packets 65-127 Octets": 105545277,
1436 },
1437 (4, "FPC4"): {
1438 "RX 1519-1522 Good Vlan frms": 0,
1439 "RX Align Errors": 0,
1440 "RX Broadcast Packets": 9216795,
1441 "RX Byte Counter": 361322476,
1442 "RX Control Frame Counter": 0,
1443 "RX FCS Errors": 0,
1444 "RX False Carrier Errors": 0,
1445 "RX Fragments": 0,
1446 "RX Jabbers": 0,
1447 "RX MTU Exceed Counter": 0,
1448 "RX Multicast Packets": 0,
1449 "RX Octets": 212488240,
1450 "RX Out of Range Length": 0,
1451 "RX Oversize Packets": 0,
1452 "RX Packets 1024-1518 Octets": 5524595,
1453 "RX Packets 128-255 Octets": 7153230,
1454 "RX Packets 1519-2047 Octets": 0,
1455 "RX Packets 2048-4095 Octets": 0,
1456 "RX Packets 256-511 Octets": 2130213,
1457 "RX Packets 4096-9216 Octets": 0,
1458 "RX Packets 512-1023 Octets": 24031621,
1459 "RX Packets 64 Octets": 69309261,
1460 "RX Packets 65-127 Octets": 104339320,
1461 "RX Pause Frame Counter": 0,
1462 "RX Symbol errors": 0,
1463 "RX Undersize Packets": 0,
1464 "RX Unsupported opcodes": 0,
1465 "TX 1519-1522 Good Vlan frms": 0,
1466 "TX Broadcast Packets": 45997099,
1467 "TX Byte Counter": 725771059,
1468 "TX Collision frames": 0,
1469 "TX Excessive Collisions": 0,
1470 "TX FCS Error Counter": 0,
1471 "TX Fragment Counter": 0,
1472 "TX Frame deferred Xmns": 0,
1473 "TX Frame excessive deferl": 0,
1474 "TX Jabbers": 0,
1475 "TX Late Collisions": 0,
1476 "TX MAC ctrl frames": 0,
1477 "TX Mult. Collision frames": 0,
1478 "TX Multicast Packets": 6,
1479 "TX Octets": 246717004,
1480 "TX Oversize Packets": 0,
1481 "TX PAUSEMAC Ctrl Frames": 0,
1482 "TX Packets 1024-1518 Octets": 72615,
1483 "TX Packets 128-255 Octets": 15940774,
1484 "TX Packets 1519-2047 Octets": 0,
1485 "TX Packets 2048-4095 Octets": 0,
1486 "TX Packets 256-511 Octets": 6737,
1487 "TX Packets 4096-9216 Octets": 0,
1488 "TX Packets 512-1023 Octets": 2934,
1489 "TX Packets 64 Octets": 1397543,
1490 "TX Packets 65-127 Octets": 91616401,
1491 "TX Single Collision frames": 0,
1492 },
1493 },
1494 )
1495
1496 @patch("jnpr.junos.Device.execute")
1497 def test_title_without_view(self, mock_execute):
1498 mock_execute.side_effect = self._mock_manager
1499 yaml_data = """
1500 ---
1501 EthernetSwitchStatistics:
1502 command: show chassis ethernet-switch statistics
1503 view: EthernetSwitchStatisticsView
1504
1505 EthernetSwitchStatisticsView:
1506 fields:
1507 fpc0: _EthSwitchStatsFpc0Table
1508 fpc1: _EthSwitchStatsFpc1Table
1509 fpc2: _EthSwitchStatsFpc2Table
1510 fpc3: _EthSwitchStatsFpc3Table
1511 fpc4: _EthSwitchStatsFpc4Table
1512 fpc5: _EthSwitchStatsFpc5Table
1513
1514 _EthSwitchStatsFpc0Table:
1515 title: Statistics for port 0 connected to device FPC0
1516
1517 _EthSwitchStatsFpc1Table:
1518 title: Statistics for port 1 connected to device FPC1
1519
1520 _EthSwitchStatsFpc2Table:
1521 title: Statistics for port 2 connected to device FPC2
1522
1523 _EthSwitchStatsFpc3Table:
1524 title: Statistics for port 3 connected to device FPC3
1525
1526 _EthSwitchStatsFpc4Table:
1527 title: Statistics for port 4 connected to device FPC4
1528
1529 _EthSwitchStatsFpc5Table:
1530 title: Statistics for port 5 connected to device FPC5
1531 """
1532 globals().update(
1533 FactoryLoader().load(
1534 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
1535 )
1536 )
1537 stats = EthernetSwitchStatistics(self.dev)
1538 stats = stats.get()
1539 self.assertEqual(
1540 dict(stats),
1541 {
1542 "fpc0": {},
1543 "fpc1": {
1544 "RX Broadcast Packets": 9206817,
1545 "RX Byte Counter": 320925468,
1546 "RX Packets 1024-1518 Octets": 3200039,
1547 "RX Packets 512-1023 Octets": 4428661,
1548 "TX Packets 128-255 Octets": 6738243,
1549 "TX Packets 64 Octets": 142545489,
1550 "TX Packets 65-127 Octets": 83498558,
1551 },
1552 "fpc2": {
1553 "RX Byte Counter": 133138157,
1554 "RX Packets 1024-1518 Octets": 7994417,
1555 "RX Packets 128-255 Octets": 5848046,
1556 "RX Packets 256-511 Octets": 14518495,
1557 "RX Packets 512-1023 Octets": 33598800,
1558 "RX Packets 64 Octets": 69281885,
1559 "RX Packets 65-127 Octets": 98793558,
1560 "TX Broadcast Packets": 46011330,
1561 "TX Byte Counter": 12099160,
1562 "TX Packets 128-255 Octets": 11245893,
1563 "TX Packets 256-511 Octets": 55746,
1564 "TX Packets 512-1023 Octets": 3669,
1565 "TX Packets 64 Octets": 140666613,
1566 "TX Packets 65-127 Octets": 105545277,
1567 },
1568 "fpc3": {},
1569 "fpc4": {
1570 "RX 1519-1522 Good Vlan frms": 0,
1571 "RX Align Errors": 0,
1572 "RX Broadcast Packets": 9216795,
1573 "RX Byte Counter": 361322476,
1574 "RX Control Frame Counter": 0,
1575 "RX FCS Errors": 0,
1576 "RX False Carrier Errors": 0,
1577 "RX Fragments": 0,
1578 "RX Jabbers": 0,
1579 "RX MTU Exceed Counter": 0,
1580 "RX Multicast Packets": 0,
1581 "RX Octets": 212488240,
1582 "RX Out of Range Length": 0,
1583 "RX Oversize Packets": 0,
1584 "RX Packets 1024-1518 Octets": 5524595,
1585 "RX Packets 128-255 Octets": 7153230,
1586 "RX Packets 1519-2047 Octets": 0,
1587 "RX Packets 2048-4095 Octets": 0,
1588 "RX Packets 256-511 Octets": 2130213,
1589 "RX Packets 4096-9216 Octets": 0,
1590 "RX Packets 512-1023 Octets": 24031621,
1591 "RX Packets 64 Octets": 69309261,
1592 "RX Packets 65-127 Octets": 104339320,
1593 "RX Pause Frame Counter": 0,
1594 "RX Symbol errors": 0,
1595 "RX Undersize Packets": 0,
1596 "RX Unsupported opcodes": 0,
1597 "TX 1519-1522 Good Vlan frms": 0,
1598 "TX Broadcast Packets": 45997099,
1599 "TX Byte Counter": 725771059,
1600 "TX Collision frames": 0,
1601 "TX Excessive Collisions": 0,
1602 "TX FCS Error Counter": 0,
1603 "TX Fragment Counter": 0,
1604 "TX Frame deferred Xmns": 0,
1605 "TX Frame excessive deferl": 0,
1606 "TX Jabbers": 0,
1607 "TX Late Collisions": 0,
1608 "TX MAC ctrl frames": 0,
1609 "TX Mult. Collision frames": 0,
1610 "TX Multicast Packets": 6,
1611 "TX Octets": 246717004,
1612 "TX Oversize Packets": 0,
1613 "TX PAUSEMAC Ctrl Frames": 0,
1614 "TX Packets 1024-1518 Octets": 72615,
1615 "TX Packets 128-255 Octets": 15940774,
1616 "TX Packets 1519-2047 Octets": 0,
1617 "TX Packets 2048-4095 Octets": 0,
1618 "TX Packets 256-511 Octets": 6737,
1619 "TX Packets 4096-9216 Octets": 0,
1620 "TX Packets 512-1023 Octets": 2934,
1621 "TX Packets 64 Octets": 1397543,
1622 "TX Packets 65-127 Octets": 91616401,
1623 "TX Single Collision frames": 0,
1624 },
1625 "fpc5": {},
1626 },
1627 )
1628
1629 @patch("jnpr.junos.Device.execute")
1630 def test_valueerror_with_no_target(self, mock_execute):
1631 mock_execute.side_effect = self._mock_manager
1632 yaml_data = r"""
1633 ---
1634 ShowToePfePacketStatsTable:
1635 command: show toe pfe {{ pfe_instance }} {{ asic_type }} {{ asic_instance }} toe-inst {{ toe_instance }} packet-stats
1636 args:
1637 pfe_instance: 0
1638 asic_type: xm
1639 asic_instance: 0
1640 toe_instance: 0
1641 target: Null
1642 item: 'Stream (\d): halt flag is NOT set'
1643 key: stream
1644 view: ShowToePfePacketStatsView
1645
1646 ShowToePfePacketStatsView:
1647 fields:
1648 tx_packets: _ShowToePfePacketStatsStream_tx_packets
1649 tx_descriptors: _ShowToePfePacketStatsStream_tx_descriptors
1650 tx_rates: _ShowToePfePacketStatsStream_tx_rates
1651 tx_errors: _ShowToePfePacketStatsStream_tx_errors
1652 rx_packets: _ShowToePfePacketStatsStream_rx_packets
1653 rx_descriptors: _ShowToePfePacketStatsStream_rx_descriptors
1654 rx_rates: _ShowToePfePacketStatsStream_rx_rates
1655 rx_errors: _ShowToePfePacketStatsStream_rx_errors
1656
1657 _ShowToePfePacketStatsStream_tx_packets:
1658 title: 'TX Packets'
1659 delimiter: ':'
1660
1661 _ShowToePfePacketStatsStream_tx_descriptors:
1662 title: 'TX Descriptors'
1663 delimiter: ':'
1664
1665 _ShowToePfePacketStatsStream_tx_rates:
1666 title: 'TX Rates:'
1667 delimiter: ':'
1668
1669 _ShowToePfePacketStatsStream_tx_errors:
1670 title: 'TX Errors:'
1671 delimiter: ':'
1672
1673 _ShowToePfePacketStatsStream_rx_packets:
1674 title: 'RX Packets'
1675 delimiter: ':'
1676
1677 _ShowToePfePacketStatsStream_rx_descriptors:
1678 title: 'RX Descriptors'
1679 delimiter: ':'
1680
1681 _ShowToePfePacketStatsStream_rx_rates:
1682 title: 'RX Rates:'
1683 delimiter: ':'
1684
1685 _ShowToePfePacketStatsStream_rx_errors:
1686 title: 'RX Errors:'
1687 delimiter: ':'
1688 """
1689 globals().update(
1690 FactoryLoader().load(
1691 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
1692 )
1693 )
1694 stats = ShowToePfePacketStatsTable(self.dev)
1695 self.assertRaises(ValueError, stats.get)
1696
1697 @patch("jnpr.junos.Device.execute")
1698 def test_item_with_fields_delimiter(self, mock_execute):
1699 mock_execute.side_effect = self._mock_manager
1700 yaml_data = r"""
1701 ---
1702 ShowToePfePacketStatsTable:
1703 command: show toe pfe {{ pfe_instance }} {{ asic_type }} {{ asic_instance }} toe-inst {{ toe_instance }} packet-stats
1704 args:
1705 pfe_instance: 0
1706 asic_type: xm
1707 asic_instance: 0
1708 toe_instance: 0
1709 target: Null
1710 item: 'Stream (\d): halt flag is NOT set'
1711 key: stream
1712 view: ShowToePfePacketStatsView
1713
1714 ShowToePfePacketStatsView:
1715 fields:
1716 tx_packets: _ShowToePfePacketStatsStream_tx_packets
1717 tx_descriptors: _ShowToePfePacketStatsStream_tx_descriptors
1718 tx_rates: _ShowToePfePacketStatsStream_tx_rates
1719 tx_errors: _ShowToePfePacketStatsStream_tx_errors
1720 rx_packets: _ShowToePfePacketStatsStream_rx_packets
1721 rx_descriptors: _ShowToePfePacketStatsStream_rx_descriptors
1722 rx_rates: _ShowToePfePacketStatsStream_rx_rates
1723 rx_errors: _ShowToePfePacketStatsStream_rx_errors
1724
1725 _ShowToePfePacketStatsStream_tx_packets:
1726 title: 'TX Packets'
1727 delimiter: ':'
1728
1729 _ShowToePfePacketStatsStream_tx_descriptors:
1730 title: 'TX Descriptors'
1731 delimiter: ':'
1732
1733 _ShowToePfePacketStatsStream_tx_rates:
1734 title: 'TX Rates:'
1735 delimiter: ':'
1736
1737 _ShowToePfePacketStatsStream_tx_errors:
1738 title: 'TX Errors:'
1739 delimiter: ':'
1740
1741 _ShowToePfePacketStatsStream_rx_packets:
1742 title: 'RX Packets'
1743 delimiter: ':'
1744
1745 _ShowToePfePacketStatsStream_rx_descriptors:
1746 title: 'RX Descriptors'
1747 delimiter: ':'
1748
1749 _ShowToePfePacketStatsStream_rx_rates:
1750 title: 'RX Rates:'
1751 delimiter: ':'
1752
1753 _ShowToePfePacketStatsStream_rx_errors:
1754 title: 'RX Errors:'
1755 delimiter: ':'
1756 """
1757 globals().update(
1758 FactoryLoader().load(
1759 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
1760 )
1761 )
1762 stats = ShowToePfePacketStatsTable(self.dev)
1763 stats = stats.get(target="fpc1")
1764 self.assertEqual(
1765 dict(stats),
1766 {
1767 0: {
1768 "rx_descriptors": {
1769 "completed": 12665827,
1770 "recycle fails": 0,
1771 "recycled": 12665827,
1772 },
1773 "rx_errors": {
1774 "SOP/prev packet": 0,
1775 "init descriptor idx invalid": 0,
1776 "no prev packet": 0,
1777 "no start packet": 0,
1778 },
1779 "rx_packets": {"accepted": 12665827},
1780 "rx_rates": {
1781 "bytes per second": 34,
1782 "completed since last count": 102,
1783 "descriptors per second": 1,
1784 "packets per second": 1,
1785 },
1786 "tx_descriptors": {"accepted": 12665916, "completed": 12665916},
1787 "tx_errors": {
1788 "FIFO not initialized": 0,
1789 "head descriptor invalid": 0,
1790 "init descriptor idx invalid": 0,
1791 "packet buffer out of range": 0,
1792 "packet length out of range": 0,
1793 "packet null": 0,
1794 },
1795 "tx_packets": {
1796 "accepted": 12665916,
1797 "rejected": 0,
1798 "transferred": 12665916,
1799 },
1800 "tx_rates": {
1801 "bytes per second": 34,
1802 "descriptors completed since last count": 102,
1803 "descriptors per second": 1,
1804 "packets per second": 1,
1805 },
1806 },
1807 1: {
1808 "rx_descriptors": {
1809 "completed": 12665827,
1810 "recycle fails": 0,
1811 "recycled": 12665827,
1812 },
1813 "rx_errors": {
1814 "SOP/prev packet": 0,
1815 "init descriptor idx invalid": 0,
1816 "no prev packet": 0,
1817 "no start packet": 0,
1818 },
1819 "rx_packets": {"accepted": 12665827},
1820 "rx_rates": {
1821 "bytes per second": 34,
1822 "completed since last count": 102,
1823 "descriptors per second": 1,
1824 "packets per second": 1,
1825 },
1826 "tx_descriptors": {"accepted": 12665916, "completed": 12665916},
1827 "tx_errors": {
1828 "FIFO not initialized": 0,
1829 "head descriptor invalid": 0,
1830 "init descriptor idx invalid": 0,
1831 "packet buffer out of range": 0,
1832 "packet length out of range": 0,
1833 "packet null": 0,
1834 },
1835 "tx_packets": {
1836 "accepted": 12665916,
1837 "rejected": 0,
1838 "transferred": 12665916,
1839 },
1840 "tx_rates": {
1841 "bytes per second": 34,
1842 "descriptors completed since last count": 102,
1843 "descriptors per second": 1,
1844 "packets per second": 1,
1845 },
1846 },
1847 2: {
1848 "rx_descriptors": {
1849 "completed": 0,
1850 "recycle fails": 0,
1851 "recycled": 0,
1852 },
1853 "rx_errors": {
1854 "SOP/prev packet": 0,
1855 "init descriptor idx invalid": 0,
1856 "no prev packet": 0,
1857 "no start packet": 0,
1858 },
1859 "rx_packets": {"accepted": 0},
1860 "rx_rates": {
1861 "bytes per second": 0,
1862 "completed since last count": 0,
1863 "descriptors per second": 0,
1864 "packets per second": 0,
1865 },
1866 "tx_descriptors": {"accepted": 0, "completed": 0},
1867 "tx_errors": {
1868 "FIFO not initialized": 0,
1869 "head descriptor invalid": 0,
1870 "init descriptor idx invalid": 0,
1871 "packet buffer out of range": 0,
1872 "packet length out of range": 0,
1873 "packet null": 0,
1874 },
1875 "tx_packets": {"accepted": 0, "rejected": 0, "transferred": 0},
1876 "tx_rates": {
1877 "bytes per second": 0,
1878 "descriptors completed since last count": 0,
1879 "descriptors per second": 0,
1880 "packets per second": 0,
1881 },
1882 },
1883 3: {
1884 "rx_descriptors": {
1885 "completed": 0,
1886 "recycle fails": 0,
1887 "recycled": 0,
1888 },
1889 "rx_errors": {
1890 "SOP/prev packet": 0,
1891 "init descriptor idx invalid": 0,
1892 "no prev packet": 0,
1893 "no start packet": 0,
1894 },
1895 "rx_packets": {"accepted": 0},
1896 "rx_rates": {
1897 "bytes per second": 0,
1898 "completed since last count": 0,
1899 "descriptors per second": 0,
1900 "packets per second": 0,
1901 },
1902 "tx_descriptors": {"accepted": 0, "completed": 0},
1903 "tx_errors": {
1904 "FIFO not initialized": 0,
1905 "head descriptor invalid": 0,
1906 "init descriptor idx invalid": 0,
1907 "packet buffer out of range": 0,
1908 "packet length out of range": 0,
1909 "packet null": 0,
1910 },
1911 "tx_packets": {"accepted": 0, "rejected": 0, "transferred": 0},
1912 "tx_rates": {
1913 "bytes per second": 0,
1914 "descriptors completed since last count": 0,
1915 "descriptors per second": 0,
1916 "packets per second": 0,
1917 },
1918 },
1919 4: {
1920 "rx_descriptors": {
1921 "completed": 67151069,
1922 "recycle fails": 0,
1923 "recycled": 67151069,
1924 },
1925 "rx_errors": {
1926 "SOP/prev packet": 0,
1927 "init descriptor idx invalid": 0,
1928 "no prev packet": 0,
1929 "no start packet": 0,
1930 },
1931 "rx_packets": {"accepted": 67151069},
1932 "rx_rates": {
1933 "bytes per second": 367,
1934 "completed since last count": 552,
1935 "descriptors per second": 5,
1936 "packets per second": 5,
1937 },
1938 "tx_descriptors": {"accepted": 70932350, "completed": 70932350},
1939 "tx_errors": {
1940 "FIFO not initialized": 0,
1941 "head descriptor invalid": 0,
1942 "init descriptor idx invalid": 0,
1943 "packet buffer out of range": 0,
1944 "packet length out of range": 0,
1945 "packet null": 0,
1946 },
1947 "tx_packets": {
1948 "accepted": 70932350,
1949 "rejected": 0,
1950 "transferred": 70932350,
1951 },
1952 "tx_rates": {
1953 "bytes per second": 301,
1954 "descriptors completed since last count": 586,
1955 "descriptors per second": 5,
1956 "packets per second": 5,
1957 },
1958 },
1959 5: {
1960 "rx_descriptors": {
1961 "completed": 0,
1962 "recycle fails": 0,
1963 "recycled": 0,
1964 },
1965 "rx_errors": {
1966 "SOP/prev packet": 0,
1967 "init descriptor idx invalid": 0,
1968 "no prev packet": 0,
1969 "no start packet": 0,
1970 },
1971 "rx_packets": {"accepted": 0},
1972 "rx_rates": {
1973 "bytes per second": 0,
1974 "completed since last count": 0,
1975 "descriptors per second": 0,
1976 "packets per second": 0,
1977 },
1978 "tx_descriptors": {"accepted": 0, "completed": 0},
1979 "tx_errors": {
1980 "FIFO not initialized": 0,
1981 "head descriptor invalid": 0,
1982 "init descriptor idx invalid": 0,
1983 "packet buffer out of range": 0,
1984 "packet length out of range": 0,
1985 "packet null": 0,
1986 },
1987 "tx_packets": {"accepted": 0, "rejected": 0, "transferred": 0},
1988 "tx_rates": {
1989 "bytes per second": 0,
1990 "descriptors completed since last count": 0,
1991 "descriptors per second": 0,
1992 "packets per second": 0,
1993 },
1994 },
1995 6: {
1996 "rx_descriptors": {
1997 "completed": 0,
1998 "recycle fails": 0,
1999 "recycled": 0,
2000 },
2001 "rx_errors": {
2002 "SOP/prev packet": 0,
2003 "init descriptor idx invalid": 0,
2004 "no prev packet": 0,
2005 "no start packet": 0,
2006 },
2007 "rx_packets": {"accepted": 0},
2008 "rx_rates": {
2009 "bytes per second": 0,
2010 "completed since last count": 0,
2011 "descriptors per second": 0,
2012 "packets per second": 0,
2013 },
2014 "tx_descriptors": {"accepted": 0, "completed": 0},
2015 "tx_errors": {
2016 "FIFO not initialized": 0,
2017 "head descriptor invalid": 0,
2018 "init descriptor idx invalid": 0,
2019 "packet buffer out of range": 0,
2020 "packet length out of range": 0,
2021 "packet null": 0,
2022 },
2023 "tx_packets": {"accepted": 0, "rejected": 0, "transferred": 0},
2024 "tx_rates": {
2025 "bytes per second": 0,
2026 "descriptors completed since last count": 0,
2027 "descriptors per second": 0,
2028 "packets per second": 0,
2029 },
2030 },
2031 7: {
2032 "rx_descriptors": {
2033 "completed": 0,
2034 "recycle fails": 0,
2035 "recycled": 0,
2036 },
2037 "rx_errors": {
2038 "SOP/prev packet": 0,
2039 "init descriptor idx invalid": 0,
2040 "no prev packet": 0,
2041 "no start packet": 0,
2042 },
2043 "rx_packets": {"accepted": 0},
2044 "rx_rates": {
2045 "bytes per second": 0,
2046 "completed since last count": 0,
2047 "descriptors per second": 0,
2048 "packets per second": 0,
2049 },
2050 "tx_descriptors": {"accepted": 0, "completed": 0},
2051 "tx_errors": {
2052 "FIFO not initialized": 0,
2053 "head descriptor invalid": 0,
2054 "init descriptor idx invalid": 0,
2055 "packet buffer out of range": 0,
2056 "packet length out of range": 0,
2057 "packet null": 0,
2058 },
2059 "tx_packets": {"accepted": 0, "rejected": 0, "transferred": 0},
2060 "tx_rates": {
2061 "bytes per second": 0,
2062 "descriptors completed since last count": 0,
2063 "descriptors per second": 0,
2064 "packets per second": 0,
2065 },
2066 },
2067 },
2068 )
2069
2070 @patch("jnpr.junos.Device.execute")
2071 def test_table_eval_expression(self, mock_execute):
2072 mock_execute.side_effect = self._mock_manager
2073 yaml_data = """
2074 ---
2075 XMChipInterruptStatsTable:
2076 command: show xmchip {{ chip_instance }} li interrupt-stats
2077 target: Null
2078 args:
2079 chip_instance: 0
2080 key:
2081 - li_block
2082 - name
2083 view: XMChipInterruptStatsView
2084 eval:
2085 total_interrupt: "reduce(lambda x,y: x+y, [v['interrupts'] for k,v in {{ data }}.items() if 'cookie_sz_err' in v.get('name')])"
2086
2087 XMChipInterruptStatsView:
2088 columns:
2089 li_block: LI Block
2090 name: Interrupt Name
2091 interrupts: Number of Interrupts
2092 last_occurance: Last Occurrence
2093 """
2094 globals().update(
2095 FactoryLoader().load(
2096 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
2097 )
2098 )
2099 stats = XMChipInterruptStatsTable(self.dev)
2100 stats = stats.get(target="fpc1")
2101 self.assertEqual(stats["total_interrupt"], 34)
2102
2103 @patch("jnpr.junos.Device.execute")
2104 def test_table_eval_expression_exception(self, mock_execute):
2105 mock_execute.side_effect = self._mock_manager
2106 yaml_data = """
2107 ---
2108 XMChipInterruptStatsTable:
2109 command: show xmchip {{ chip_instance }} li interrupt-stats
2110 target: Null
2111 args:
2112 chip_instance: 0
2113 key:
2114 - li_block
2115 - name
2116 view: XMChipInterruptStatsView
2117 eval:
2118 total_interrupt: "xxxx"
2119
2120 XMChipInterruptStatsView:
2121 columns:
2122 li_block: LI Block
2123 name: Interrupt Name
2124 interrupts: Number of Interrupts
2125 last_occurance: Last Occurrence
2126 """
2127 globals().update(
2128 FactoryLoader().load(
2129 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
2130 )
2131 )
2132 stats = XMChipInterruptStatsTable(self.dev)
2133 stats = stats.get(target="fpc1")
2134 self.assertEqual(stats["total_interrupt"], None)
2135
2136 @patch("jnpr.junos.Device.execute")
2137 def test_table_eval_with_filters(self, mock_execute):
2138 mock_execute.side_effect = self._mock_manager
2139 yaml_data = """
2140 ---
2141 FPCThreads:
2142 command: show threads
2143 target: fpc2
2144 key: Name
2145 view: FPCThreadView
2146
2147 FPCThreadView:
2148 columns:
2149 pid_pr: PID PR
2150 state: State
2151 name: Name
2152 stack: Stack Use
2153 time: Time (Last/Max/Total)
2154 cpu: cpu
2155 eval:
2156 cpu: "'{{ cpu }}'[:-1]"
2157 filters:
2158 - cpu
2159 - state
2160 """
2161 globals().update(
2162 FactoryLoader().load(
2163 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
2164 )
2165 )
2166 stats = FPCThreads(self.dev)
2167 stats = stats.get()
2168 self.assertEqual(
2169 dict(stats),
2170 {
2171 "100ms Periodic": {"cpu": "0", "state": "asleep"},
2172 "10s Low Periodic": {"cpu": "0", "state": "asleep"},
2173 "10s Medium Periodic": {"cpu": "0", "state": "asleep"},
2174 "1s Low Periodic": {"cpu": "0", "state": "asleep"},
2175 "1s Medium Periodic": {"cpu": "0", "state": "asleep"},
2176 "50ms Periodic": {"cpu": "0", "state": "asleep"},
2177 "CFM Data thread": {"cpu": "0", "state": "asleep"},
2178 "CFM Manager": {"cpu": "0", "state": "asleep"},
2179 "CFP": {"cpu": "0", "state": "asleep"},
2180 "CLKSYNC Manager": {"cpu": "0", "state": "asleep"},
2181 "CLNS Err Input": {"cpu": "0", "state": "asleep"},
2182 "CLNS Option Input": {"cpu": "0", "state": "asleep"},
2183 "CXP": {"cpu": "0", "state": "asleep"},
2184 "Cassis Free Timer": {"cpu": "3", "state": "asleep"},
2185 "Cattle-Prod Daemon": {"cpu": "0", "state": "asleep"},
2186 "Console": {"cpu": "0", "state": "asleep"},
2187 "Cube Server": {"cpu": "0", "state": "asleep"},
2188 "DCC Background": {"cpu": "0", "state": "asleep"},
2189 "DDOS Policers": {"cpu": "0", "state": "asleep"},
2190 "DFW Alert": {"cpu": "0", "state": "asleep"},
2191 "DSX50ms": {"cpu": "0", "state": "asleep"},
2192 "DSXonesec": {"cpu": "0", "state": "asleep"},
2193 "Firmware Upgrade": {"cpu": "0", "state": "asleep"},
2194 "GR253": {"cpu": "0", "state": "asleep"},
2195 "HSL2": {"cpu": "0", "state": "asleep"},
2196 "Heap Accouting": {"cpu": "0", "state": "asleep"},
2197 "Host Loopback Periodic": {"cpu": "0", "state": "asleep"},
2198 "ICMP Input": {"cpu": "0", "state": "asleep"},
2199 "ICMP6 Input": {"cpu": "0", "state": "asleep"},
2200 "IFCM": {"cpu": "0", "state": "asleep"},
2201 "IGMP": {"cpu": "0", "state": "asleep"},
2202 "IGMP Input": {"cpu": "0", "state": "asleep"},
2203 "IP Option Input": {"cpu": "0", "state": "asleep"},
2204 "IP Reassembly": {"cpu": "0", "state": "asleep"},
2205 "IP6 Option Input": {"cpu": "0", "state": "asleep"},
2206 "IPC Test Daemon": {"cpu": "0", "state": "asleep"},
2207 "IPv4 PFE Control Background": {"cpu": "0", "state": "asleep"},
2208 "JNH Exception Counter Background Thread": {
2209 "cpu": "0",
2210 "state": "asleep",
2211 },
2212 "JNH KA Transmit": {"cpu": "0", "state": "asleep"},
2213 "JNH Partition Mem Recovery": {"cpu": "0", "state": "asleep"},
2214 "L2ALM Manager": {"cpu": "0", "state": "asleep"},
2215 "L2PD": {"cpu": "0", "state": "asleep"},
2216 "L2TP-SF KA Transmit": {"cpu": "0", "state": "asleep"},
2217 "LKUP ASIC UCODE Rebalance Service": {
2218 "cpu": "1",
2219 "state": "asleep",
2220 },
2221 "LKUP ASIC Wedge poll thread": {"cpu": "0", "state": "asleep"},
2222 "LU Background Service": {"cpu": "4", "state": "asleep"},
2223 "LU-CNTR Reader": {"cpu": "0", "state": "asleep"},
2224 "MSA300PIN": {"cpu": "0", "state": "asleep"},
2225 "Maintenance": {"cpu": "0", "state": "asleep"},
2226 "NH Probe Service": {"cpu": "0", "state": "asleep"},
2227 "OTN": {"cpu": "0", "state": "asleep"},
2228 "PFE Manager": {"cpu": "0", "state": "asleep"},
2229 "PFE Statistics": {"cpu": "0", "state": "asleep"},
2230 "PFEMAN SRRD Thread": {"cpu": "0", "state": "asleep"},
2231 "PFEMAN Service Thread": {"cpu": "0", "state": "asleep"},
2232 "PIC": {"cpu": "0", "state": "asleep"},
2233 "PIC Periodic": {"cpu": "0", "state": "asleep"},
2234 "PPM Data thread": {"cpu": "0", "state": "asleep"},
2235 "PPM Manager": {"cpu": "0", "state": "asleep"},
2236 "PQ3 PCI Periodic": {"cpu": "0", "state": "asleep"},
2237 "PRECL Chip Generic": {"cpu": "0", "state": "asleep"},
2238 "PZARB Timeout": {"cpu": "0", "state": "asleep"},
2239 "Pfesvcsor": {"cpu": "0", "state": "asleep"},
2240 "QSFP": {"cpu": "0", "state": "asleep"},
2241 "RCM Pfe Manager": {"cpu": "0", "state": "asleep"},
2242 "RDMAN": {"cpu": "0", "state": "asleep"},
2243 "RDP Input": {"cpu": "0", "state": "asleep"},
2244 "RDP Timers": {"cpu": "0", "state": "asleep"},
2245 "RFC2544 periodic": {"cpu": "0", "state": "asleep"},
2246 "RPM Msg thread": {"cpu": "0", "state": "asleep"},
2247 "RSMON syslog thread": {"cpu": "0", "state": "asleep"},
2248 "SFP": {"cpu": "0", "state": "asleep"},
2249 "SNTP Daemon": {"cpu": "0", "state": "asleep"},
2250 "Services TOD": {"cpu": "0", "state": "asleep"},
2251 "Sheaf Background": {"cpu": "0", "state": "asleep"},
2252 "Stats Page Ager": {"cpu": "0", "state": "asleep"},
2253 "Syslog": {"cpu": "0", "state": "asleep"},
2254 "TCP Receive": {"cpu": "0", "state": "asleep"},
2255 "TCP Timers": {"cpu": "0", "state": "asleep"},
2256 "TNP Hello": {"cpu": "0", "state": "asleep"},
2257 "TNPC CM": {"cpu": "0", "state": "asleep"},
2258 "TOE Coredump": {"cpu": "0", "state": "asleep"},
2259 "TTP Receive": {"cpu": "0", "state": "asleep"},
2260 "TTP Transmit": {"cpu": "0", "state": "asleep"},
2261 "TTRACE Creator": {"cpu": "0", "state": "asleep"},
2262 "TTRACE Tracer": {"cpu": "0", "state": "asleep"},
2263 "Timer Services": {"cpu": "0", "state": "asleep"},
2264 "Trap_Info Read PFE 0.0": {"cpu": "0", "state": "asleep"},
2265 "Trap_Info Read PFE 0.1": {"cpu": "0", "state": "asleep"},
2266 "Trap_Info Read PFE 1.0": {"cpu": "0", "state": "asleep"},
2267 "Trap_Info Read PFE 1.1": {"cpu": "0", "state": "asleep"},
2268 "UDP Input": {"cpu": "0", "state": "asleep"},
2269 "Ukern Syslog": {"cpu": "0", "state": "asleep"},
2270 "VBF MC Purge": {"cpu": "0", "state": "asleep"},
2271 "VBF PFE Events": {"cpu": "0", "state": "asleep"},
2272 "VBF Walker": {"cpu": "0", "state": "asleep"},
2273 "VRRP Manager": {"cpu": "0", "state": "asleep"},
2274 "Virtual Console": {"cpu": "0", "state": "asleep"},
2275 "XFP": {"cpu": "0", "state": "asleep"},
2276 "XM Chip Generic": {"cpu": "0", "state": "asleep"},
2277 "XM Chip Statistics": {"cpu": "0", "state": "asleep"},
2278 "XM Chip Wedge Detection and Recovery": {
2279 "cpu": "0",
2280 "state": "asleep",
2281 },
2282 "bulkget Manager": {"cpu": "0", "state": "asleep"},
2283 "cos halp stats daemon": {"cpu": "0", "state": "asleep"},
2284 "jnh errors daemon": {"cpu": "0", "state": "asleep"},
2285 "mac_db": {"cpu": "0", "state": "asleep"},
2286 "zlAprTaskzl303xx": {"cpu": "0", "state": "asleep"},
2287 "zlHybridTaskzl303xx": {"cpu": "0", "state": "asleep"},
2288 "zlInt08Taskzl303xx": {"cpu": "1", "state": "asleep"},
2289 "zlInt09Taskzl303xx": {"cpu": "0", "state": "asleep"},
2290 "zlPktTxSchedTaskzl303xx": {"cpu": "0", "state": "asleep"},
2291 "zlPtpTaskzl303xx": {"cpu": "0", "state": "asleep"},
2292 "zlSpllTaskzl303xx": {"cpu": "0", "state": "asleep"},
2293 "zlTimerTaskzl303xx": {"cpu": "0", "state": "asleep"},
2294 "zlTodMgrTaskzl303xx": {"cpu": "0", "state": "asleep"},
2295 "zlTsEngTaskzl303xx": {"cpu": "0", "state": "asleep"},
2296 "zlTxTsMgrTaskzl303xx": {"cpu": "0", "state": "asleep"},
2297 },
2298 )
2299
2300 @patch("jnpr.junos.Device.execute")
2301 def test_new_line_in_data(self, mock_execute):
2302 mock_execute.side_effect = self._mock_manager
2303 yaml_data = r"""
2304 ---
2305 CChipLoStatsTable:
2306 command: show xmchip {{ chip_instance }} lo stats 0
2307 target: fpc4
2308 args:
2309 chip_instance: 0
2310 title: 'LU Parcel Name Counter Name'
2311 key:
2312 - index
2313 - parcel
2314 - counter
2315 eval:
2316 cchip-lookup-out-errors: >
2317 sum([v['total'] for k,v in {{ data }}.items() if v['parcel']=='Error'])
2318 view: CChipLoStatsView
2319
2320 CChipLoStatsView:
2321 regex:
2322 index: '^\d+'
2323 parcel: '\w*(?=_)?\w*'
2324 counter: '(\w+(\s\w+)+)'
2325 total: '\d+'
2326 rate: '\d+ pps'
2327 """
2328 globals().update(
2329 FactoryLoader().load(
2330 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
2331 )
2332 )
2333 stats = CChipLoStatsTable(self.dev)
2334 stats = stats.get(target="fpc0")
2335 self.assertEqual(stats["cchip-lookup-out-errors"], 500)
2336
2337 @patch("jnpr.junos.Device.execute")
2338 def test_textfsm_table(self, mock_execute):
2339 mock_execute.side_effect = self._mock_manager
2340 yaml_data = """
2341 ---
2342 ARPtable:
2343 command: show arp no-resolve
2344 platform: juniper_junos
2345 key: ip
2346 use_textfsm: True
2347 view: ARPview
2348
2349 ARPview:
2350 fields:
2351 mac: MAC
2352 ip: IP_ADDRESS
2353 interface: INTERFACE
2354 flag: FLAGS
2355 """
2356 globals().update(
2357 FactoryLoader().load(
2358 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
2359 )
2360 )
2361 stats = ARPtable(self.dev)
2362 stats = stats.get()
2363 self.assertEqual(len(stats), 34)
2364 self.assertIn("10.221.128.201", stats)
2365
2366 @patch("jnpr.junos.Device.execute")
2367 def test_textfsm_table_mutli_key(self, mock_execute):
2368 mock_execute.side_effect = self._mock_manager
2369 yaml_data = """
2370 ---
2371 ARPtable:
2372 command: show arp no-resolve
2373 platform: juniper_junos
2374 key:
2375 - ip
2376 - mac
2377 use_textfsm: True
2378 view: ARPview
2379
2380 ARPview:
2381 fields:
2382 mac: MAC
2383 ip: IP_ADDRESS
2384 interface: INTERFACE
2385 flag: FLAGS
2386 """
2387 globals().update(
2388 FactoryLoader().load(
2389 yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
2390 )
2391 )
2392 stats = ARPtable(self.dev)
2393 stats = stats.get()
2394 self.assertIn(
2395 "('10.221.129.121', '56:68:ad:d8:10:f4')", json.loads(stats.to_json())
2396 )
2397
2398 def _read_file(self, fname):
2399 from ncclient.xml_ import NCElement
2400
2401 fpath = os.path.join(os.path.dirname(__file__), "rpc-reply", fname)
2402 foo = open(fpath).read()
2403 rpc_reply = NCElement(
2404 foo, self.dev._conn._device_handler.transform_reply()
2405 )._NCElement__doc
2406 return rpc_reply
2407
2408 def _mock_manager(self, *args, **kwargs):
2409 if kwargs and "normalize" not in kwargs:
2410 device_params = kwargs["device_params"]
2411 device_handler = make_device_handler(device_params)
2412 session = SSHSession(device_handler)
2413 return Manager(session, device_handler)
2414
2415 if args:
2416 if args[0].tag == "request-pfe-execute":
2417 file_name = (args[0].findtext("command")).replace(" ", "_")
2418 return self._read_file(file_name + ".xml")
2419 elif args[0].tag == "command":
2420 file_name = (args[0].text).replace(" ", "_")
2421 return self._read_file(file_name + ".xml")