Skip to content
Snippets Groups Projects
test_juniper_data.py 3.60 KiB
import ast
import os

import jsonschema
from lxml import etree
import pytest

from inventory_provider import juniper

import inventory_provider

TEST_DATA_DIRNAME = os.path.realpath(os.path.join(
    inventory_provider.__path__[0],
    "..",
    "test",
    "data"))


class MockedJunosRpc(object):

    def __init__(self, hostname):
        filename = os.path.join(TEST_DATA_DIRNAME, "%s-netconf.xml" % hostname)
        self.config = etree.parse(filename)

    def get_config(self):
        return self.config


class MockedJunosDevice(object):

    def __init__(self, **kwargs):
        self.rpc = MockedJunosRpc(kwargs['host'])

    def open(self):
        pass


@pytest.fixture
def netconf_doc(mocker, router, data_config):
    mocker.patch(
        'inventory_provider.juniper.Device',
        MockedJunosDevice)
    return juniper.load_config(router, data_config['ssh'])


def test_interface_list(netconf_doc):

    schema = {
        "$schema": "http://json-schema.org/draft-07/schema#",
        "type": "array",
        "items": {
            "type": "object",
            "properties": {
                "name": {"type": "string"},
                "description": {"type": "string"},
                "ipv4": {
                    "type": "array",
                    "items": {"type": "string"}
                },
                "ipv6": {
                    "type": "array",
                    "items": {"type": "string"}
                }
            },
            "required": ["name", "description", "ipv4", "ipv6"],
            "additionalProperties": False
        }
    }

    interfaces = list(juniper.list_interfaces(netconf_doc))
    jsonschema.validate(interfaces, schema)
    assert interfaces  # at least shouldn't be empty


def test_bgp_list(netconf_doc):

    schema = {
        "$schema": "http://json-schema.org/draft-07/schema#",
        "type": "array",
        "items": {
            "type": "object",
            "properties": {
                "name": {"type": "string"},
                "description": {"type": "string"},
                "as": {
                    "type": "object",
                    "properties": {
                        "local": {"type": "integer"},
                        "peer": {"type": "integer"},
                    },
                    "required": ["local", "peer"],
                    "additionalProperties": False
                }
            },
            "required": ["name", "description", "as"],
            "additionalProperties": False
        }
    }

    routes = list(juniper.list_bgp_routes(netconf_doc))
    jsonschema.validate(routes, schema)


NETIFACES_TEST_DATA_STRING = """{
    'lo0':  {2: [{'addr': '127.0.0.1', 'netmask': '255.0.0.0', 'peer': '127.0.0.1'}],
             30: [{'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}]},
    'eth0': {18: [{'addr': '78:4f:43:76:73:ba'}],
             2: [{'addr': '83.97.92.239', 'netmask': '255.255.252.0', 'broadcast': '83.97.95.255'}],
             30: [{'addr': 'fe80::250:56ff:fea1:8340', 'netmask': 'ffff:ffff:ffff:ffff::/64', 'flags': 1024},
                 {'addr': '2001:798:3::104', 'netmask': 'ffff:ffff:ffff:ffff::/64', 'flags': 1088}]}
}"""  # noqa E501

NETIFACES_TEST_DATA = ast.literal_eval(NETIFACES_TEST_DATA_STRING)


def test_snmp_community_string(mocker, netconf_doc):
    mocker.patch('netifaces.interfaces', lambda: NETIFACES_TEST_DATA.keys())
    mocker.patch('netifaces.ifaddresses', lambda n: NETIFACES_TEST_DATA[n])
    assert juniper.snmp_community_string(netconf_doc) == '0pBiFbD'