diff --git a/tests/conftest.py b/tests/conftest.py index 4c67f2ed..99d65dd3 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -25,6 +25,8 @@ import sys import os +sys.path.insert(0, "..") + from ciscoconfparse.ciscoconfparse import CiscoConfParse from ciscoconfparse.ccp_util import PythonOptimizeCheck import dns.exception diff --git a/tests/test_Ccp_Util.py b/tests/test_Ccp_Util.py index 9bcae077..b41b3e03 100755 --- a/tests/test_Ccp_Util.py +++ b/tests/test_Ccp_Util.py @@ -37,6 +37,7 @@ from ciscoconfparse.ccp_util import collapse_addresses import pytest +from loguru import logger from ipaddress import IPv4Network, IPv6Network, IPv4Address, IPv6Address import ipaddress @@ -611,9 +612,15 @@ def test_dns_lookup(): result_correct_address = "65.19.187.2" result_correct = {"addrs": [result_correct_address], "name": test_hostname, "error": "", "record_type": "A"} try: - test_result = dns_lookup(test_hostname) - except Timeout: - pytest.skip("Skipping due to DNS resolver timeout") + test_result = dns_lookup(test_hostname, timeout=0.45) + except Timeout as ttt: + error = f"Skipping due to DNS resolver timeout: {ttt}" + logger.warning(error) + pytest.skip(error) + except Exception as eee: + error = f"Skipping test_dns_lookup due to Exception: {eee}" + logger.warning(error) + pytest.skip(error) if test_result["error"] != "": assert dns_lookup(test_hostname) == result_correct @@ -624,9 +631,8 @@ def test_dns_lookup(): def test_reverse_dns_lookup(): #result_correct = {"addrs": ["127.0.0.1"], "name": "localhost.", "error": ""} - test_result = reverse_dns_lookup("127.0.0.1") - assert isinstance(test_result, dict) try: + test_result = reverse_dns_lookup("127.0.0.1", timeout=0.1) assert test_result["error"] == "" except Exception: pytest.skip(test_result["error"]) @@ -703,16 +709,18 @@ def test_CiscoInterface_07(): def test_CiscoRange_01(): """Basic vlan range test""" - result_correct = {CiscoInterface("1")} - uut_str = "1" + result_correct = {"1", "2", "3"} + uut_str = "1-3" + assert isinstance(uut_str, str) assert CiscoRange(uut_str).as_set == result_correct assert CiscoRange(uut_str).iterate_attribute == "port" def test_CiscoRange_02(): """Basic vlan range test""" - result_correct = {CiscoInterface("1"), CiscoInterface("3")} + result_correct = {"1", "3"} uut_str = "1,3" + assert isinstance(uut_str, str) assert CiscoRange(uut_str).as_set == result_correct assert CiscoRange(uut_str).iterate_attribute == "port" @@ -720,193 +728,127 @@ def test_CiscoRange_02(): def test_CiscoRange_03(): """Basic vlan range test""" result_correct = { - CiscoInterface("1"), - CiscoInterface("2"), - CiscoInterface("3"), - CiscoInterface("4"), - CiscoInterface("5"), + "1", + "2", + "3", + "4", + "5", } uut_str = "1,2-4,5" + assert isinstance(uut_str, str) assert CiscoRange(uut_str).as_set == result_correct assert CiscoRange(uut_str).iterate_attribute == "port" def test_CiscoRange_04(): """Basic vlan range test""" - result_correct = { - CiscoInterface("1"), - CiscoInterface("2"), - CiscoInterface("3"), - CiscoInterface("4"), - CiscoInterface("5"), - } + result_correct = {"1", "2", "3", "4", "5"} uut_str = "1-3,4,5" + assert isinstance(uut_str, str) assert CiscoRange(uut_str).as_set == result_correct assert CiscoRange(uut_str).iterate_attribute == "port" def test_CiscoRange_05(): """Basic vlan range test""" - result_correct = { - CiscoInterface("1"), - CiscoInterface("2"), - CiscoInterface("3"), - CiscoInterface("4"), - CiscoInterface("5"), - } + result_correct = {"1", "2", "3", "4", "5"} uut_str = "1,2,3-5" + assert isinstance(uut_str, str) assert CiscoRange(uut_str).as_set == result_correct assert CiscoRange(uut_str).iterate_attribute == "port" def test_CiscoRange_06(): """Basic slot range test""" - result_correct = { - CiscoInterface("1/1"), - CiscoInterface("1/2"), - CiscoInterface("1/3"), - CiscoInterface("1/4"), - CiscoInterface("1/5"), - } + result_correct = {"1/1", "1/2", "1/3", "1/4", "1/5"} uut_str = "1/1-3,4,5" + assert isinstance(uut_str, str) assert CiscoRange(uut_str).as_set == result_correct assert CiscoRange(uut_str).iterate_attribute == "port" def test_CiscoRange_07(): """Basic slot range test""" - result_correct = { - CiscoInterface("1/1"), - CiscoInterface("1/2"), - CiscoInterface("1/3"), - CiscoInterface("1/4"), - CiscoInterface("1/5"), - } + result_correct = {"1/1", "1/2", "1/3", "1/4", "1/5"} uut_str = "1/1,2-4,5" + assert isinstance(uut_str, str) assert CiscoRange(uut_str).as_set == result_correct def test_CiscoRange_08(): """Basic slot range test""" - result_correct = { - CiscoInterface("1/1"), - CiscoInterface("1/2"), - CiscoInterface("1/3"), - CiscoInterface("1/4"), - CiscoInterface("1/5"), - } + result_correct = {"1/1", "1/2", "1/3", "1/4", "1/5"} uut_str = "1/1,2,3-5" + assert isinstance(uut_str, str) assert CiscoRange(uut_str).as_set == result_correct def test_CiscoRange_09(): """Basic slot range test""" - result_correct = { - CiscoInterface("2/1/1"), - CiscoInterface("2/1/2"), - CiscoInterface("2/1/3"), - CiscoInterface("2/1/4"), - CiscoInterface("2/1/5"), - } + result_correct = {"2/1/1", "2/1/2", "2/1/3", "2/1/4", "2/1/5"} uut_str = "2/1/1-3,4,5" + assert isinstance(uut_str, str) assert CiscoRange(uut_str).as_set == result_correct def test_CiscoRange_10(): """Basic slot range test""" - result_correct = { - CiscoInterface("2/1/1"), - CiscoInterface("2/1/2"), - CiscoInterface("2/1/3"), - CiscoInterface("2/1/4"), - CiscoInterface("2/1/5"), - } + result_correct = {"2/1/1", "2/1/2", "2/1/3", "2/1/4", "2/1/5"} uut_str = "2/1/1,2-4,5" + assert isinstance(uut_str, str) assert CiscoRange(uut_str).as_set == result_correct def test_CiscoRange_11(): """Basic slot range test""" - result_correct = { - CiscoInterface("2/1/1"), - CiscoInterface("2/1/2"), - CiscoInterface("2/1/3"), - CiscoInterface("2/1/4"), - CiscoInterface("2/1/5"), - } + result_correct = {"2/1/1", "2/1/2", "2/1/3", "2/1/4", "2/1/5"} uut_str = "2/1/1,2,3-5" + assert isinstance(uut_str, str) assert CiscoRange(uut_str).as_set == result_correct if False: def test_CiscoRange_12(): """Basic interface slot range test""" - result_correct = { - CiscoInterface("interface Eth2/1/1"), - CiscoInterface("interface Eth2/1/2"), - CiscoInterface("interface Eth2/1/3"), - CiscoInterface("interface Eth2/1/4"), - CiscoInterface("interface Eth2/1/5"), - } + result_correct = {"2/1/1", "2/1/2", "2/1/3", "2/1/4", "2/1/5"} uut_str = "interface Eth2/1/1-3,4,5" + assert isinstance(uut_str, str) assert CiscoRange(uut_str).as_set == result_correct def test_CiscoRange_13(): """Basic interface slot range test""" - result_correct = { - CiscoInterface("interface Eth2/1/1"), - CiscoInterface("interface Eth2/1/2"), - CiscoInterface("interface Eth2/1/3"), - CiscoInterface("interface Eth2/1/4"), - CiscoInterface("interface Eth2/1/5"), - } + result_correct = {"2/1/1", "2/1/2", "2/1/3", "2/1/4", "2/1/5"} uut_str = "interface Eth2/1/1,2-4,5" + assert isinstance(uut_str, str) assert CiscoRange(uut_str).as_set == result_correct def test_CiscoRange_14(): """Basic interface slot range test""" - result_correct = { - CiscoInterface("interface Eth2/1/1"), - CiscoInterface("interface Eth2/1/2"), - CiscoInterface("interface Eth2/1/3"), - CiscoInterface("interface Eth2/1/4"), - CiscoInterface("interface Eth2/1/5"), - } + result_correct = {"2/1/1", "2/1/2", "2/1/3", "2/1/4", "2/1/5"} uut_str = "interface Eth2/1/1,2,3-5" + assert isinstance(uut_str, str) assert CiscoRange(uut_str).as_set == result_correct def test_CiscoRange_15(): """Basic interface slot range test""" - result_correct = { - CiscoInterface("interface Eth2/1/1"), - CiscoInterface("interface Eth2/1/2"), - CiscoInterface("interface Eth2/1/3"), - CiscoInterface("interface Eth2/1/4"), - CiscoInterface("interface Eth2/1/5"), - } + result_correct = {"2/1/1", "2/1/2", "2/1/3", "2/1/4", "2/1/5"} uut_str = "interface Eth 2/1/1,2,3-5" + assert isinstance(uut_str, str) assert CiscoRange(uut_str).as_set == result_correct def test_CiscoRange_18(): """Parse a string with a common prefix on all of the CiscoRange() inputs""" result_correct = { - CiscoInterface("Eth1/1"), - CiscoInterface("Eth1/10"), - CiscoInterface("Eth1/12"), - CiscoInterface("Eth1/13"), - CiscoInterface("Eth1/14"), - CiscoInterface("Eth1/15"), - CiscoInterface("Eth1/16"), - CiscoInterface("Eth1/17"), - CiscoInterface("Eth1/18"), - CiscoInterface("Eth1/19"), - CiscoInterface("Eth1/20"), + "Eth1/1", "Eth1/10", "Eth1/12", "Eth1/13", "Eth1/14", + "Eth1/15", "Eth1/16", "Eth1/17", "Eth1/18", "Eth1/19", + "Eth1/20" } uut_str = "Eth1/1,Eth1/12-20,Eth1/16,Eth1/10" + assert isinstance(uut_str, str) assert CiscoRange(uut_str).as_set == result_correct @@ -914,28 +856,33 @@ def test_CiscoRange_18(): def test_CiscoRange_19(): """Parse a string with a common prefix on all of the CiscoRange() inputs""" result_correct = { - CiscoInterface("interface Eth1/1"), - CiscoInterface("interface Eth1/10"), - CiscoInterface("interface Eth1/12"), - CiscoInterface("interface Eth1/13"), - CiscoInterface("interface Eth1/14"), - CiscoInterface("interface Eth1/15"), - CiscoInterface("interface Eth1/16"), - CiscoInterface("interface Eth1/17"), - CiscoInterface("interface Eth1/18"), - CiscoInterface("interface Eth1/19"), - CiscoInterface("interface Eth1/20"), + "interface Eth1/1", "interface Eth1/10", "interface Eth1/12", + "interface Eth1/13", "interface Eth1/14", "interface Eth1/15", + "interface Eth1/16", "interface Eth1/17", "interface Eth1/18", + "interface Eth1/19", "interface Eth1/20" } uut_str = "interface Eth1/1,interface Eth1/12-20,interface Eth1/16,interface Eth1/10" + assert isinstance(uut_str, str) assert CiscoRange(uut_str).as_set == result_correct def test_CiscoRange_compressed_str_01(): """compressed_str test""" uut_str = "1,2, 3, 6, 7, 8 , 9, 911" + assert isinstance(uut_str, str) assert CiscoRange(uut_str).compressed_str == "1-3,6-9,911" + assert "1" in CiscoRange(uut_str) + assert "2" in CiscoRange(uut_str) + assert "3" in CiscoRange(uut_str) + assert "6" in CiscoRange(uut_str) + assert "7" in CiscoRange(uut_str) + assert "8" in CiscoRange(uut_str) + assert "9" in CiscoRange(uut_str) + assert "911" in CiscoRange(uut_str) def test_CiscoRange_contains(): uut_str = "Ethernet1/1-20" - assert CiscoInterface("Ethernet1/2") in CiscoRange(uut_str) + assert isinstance(uut_str, str) + # Ethernet1/5 is in CiscoRange("Ethernet1/1-20")... + assert "Ethernet1/5" in CiscoRange(uut_str) diff --git a/tests/test_Models_Cisco.py b/tests/test_Models_Cisco.py index ecc37447..1a049f07 100755 --- a/tests/test_Models_Cisco.py +++ b/tests/test_Models_Cisco.py @@ -280,9 +280,8 @@ def testVal_IOSIntfLine_trunk_vlan_allowed_01(): ] cfg = CiscoConfParse(lines, factory=True) intf_obj = cfg.find_objects("^interface")[0] - logger.critical(f"""{len(intf_obj.trunk_vlans_allowed.as_list) == len(range(1, 4095))}""") - assert len(intf_obj.trunk_vlans_allowed.as_list) == len(range(1, 4095)) - assert intf_obj.trunk_vlans_allowed.as_list == list(range(1, 4095)) + assert len(intf_obj.trunk_vlans_allowed.as_set) == len(range(1, 4095)) + assert intf_obj.trunk_vlans_allowed.as_set == {str(ii) for ii in range(1, 4095)} def testVal_IOSIntfLine_trunk_vlan_allowed_02(): @@ -296,7 +295,7 @@ def testVal_IOSIntfLine_trunk_vlan_allowed_02(): ] cfg = CiscoConfParse(lines, factory=True) intf_obj = cfg.find_objects("^interface")[0] - assert intf_obj.trunk_vlans_allowed.as_list == [2, 4, 6, 911] + assert intf_obj.trunk_vlans_allowed.as_set == {"2", "4", "6", "911"} def testVal_IOSIntfLine_trunk_vlan_allowed_03(): @@ -311,7 +310,7 @@ def testVal_IOSIntfLine_trunk_vlan_allowed_03(): ] cfg = CiscoConfParse(lines, factory=True) intf_obj = cfg.find_objects("^interface")[0] - assert intf_obj.trunk_vlans_allowed.as_list == [4, 6, 911] + assert intf_obj.trunk_vlans_allowed.as_set == {"4", "6", "911"} def testVal_IOSIntfLine_trunk_vlan_allowed_04(): @@ -326,7 +325,7 @@ def testVal_IOSIntfLine_trunk_vlan_allowed_04(): ] cfg = CiscoConfParse(lines, factory=True) intf_obj = cfg.find_objects("^interface")[0] - assert intf_obj.trunk_vlans_allowed.as_list == [1] + assert intf_obj.trunk_vlans_allowed.as_set == {"1"} def testVal_IOSIntfLine_trunk_vlan_allowed_05(): @@ -341,7 +340,7 @@ def testVal_IOSIntfLine_trunk_vlan_allowed_05(): ] cfg = CiscoConfParse(lines, factory=True) intf_obj = cfg.find_objects("^interface")[0] - assert intf_obj.trunk_vlans_allowed.as_list == [1,] + assert intf_obj.trunk_vlans_allowed.as_set == {"1",} def testVal_IOSIntfLine_trunk_vlan_allowed_06(): @@ -350,16 +349,14 @@ def testVal_IOSIntfLine_trunk_vlan_allowed_06(): "interface GigabitEthernet 1/1", " switchport mode trunk", " switchport trunk allowed vlan none", - " switchport trunk allowed vlan add 2-1000", - " switchport trunk allowed vlan add 1010-4094", + " switchport trunk allowed vlan add 2-5", + " switchport trunk allowed vlan add 17-19", " switchport trunk native vlan 911", "!", ] cfg = CiscoConfParse(lines, factory=True) intf_obj = cfg.find_objects("^interface")[0] - assert intf_obj.trunk_vlans_allowed == CiscoRange( - "2-1000,1010-4094", result_type=int - ) + assert intf_obj.trunk_vlans_allowed == CiscoRange(text="2-5,17-19", result_type=str) def testVal_IOSIntfLine_trunk_vlan_allowed_07(): @@ -376,7 +373,7 @@ def testVal_IOSIntfLine_trunk_vlan_allowed_07(): """ cfg = CiscoConfParse(config.splitlines(), factory=True) intf_obj = cfg.find_objects("^interface")[0] - assert intf_obj.trunk_vlans_allowed == CiscoRange("2-19,21-4094", result_type=int) + assert intf_obj.trunk_vlans_allowed == CiscoRange("2-19,21-4094", result_type=str) def testVal_IOSIntfLine_trunk_vlan_allowed_08(): @@ -389,7 +386,7 @@ def testVal_IOSIntfLine_trunk_vlan_allowed_08(): """ cfg = CiscoConfParse(config.splitlines(), factory=True) intf_obj = cfg.find_objects("^interface")[0] - assert intf_obj.trunk_vlans_allowed == CiscoRange() + assert intf_obj.trunk_vlans_allowed == CiscoRange(text="", result_type=str) def testVal_IOSIntfLine_abbvs(parse_c03_factory):