Skip to content

Commit

Permalink
fixed bug with adding empty result to overall results due to using * …
Browse files Browse the repository at this point in the history
…list path formatter
  • Loading branch information
dmulyalin committed Oct 17, 2021
1 parent ae6b427 commit dc58fd4
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 2 deletions.
181 changes: 180 additions & 1 deletion test/pytest/test_answers_and_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6552,4 +6552,183 @@ def test_issue_57_one_more_answer():
'priority': '32768',
'status': '0',
'sys_id': '0x8000'}]}}}]]
# test_issue_57_one_more_answer()
# test_issue_57_one_more_answer()


def test_issue_57_one_more_empty_dict_in_res():
"""
Without fix this results produced:
[[{'portchannel': {'1': {'local_members': [{},
{'flag': '{ACG}',
'interface': 'GE6/0/1',
'oper_key': '1',
'priority': '32768',
'status': 'U'},
{'flag': '{ACG}',
'interface': 'GE6/0/2',
'oper_key': '1',
'priority': '32768',
'status': 'U'}],
'remote_members': [{},
{'flag': '{EF}',
'interface': 'GE6/0/1',
'mac': '0000-0000-0000',
'oper_key': '0',
'priority': '32768',
'status': '0',
'sys_id': '0x8000'},
{'flag': '{EF}',
'interface': 'GE6/0/2',
'mac': '0000-0000-0000',
'oper_key': '0',
'priority': '32768',
'status': '0',
'sys_id': '0x8000'}]},
'2': {'local_members': [{},
{'flag': '{ACG}',
'interface': 'GE6/0/3',
'oper_key': '2',
'priority': '32768',
'status': 'U'},
{'flag': '{ACG}',
'interface': 'GE6/0/4',
'oper_key': '2',
'priority': '32768',
'status': 'U'}],
'remote_members': [{},
{'flag': '{EF}',
'interface': 'GE6/0/3',
'mac': '0000-0000-0000',
'oper_key': '0',
'priority': '32768',
'status': '0',
'sys_id': '0x8000'},
{'flag': '{EF}',
'interface': 'GE6/0/4',
'mac': '0000-0000-0000',
'oper_key': '0',
'priority': '32768',
'status': '0',
'sys_id': '0x8000'}]}}}]]
Above results contain empty dictionary list item, this is because
local_members* and remote_members* use * to indicate list item
as a result self.dict_by_path was returning E as a list element,
and results were appended to that element, but results are empty dictionary,
update saving logic to check if results are empty and skip appending them
if so.
"""
data = """
Loadsharing Type: Shar -- Loadsharing, NonS -- Non-Loadsharing
Port Status: S -- Selected, U -- Unselected,
I -- Individual, * -- Management port
Flags: A -- LACP_Activity, B -- LACP_Timeout, C -- Aggregation,
D -- Synchronization, E -- Collecting, F -- Distributing,
G -- Defaulted, H -- Expired
Aggregate Interface: Bridge-Aggregation1
Aggregation Mode: Dynamic
Loadsharing Type: Shar
Management VLAN : None
System ID: 0x8000, d07e-28b5-a200
Local:
Port Status Priority Oper-Key Flag
--------------------------------------------------------------------------------
GE6/0/1 U 32768 1 {ACG}
GE6/0/2 U 32768 1 {ACG}
Remote:
Actor Partner Priority Oper-Key SystemID Flag
--------------------------------------------------------------------------------
GE6/0/1 0 32768 0 0x8000, 0000-0000-0000 {EF}
GE6/0/2 0 32768 0 0x8000, 0000-0000-0000 {EF}
Aggregate Interface: Bridge-Aggregation2
Aggregation Mode: Dynamic
Loadsharing Type: Shar
Management VLAN : None
System ID: 0x8000, d07e-28b5-a200
Local:
Port Status Priority Oper-Key Flag
--------------------------------------------------------------------------------
GE6/0/3 U 32768 2 {ACG}
GE6/0/4 U 32768 2 {ACG}
Remote:
Actor Partner Priority Oper-Key SystemID Flag
--------------------------------------------------------------------------------
GE6/0/3 0 32768 0 0x8000, 0000-0000-0000 {EF}
GE6/0/4 0 32768 0 0x8000, 0000-0000-0000 {EF}
"""
template = """
<group name = "portchannel.{{channel_number}}">
Aggregate Interface: Bridge-Aggregation{{ channel_number}}
<group name = "local_members*">
Local: {{_start_}}
<group>
{{interface }} {{status}} {{priority}} {{oper_key | DIGIT }} {{flag}}
</group>
</group>
<group name = "remote_members*">
Remote: {{_start_}}
<group>
{{interface }} {{status}} {{priority}} {{oper_key}} {{sys_id}}, {{ mac | MAC }} {{flag}}
</group>
</group>
</group>
"""
parser = ttp(data, template)
parser.parse()
res = parser.result()
# pprint.pprint(res)
assert res == [[{'portchannel': {'1': {'local_members': [{'flag': '{ACG}',
'interface': 'GE6/0/1',
'oper_key': '1',
'priority': '32768',
'status': 'U'},
{'flag': '{ACG}',
'interface': 'GE6/0/2',
'oper_key': '1',
'priority': '32768',
'status': 'U'}],
'remote_members': [{'flag': '{EF}',
'interface': 'GE6/0/1',
'mac': '0000-0000-0000',
'oper_key': '0',
'priority': '32768',
'status': '0',
'sys_id': '0x8000'},
{'flag': '{EF}',
'interface': 'GE6/0/2',
'mac': '0000-0000-0000',
'oper_key': '0',
'priority': '32768',
'status': '0',
'sys_id': '0x8000'}]},
'2': {'local_members': [{'flag': '{ACG}',
'interface': 'GE6/0/3',
'oper_key': '2',
'priority': '32768',
'status': 'U'},
{'flag': '{ACG}',
'interface': 'GE6/0/4',
'oper_key': '2',
'priority': '32768',
'status': 'U'}],
'remote_members': [{'flag': '{EF}',
'interface': 'GE6/0/3',
'mac': '0000-0000-0000',
'oper_key': '0',
'priority': '32768',
'status': '0',
'sys_id': '0x8000'},
{'flag': '{EF}',
'interface': 'GE6/0/4',
'mac': '0000-0000-0000',
'oper_key': '0',
'priority': '32768',
'status': '0',
'sys_id': '0x8000'}]}}}]]
# test_issue_57_one_more_empty_dict_in_res()
2 changes: 1 addition & 1 deletion ttp/ttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2932,7 +2932,7 @@ def save_curelements(self, result_data, result_path):
"""Method to save current group results in self.results"""
# get ELEMENT from self.results by result_path
E = self.dict_by_path(PATH=result_path, ELEMENT=self.results)
if isinstance(E, list):
if isinstance(E, list) and result_data:
E.append(result_data)
elif isinstance(E, dict):
# check if result_path endswith "**" - update result's ELEMENET without converting it into list:
Expand Down

0 comments on commit dc58fd4

Please sign in to comment.