diff --git a/inventory_provider/routes/testing.py b/inventory_provider/routes/testing.py index 16f1d670265c555f20938d5e2099fe5eb660f0c8..1770be9db9cc80e6a543010105935d0788101e43 100644 --- a/inventory_provider/routes/testing.py +++ b/inventory_provider/routes/testing.py @@ -1,4 +1,6 @@ -from flask import Blueprint, Response +import json + +from flask import Blueprint, Response, current_app, jsonify from inventory_provider.routes import common routes = Blueprint("inventory-data-testing-support-routes", __name__) @@ -8,3 +10,33 @@ routes = Blueprint("inventory-data-testing-support-routes", __name__) def flushdb(): common.get_redis().flushdb() return Response('OK') + + +@routes.route("/infinera-dna-addresses", methods=['GET', 'POST']) +@common.require_accepts_json +def infinera_addresses(): + infinera_config = current_app.config[ + "INVENTORY_PROVIDER_CONFIG"]["infinera-dna"] + return jsonify([dna['address'] for dna in infinera_config]) + + +@routes.route("/coriant-tnms-addresses", methods=['GET', 'POST']) +@common.require_accepts_json +def coriant_addresses(): + coriant_config = current_app.config[ + "INVENTORY_PROVIDER_CONFIG"]["coriant-tnms"] + return jsonify([tnms['address'] for tnms in coriant_config]) + + +@routes.route("/juniper-server-addresses", methods=['GET', 'POST']) +@common.require_accepts_json +def juniper_addresses(): + # TODO: this route (and corant, infinera routes) can be removed + r = common.get_redis() + routers = [] + for k in r.keys('junosspace:*'): + info = r.get(k.decode('utf-8')) + assert info # sanity: value shouldn't be empty + info = json.loads(info.decode('utf-8')) + routers.append(info['address']) + return jsonify(routers) diff --git a/test/test_testing_routes.py b/test/test_testing_routes.py index 44ae19a006225b9386cebb8eba3d0b71f62a9d72..c3fa0e53881b64e136813d31ca74fb6e60e25019 100644 --- a/test/test_testing_routes.py +++ b/test/test_testing_routes.py @@ -1,4 +1,48 @@ +import json +import jsonschema + +DEFAULT_REQUEST_HEADERS = { + "Content-type": "application/json", + "Accept": ["application/json"] +} + +ROUTER_LIST_SCHEMA = { + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "array", + "items": {"type": "string"} +} + def test_flushdb(client): rv = client.post("/testing/flushdb") assert rv.status_code == 200 + + +def test_infinera_addresses(client): + rv = client.post( + "/testing/infinera-dna-addresses", + headers=DEFAULT_REQUEST_HEADERS) + assert rv.status_code == 200 + jsonschema.validate( + json.loads(rv.data.decode("utf-8")), + ROUTER_LIST_SCHEMA) + + +def test_coriant_addresses(client): + rv = client.post( + "/testing/coriant-tnms-addresses", + headers=DEFAULT_REQUEST_HEADERS) + assert rv.status_code == 200 + jsonschema.validate( + json.loads(rv.data.decode("utf-8")), + ROUTER_LIST_SCHEMA) + + +def test_juniper_addresses(mocker, client): + rv = client.post( + "/testing/juniper-server-addresses", + headers=DEFAULT_REQUEST_HEADERS) + assert rv.status_code == 200 + response_data = json.loads(rv.data.decode('utf-8')) + jsonschema.validate(response_data, ROUTER_LIST_SCHEMA) + assert len(response_data) > 0 # test data is not empty