diff --git a/test/test_alarmdb_routes.py b/test/test_alarmdb_routes.py new file mode 100644 index 0000000000000000000000000000000000000000..ae6b2b3b31a04b38f2a48dbac257cf392535a43d --- /dev/null +++ b/test/test_alarmdb_routes.py @@ -0,0 +1,107 @@ +import json +import jsonschema +import os +import pytest +import tempfile + +import inventory_provider + + +DEFAULT_REQUEST_HEADERS = { + "Content-type": "application/json", + "Accept": ["application/json"] +} + + +def data_config_filename(tmp_dir_name): + config = { + "alarms-db": { + "hostname": "xxxxxx", + "dbname": "xxxxxx", + "username": "xxxxxx", + "password": "xxxxxx" + }, + "ops-db": { + "hostname": "xxxxxxx.yyyyy.zzz", + "dbname": "xxxxxx", + "username": "xxxxxx", + "password": "xxxxxxxx" + }, + "oid_list.conf": os.path.join( + tmp_dir_name, + "oid_list.conf"), + "routers_community.conf": os.path.join( + tmp_dir_name, + "routers_community.conf"), + "ssh": { + "private-key": "private-key-filename", + "known-hosts": "known-hosts=filename" + }, + "redis": { + "hostname": "xxxxxx", + "port": 0 + } + } + + with open(config["oid_list.conf"], "w") as f: + f.write("") + + with open(config["routers_community.conf"], "w") as f: + f.write("") + + filename = os.path.join(tmp_dir_name, "config.json") + with open(filename, "w") as f: + f.write(json.dumps(config)) + + return filename + + +@pytest.fixture +def app_config(): + with tempfile.TemporaryDirectory() as tmpdir: + + app_config_filename = os.path.join(tmpdir, "app.config") + with open(app_config_filename, "w") as f: + f.write("%s = '%s'\n" % ( + "INVENTORY_PROVIDER_CONFIG_FILENAME", + data_config_filename(tmpdir))) + + yield app_config_filename + + +@pytest.fixture +def client(app_config): + os.environ["SETTINGS_FILENAME"] = app_config + with inventory_provider.create_app().test_client() as c: + yield c + + +def test_get_interface_status(mocker, client): + mocked_conn = mocker.patch('inventory_provider.routes.alarmsdb' + '.alarmsdb.connection') + mocked_conn.return_value.__enter__.return_value = None + + mocked_inteface_status = mocker.patch( + 'inventory_provider.routes.alarmsdb.' + 'alarmsdb.get_last_known_interface_status') + mocked_inteface_status.return_value = "up" + + rv = client.get( + '/alarmsdb/interface-status?' + 'equipment=mx1.lon.uk.geant.net&interface=xe-1/2/2', + headers=DEFAULT_REQUEST_HEADERS) + + interfaces_list_schema = { + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "object", + "properties": { + "status": { + "type": "string", + } + } + } + + response = json.loads(rv.data.decode("utf-8")) + jsonschema.validate(response, interfaces_list_schema) + assert response == {"status": "up"} +