diff --git a/inventory_provider/db/opsdb.py b/inventory_provider/db/opsdb.py index a53e40656a2b0acc0613a4d0cc42497340325899..b39e51e741c664abc87f9cfab45d2340d74b0103 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 b20be5b9fe3ed73e4aee9527727de7d2fb58dacf..0cfac3b4f2c077dc2f3259434a4044670c8c6009 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 73b6c207cd82e70e741b3f398058819deb7b0164..6eef23b86a31c64e6b6a1568693a5a1d609484a1 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 f196d15640f0d202da9b52bd4336a05f762c663a..aa47ad7d9f517381e7659e24c7d479bc498092bf 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 cdbdc396d34edd9aad75a33b7358c90ed22c45df..413565fdd7bbde28267a92d3242f5ad7f2caf2b2 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 dd4315cef5d0396b57009619b7ad186204797631..b11771ef035c47eb9d33ff4cb99b7bbc1f934cd7 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 3c1cd39027a75b78eab5a714dd2693ad43ff761c..0584ed3e035c0bd72de37bbb5e98220fea05e432 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)