Skip to content

Commit

Permalink
added tests for issue #55; modified handling of empty characters betw…
Browse files Browse the repository at this point in the history
…een words, now instead of matching '\\ +' only TTP accounts for tabs between words as well '[ \t]+'
  • Loading branch information
dmulyalin committed Aug 15, 2021
1 parent 76cab98 commit e7b65c5
Show file tree
Hide file tree
Showing 10 changed files with 418 additions and 14 deletions.
8 changes: 6 additions & 2 deletions docs/source/Groups/Functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -665,9 +665,13 @@ itemize
``itemize="key='name', path='path.to.result'"`` or ``functions="itemize(key='name', path='path.to.result')"``

* key - mandatory, name of the key to use create a list of items from
* path - optional, by default path taken from group name attribute, dot separated string of there to save a list of items within results tree
* path - optional, by default path taken from group name attribute, dot separated string of there to save a list of itemized items within results tree

This function allows to take single item result from group match results and place it into the list at path provided. Motivation behind this function is to be able to provide create a list of items out of match results produced by group. For instance produce a list of all IPs configured on device or VRFs or OSPF processes etc. without the need to iterate over parsing results to extract items in question.
This function allows to take single result item from group match results and place it into the list at specified path.

Motivation behind this function is to be able to create a list of items out of group match results. For instance produce a list of all IP addresses configured on device or VRFs or OSPF processes without the need to iterate over parsing results to extract items in question.

.. warning:: Prior to TTP 0.8.0 itemize does not support dynamic path as group name attribute or as itemize path attribute.

**Example**

