diff --git a/tests/test_Ccp_Abc.py b/tests/test_Ccp_Abc.py index fd02caf..92d3658 100755 --- a/tests/test_Ccp_Abc.py +++ b/tests/test_Ccp_Abc.py @@ -1,16 +1,17 @@ #!/usr/bin/env python +from loguru import logger +import pytest +from ciscoconfparse.ciscoconfparse import CiscoConfParse +from ciscoconfparse.errors import DynamicAddressException +from ciscoconfparse.ccp_util import IPv4Obj, CiscoRange, CiscoIOSInterface +from ciscoconfparse.ccp_abc import BaseCfgLine +from ciscoconfparse.models_cisco import IOSCfgLine import sys -import os sys.path.insert(0, "..") -from ciscoconfparse.ccp_util import IPv4Obj, CiscoRange, CiscoIOSInterface -from ciscoconfparse.errors import DynamicAddressException -from ciscoconfparse.ciscoconfparse import CiscoConfParse -import pytest -from loguru import logger r""" test_Ccp_Abc.py - Parse, Query, Build, and Modify IOS-style configs @@ -33,6 +34,157 @@ mike [~at~] pennington [/dot\] net """ + +def testVal_BaseCfgLine_obj_01(): + """Test the text and other attributes of ccp_abc.BaseCfgLine()""" + obj01 = BaseCfgLine(all_lines=None, line="hostname Foo") + assert obj01.text == "hostname Foo" + assert obj01.linenum == -1 + assert obj01.child_indent == 0 + assert obj01.is_comment is False + assert str(obj01) == "" + + +def testVal_BaseCfgLine_eq_01(): + """Test the equality of BaseCfgLine() objects""" + obj01 = BaseCfgLine(all_lines=None, line="hostname Foo") + obj02 = BaseCfgLine(all_lines=None, line="hostname Foo") + assert obj01 == obj02 + + +def testVal_BaseCfgLine_neq_01(): + """Test the inequality of BaseCfgLine() objects if their linenumbers are different""" + obj01 = BaseCfgLine(all_lines=None, line="hostname Foo") + obj01.linenum = 1 + obj02 = BaseCfgLine(all_lines=None, line="hostname Foo") + obj02.linenum = 2 + assert obj01 != obj02 + + +def testVal_BaseCfgLine_gt_01(): + """Test the __gt__ of BaseCfgLine() objects if their linenumbers are different""" + obj01 = BaseCfgLine(all_lines=None, line="hostname Foo") + obj01.linenum = 1 + obj02 = BaseCfgLine(all_lines=None, line="hostname Foo") + obj02.linenum = 2 + assert obj02 > obj01 + + +def testVal_BaseCfgLine_lt_01(): + """Test the __lt__ of BaseCfgLine() objects if their linenumbers are different""" + obj01 = BaseCfgLine(all_lines=None, line="hostname Foo") + obj01.linenum = 1 + obj02 = BaseCfgLine(all_lines=None, line="hostname Foo") + obj02.linenum = 2 + assert obj01 < obj02 + + +def testVal_BaseCfgLine_index_01(): + """Test BaseCfgLine().index""" + obj01 = BaseCfgLine(all_lines=None, line="hostname Foo") + obj01.linenum = 1 + assert obj01.linenum == 1 + assert obj01.index == obj01.linenum + + +def testVal_BaseCfgLine_calculate_line_id_01(): + """Test BaseCfgLine().calculate_line_id() is an integer. calculate_line_id() returns a different number for each unique object and it cannot be easily predicted""" + obj01 = BaseCfgLine(all_lines=None, line="hostname Foo") + obj01.linenum = 1 + assert isinstance(obj01.calculate_line_id(), int) + + +def testVal_BaseCfgLine_diff_id_list_01(): + """Test BaseCfgLine().diff_id_list() is a list. diff_id_list() returns a different contents for each unique object and it cannot be easily predicted""" + obj01 = BaseCfgLine(all_lines=None, line="hostname Foo") + obj01.linenum = 1 + assert len(obj01.diff_id_list) == 1 + assert isinstance(obj01.diff_id_list, list) + assert isinstance(obj01.diff_id_list[0], int) + + +def testVal_BaseCfgLine_safe_escape_curly_braces_01(): + """Test BaseCfgLine().safe_escape_curly_braces()""" + obj01 = BaseCfgLine(all_lines=None, line="hostname Foo") + obj01.linenum = 1 + assert obj01.safe_escape_curly_braces("hello{") == "hello{{" + + +def testVal_BaseCfgLine_safe_escape_curly_braces_02(): + """Test BaseCfgLine().safe_escape_curly_braces()""" + obj01 = BaseCfgLine(all_lines=None, line="hostname Foo") + obj01.linenum = 1 + assert obj01.safe_escape_curly_braces("hello}") == "hello}}" + + +def testVal_BaseCfgLine_safe_escape_curly_braces_03(): + """Test BaseCfgLine().safe_escape_curly_braces()""" + obj01 = BaseCfgLine(all_lines=None, line="hostname Foo") + obj01.linenum = 1 + assert obj01.safe_escape_curly_braces("{hello} {") == "{{hello}} {{" + + +def testVal_BaseCfgLine_parent_01(): + """Test BaseCfgLine().parent""" + parse = CiscoConfParse( + ["interface GigabitEthernet1/1", " ip address 192.0.2.1 255.255.255.0"], + syntax="ios", + ) + obj01 = parse.find_objects("interface")[0] + obj01.linenum = 1 + assert len(obj01.children) == 1 + assert obj01.children[0].parent is obj01 + + +def testVal_BaseCfgLine_hash_children_01(): + """Test BaseCfgLine().hash_children""" + parse = CiscoConfParse( + ["interface GigabitEthernet1/1", " ip address 192.0.2.1 255.255.255.0"], + syntax="ios", + ) + obj01 = parse.find_objects("interface")[0] + obj01.linenum = 1 + assert len(obj01.children) == 1 + assert isinstance(obj01.children, list) + assert isinstance(obj01.hash_children, int) + + +def testVal_BaseCfgLine_family_endpoint_01(): + """Test BaseCfgLine().family_endpoint""" + obj01 = BaseCfgLine(all_lines=None, line="interface Ethernet0/0") + obj01.linenum = 1 + obj02 = BaseCfgLine(all_lines=None, line=" ip address 192.0.2.1 255.255.255.0") + obj02.linenum = 2 + obj03 = BaseCfgLine(all_lines=None, line=" no ip proxy-arp") + obj03.linenum = 3 + obj01.children = [obj02, obj03] + assert len(obj01.children) == 2 + assert obj01.family_endpoint == 3 + + +def testVal_BaseCfgLine_verbose_01(): + """Test BaseCfgLine().verbose""" + obj01 = BaseCfgLine(all_lines=None, line="interface Ethernet0/0") + obj01.linenum = 1 + obj02 = BaseCfgLine(all_lines=None, line=" ip address 192.0.2.1 255.255.255.0") + obj02.linenum = 2 + obj03 = BaseCfgLine(all_lines=None, line=" no ip proxy-arp") + obj03.linenum = 3 + obj01.children = [obj02, obj03] + assert obj01.verbose == "" + assert obj02.verbose == "" + + +def testVal_BaseCfgLine_is_comment_01(): + """Test BaseCfgLine().is_comment""" + obj01 = BaseCfgLine(all_lines=None, line="! hostname Foo", comment_delimiter="!") + assert obj01.is_comment is True + obj02 = BaseCfgLine(all_lines=None, line="hostname !Foo", comment_delimiter="!") + assert obj02.is_comment is False + obj03 = BaseCfgLine(all_lines=None, line="hostname Foo!", comment_delimiter="!") + assert obj03.is_comment is False + + def testVal_re_match_iter_typed_parent_default_type_norecurse(): """Test that re_match_iter_typed(recurse=False) finds the parent and returns the default `result_type`, which is `str`""" config = """! @@ -85,6 +237,7 @@ class ALL assert obj.text == "interface GigabitEthernet 1/1" assert uut_result == "GigabitEthernet 1/1" + def testVal_re_match_iter_typed_first_child_default_type_norecurse(): """Test that re_match_iter_typed(recurse=False) finds the first child and returns the default `result_type`, which is `str`""" config = """! @@ -137,6 +290,7 @@ class ALL assert obj.text == "interface GigabitEthernet 1/1" assert uut_result == "trunk" + def testVal_re_match_iter_typed_first_child_default_type_recurse(): """Test that re_match_iter_typed(recurse=True) finds the first child and returns the default `result_type`, which is `str`""" config = """! @@ -189,6 +343,7 @@ class ALL assert obj.text == "interface GigabitEthernet 1/1" assert uut_result == "trunk" + def testVal_re_match_iter_typed_child_deep_fail_norecurse(): """Test that re_match_iter_typed(recurse=False) fails on a deep recurse through multiple children""" config = """! @@ -241,6 +396,7 @@ class ALL assert obj.text == "policy-map EXTERNAL_CBWFQ" assert uut_result == "_no_match" + def testVal_re_match_iter_typed_child_deep_pass_recurse(): """Test that re_match_iter_typed(recurse=False) finds during a deep recurse through multiple levels of children""" config = """! @@ -293,6 +449,7 @@ class ALL assert obj.text == "policy-map EXTERNAL_CBWFQ" assert uut_result == "drop" + def testVal_re_match_iter_typed_second_child_default_type_recurse(): """Test that re_match_iter_typed(recurse=False) finds the second child and returns the default `result_type`, which is `str`""" config = """! @@ -345,6 +502,7 @@ class ALL assert obj.text == "interface GigabitEthernet 1/1" assert uut_result == "911" + def testVal_re_match_iter_typed_second_child_int_type_recurse(): """Test that re_match_iter_typed(recurse=False) finds the second child and returns the default `result_type`, which is `str`""" config = """! @@ -398,6 +556,7 @@ class ALL assert obj.text == "interface GigabitEthernet 1/1" assert uut_result == 911 + def testVal_re_match_iter_typed_named_regex_group_second_child_int_type_recurse(): """Test that re_match_iter_typed(recurse=False) finds the second child with a named regex""" config = """! @@ -451,6 +610,7 @@ class ALL assert obj.text == "interface GigabitEthernet 1/1" assert uut_result == 911 + def testVal_re_match_iter_typed_intf_norecurse(): """Test that re_match_iter_typed(recurse=False) finds the parent and returns a `str`""" config = """! @@ -503,6 +663,7 @@ class ALL assert obj.text == "interface GigabitEthernet 1/1" assert uut_result["intf"] == "GigabitEthernet 1/1" + def testVal_re_match_iter_typed_vlan_recurse(): """Test that re_match_iter_typed(recurse=False) finds the second child and returns an `int`""" config = """!