Skip to content
Snippets Groups Projects
Select Git revision
  • 99dd0408d7b61a40b355f02f4fa67bc26e2bb19d
  • develop default
  • master protected
  • inventoryProvider-functional
  • inventoryProvider-morework2
  • circuit-service-details-fix
  • lookup-SPECTRUM-SCHF-ports
  • inventoryProvider-1267-cleanup
  • inventoryProvider-moreWork
  • feature/DBOARD3-958
  • release/0.110
  • fix-uuid-validation-error
  • docker-poc
  • 0.160
  • 0.159
  • 0.158
  • 0.157
  • 0.156
  • 0.155
  • 0.154
  • 0.153
  • 0.152
  • 0.151
  • 0.150
  • 0.149
  • 0.148
  • 0.147
  • 0.146
  • 0.145
  • 0.144
  • 0.143
  • 0.142
  • 0.141
33 results

test_juniper_data_global.py

Blame
  • test_juniper_data_global.py 5.07 KiB
    import ast
    import netifaces
    import ipaddress
    import os
    
    from lxml import etree
    import pytest
    
    from inventory_provider import juniper
    from inventory_provider.juniper import asn_to_int
    
    NETIFACES_TEST_DATA_STRING = """{
        'lo0': {{AF_INET}: [{'addr': '127.0.0.1', 'netmask': '255.0.0.0', 'peer': '127.0.0.1'}],
                {AF_INET6}: [{'addr': '::1', 'netmask': 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128', 'peer': '::1', 'flags': 0},
                     {'addr': 'fe80::1%lo0', 'netmask': 'ffff:ffff:ffff:ffff::/64', 'flags': 0}]},
        'gif0': {},
        'stf0': {},
        'XHC20': {},
        'XHC4': {},
        'XHC3': {},
        'en3': {{AF_LINK}: [{'addr': 'b6:00:24:b9:f0:01'}]},
        'en8': {{AF_LINK}: [{'addr': 'b6:00:24:b9:f0:00'}]},
        'en4': {{AF_LINK}: [{'addr': 'b6:00:24:b9:f0:05'}]},
        'en9': {{AF_LINK}: [{'addr': 'b6:00:24:b9:f0:04'}]},
        'en0': {{AF_LINK}: [{'addr': '78:4f:43:76:73:ba'}],
                {AF_INET}: [{'addr': '195.169.24.149', 'netmask': '255.255.255.128', 'broadcast': '195.169.24.255'}],
                {AF_INET6}: [{'addr': 'fe80::1c97:ec77:3f3{AF_INET}:cdfe%en0', 'netmask': 'ffff:ffff:ffff:ffff::/64', 'flags': 1024},
                     {'addr': '2001:610:9d8:4:4d7:f763:9815:e78d', 'netmask': 'ffff:ffff:ffff:ffff::/64', 'flags': 1088},
                     {'addr': '2001:610:9d8:4:492e:61b6:2c9{AF_INET}:c387', 'netmask': 'ffff:ffff:ffff:ffff::/64', 'flags': 192}]},
        'p2p0': {{AF_LINK}: [{'addr': '0a:4f:43:76:73:ba'}]},
        'awdl0': {{AF_LINK}: [{'addr': '8e:87:e3:bb:9{AF_INET}:1f'}],
                  {AF_INET6}: [{'addr': 'fe80::8c87:e3ff:febb:921f%awdl0', 'netmask': 'ffff:ffff:ffff:ffff::/64', 'flags': 0}]},
        'bridge0': {{AF_LINK}: [{'addr': 'b6:00:24:b9:f0:01'}]},
        'utun0': {{AF_INET6}: [{'addr': 'fe80::8328:d0ef:52b4:d379%utun0', 'netmask': 'ffff:ffff:ffff:ffff::/64', 'flags': 0}]},
        'utun1': {{AF_INET6}: [{'addr': 'fe80::5a75:c789:2fa0:6ee4%utun1', 'netmask': 'ffff:ffff:ffff:ffff::/64', 'flags': 0}]},
        'XHC0': {},
        'XHC1': {},
        'XHC2': {},
        'en21': {{AF_LINK}: [{'addr': '64:4b:f0:10:23:25'}],
                 {AF_INET}: [{'addr': '195.169.24.170', 'netmask': '255.255.255.128', 'broadcast': '195.169.24.255'}],
                 {AF_INET6}: [{'addr': 'fe80::41c:798c:3fff:f8c9%en21', 'netmask': 'ffff:ffff:ffff:ffff::/64', 'flags': 1024},
                      {'addr': '2001:610:9d8:4:c1e:440{AF_INET}:e7cf:547f', 'netmask': 'ffff:ffff:ffff:ffff::/64', 'flags': 1088},
                      {'addr': '2001:610:9d8:4:911c:954d:d4e{AF_INET}:baef', 'netmask': 'ffff:ffff:ffff:ffff::/64', 'flags': 192}]},
        'en5': {{AF_LINK}: [{'addr': 'ac:de:48:00:11:22'}],
                {AF_INET6}: [{'addr': 'fe80::aede:48ff:fe00:1122%en5', 'netmask': 'ffff:ffff:ffff:ffff::/64', 'flags': 0}]},
        'en18': {{AF_LINK}: [{'addr': 'ca:3c:85:86:34:2a'}], {AF_INET}: [{'addr': '169.254.184.83', 'netmask': '255.255.0.0'}]},
    }"""  # noqa: E501
    
    
    @pytest.fixture
    def mocked_netifaces(mocker):
        s = NETIFACES_TEST_DATA_STRING
        for k, v in {
                    'AF_INET': netifaces.AF_INET,
                    'AF_INET6': netifaces.AF_INET6,
                    'AF_LINK': netifaces.AF_LINK
                }.items():
            s = s.replace('{%s}' % k, str(v))
        data = ast.literal_eval(s)
        mocker.patch('netifaces.interfaces', lambda: data.keys())
        mocker.patch('netifaces.ifaddresses', lambda n: data[n])
    
    
    def test_local_v4_interfaces(mocked_netifaces):
        addresses = list(juniper.local_interfaces())
        assert len(addresses) == 3
        for a in addresses:
            assert isinstance(a, ipaddress.IPv4Interface)
    
    
    def test_local_v6_interfaces(mocked_netifaces):
        addresses = list(juniper.local_interfaces(netifaces.AF_INET6))
        assert len(addresses) == 4
        for a in addresses:
            assert isinstance(a, ipaddress.IPv6Interface)
    
    
    def test_asn_to_int_functionality():
        """Test that ASN to int conversion works with and without dot notation."""
    
        # Test with dot notation
        assert asn_to_int('1.1') == 65537
        assert asn_to_int('64512.2') == 4227858434
        assert asn_to_int('0.65535') == 65535
    
        # Test without dot notation
        assert asn_to_int('65537') == 65537
        assert asn_to_int('0') == 0
        assert asn_to_int('4227858434') == 4227858434
    
        with pytest.raises(ValueError):
            asn_to_int('66512.2')
            asn_to_int('Test')
    
    
    def test_DBOARD3_833():
        """Test that the bundle inactive attribute is handled correctly.
    
        The test data contains an interface: et-4/0/2 with the bundle
        configuration (for ae6) marked as 'inactive'.  et-4/0/2 also
        has a logical interface configured.
    
        This test confirms that neither et-4/0/2 nor et-4/0/2.0 are
        part of a bundle.
        """
    
        test_filename = os.path.join(
            os.path.dirname(__file__),
            'data',
            'DBOARD3-833.xml')
        with open(test_filename, 'r') as f:
            netconf_config = etree.XML(f.read())
    
        interfaces = {
            i['name']: i for i in juniper.list_interfaces(netconf_config)}
    
        assert not interfaces['et-4/0/2']['bundle']
        assert not interfaces['et-4/0/2.0']['bundle']
    
        # additional sanity check: parent bundle should be empty or exactly one
        for ifc in interfaces.values():
            assert len(ifc['bundle']) in (0, 1)