Expand Down
Binary file modified test/pytest/Output/excel_out_test_excel_formatter.xlsx
Binary file not shown.
Binary file modified test/pytest/Output/excel_out_test_excel_formatter_update.xlsx
Binary file not shown.
Binary file not shown.
Binary file not shown.
231 changes: 229 additions & 2 deletions test/pytest/test_answers_and_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4806,7 +4806,7 @@ def test_vlans_parsing():
Gi0 1,8,999
Gi7 1,100,120,1000
"""
parser = ttp(data, template)
parser = ttp(data, template, log_level="DEBUG")
parser.parse()
res = parser.result()
# pprint.pprint(res, width=120)
Expand All @@ -4819,4 +4819,231 @@ def test_vlans_parsing():
'vlans': ['1', '100', '120', '1000', '1002', '1003', '1004', '1005']}]},
'vlans_forwarding': {'interfaces': [{'port': 'Gi0', 'vlans': ['1', '8', '999']},
{'port': 'Gi7', 'vlans': ['1', '100', '120', '1000']}]}}]]
test_vlans_parsing()
# test_vlans_parsing()

def test_asa_acls_issue_55_uses_itemize_with_dynamic_path():
data = """
object-group service gokuhead
service-object tcp-udp destination eq gokurpc
service-object tcp destination eq 902
service-object tcp destination eq https
service-object tcp destination eq nfs
service-object tcp destination eq 10025
object-group network gohan
network-object object gohan-01
network-object object gohan-02
network-object object vlan_944
network-object object gohan-03
network-object object gohan-05
network-object object gohan-06
object-group service sql tcp
port-object eq 1433
object-group network vegeta
group-object trunks
network-object object vegeta-01
object-group network Space-Users
network-object object ab
network-object object ac
network-object object ad
network-object object ae
network-object object af
network-object object ag
network-object object ah
network-object object ai
network-object object aj
object-group network dalmatians
network-object object dog-01
group-object trunks
network-object object vlan_950
group-object Space-Users
network-object object Darts-Summary
"""
template = """
<vars>
SVC_PORTS = "tcp-udp|tcp|udp"
</vars>
<group name="object-{{ object_type }}-groups**.{{ object_name }}**">
object-group {{ object_type }} {{ object_name | _start_ }}
object-group {{ object_type }} {{ object_name | _start_ }} {{ protocol | re("SVC_PORTS")}}
description {{ description | re(".*") }}
<group name="{{ type }}-objects" itemize="obj_name" method="table">
network-object object {{ obj_name | let("type", "network") }}
network-object host {{ obj_name | IP | let("type", "network") }}
group-object {{ obj_name | let("type", "group") }}
service-object object {{ obj_name | let("type", "service") }}
service-object {{ obj_name | let("type", "service") }}
</group>
<group name="service-object-ports*">
service-object {{ protocol | re("SVC_PORTS") }} destination eq {{port}}
</group>
<group name="service-object-port-ranges*">
service-object {{ protocol | re("SVC_PORTS") }} destination range {{port_begin}} {{port_end}}
</group>
<group name="service-port-objects" itemize="port_obj">
port-object eq {{ port_obj }}
</group>
</group>
"""
parser = ttp(data, template)
parser.parse()
res = parser.result()
# pprint.pprint(res, width=80)
assert res == [[{'object-network-groups': {'Space-Users': {'network-objects': ['ab',
'ac',
'ad',
'ae',
'af',
'ag',
'ah',
'ai',
'aj']},
'dalmatians': {'group-objects': ['trunks',
'Space-Users'],
'network-objects': ['dog-01',
'vlan_950',
'Darts-Summary']},
'gohan': {'network-objects': ['gohan-01',
'gohan-02',
'vlan_944',
'gohan-03',
'gohan-05',
'gohan-06']},
'vegeta': {'group-objects': ['trunks'],
'network-objects': ['vegeta-01']}},
'object-service-groups': {'gokuhead': {'service-object-ports': [{'port': 'gokurpc',
'protocol': 'tcp-udp'},
{'port': '902',
'protocol': 'tcp'},
{'port': 'https',
'protocol': 'tcp'},
{'port': 'nfs',
'protocol': 'tcp'},
{'port': '10025',
'protocol': 'tcp'}]},
'sql': {'protocol': 'tcp',
'service-port-objects': ['1433']}}}]]

# test_asa_acls_issue_55()


def test_asa_acls_issue_55():
data = """
object-group service gokuhead
service-object tcp-udp destination eq gokurpc
service-object tcp destination eq 902
service-object tcp destination eq https
service-object tcp destination eq nfs
service-object tcp destination eq 10025
object-group network gohan
network-object object gohan-01
network-object object gohan-02
network-object object vlan_944
network-object object gohan-03
network-object object gohan-05
network-object object gohan-06
object-group service sql tcp
port-object eq 1433
object-group network vegeta
group-object trunks
network-object object vegeta-01
object-group network Space-Users
network-object object ab
network-object object ac
network-object object ad
network-object object ae
network-object object af
network-object object ag
network-object object ah
network-object object ai
network-object object aj
object-group network dalmatians
network-object object dog-01
group-object trunks
network-object object vlan_950
group-object Space-Users
network-object object Darts-Summary
"""
template = """
<vars>
SVC_PORTS = "tcp-udp|tcp|udp"
</vars>
<group name="object-{{ object_type }}-groups**.{{ object_name }}**">
object-group {{ object_type }} {{ object_name | _start_ }}
object-group {{ object_type }} {{ object_name | _start_ }} {{ protocol | re("SVC_PORTS")}}
description {{ description | re(".*") }}
<group name="network-objects" itemize="obj_name" method="table">
network-object object {{ obj_name | }}
network-object host {{ obj_name | IP }}
</group>
<group name="group-objects" itemize="obj_name" method="table">
group-object {{ obj_name }}
</group>
<group name="group-objects" itemize="obj_name" method="table">
service-object object {{ obj_name }}
service-object {{ obj_name }}
</group>
<group name="service-object-ports*">
service-object {{ protocol | re("SVC_PORTS") }} destination eq {{port}}
</group>
<group name="service-object-port-ranges*">
service-object {{ protocol | re("SVC_PORTS") }} destination range {{port_begin}} {{port_end}}
</group>
<group name="service-port-objects" itemize="port_obj">
port-object eq {{ port_obj }}
</group>
</group>
"""
parser = ttp(data, template)
parser.parse()
res = parser.result()
# pprint.pprint(res, width=80)
assert res == [[{'object-network-groups': {'Space-Users': {'network-objects': ['ab',
'ac',
'ad',
'ae',
'af',
'ag',
'ah',
'ai',
'aj']},
'dalmatians': {'group-objects': ['trunks',
'Space-Users'],
'network-objects': ['dog-01',
'vlan_950',
'Darts-Summary']},
'gohan': {'network-objects': ['gohan-01',
'gohan-02',
'vlan_944',
'gohan-03',
'gohan-05',
'gohan-06']},
'vegeta': {'group-objects': ['trunks'],
'network-objects': ['vegeta-01']}},
'object-service-groups': {'gokuhead': {'service-object-ports': [{'port': 'gokurpc',
'protocol': 'tcp-udp'},
{'port': '902',
'protocol': 'tcp'},
{'port': 'https',
'protocol': 'tcp'},
{'port': 'nfs',
'protocol': 'tcp'},
{'port': '10025',
'protocol': 'tcp'}]},
'sql': {'protocol': 'tcp',
'service-port-objects': ['1433']}}}]]

# test_asa_acls_issue_55()
Loading

0 comments on commit e7b65c5

Please sign in to comment.