diff --git a/test/conftest.py b/test/conftest.py index 6805738f2c643787761568ffbbb8be763c877550..6c7dad1c057af2f82d1fbe0d2450140a1deee720 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -1,70 +1,18 @@ +import io import json import os +import shutil 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 +TEST_DATA_DIRNAME = os.path.realpath(os.path.join( + inventory_provider.__path__[0], + "..", + "test", + "data")) def data_config_filename(tmp_dir_name): @@ -98,11 +46,15 @@ def data_config_filename(tmp_dir_name): } } - with open(config["oid_list.conf"], "w") as f: - f.write(OID_LIST_CONF) + shutil.copyfile( + os.path.join(TEST_DATA_DIRNAME, 'oid_list.conf'), + config['oid_list.conf'] + ) - with open(config["routers_community.conf"], "w") as f: - f.write(ROUTERS_COMMUNITY_CONF) + shutil.copyfile( + os.path.join(TEST_DATA_DIRNAME, 'routers_community.conf'), + config['routers_community.conf'] + ) filename = os.path.join(tmp_dir_name, "config.json") with open(filename, "w") as f: @@ -124,9 +76,7 @@ def data_config(): @pytest.fixture def cached_test_data(): - filename = os.path.join( - os.path.dirname(__file__), - "router-info.json") + filename = os.path.join(TEST_DATA_DIRNAME, "router-info.json") with open(filename) as f: return json.loads(f.read()) @@ -149,11 +99,3 @@ def client(app_config): os.environ["SETTINGS_FILENAME"] = app_config with inventory_provider.create_app().test_client() as c: yield c - - -def pytest_generate_tests(metafunc): - params = _tmp_data_config() - metafunc.parametrize( - "router", - list([r['hostname'] for r in params['routers']]) - ) diff --git a/test/data/juniper-netconf.xsd b/test/data/juniper-netconf.xsd deleted file mode 100644 index 6e18961529dc601e2c0efce7f474ae371777f8c9..0000000000000000000000000000000000000000 Binary files a/test/data/juniper-netconf.xsd and /dev/null differ diff --git a/test/data/oid_list.conf b/test/data/oid_list.conf new file mode 100644 index 0000000000000000000000000000000000000000..ad5fba8656ec81b6fb206cb70f638a744dba63bc --- /dev/null +++ b/test/data/oid_list.conf @@ -0,0 +1,14 @@ +# +# 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 \ No newline at end of file diff --git a/test/router-info.json b/test/data/router-info.json similarity index 100% rename from test/router-info.json rename to test/data/router-info.json diff --git a/test/data/routers_community.conf b/test/data/routers_community.conf new file mode 100644 index 0000000000000000000000000000000000000000..8b95c7bdcffbd3a6228515b2b01f86f2943a7cb8 --- /dev/null +++ b/test/data/routers_community.conf @@ -0,0 +1,41 @@ +###################################################################################################################################### +## ## +## 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 \ No newline at end of file diff --git a/test/data/update_netconf_config_schema.py b/test/data/update_netconf_config_schema.py deleted file mode 100644 index 1905b2c283ed75c5550d6693d0efa854a2eaa270..0000000000000000000000000000000000000000 --- a/test/data/update_netconf_config_schema.py +++ /dev/null @@ -1,27 +0,0 @@ -import os -from lxml import etree -from inventory_provider import juniper - -def load_config_schema(hostname, ssh_params): - return juniper._rpc(hostname, ssh_params).get_xnm_information( - type='xml-schema', - namespace='junos-configuration') - - -if __name__ == "__main__": - from inventory_provider import config - with open("config.json") as f: - params = config.load(f) - # for r in params['routers']: - # netconf = _load_netconf(r['hostname'], params['ssh']) - # with open(os.path.join(TEST_DATA_DIR, "%s-netconf.xml" % r['hostname']), 'w') as f: - # f.write(etree.tostring(netconf, encoding='unicode')) - # x = dev.rpc.get_software_information() - - hostname = 'mx1.ams.nl.geant.net' - # hostname = params['routers'][0]['hostname'] - - OUTPUT_DIRNAME = os.path.dirname(__file__) - schema_doc = load_config_schema(hostname, params['ssh']) - with open(os.path.join(OUTPUT_DIRNAME, 'juniper-netconf.xsd'), 'w') as f: - f.write(etree.tostring(schema_doc, pretty_print=True).decode('utf-8')) \ No newline at end of file diff --git a/test/per_router/conftest.py b/test/per_router/conftest.py new file mode 100644 index 0000000000000000000000000000000000000000..f3684132df102accd9919d35574c920ae3664940 --- /dev/null +++ b/test/per_router/conftest.py @@ -0,0 +1,18 @@ +import os +from inventory_provider import config +import inventory_provider + +TEST_DATA_DIRNAME = os.path.realpath(os.path.join( + inventory_provider.__path__[0], + "..", + "test", + "data")) + + +def pytest_generate_tests(metafunc): + filename = os.path.join(TEST_DATA_DIRNAME, "routers_community.conf") + with open(filename) as f: + routers = config._load_routers(f) + metafunc.parametrize( + "router", + list([r['hostname'] for r in routers])) diff --git a/test/test_data_routes.py b/test/per_router/test_data_routes.py similarity index 95% rename from test/test_data_routes.py rename to test/per_router/test_data_routes.py index e626961b2097e4d166738606096c125d211f2292..14065597da5fd33e4130f1a2ed23ee6a6036a4ed 100644 --- a/test/test_data_routes.py +++ b/test/per_router/test_data_routes.py @@ -4,16 +4,19 @@ import os import pytest import jsonschema +import inventory_provider + +TEST_DATA_DIRNAME = os.path.realpath(os.path.join( + inventory_provider.__path__[0], + "..", + "test", + "data")) + DEFAULT_REQUEST_HEADERS = { "Content-type": "application/json", "Accept": ["application/json"] } -MODULE_DIR = os.path.realpath(os.path.join( - os.path.dirname(__file__), - "..", - "inventory_provider")) - # def test_version_request(router, client): # version_schema = { @@ -49,7 +52,7 @@ class MockedRedis(object): def __init__(self, *args, **kwargs): if MockedRedis.db is None: test_data_filename = os.path.join( - os.path.dirname(__file__), + TEST_DATA_DIRNAME, "router-info.json") with open(test_data_filename) as f: MockedRedis.db = json.loads(f.read()) @@ -75,9 +78,6 @@ class MockedRedis(object): @pytest.fixture def client_with_mocked_data(mocker, client): - mocker.patch( - 'inventory_provider.router_details.redis.StrictRedis', - MockedRedis) mocker.patch( 'inventory_provider.routes.data.redis.StrictRedis', MockedRedis) @@ -104,6 +104,7 @@ def test_router_interfaces(router, client_with_mocked_data): "/data/interfaces/" + router, headers=DEFAULT_REQUEST_HEADERS) + assert rv.status_code == 200 response = json.loads(rv.data.decode("utf-8")) jsonschema.validate(response, interfaces_list_schema) assert response # at least shouldn't be empty diff --git a/test/test_juniper_data.py b/test/per_router/test_juniper_data.py similarity index 88% rename from test/test_juniper_data.py rename to test/per_router/test_juniper_data.py index 989e7b946f021345ebfd8f30e202846f360894dc..de50bfd4ee3a3a7891dca2ecd95af72e386aafd3 100644 --- a/test/test_juniper_data.py +++ b/test/per_router/test_juniper_data.py @@ -6,12 +6,19 @@ import pytest from inventory_provider import juniper -TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') +import inventory_provider + +TEST_DATA_DIRNAME = os.path.realpath(os.path.join( + inventory_provider.__path__[0], + "..", + "test", + "data")) + class MockedJunosRpc(object): def __init__(self, hostname): - filename = os.path.join(TEST_DATA_DIR, "%s-netconf.xml" % hostname) + filename = os.path.join(TEST_DATA_DIRNAME, "%s-netconf.xml" % hostname) self.config = etree.parse(filename) def get_config(self): @@ -30,7 +37,7 @@ class MockedJunosDevice(object): @pytest.fixture def netconf_doc(mocker, router, data_config): mocker.patch( - 'inventory_provider.netconf.Device', + 'inventory_provider.juniper.Device', MockedJunosDevice) return juniper.load_config(router, data_config['ssh']) diff --git a/test/test_snmp_handling.py b/test/test_snmp_handling.py index b0ad941c9da8c43c3ff3c6b6e20de7475c9a3e98..f8138a68e6a090befa102cf3ab50eed7b656f325 100644 --- a/test/test_snmp_handling.py +++ b/test/test_snmp_handling.py @@ -6,27 +6,18 @@ import pytest from inventory_provider import snmp -OID_TEST_CONFIG = """# -# 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. -# +import inventory_provider -## 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 +TEST_DATA_DIRNAME = os.path.realpath(os.path.join( + inventory_provider.__path__[0], + "..", + "test", + "data")) @pytest.fixture def snmp_walk_responses(): - test_data_dir = os.path.join(os.path.dirname(__file__), "data") - test_data_filename = os.path.join(test_data_dir, "snmp-walk.json") + test_data_filename = os.path.join(TEST_DATA_DIRNAME, "snmp-walk.json") with open(test_data_filename) as f: return json.loads(f.read())