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

Finished feature improve-test-coverage.

parents 1ab78caa 01afae14
No related branches found
No related tags found
No related merge requests found
import json
import logging
import click
from inventory_provider import constants
from inventory_provider import router_details
from inventory_provider import config
def _validate_config(ctx, param, value):
return config.load(value)
@click.command()
@click.option(
"--params",
# required=True,
type=click.File(),
help="Configuration filename",
default=open("config.json"),
callback=_validate_config)
def cli(params):
router_details.update_network_details(params)
result = router_details.load_network_details(params["redis"])
filename = "/tmp/router-info.json"
logging.debug("writing output to: " + filename)
with open(filename, "w") as f:
f.write(json.dumps(result))
if __name__ == "__main__":
logging.basicConfig(level=logging.WARNING)
logging.getLogger(constants.SNMP_LOGGER_NAME).setLevel(logging.DEBUG)
logging.getLogger(constants.THREADING_LOGGER_NAME).setLevel(logging.INFO)
logging.getLogger(constants.JUNIPER_LOGGER_NAME).setLevel(logging.DEBUG)
logging.getLogger(constants.DATABASE_LOGGER_NAME).setLevel(logging.DEBUG)
cli()
......@@ -111,7 +111,7 @@ def snmp_ids(hostname):
ifc_data = json.loads(ifc_data_string.decode('utf-8'))
result = [
{ 'index': i['index'], 'name': _ifc_name(i)}
{'index': i['index'], 'name': _ifc_name(i)}
for i in ifc_data]
return Response(
json.dumps(result),
......
This diff is collapsed.
......@@ -104,4 +104,3 @@ def test_get_interface_status(mocker, client):
response = json.loads(rv.data.decode("utf-8"))
jsonschema.validate(response, interfaces_list_schema)
assert response == {"status": "up"}
......@@ -278,7 +278,6 @@ def test_snmp_ids(client_with_mocked_data):
}
}
for hostname in _routers(client_with_mocked_data):
rv = client_with_mocked_data.post(
"/data/snmp/" + hostname,
......
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.
# 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
@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")
with open(test_data_filename) as f:
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):
expected_result_schema = {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"v4Address": {"type": "string"},
"v4Mask": {"type": "string"},
"v4InterfaceName": {"type": "string"},
"v6Address": {"type": "string"},
"v6Mask": {"type": "string"},
"v6InterfaceName": {"type": "string"},
"index": {"type": "string"}
},
"required": ["index"],
"additionalProperties": False
}
}
def _mocked_walk(agent_hostname, community, base_oid):
return [e for e in snmp_walk_responses
if e['oid'].startswith(base_oid)]
mocker.patch(
'inventory_provider.snmp.walk',
_mocked_walk)
interfaces = snmp.get_router_interfaces(
'ignored', 'ignored', {'oids': oid_config})
interfaces = list(interfaces)
jsonschema.validate(interfaces, expected_result_schema)
assert interfaces, "interface list isn't empty"
for ifc in interfaces:
if 'v4Address' in ifc \
and 'v4Mask' in ifc \
and 'v4InterfaceName' in ifc:
continue
if 'v6Address' in ifc \
and 'v6Mask' in ifc \
and 'v6InterfaceName' in ifc:
continue
assert False, "address details not found in interface dict"
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