From c2bfae9ab14ebb4c730953d2729a854249b4ad58 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Tue, 3 Mar 2020 20:49:44 +0530 Subject: [PATCH 1/3] handle sax parser input for nested fields --- lib/jnpr/junos/factory/optable.py | 24 ++++++---- tests/unit/factory/test_optable.py | 45 ++++++++++++++----- tests/unit/factory/test_table.py | 1 - .../facts/test_get_software_information.py | 1 - 4 files changed, 49 insertions(+), 22 deletions(-) diff --git a/lib/jnpr/junos/factory/optable.py b/lib/jnpr/junos/factory/optable.py index d237da021..4294a4926 100644 --- a/lib/jnpr/junos/factory/optable.py +++ b/lib/jnpr/junos/factory/optable.py @@ -133,17 +133,23 @@ def generate_sax_parser_input(obj): # fields: # input-bytes: traffic-statistics/input-bytes # output-bytes: traffic-statistics/output-bytes - existing_elem = parser_ingest.xpath(tags[0]) - if existing_elem: - obj = existing_elem[0] - for tag in tags[1:]: - obj.append(E(tag)) + # or + # fields: + # prefix-count: bgp-option-information/prefix-limit/prefix-count + # prefix-dummy: bgp-option-information/prefix-limit/prefix-dummy + local_obj = parser_ingest + for tag in tags[:-1]: + existing_elem = local_obj.xpath(tag) + if existing_elem: + local_obj = existing_elem[0] + else: + continue else: - continue + local_obj.append(E(tags[-1])) else: - obj = E(tags[0]) - for tag in tags[1:]: - obj.append(E(tag)) + obj = E(tags[-1]) + for tag in tags[:-1][::-1]: + obj = E(tag, obj) map_multilayer_fields[tags[0]] = obj parser_ingest.insert(i + 1, obj) else: diff --git a/tests/unit/factory/test_optable.py b/tests/unit/factory/test_optable.py index b81e7a517..c9ea23b43 100644 --- a/tests/unit/factory/test_optable.py +++ b/tests/unit/factory/test_optable.py @@ -94,9 +94,32 @@ def bad(key): self.assertRaises(ValueError, bad, 'bunk') - def test_generate_sax_parser_item_with_many_slash(self): + def test_generate_sax_parser_fields_with_many_slash(self): yaml_data = """ --- +bgpNeighborTable: + rpc: get-bgp-neighbor-information + item: bgp-peer + key: peer-address + view: bgpNeighborView +bgpNeighborView: + fields: + prefix-count: bgp-option-information/prefix-limit/prefix-count + prefix-dummy: bgp-option-information/prefix-limit/prefix-dummy +""" + globals().update(FactoryLoader().load(yaml.load(yaml_data, + Loader=yaml.Loader))) + tbl = bgpNeighborTable(self.dev) + data = generate_sax_parser_input(tbl) + self.assertEqual(data.tag, 'bgp-peer') + self.assertEqual(len(etree.tostring(data)), len( + b'' + b'' + b'')) + + def test_generate_sax_parser_item_with_many_slash(self): + yaml_data = """ +--- taskmallocdetail: rpc: get-task-memory-information args: @@ -112,16 +135,16 @@ def test_generate_sax_parser_item_with_many_slash(self): tmmaxallocs: tm-max-allocs tmmaxallocbytes: tm-max-alloc-bytes tmfunctioncalls: tm-function-calls -""" - globals().update(FactoryLoader().load(yaml.load(yaml_data, - Loader=yaml.Loader))) - tbl = taskmallocdetail(self.dev) - data = generate_sax_parser_input(tbl) - self.assertEqual(data.tag, 'task-memory-malloc-usage-report') - self.assertEqual(len(etree.tostring(data)), len( - b'')) + """ + globals().update(FactoryLoader().load(yaml.load(yaml_data, + Loader=yaml.Loader))) + tbl = taskmallocdetail(self.dev) + data = generate_sax_parser_input(tbl) + self.assertEqual(data.tag, 'task-memory-malloc-usage-report') + self.assertEqual(len(etree.tostring(data)), len( + b'')) def test_generate_sax_parser_same_parents_with_diff_fields(self): yaml_data = """ diff --git a/tests/unit/factory/test_table.py b/tests/unit/factory/test_table.py index 9ca79b292..6c5a51fe9 100644 --- a/tests/unit/factory/test_table.py +++ b/tests/unit/factory/test_table.py @@ -125,7 +125,6 @@ def test_table__contains__(self, mock_execute): def test_table_items(self, mock_execute): mock_execute.side_effect = self._mock_manager self.ppt.get('ge-0/0/0') - print (self.ppt.items()) self.assertEqual(len(self.ppt.items()[1][1]), 8) def test_table_get_return_none(self): diff --git a/tests/unit/facts/test_get_software_information.py b/tests/unit/facts/test_get_software_information.py index cca40a033..f83e5c712 100644 --- a/tests/unit/facts/test_get_software_information.py +++ b/tests/unit/facts/test_get_software_information.py @@ -110,7 +110,6 @@ def test_sw_info_dual(self, mock_execute): @patch('jnpr.junos.Device.execute') def test_sw_info_dual_other_re_off(self, mock_execute): mock_execute.side_effect = self._mock_manager_dual_other_re_off - print (self.dev.facts) self.assertEqual(self.dev.facts['junos_info']['re1']['text'], '18.3I20180716_1639') self.assertEqual(self.dev.facts['hostname'], 'R1_re01') From d933563b96868a4ce259612b31e1bb26f8320f4d Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Tue, 3 Mar 2020 20:58:30 +0530 Subject: [PATCH 2/3] fix indentation --- tests/unit/factory/test_optable.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/unit/factory/test_optable.py b/tests/unit/factory/test_optable.py index c9ea23b43..b51b9beee 100644 --- a/tests/unit/factory/test_optable.py +++ b/tests/unit/factory/test_optable.py @@ -118,7 +118,7 @@ def test_generate_sax_parser_fields_with_many_slash(self): b'')) def test_generate_sax_parser_item_with_many_slash(self): - yaml_data = """ + yaml_data = """ --- taskmallocdetail: rpc: get-task-memory-information @@ -136,15 +136,15 @@ def test_generate_sax_parser_item_with_many_slash(self): tmmaxallocbytes: tm-max-alloc-bytes tmfunctioncalls: tm-function-calls """ - globals().update(FactoryLoader().load(yaml.load(yaml_data, - Loader=yaml.Loader))) - tbl = taskmallocdetail(self.dev) - data = generate_sax_parser_input(tbl) - self.assertEqual(data.tag, 'task-memory-malloc-usage-report') - self.assertEqual(len(etree.tostring(data)), len( - b'')) + globals().update(FactoryLoader().load(yaml.load(yaml_data, + Loader=yaml.Loader))) + tbl = taskmallocdetail(self.dev) + data = generate_sax_parser_input(tbl) + self.assertEqual(data.tag, 'task-memory-malloc-usage-report') + self.assertEqual(len(etree.tostring(data)), len( + b'')) def test_generate_sax_parser_same_parents_with_diff_fields(self): yaml_data = """ From 811b325c3506b2dfd8d7fbe9d2783c08a76e336d Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Tue, 3 Mar 2020 21:01:44 +0530 Subject: [PATCH 3/3] fix indentation --- tests/unit/factory/test_optable.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/factory/test_optable.py b/tests/unit/factory/test_optable.py index fac7e77ff..e65265a12 100644 --- a/tests/unit/factory/test_optable.py +++ b/tests/unit/factory/test_optable.py @@ -136,7 +136,7 @@ def test_generate_sax_parser_item_with_many_slash(self): tmmaxallocs: tm-max-allocs tmmaxallocbytes: tm-max-alloc-bytes tmfunctioncalls: tm-function-calls - """ +""" globals().update(FactoryLoader().load(yaml.load(yaml_data, Loader=yaml.Loader))) tbl = taskmallocdetail(self.dev)