Skip to content
Snippets Groups Projects
test_juniper_data.py 4.81 KiB
import ipaddress
import jsonschema
from inventory_provider import juniper


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"},
                "bundle": {
                    "type": "array",
                    "items": {"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_peering_data(netconf_doc):

    schema = {
        "$schema": "http://json-schema.org/draft-07/schema#",
        "definitions": {
            "instance-peering": {
                "type": "object",
                "properties": {
                    "instance": {"type": "string"},
                    "group": {"type": "string"},
                    "description": {"type": "string"},
                    "address": {"type": "string"},
                    "remote-asn": {"type": "integer"},
                    "local-asn": {"type": "integer"}
                },
                # description and-or local-asn is not always present,
                # just based on empirical tests - not a problem
                "required": ["instance", "group", "address", "remote-asn"],
                "additionalProperties": False
            },
            "logical-system-peering": {
                "type": "object",
                "properties": {
                    "logical-system": {"type": "string"},
                    "group": {"type": "string"},
                    "description": {"type": "string"},
                    "address": {"type": "string"},
                    "remote-asn": {"type": "integer"},
                    "local-asn": {"type": "integer"}
                },
                # local/remote-asn and/or description are not always present,
                # just based on empirical tests - not a problem
                "required": ["logical-system", "group", "address"],
                "additionalProperties": False
            },
            "peering": {
                "oneOf": [
                    {"$ref": "#/definitions/instance-peering"},
                    {"$ref": "#/definitions/logical-system-peering"}
                ]
            }
        },
        "type": "array",
        "items": {"$ref": "#/definitions/peering"}
    }

    peerings = list(juniper.all_bgp_peers(netconf_doc))
    jsonschema.validate(peerings, schema)
    assert peerings  # there's always at least one

    # confirm the addresses are in canonical (exploded) form
    for p in peerings:
        canonical_address = ipaddress.ip_address(p['address']).exploded
        assert p['address'] == canonical_address


def test_snmp_community_string(mocked_netifaces, netconf_doc):
    assert juniper.snmp_community_string(netconf_doc) == '0pBiFbD'


def test_interface_addresses_list(netconf_doc):
    schema = {
        "$schema": "http://json-schema.org/draft-07/schema#",

        "definitions": {
            "v4a": {
                "type": "string",
                "pattern": r'^(\d+\.){3}\d+$'
            },
            "v6a": {
                "type": "string",
                "pattern": r'^([a-f\d]{4}:){7}[a-f\d]{4}$'
            },
            "v4i": {
                "type": "string",
                "pattern": r'^(\d+\.){3}\d+/\d+$'
            },
            "v6i": {
                "type": "string",
                "pattern": r'^[a-f\d:]+/\d+$'
            }
        },

        "type": "array",
        "items": {
            "type": "object",
            "properties": {
                "name": {
                    "oneOf": [
                        {"$ref": "#/definitions/v4a"},
                        {"$ref": "#/definitions/v6a"}
                    ]
                },
                "interface address": {
                    "oneOf": [
                        {"$ref": "#/definitions/v4i"},
                        {"$ref": "#/definitions/v6i"}
                    ]
                },
                "interface name": {"type": "string"},
            },
            "required": ["name", "interface address", "interface name"],
            "additionalProperties": False
        }
    }

    addresses = list(juniper.interface_addresses(netconf_doc))
    jsonschema.validate(addresses, schema)