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

added a response schema and test this

parent 834595eb
No related branches found
No related tags found
No related merge requests found
...@@ -139,6 +139,36 @@ IP_ADDRESS_LIST_SCHEMA = { ...@@ -139,6 +139,36 @@ IP_ADDRESS_LIST_SCHEMA = {
'minItems': 1 'minItems': 1
} }
PEERING_ADDRESS_SERVICES_LIST = {
'$schema': 'http://json-schema.org/draft-07/schema#',
'definitions': {
'service': {
'properties': {
'name': {'type': 'string'},
'type': {'type': 'string'},
'status': {'type': 'string'}
},
'required': ['name', 'type', 'status'],
'additionalProperties': False
},
'address-service-info': {
'properties': {
'address': {'type': 'string'},
'hostname': {'type': 'string'},
'interface': {'type': 'string'},
'services': {
'type': 'array',
'items': {'$ref': '#/definitions/service'}
}
},
'required': ['address', 'hostname', 'interface', 'services'],
'additionalProperties': False
}
},
'type': 'array',
'items': {'$ref': '#/definitions/address-service-info'}
}
@routes.after_request @routes.after_request
def after_request(resp): def after_request(resp):
...@@ -414,7 +444,7 @@ def _get_subnet_interfaces(address, r): ...@@ -414,7 +444,7 @@ def _get_subnet_interfaces(address, r):
return [] return []
def _get_peer_address_services(address: str, r): def _get_peer_address_services(address: str, r: 'StrictRedis'):
for ifc_info in _get_subnet_interfaces(address, r): for ifc_info in _get_subnet_interfaces(address, r):
ims_source_equipment = get_ims_equipment_name( ims_source_equipment = get_ims_equipment_name(
ifc_info['router'], r) ifc_info['router'], r)
...@@ -549,10 +579,19 @@ def get_peering_services(): ...@@ -549,10 +579,19 @@ def get_peering_services():
""" """
Handler for `/msr/bgp/peering-services` Handler for `/msr/bgp/peering-services`
Takes a json-formatted payload with the following schema: This method must be called with POST method, and the payload
should be a json-formatted list of addresses (strings), which will
be validated against the following schema:
.. asjson::
inventory_provider.routes.msr.IP_ADDRESS_LIST_SCHEMA
The response will be formatted as follows:
.. asjson::
inventory_provider.routes.msr.PEERING_ADDRESS_SERVICES_LIST
A parameter `no-threads` can be given. If its truthiness A `no-threads` can be also be given. If its truthiness
value evaluates to True, then the lookups are done in a single thread. value evaluates to True, then the lookups are done in a single thread.
(This functionality is mainly for testing/debugging - it's not (This functionality is mainly for testing/debugging - it's not
expected to be used in production.) expected to be used in production.)
......
...@@ -4,11 +4,13 @@ import jsonschema ...@@ -4,11 +4,13 @@ import jsonschema
import pytest import pytest
from inventory_provider.routes.msr import PEERING_LIST_SCHEMA, \ from inventory_provider.routes.msr import PEERING_LIST_SCHEMA, \
PEERING_GROUP_LIST_SCHEMA, IP_ADDRESS_LIST_SCHEMA PEERING_GROUP_LIST_SCHEMA, PEERING_ADDRESS_SERVICES_LIST, \
_get_peer_address_services
from inventory_provider.routes.poller import SERVICES_LIST_SCHEMA from inventory_provider.routes.poller import SERVICES_LIST_SCHEMA
from inventory_provider.tasks.common import _get_redis
DEFAULT_REQUEST_HEADERS = { DEFAULT_REQUEST_HEADERS = {
"Content-type": "application/json",
"Accept": ["application/json"] "Accept": ["application/json"]
} }
...@@ -242,6 +244,24 @@ _OUTAGE_PEER_ADDRESSES = [ ...@@ -242,6 +244,24 @@ _OUTAGE_PEER_ADDRESSES = [
] ]
@pytest.mark.parametrize('address', [
'62.40.127.141',
'62.40.127.139'
])
def test_lookup_peer_services(address, mocked_redis):
_redis_instance = _get_redis({
'redis': {
'hostname': None,
'port': None
},
'redis-databases': [0, 7]
})
info = list(_get_peer_address_services(address, r=_redis_instance))
jsonschema.validate(info, PEERING_ADDRESS_SERVICES_LIST)
assert all(x['services'] for x in info)
def test_peering_services(client): def test_peering_services(client):
headers = {'Content-Type': 'application/json'} headers = {'Content-Type': 'application/json'}
headers.update(DEFAULT_REQUEST_HEADERS) headers.update(DEFAULT_REQUEST_HEADERS)
...@@ -252,9 +272,9 @@ def test_peering_services(client): ...@@ -252,9 +272,9 @@ def test_peering_services(client):
assert rv.status_code == 200 assert rv.status_code == 200
assert rv.is_json assert rv.is_json
response_data = json.loads(rv.data.decode('utf-8')) response_data = json.loads(rv.data.decode('utf-8'))
# jsonschema.validate(response_data, IP_ADDRESS_LIST_SCHEMA)
assert response_data # test data is non-empty assert response_data # test data is non-empty
jsonschema.validate(response_data, PEERING_ADDRESS_SERVICES_LIST)
def test_peering_services_single_threaded(client): def test_peering_services_single_threaded(client):
...@@ -268,6 +288,6 @@ def test_peering_services_single_threaded(client): ...@@ -268,6 +288,6 @@ def test_peering_services_single_threaded(client):
assert rv.status_code == 200 assert rv.status_code == 200
assert rv.is_json assert rv.is_json
response_data = json.loads(rv.data.decode('utf-8')) response_data = json.loads(rv.data.decode('utf-8'))
# jsonschema.validate(response_data, IP_ADDRESS_LIST_SCHEMA)
assert response_data # test data is non-empty assert response_data # test data is non-empty
jsonschema.validate(response_data, PEERING_ADDRESS_SERVICES_LIST)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment