diff --git a/test/test_data_routes.py b/test/test_data_routes.py index e7ffe89fb3381e248cdfd1d0ca4be0de798a6c0a..06070847b218d626d2335655e923fe79716d5dc9 100644 --- a/test/test_data_routes.py +++ b/test/test_data_routes.py @@ -189,6 +189,17 @@ class MockedRedis(object): def set(self, key, value): MockedRedis.db[key] = value + def hget(self, key, field): + value = MockedRedis.db[key] + return json.dumps(value[field]).encode('utf-8') + + def hgetall(self, key): + result = {} + for k, v in MockedRedis.db[key].items(): + result[k.encode('utf-8')] \ + = json.dumps(v).encode('utf-8') + return result + def keys(self, *args, **kwargs): return list([k.encode("utf-8") for k in MockedRedis.db.keys()]) @@ -203,19 +214,121 @@ def client_with_mocked_data(mocker, client): return client -def test_routers_list(client_with_mocked_data): - +def _routers(client): routers_list_schema = { "$schema": "http://json-schema.org/draft-07/schema#", "type": "array", "items": {"type": "string"} } - rv = client_with_mocked_data.post( + rv = client.post( "data/routers", headers=DEFAULT_REQUEST_HEADERS) assert rv.status_code == 200 response = json.loads(rv.data.decode("utf-8")) jsonschema.validate(response, routers_list_schema) - assert response # shouldn't be empty + return response + + +def test_routers_list(client_with_mocked_data): + assert _routers(client_with_mocked_data) + + +def test_router_interfaces(client_with_mocked_data): + + interfaces_list_schema = { + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "array", + "items": {"type": "string"} + } + + for router in _routers(client_with_mocked_data): + rv = client_with_mocked_data.post( + "/data/interfaces/" + router, + headers=DEFAULT_REQUEST_HEADERS) + + response = json.loads(rv.data.decode("utf-8")) + jsonschema.validate(response, interfaces_list_schema) + assert response # at least shouldn't be empty + + +def test_router_bgp_route(client_with_mocked_data): + + bgp_list_schema = { + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "array", + "items": { + "type": "object", + "properties": { + "description": {"type": "string"}, + "as": { + "type": "object", + "properties": { + "peer": { + "type": "string", + "pattern": r'^\d+$' + }, + "local": { + "type": "string", + "pattern": r'^\d+$' + }, + }, + "required": ["peer", "local"], + "additionalProperties": False + }, + }, + "required": ["description", "as"], + "additionalProperties": False + } + } + + routers_with_bpg_configs = [ + "mx1.mil2.it.geant.net", + "mx1.vie.at.geant.net", + "mx1.fra.de.geant.net", + "mx1.ams.nl.geant.net", + "mx1.pra.cz.geant.net", + "mx1.dub.ie.geant.net", + "mx1.mad.es.geant.net", + "mx1.gen.ch.geant.net", + "mx1.mar.fr.geant.net", + "mx1.lon.uk.geant.net" + ] + + for router in _routers(client_with_mocked_data): + + if router not in routers_with_bpg_configs: + continue + + rv = client_with_mocked_data.post( + "/data/bgp/" + router, + headers=DEFAULT_REQUEST_HEADERS) + + response = json.loads(rv.data.decode("utf-8")) + jsonschema.validate(response, bgp_list_schema) + assert response # at least shouldn't be empty + + +def test_router_debug_data_route(client_with_mocked_data): + debug_data_schema = { + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "object", + "items": { + "type": "object", + "properties": { + "vrr": {"type": "object"}, + "bgp": {"type": "array"}, + "interfaces": {"type": "array"} + }, + "required": ["vrr", "bgp", "interfaces"], + "additionalProperties": False + } + } + + for router in _routers(client_with_mocked_data): + rv = client_with_mocked_data.post( + "/data/debug-dump/" + router, + headers=DEFAULT_REQUEST_HEADERS) + response = json.loads(rv.data.decode("utf-8")) + jsonschema.validate(response, debug_data_schema)