Skip to content
Snippets Groups Projects
Commit a0234906 authored by Erik Reid's avatar Erik Reid
Browse files

Finished feature celery-task-tests.

parents f421cea0 811b5234
No related branches found
No related tags found
No related merge requests found
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
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')
......
"""
just checks that the worker methods call the right functions
and some data ends up in the right place ... otherwise not very detailed
"""
import logging
import pytest
from inventory_provider.tasks import worker
class MockedRedis(object):
db = {}
def __init__(self, *args, **kwargs):
pass
def hset(self, name, key, value):
MockedRedis.db.setdefault(name, {})[key] = value
@pytest.fixture
def mocked_worker_module(mocker, data_config):
worker.InventoryTask.config = data_config
worker.InventoryTask.logger = logging.getLogger()
MockedRedis.db = {}
mocker.patch(
'inventory_provider.tasks.worker.redis.StrictRedis',
MockedRedis)
def test_juniper_refresh_bgp(
mocked_worker_module, mocker, cached_test_data):
def _mocked_fetch_bpg_config(hostname, _):
return cached_test_data[hostname]["bgp"]
mocker.patch(
'inventory_provider.tasks.worker.juniper.fetch_bgp_config',
_mocked_fetch_bpg_config)
for hostname in cached_test_data.keys():
assert 'hostname' not in MockedRedis.db
worker.juniper_refresh_bgp(hostname)
assert MockedRedis.db[hostname]['bgp']
def test_juniper_refresh_interfaces(
mocked_worker_module, mocker, cached_test_data):
def _mocked_fetch_interfaces(hostname, _):
return cached_test_data[hostname]["interfaces"]
mocker.patch(
'inventory_provider.tasks.worker.juniper.fetch_interfaces',
_mocked_fetch_interfaces)
for hostname in cached_test_data.keys():
assert 'hostname' not in MockedRedis.db
worker.juniper_refresh_interfaces(hostname)
assert MockedRedis.db[hostname]['interfaces']
def test_juniper_refresh_vrr(
mocked_worker_module, mocker, cached_test_data):
def _mocked_fetch_vrr_config(hostname, _):
return cached_test_data[hostname]["vrr"]
mocker.patch(
'inventory_provider.tasks.worker.juniper.fetch_vrr_config',
_mocked_fetch_vrr_config)
for hostname in cached_test_data.keys():
assert 'hostname' not in MockedRedis.db
worker.juniper_refresh_vrr(hostname)
assert MockedRedis.db[hostname]['vrr']
def test_snmp_refresh_interfaces(
mocked_worker_module, mocker, cached_test_data):
def _mocked_snmp_interfaces(hostname, community, _):
return cached_test_data[hostname]["snmp-interfaces"]
mocker.patch(
'inventory_provider.tasks.worker.snmp.get_router_interfaces',
_mocked_snmp_interfaces)
for hostname in cached_test_data.keys():
assert 'hostname' not in MockedRedis.db
worker.snmp_refresh_interfaces(hostname, 'fake-community')
assert MockedRedis.db[hostname]['snmp-interfaces']
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,18 +43,16 @@ def test_version_request(client):
version_schema)
TEST_DATA_FILENAME = os.path.join(
os.path.dirname(__file__),
"router-info.json")
class MockedRedis(object):
db = None
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):
......
import json
import os
import re
import jsonschema
import pytest
from inventory_provider import juniper
......@@ -77,14 +75,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:
......
import json
import os
from io import StringIO
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 +31,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 +61,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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment