From 9d771fdea48bf35099f8aef17fa2303514e7b1a4 Mon Sep 17 00:00:00 2001 From: Erik Reid <erik.reid@geant.org> Date: Fri, 12 Apr 2019 10:50:51 +0200 Subject: [PATCH] added /data/pop route, some name refactoring --- inventory_provider/db/opsdb.py | 5 +-- inventory_provider/routes/classifier.py | 2 +- inventory_provider/routes/data.py | 2 +- test/per_router/test_data_routes.py | 4 +- test/test_classifier_routes.py | 4 +- test/test_general_data_routes.py | 57 +++++++++++++++++++++++++ test/test_opsdb_queries.py | 4 +- 7 files changed, 67 insertions(+), 11 deletions(-) diff --git a/inventory_provider/db/opsdb.py b/inventory_provider/db/opsdb.py index a53e4065..b39e51e7 100644 --- a/inventory_provider/db/opsdb.py +++ b/inventory_provider/db/opsdb.py @@ -212,7 +212,7 @@ def get_circuit_hierarchy(connection): # pragma: no cover return r -def get_pop_info(connection, equipment_name): +def lookup_pop_info(connection, equipment_name): query = """ SELECT e.name AS equipment_name, @@ -251,8 +251,7 @@ ORDER BY FIELD(e.status, 'Operational', 'Installed') return list([_row2rsp(r) for r in rows]) - -def get_coriant_path(connection, equipment_name, card_id, port_number): +def lookup_coriant_path(connection, equipment_name, card_id, port_number): base_query = """ SELECT diff --git a/inventory_provider/routes/classifier.py b/inventory_provider/routes/classifier.py index b20be5b9..0cfac3b4 100644 --- a/inventory_provider/routes/classifier.py +++ b/inventory_provider/routes/classifier.py @@ -298,7 +298,7 @@ def get_coriant_info(equipment_name, card_id, port_number): config = current_app.config['INVENTORY_PROVIDER_CONFIG'] with db.connection(config['ops-db']) as cx: - path = opsdb.get_coriant_path( + path = opsdb.lookup_coriant_path( cx, equipment_name, card_id, port_number) if not path: diff --git a/inventory_provider/routes/data.py b/inventory_provider/routes/data.py index 73b6c207..6eef23b8 100644 --- a/inventory_provider/routes/data.py +++ b/inventory_provider/routes/data.py @@ -47,7 +47,7 @@ def equipment_location(equipment_name): config = current_app.config['INVENTORY_PROVIDER_CONFIG'] with db.connection(config['ops-db']) as cx: - result = opsdb.get_equipment_location_data(cx, equipment_name) + result = opsdb.lookup_pop_info(cx, equipment_name) if not result: return Response( diff --git a/test/per_router/test_data_routes.py b/test/per_router/test_data_routes.py index f196d156..aa47ad7d 100644 --- a/test/per_router/test_data_routes.py +++ b/test/per_router/test_data_routes.py @@ -59,7 +59,7 @@ def test_snmp_ids(router, client_with_mocked_data): } rv = client_with_mocked_data.post( - "/data/snmp/" + router, + "/testing/snmp/" + router, headers=DEFAULT_REQUEST_HEADERS) response = json.loads(rv.data.decode("utf-8")) @@ -111,7 +111,7 @@ def test_router_bgp_routes(router, client_with_mocked_data): } rv = client_with_mocked_data.post( - "/data/bgp/" + router, + "/testing/bgp/" + router, headers=DEFAULT_REQUEST_HEADERS) assert rv.status_code == 200 diff --git a/test/test_classifier_routes.py b/test/test_classifier_routes.py index cdbdc396..413565fd 100644 --- a/test/test_classifier_routes.py +++ b/test/test_classifier_routes.py @@ -292,7 +292,7 @@ def test_coriant_info(client, mocker): mocker.patch( 'inventory_provider.db.db.connection', mocked_connection) mocker.patch( - 'inventory_provider.db.opsdb.get_coriant_path', + 'inventory_provider.db.opsdb.lookup_coriant_path', lambda a, b, c, d: {'C': a, 'E': b, 'CID': c, 'P': d}) rv = client.get( @@ -319,7 +319,7 @@ def test_coriant_info_not_found(client, mocker): mocker.patch( 'inventory_provider.db.db.connection', mocked_connection) mocker.patch( - 'inventory_provider.db.opsdb.get_coriant_path', + 'inventory_provider.db.opsdb.lookup_coriant_path', lambda a, b, c, d: None) rv = client.get( diff --git a/test/test_general_data_routes.py b/test/test_general_data_routes.py index dd4315ce..b11771ef 100644 --- a/test/test_general_data_routes.py +++ b/test/test_general_data_routes.py @@ -1,5 +1,9 @@ +import contextlib import json import jsonschema +import pytest + +from inventory_provider.db import db DEFAULT_REQUEST_HEADERS = { "Content-type": "application/json", @@ -21,3 +25,56 @@ def test_get_routers(client): response = json.loads(rv.data.decode("utf-8")) jsonschema.validate(response, version_schema) assert response + + +def test_pop_info(client, mocker): + """ + just check the correct method is called, but mock out all sql access + """ + + expected_pop_info = { + 'C': 'bogus connection', + 'E': 'bogus equipment name' + } + + @contextlib.contextmanager + def mocked_connection(ignored): + yield expected_pop_info['C'] + + mocker.patch( + 'inventory_provider.db.db.connection', mocked_connection) + mocker.patch( + 'inventory_provider.db.opsdb.lookup_pop_info', + lambda c, e: {'C': c, 'E': e}) + + rv = client.get( + '/data/pop/{E}'.format(**expected_pop_info), + headers=DEFAULT_REQUEST_HEADERS) + + assert rv.status_code == 200 + assert rv.is_json + response = json.loads(rv.data.decode('utf-8')) + + assert response == expected_pop_info + + +def test_pop_not_found(client, mocker): + """ + just check the correct method is called, but mock out all sql access + """ + + @contextlib.contextmanager + def mocked_connection(ignored): + yield 'xyz' + + mocker.patch( + 'inventory_provider.db.db.connection', mocked_connection) + mocker.patch( + 'inventory_provider.db.opsdb.lookup_pop_info', + lambda a, b: None) + + rv = client.get( + '/data/pop/aabbcc', + headers=DEFAULT_REQUEST_HEADERS) + + assert rv.status_code == 404 diff --git a/test/test_opsdb_queries.py b/test/test_opsdb_queries.py index 3c1cd390..0584ed3e 100644 --- a/test/test_opsdb_queries.py +++ b/test/test_opsdb_queries.py @@ -154,7 +154,7 @@ CORIANT_PATH_METADATA = { 'mx1.gen.ch.geant.net' ]) def test_equipment_location(connection, equipment): - circuit = opsdb.get_pop_info(connection, equipment) + circuit = opsdb.lookup_pop_info(connection, equipment) jsonschema.validate(circuit, EQUIPMENT_LOCATION_METADATA) assert len(circuit) == 1 @@ -166,5 +166,5 @@ def test_equipment_location(connection, equipment): ('grv3.lon.uk.geant.net', '1-1', '1'), ]) def test_coriant_path(connection, equipment, card, port): - circuit = opsdb.get_coriant_path(connection, equipment, card, port) + circuit = opsdb.lookup_coriant_path(connection, equipment, card, port) jsonschema.validate(circuit, CORIANT_PATH_METADATA) -- GitLab