Skip to content

Commit

Permalink
Merge pull request #1067 from vnitinv/master
Browse files Browse the repository at this point in the history
textfsm table with multi key
  • Loading branch information
Nitin Kr committed Aug 10, 2020
2 parents b0fb992 + 8137345 commit d19968b
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/jnpr/junos/factory/cmdtable.py
Expand Up @@ -384,7 +384,15 @@ def _filter_output(self, cli_table):
for row in cli_table:
temp_dict = self._parse_row(row, cli_table, reverse_fields)
logger.debug("data at index {} is {}".format(row.row, temp_dict))
if self.KEY in temp_dict:
if isinstance(self.KEY, list):
key_list = []
for key in self.KEY:
if key not in fields:
key_list.append(temp_dict.pop(key))
else:
key_list.append(temp_dict[key])
output[tuple(key_list)] = temp_dict
elif self.KEY in temp_dict:
if self.KEY not in fields:
output[temp_dict.pop(self.KEY)] = temp_dict
else:
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/factory/rpc-reply/show_arp_no-resolve.xml
@@ -0,0 +1,37 @@
<output>
MAC Address Address Interface Flags
f0:76:1c:9d:23:19 10.221.128.201 fxp0.0 none
fc:45:96:f3:be:1c 10.221.128.202 fxp0.0 none
56:68:ad:d8:12:f0 10.221.129.54 fxp0.0 none
56:68:ad:d8:13:af 10.221.129.93 fxp0.0 none
56:68:ad:d8:10:f4 10.221.129.121 fxp0.0 none
56:68:ad:d8:10:c6 10.221.129.191 fxp0.0 none
56:68:ad:d8:10:ec 10.221.129.221 fxp0.0 none
56:68:ad:d8:11:a1 10.221.129.231 fxp0.0 none
56:68:ad:d8:15:f3 10.221.129.239 fxp0.0 none
56:68:ad:d8:1d:3a 10.221.130.6 fxp0.0 none
56:68:ad:d8:13:32 10.221.130.28 fxp0.0 none
56:68:ad:d8:17:f8 10.221.130.54 fxp0.0 none
56:68:ad:d8:15:eb 10.221.130.56 fxp0.0 none
56:68:ad:d8:17:82 10.221.130.130 fxp0.0 none
56:68:ad:d8:10:eb 10.221.130.170 fxp0.0 none
56:68:ad:d8:12:f8 10.221.130.186 fxp0.0 none
56:68:ad:d8:1f:f8 10.221.130.219 fxp0.0 none
56:68:ad:d8:13:14 10.221.130.235 fxp0.0 none
56:68:ad:d8:17:10 10.221.130.250 fxp0.0 none
56:68:ad:d8:15:3e 10.221.131.11 fxp0.0 none
56:68:ad:d8:17:83 10.221.131.16 fxp0.0 none
56:68:ad:d8:18:ae 10.221.131.36 fxp0.0 none
56:68:ad:d8:1c:7f 10.221.131.78 fxp0.0 none
56:68:ad:d8:1a:c1 10.221.131.82 fxp0.0 none
56:68:ad:d8:17:9a 10.221.131.119 fxp0.0 none
56:68:ad:d8:18:b2 10.221.131.128 fxp0.0 none
56:68:ad:d8:19:0c 10.221.131.132 fxp0.0 none
56:68:ad:d8:19:4f 10.221.131.158 fxp0.0 none
56:68:ad:d8:18:86 10.221.131.207 fxp0.0 none
56:68:ad:d8:16:76 10.221.131.208 fxp0.0 none
56:68:ad:d8:1a:74 10.221.131.212 fxp0.0 none
56:68:ad:d8:1d:68 10.221.132.32 fxp0.0 none
56:68:ad:d8:20:44 10.221.132.62 fxp0.0 none
56:68:ad:d8:1d:3b 10.221.132.199 fxp0.0 none
</output>
62 changes: 62 additions & 0 deletions tests/unit/factory/test_cmdtable.py
Expand Up @@ -14,6 +14,7 @@
import yamlordereddictloader
from jnpr.junos.factory.factory_loader import FactoryLoader
import yaml
import json


@attr("unit")
Expand Down Expand Up @@ -2334,6 +2335,67 @@ def test_new_line_in_data(self, mock_execute):
stats = stats.get(target="fpc0")
self.assertEqual(stats["cchip-lookup-out-errors"], 500)

@patch("jnpr.junos.Device.execute")
def test_textfsm_table(self, mock_execute):
mock_execute.side_effect = self._mock_manager
yaml_data = """
---
ARPtable:
command: show arp no-resolve
platform: juniper_junos
key: ip
use_textfsm: True
view: ARPview
ARPview:
fields:
mac: MAC
ip: IP_ADDRESS
interface: INTERFACE
flag: FLAGS
"""
globals().update(
FactoryLoader().load(
yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
)
)
stats = ARPtable(self.dev)
stats = stats.get()
self.assertEqual(len(stats), 34)
self.assertIn("10.221.128.201", stats)

@patch("jnpr.junos.Device.execute")
def test_textfsm_table_mutli_key(self, mock_execute):
mock_execute.side_effect = self._mock_manager
yaml_data = """
---
ARPtable:
command: show arp no-resolve
platform: juniper_junos
key:
- ip
- mac
use_textfsm: True
view: ARPview
ARPview:
fields:
mac: MAC
ip: IP_ADDRESS
interface: INTERFACE
flag: FLAGS
"""
globals().update(
FactoryLoader().load(
yaml.load(yaml_data, Loader=yamlordereddictloader.Loader)
)
)
stats = ARPtable(self.dev)
stats = stats.get()
self.assertIn(
"('10.221.129.121', '56:68:ad:d8:10:f4')", json.loads(stats.to_json())
)

def _read_file(self, fname):
from ncclient.xml_ import NCElement

Expand Down

0 comments on commit d19968b

Please sign in to comment.