From fecbb4b44e70172cd03fc870a9007243ec2186d2 Mon Sep 17 00:00:00 2001 From: Erik Reid <erik.reid@geant.org> Date: Tue, 11 Dec 2018 14:53:59 +0100 Subject: [PATCH] moved common fixtures to conftest.py --- test/conftest.py | 146 ++++++++++++++++++++++++++++++++++++ test/test_alarmdb_routes.py | 69 ----------------- test/test_data_routes.py | 137 +-------------------------------- test/test_juniper_data.py | 8 -- test/test_snmp_handling.py | 11 +-- 5 files changed, 152 insertions(+), 219 deletions(-) create mode 100644 test/conftest.py diff --git a/test/conftest.py b/test/conftest.py new file mode 100644 index 00000000..2789c343 --- /dev/null +++ b/test/conftest.py @@ -0,0 +1,146 @@ +import json +import os +import tempfile +import pytest +import inventory_provider +from inventory_provider import config + +OID_LIST_CONF = """ +# +# This file is located in dbupdates/conf and is used by scripts under dbupdates/scripts. +# It holds OID values for retrieving details of a router. +# + +## IPv4 +v4Address=.1.3.6.1.2.1.4.20.1.1 +v4InterfaceOID=.1.3.6.1.2.1.4.20.1.2 +v4InterfaceName=.1.3.6.1.2.1.31.1.1.1.1 +v4Mask=.1.3.6.1.2.1.4.20.1.3 + +## IPv6 +v6AddressAndMask=.1.3.6.1.2.1.55.1.8.1.2 +v6InterfaceName=.1.3.6.1.2.1.55.1.5.1.2 +""" # noqa E501 + +ROUTERS_COMMUNITY_CONF = """ +###################################################################################################################################### +## ## +## This is a configuration file that stores router names and the SNMP community name in <router>=<community>,<IP address> format. ## +## ## +###################################################################################################################################### + +mx2.ath.gr.geant.net=0pBiFbD,62.40.114.59 +mx1.tal.ee.geant.net=0pBiFbD,62.40.96.1 +mx2.tal.ee.geant.net=0pBiFbD,62.40.96.2 +mx2.rig.lv.geant.net=0pBiFbD,62.40.96.4 +mx1.kau.lt.geant.net=0pBiFbD,62.40.96.6 +mx2.kau.lt.geant.net=0pBiFbD,62.40.96.5 +mx2.zag.hr.geant.net=0pBiFbD,62.40.96.8 +mx2.lju.si.geant.net=0pBiFbD,62.40.96.10 +mx1.bud.hu.geant.net=0pBiFbD,62.40.97.1 +mx1.pra.cz.geant.net=0pBiFbD,62.40.97.2 +mx2.bra.sk.geant.net=0pBiFbD,62.40.97.4 +mx1.lon.uk.geant.net=0pBiFbD,62.40.97.5 +mx1.vie.at.geant.net=0pBiFbD,62.40.97.7 +mx2.bru.be.geant.net=0pBiFbD,62.40.96.20 +mx1.poz.pl.geant.net=0pBiFbD,62.40.97.10 +mx1.ams.nl.geant.net=0pBiFbD,62.40.97.11 +mx1.fra.de.geant.net=0pBiFbD,62.40.97.12 +mx1.par.fr.geant.net=0pBiFbD,62.40.97.13 +mx1.gen.ch.geant.net=0pBiFbD,62.40.97.14 +mx1.mil2.it.geant.net=0pBiFbD,62.40.97.15 +mx1.lis.pt.geant.net=0pBiFbD,62.40.96.16 +mx2.lis.pt.geant.net=0pBiFbD,62.40.96.17 +mx1.mad.es.geant.net=0pBiFbD,62.40.97.16 +mx1.sof.bg.geant.net=0pBiFbD,62.40.96.21 +mx1.buc.ro.geant.net=0pBiFbD,62.40.96.19 +mx1.ham.de.geant.net=0pBiFbD,62.40.96.26 +mx1.dub.ie.geant.net=0pBiFbD,62.40.96.3 +mx1.dub2.ie.geant.net=0pBiFbD,62.40.96.25 +mx1.mar.fr.geant.net=0pBiFbD,62.40.96.12 +mx1.lon2.uk.geant.net=0pBiFbD,62.40.96.15 +# rt1.clpk.us.geant.net=GEANT_RO,10.200.64.128 +# rt1.denv.us.geant.net=GEANT_RO,10.200.67.128 +mx1.ath2.gr.geant.net=0pBiFbD,62.40.96.39 +# qfx.par.fr.geant.net=0pBiFbD,62.40.117.170 +# qfx.fra.de.geant.net=0pBiFbD,62.40.117.162 +""" # noqa E501 + + +def data_config_filename(tmp_dir_name): + config = { + "alarms-db": { + "hostname": "xxxxxxx.yyyyy.zzz", + "dbname": "xxxxxx", + "username": "xxxxxx", + "password": "xxxxxxxx" + }, + "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": 6379 + } + } + + with open(config["oid_list.conf"], "w") as f: + f.write(OID_LIST_CONF) + + with open(config["routers_community.conf"], "w") as f: + f.write(ROUTERS_COMMUNITY_CONF) + + 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 data_config(): + with tempfile.TemporaryDirectory() as tmpdir: + with open(data_config_filename(tmpdir)) as f: + return config.load(f) + + +@pytest.fixture +def cached_test_data(): + filename = os.path.join( + os.path.dirname(__file__), + "router-info.json") + with open(filename) as f: + return json.loads(f.read()) + + +@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 diff --git a/test/test_alarmdb_routes.py b/test/test_alarmdb_routes.py index e6f1ed7c..2e16e72d 100644 --- a/test/test_alarmdb_routes.py +++ b/test/test_alarmdb_routes.py @@ -1,11 +1,5 @@ import json import jsonschema -import os -import pytest -import tempfile - -import inventory_provider - DEFAULT_REQUEST_HEADERS = { "Content-type": "application/json", @@ -13,69 +7,6 @@ DEFAULT_REQUEST_HEADERS = { } -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') diff --git a/test/test_data_routes.py b/test/test_data_routes.py index 20ddf532..55c5d04b 100644 --- a/test/test_data_routes.py +++ b/test/test_data_routes.py @@ -1,15 +1,10 @@ import json # import logging import os -import tempfile import pytest import jsonschema -import inventory_provider - -# logging.basicConfig(level=logging.DEBUG) - DEFAULT_REQUEST_HEADERS = { "Content-type": "application/json", "Accept": ["application/json"] @@ -20,130 +15,6 @@ MODULE_DIR = os.path.realpath(os.path.join( "..", "inventory_provider")) -OID_LIST_CONF = """ -# -# This file is located in dbupdates/conf and is used by scripts under dbupdates/scripts. -# It holds OID values for retrieving details of a router. -# - -## IPv4 -v4Address=.1.3.6.1.2.1.4.20.1.1 -v4InterfaceOID=.1.3.6.1.2.1.4.20.1.2 -v4InterfaceName=.1.3.6.1.2.1.31.1.1.1.1 -v4Mask=.1.3.6.1.2.1.4.20.1.3 - -## IPv6 -v6AddressAndMask=.1.3.6.1.2.1.55.1.8.1.2 -v6InterfaceName=.1.3.6.1.2.1.55.1.5.1.2 -""" # noqa E501 - -ROUTERS_COMMUNITY_CONF = """ -###################################################################################################################################### -## ## -## This is a configuration file that stores router names and the SNMP community name in <router>=<community>,<IP address> format. ## -## ## -###################################################################################################################################### - -mx2.ath.gr.geant.net=0pBiFbD,62.40.114.59 -mx1.tal.ee.geant.net=0pBiFbD,62.40.96.1 -mx2.tal.ee.geant.net=0pBiFbD,62.40.96.2 -mx2.rig.lv.geant.net=0pBiFbD,62.40.96.4 -mx1.kau.lt.geant.net=0pBiFbD,62.40.96.6 -mx2.kau.lt.geant.net=0pBiFbD,62.40.96.5 -mx2.zag.hr.geant.net=0pBiFbD,62.40.96.8 -mx2.lju.si.geant.net=0pBiFbD,62.40.96.10 -mx1.bud.hu.geant.net=0pBiFbD,62.40.97.1 -mx1.pra.cz.geant.net=0pBiFbD,62.40.97.2 -mx2.bra.sk.geant.net=0pBiFbD,62.40.97.4 -mx1.lon.uk.geant.net=0pBiFbD,62.40.97.5 -mx1.vie.at.geant.net=0pBiFbD,62.40.97.7 -mx2.bru.be.geant.net=0pBiFbD,62.40.96.20 -mx1.poz.pl.geant.net=0pBiFbD,62.40.97.10 -mx1.ams.nl.geant.net=0pBiFbD,62.40.97.11 -mx1.fra.de.geant.net=0pBiFbD,62.40.97.12 -mx1.par.fr.geant.net=0pBiFbD,62.40.97.13 -mx1.gen.ch.geant.net=0pBiFbD,62.40.97.14 -mx1.mil2.it.geant.net=0pBiFbD,62.40.97.15 -mx1.lis.pt.geant.net=0pBiFbD,62.40.96.16 -mx2.lis.pt.geant.net=0pBiFbD,62.40.96.17 -mx1.mad.es.geant.net=0pBiFbD,62.40.97.16 -mx1.sof.bg.geant.net=0pBiFbD,62.40.96.21 -mx1.buc.ro.geant.net=0pBiFbD,62.40.96.19 -mx1.ham.de.geant.net=0pBiFbD,62.40.96.26 -mx1.dub.ie.geant.net=0pBiFbD,62.40.96.3 -mx1.dub2.ie.geant.net=0pBiFbD,62.40.96.25 -mx1.mar.fr.geant.net=0pBiFbD,62.40.96.12 -mx1.lon2.uk.geant.net=0pBiFbD,62.40.96.15 -# rt1.clpk.us.geant.net=GEANT_RO,10.200.64.128 -# rt1.denv.us.geant.net=GEANT_RO,10.200.67.128 -mx1.ath2.gr.geant.net=0pBiFbD,62.40.96.39 -# qfx.par.fr.geant.net=0pBiFbD,62.40.117.170 -# qfx.fra.de.geant.net=0pBiFbD,62.40.117.162 -""" # noqa E501 - - -def data_config_filename(tmp_dir_name): - config = { - "alarms-db": { - "hostname": "xxxxxxx.yyyyy.zzz", - "dbname": "xxxxxx", - "username": "xxxxxx", - "password": "xxxxxxxx" - }, - "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": 6379 - } - } - - with open(config["oid_list.conf"], "w") as f: - f.write(OID_LIST_CONF) - - with open(config["routers_community.conf"], "w") as f: - f.write(ROUTERS_COMMUNITY_CONF) - - 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_version_request(client): version_schema = { @@ -172,9 +43,6 @@ def test_version_request(client): version_schema) -TEST_DATA_FILENAME = os.path.join( - os.path.dirname(__file__), - "router-info.json") class MockedRedis(object): @@ -183,7 +51,10 @@ class MockedRedis(object): def __init__(self, *args, **kwargs): if MockedRedis.db is None: - with open(TEST_DATA_FILENAME) as f: + test_data_filename = os.path.join( + os.path.dirname(__file__), + "router-info.json") + with open(test_data_filename) as f: MockedRedis.db = json.loads(f.read()) def set(self, key, value): diff --git a/test/test_juniper_data.py b/test/test_juniper_data.py index b35403f9..486dc7df 100644 --- a/test/test_juniper_data.py +++ b/test/test_juniper_data.py @@ -77,14 +77,6 @@ CACHE_SCHEMA = { } -@pytest.fixture -def cached_test_data(): - with open("router-info.json") as f: - cache = json.loads(f.read()) - jsonschema.validate(cache, CACHE_SCHEMA) - return cache - - def _parsed_old_style_output_data(s): for l in s.splitlines(): if not l: diff --git a/test/test_snmp_handling.py b/test/test_snmp_handling.py index f6794197..8c6e198a 100644 --- a/test/test_snmp_handling.py +++ b/test/test_snmp_handling.py @@ -6,7 +6,6 @@ import jsonschema import pytest from inventory_provider import snmp -from inventory_provider import config OID_TEST_CONFIG = """# # This file is located in dbupdates/conf and is used by scripts under dbupdates/scripts. @@ -33,13 +32,7 @@ def snmp_walk_responses(): return json.loads(f.read()) -@pytest.fixture -def oid_config(): - with StringIO(OID_TEST_CONFIG) as s: - return config._load_oids(s) - - -def test_snmp_interfaces(mocker, oid_config, snmp_walk_responses): +def test_snmp_interfaces(mocker, data_config, snmp_walk_responses): expected_result_schema = { "$schema": "http://json-schema.org/draft-07/schema#", @@ -69,7 +62,7 @@ def test_snmp_interfaces(mocker, oid_config, snmp_walk_responses): _mocked_walk) interfaces = snmp.get_router_interfaces( - 'ignored', 'ignored', {'oids': oid_config}) + 'ignored', 'ignored', {'oids': data_config['oids']}) interfaces = list(interfaces) jsonschema.validate(interfaces, expected_result_schema) -- GitLab