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

removed inventory option, only one router

parent 6f9850a3
No related branches found
No related tags found
No related merge requests found
......@@ -2,12 +2,13 @@ import contextlib
from datetime import datetime
from functools import partial
import json
import socket
import click
import jsonschema
from brian_polling_manager.interface_stats import config, brian, errors, juniper
from brian_polling_manager import inventory, influx
from brian_polling_manager import influx
import logging.config
......@@ -99,20 +100,13 @@ def _validate_config(_unused_ctx, _unused_param, file):
raise click.BadParameter(e)
def _managed_routers(inventory_base_urls):
return {_ifc['router'] for _ifc in inventory.load_interfaces(inventory_base_urls)}
def fqdn_netconf_doc_map(app_config_params):
_result = {}
for fqdn in _managed_routers(inventory_base_urls=app_config_params['inventory']):
_result[fqdn] = {
'timestamp': datetime.now(),
'doc': juniper.get_interface_info_ncrpc(fqdn)
}
return _result
def _validate_hostname(_unused_ctx, _unused_param, hostname):
try:
socket.gethostbyname(hostname)
except socket.error:
raise click.BadParameter(f'{hostname} is not resolveable')
def _brian_points(router_fqdn, netconf_doc, timestamp, measurement_name):
_ctr2point = partial(ctr2point, measurement_name, timestamp)
......@@ -137,7 +131,7 @@ def _error_points(router_fqdn, netconf_doc, timestamp, measurement_name):
yield from map(_ctr2point, counters)
def _main(app_config_params: dict):
def _main(router_fqdn: str, app_config_params: dict):
"""
callable entry point, without click
... tmp, for testing
......@@ -148,29 +142,28 @@ def _main(app_config_params: dict):
setup_logging()
nc_doc_map = fqdn_netconf_doc_map(app_config_params)
netconf_doc = juniper.get_interface_info_ncrpc(router_fqdn)
netconf_timestamp = datetime.now()
influx_params = app_config_params['influx']['brian-counters']
for _fqdn, _ncdoc in nc_doc_map.items():
points = _brian_points(
router_fqdn=_fqdn,
netconf_doc=_ncdoc['doc'],
timestamp=_ncdoc['timestamp'],
measurement_name=influx_params['measurement'])
points = _brian_points(
router_fqdn=router_fqdn,
netconf_doc=netconf_doc,
timestamp=netconf_timestamp,
measurement_name=influx_params['measurement'])
with contextlib.closing(influx.influx_client(influx_params)) as client:
client.write_points(points)
with contextlib.closing(influx.influx_client(influx_params)) as client:
client.write_points(points)
influx_params = app_config_params['influx']['error-counters']
for _fqdn, _ncdoc in nc_doc_map.items():
points = _error_points(
router_fqdn=_fqdn,
netconf_doc=_ncdoc['doc'],
timestamp=_ncdoc['timestamp'],
measurement_name=influx_params['measurement'])
points = _error_points(
router_fqdn=router_fqdn,
netconf_doc=netconf_doc,
timestamp=netconf_timestamp,
measurement_name=influx_params['measurement'])
with contextlib.closing(influx.influx_client(influx_params)) as client:
client.write_points(points)
with contextlib.closing(influx.influx_client(influx_params)) as client:
client.write_points(points)
@click.command()
......@@ -180,6 +173,12 @@ def _main(app_config_params: dict):
type=click.File('r'),
help='Config filename',
callback=_validate_config)
@click.option(
'--router', 'router_fqdn',
required=True,
type=click.STRING,
help='router fqdn',
callback=_validate_hostname)
def main(app_config_params: dict):
_main(app_config_params)
......
......@@ -19,7 +19,7 @@ import pytest
import yaml
from brian_polling_manager.interface_stats import brian, errors, juniper, cli
from brian_polling_manager import inventory, influx, interface_stats
from brian_polling_manager import influx, interface_stats
DATA_DIRNAME = os.path.join(os.path.dirname(__file__), 'data', 'interface-info-snapshots')
DATA_FILENAME_EXTENSION = '-interface-info.xml'
......@@ -72,22 +72,16 @@ def test_validate_logical_counters_schema(router_fqdn, ifc_netconf_rpc):
for ifc in juniper.logical_interface_counters(doc):
jsonschema.validate(ifc, interface_stats.LOGICAL_INTERFACE_COUNTER_SCHEMA)
@pytest.fixture
def mocked_poller_interfaces():
with patch('brian_polling_manager.inventory.load_interfaces') as rpc:
with open(os.path.join(DATA_DIRNAME, 'poller-interfaces.json')) as f:
rpc.return_value = json.load(f)
yield # keep the patch in place until caller context finishe
def poller_interfaces():
with open(os.path.join(DATA_DIRNAME, 'poller-interfaces.json')) as f:
return json.load(f)
@pytest.fixture
def polled_interfaces(mocked_poller_interfaces):
polled = {}
for ifc in inventory.load_interfaces(['https://bogus-hostname']):
for ifc in poller_interfaces():
if ifc['dashboards']:
polled.setdefault(ifc['router'], set()).add(ifc['name'])
return polled
......@@ -201,11 +195,6 @@ def app_config_params(free_host_port):
with tempfile.NamedTemporaryFile() as f:
yield {
'ssh-config': f.name,
'inventory': [
'https://bogus-hostname-1',
'https://bogus-hostname-2',
'https://bogus-hostname-3',
],
'influx': {
'brian-counters': {
'hostname': 'localhost',
......@@ -418,15 +407,23 @@ def _use_docker_compose():
@pytest.mark.skipif(
not _use_docker_compose(),
reason='docker compose not found or disabled')
def test_e2e(app_config_params, ifc_netconf_rpc, testenv_containers, mocked_poller_interfaces):
def _mocked_managed_routers(inventory_base_urls):
all_routers = {_ifc['router'] for _ifc in inventory.load_interfaces(['https://abc'])}
return [_ifc for _ifc in all_routers if not _ifc.startswith('srx')]
def test_e2e(app_config_params, ifc_netconf_rpc, testenv_containers):
"""
load all router interfaces into a tmp influx container, check that
all potential counter fields are populated
with patch('brian_polling_manager.interface_stats.cli._managed_routers') as rpc:
rpc.side_effect = _mocked_managed_routers
cli._main(app_config_params)
:param app_config_params:
:param ifc_netconf_rpc:
:param testenv_containers:
:return:
"""
managed_routers = {
_h['router'] for _h in poller_interfaces()
# TODO: srx's are excluded for now, since there's no test data
if not _h['router'].startswith('srx')
}
for router_fqdn in managed_routers:
cli._main(router_fqdn=router_fqdn, app_config_params=app_config_params)
def verify_influx_content(influx_config, expected_fields):
# for each expected field, verify that there's
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment