Skip to content
Snippets Groups Projects
Commit 2ed4f945 authored by Erik Reid's avatar Erik Reid
Browse files

also read top-level peerings

parent 98d1d157
No related branches found
No related tags found
No related merge requests found
......@@ -107,6 +107,64 @@ UNIT_SCHEMA = """<?xml version="1.1" encoding="UTF-8" ?>
</xs:schema>
""" # noqa: E501
PEERING_LIST_SCHEMA = {
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"top-level-peering": {
"type": "object",
"properties": {
"group": {"type": "string"},
"description": {"type": "string"},
"address": {"type": "string"},
"remote-asn": {"type": "integer"},
"local-asn": {"type": "integer"}
},
# lots of internal peerings - so maybe no explicit asn's
"required": ["group", "address"],
"additionalProperties": False
},
"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/top-level-peering"},
{"$ref": "#/definitions/instance-peering"},
{"$ref": "#/definitions/logical-system-peering"}
]
}
},
"type": "array",
"items": {"$ref": "#/definitions/peering"}
}
class NetconfHandlingError(Exception):
pass
......@@ -244,6 +302,13 @@ def all_bgp_peers(netconf_config):
info['description'] = description.text
return info
for group in netconf_config.xpath('//configuration/protocols/bgp/group'):
group_name = group.find('name').text
for neighbor in group.xpath('./neighbor'):
peer = _peering_params(neighbor)
peer['group'] = group_name
yield peer
for instance in netconf_config.xpath(
'//configuration/routing-instances/instance'):
instance_name = instance.find('name').text
......
import json
import pytest
import jsonschema
from inventory_provider.juniper import PEERING_LIST_SCHEMA
DEFAULT_REQUEST_HEADERS = {
"Content-type": "application/json",
......@@ -82,55 +83,11 @@ def test_router_bgp_routes(router, client):
pytest.skip('%s is not expected to have bgp peers' % router)
return
bgp_list_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"}
}
rv = client.post(
"/testing/bgp/" + router,
headers=DEFAULT_REQUEST_HEADERS)
assert rv.status_code == 200
response = json.loads(rv.data.decode("utf-8"))
jsonschema.validate(response, bgp_list_schema)
jsonschema.validate(response, PEERING_LIST_SCHEMA)
assert response # at least shouldn't be empty
......@@ -38,52 +38,8 @@ def test_interface_list(netconf_doc):
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)
jsonschema.validate(peerings, juniper.PEERING_LIST_SCHEMA)
assert peerings # there's always at least one
# confirm the addresses are in canonical (exploded) form
......
......@@ -88,9 +88,26 @@ def test_build_juniper_peering_db(mocked_worker_module):
:param mocked_worker_module: fixture
"""
peering_list_schema = {
# same as inventory_provider.juniper.PEERING_LIST_SCHEMA,
# but with "hostname" in every returned record
PEERING_LIST_SCHEMA = {
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"top-level-peering": {
"type": "object",
"properties": {
"group": {"type": "string"},
"description": {"type": "string"},
"address": {"type": "string"},
"remote-asn": {"type": "integer"},
"local-asn": {"type": "integer"},
"hostname": {"type": "string"}
},
# lots of internal peerings - so maybe no explicit asn's
"required": ["group", "address"],
"additionalProperties": False
},
"instance-peering": {
"type": "object",
"properties": {
......@@ -104,9 +121,7 @@ def test_build_juniper_peering_db(mocked_worker_module):
},
# description and-or local-asn is not always present,
# just based on empirical tests - not a problem
"required": ["instance", "group",
"address", "remote-asn",
"hostname"],
"required": ["instance", "group", "address", "remote-asn"],
"additionalProperties": False
},
"logical-system-peering": {
......@@ -122,11 +137,12 @@ def test_build_juniper_peering_db(mocked_worker_module):
},
# local/remote-asn and/or description are not always present,
# just based on empirical tests - not a problem
"required": ["logical-system", "group", "address", "hostname"],
"required": ["logical-system", "group", "address"],
"additionalProperties": False
},
"peering": {
"oneOf": [
{"$ref": "#/definitions/top-level-peering"},
{"$ref": "#/definitions/instance-peering"},
{"$ref": "#/definitions/logical-system-peering"}
]
......@@ -167,7 +183,7 @@ def test_build_juniper_peering_db(mocked_worker_module):
assert address == canonical
continue
jsonschema.validate(value, peering_list_schema)
jsonschema.validate(value, PEERING_LIST_SCHEMA)
assert found_record
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment