Skip to content
Snippets Groups Projects
test_juniper_data_global.py 3.59 KiB
import ast
import netifaces
import ipaddress

import pytest

from inventory_provider import juniper

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)