diff --git a/inventory_provider/routes/msr.py b/inventory_provider/routes/msr.py index 9a515c95ff048398f253fd032758be51d3ba25e1..72df4fd8a70ec115317d4e25b5ac6e73addb1724 100644 --- a/inventory_provider/routes/msr.py +++ b/inventory_provider/routes/msr.py @@ -139,6 +139,36 @@ IP_ADDRESS_LIST_SCHEMA = { '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 def after_request(resp): @@ -414,7 +444,7 @@ def _get_subnet_interfaces(address, r): 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): ims_source_equipment = get_ims_equipment_name( ifc_info['router'], r) @@ -549,10 +579,19 @@ def get_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. (This functionality is mainly for testing/debugging - it's not expected to be used in production.) diff --git a/test/test_msr_routes.py b/test/test_msr_routes.py index ea1cc2dff3707ec10eae18011cf74c769af05d82..2e266107c15ea45c30b2d9edee52a5b7f2497262 100644 --- a/test/test_msr_routes.py +++ b/test/test_msr_routes.py @@ -4,11 +4,13 @@ import jsonschema import pytest 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.tasks.common import _get_redis + DEFAULT_REQUEST_HEADERS = { - "Content-type": "application/json", "Accept": ["application/json"] } @@ -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): headers = {'Content-Type': 'application/json'} headers.update(DEFAULT_REQUEST_HEADERS) @@ -252,9 +272,9 @@ def test_peering_services(client): assert rv.status_code == 200 assert rv.is_json 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 + jsonschema.validate(response_data, PEERING_ADDRESS_SERVICES_LIST) 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.is_json 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 + jsonschema.validate(response_data, PEERING_ADDRESS_SERVICES_LIST)