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

test with CliRunner

parent 9ab9d775
No related branches found
No related tags found
No related merge requests found
...@@ -72,9 +72,9 @@ def setup_logging(): ...@@ -72,9 +72,9 @@ def setup_logging():
def ctr2point(measurement, timestamp, counters): def ctr2point(measurement, timestamp, counters):
""" """
:param measurement: the measurement where the point will be written :param measurement: the measurement where the point will be written
:param counters: a ERROR_POINT_SCHEMA object :param counters: either a BRIAN_COUNTER_DICT_SCHEMA or
ERROR_COUNTER_DICT_SCHEMA object
:return: a brian_polling_manager.influx.INFLUX_POINT object :return: a brian_polling_manager.influx.INFLUX_POINT object
""" """
def _is_tag(key_name): def _is_tag(key_name):
...@@ -104,9 +104,19 @@ def _validate_hostname(_unused_ctx, _unused_param, hostname): ...@@ -104,9 +104,19 @@ def _validate_hostname(_unused_ctx, _unused_param, hostname):
socket.gethostbyname(hostname) socket.gethostbyname(hostname)
except socket.error: except socket.error:
raise click.BadParameter(f'{hostname} is not resolveable') raise click.BadParameter(f'{hostname} is not resolveable')
return hostname
def _brian_points(router_fqdn, netconf_doc, timestamp, measurement_name): def _brian_points(router_fqdn, netconf_doc, timestamp, measurement_name):
"""
returns an interable of points that can be written to influxdb
:param router_fqdn: 'hostname' tag to be set in the generated points
:param netconf_doc: a valid loaded netconf doc
:param timestamp: timestamp to put in the generated points
:param measurement_name: measurement name to put in the generated points
:return:
"""
_ctr2point = partial(ctr2point, measurement_name, timestamp) _ctr2point = partial(ctr2point, measurement_name, timestamp)
interfaces = juniper.physical_interface_counters(netconf_doc) interfaces = juniper.physical_interface_counters(netconf_doc)
...@@ -119,6 +129,15 @@ def _brian_points(router_fqdn, netconf_doc, timestamp, measurement_name): ...@@ -119,6 +129,15 @@ def _brian_points(router_fqdn, netconf_doc, timestamp, measurement_name):
def _error_points(router_fqdn, netconf_doc, timestamp, measurement_name): def _error_points(router_fqdn, netconf_doc, timestamp, measurement_name):
"""
returns an interable of points that can be written to influxdb
:param router_fqdn: 'hostname' tag to be set in the generated points
:param netconf_doc: a valid loaded netconf doc
:param timestamp: timestamp to put in the generated points
:param measurement_name: measurement name to put in the generated points
:return:
"""
_ctr2point = partial(ctr2point, measurement_name, timestamp) _ctr2point = partial(ctr2point, measurement_name, timestamp)
interfaces = juniper.physical_interface_counters(netconf_doc) interfaces = juniper.physical_interface_counters(netconf_doc)
...@@ -132,8 +151,10 @@ def _error_points(router_fqdn, netconf_doc, timestamp, measurement_name): ...@@ -132,8 +151,10 @@ def _error_points(router_fqdn, netconf_doc, timestamp, measurement_name):
def _main(router_fqdn: str, app_config_params: dict): def _main(router_fqdn: str, app_config_params: dict):
""" """
callable entry point, without click callable entry point, without click - for testing
... tmp, for testing
loads the netconf 'show interfaces' info for the router fqdn,
then uses this to generate brian and error points and writes them to influxdb
note: the optional inventory param is only used to validate the hostname note: the optional inventory param is only used to validate the hostname
...@@ -152,6 +173,7 @@ def _main(router_fqdn: str, app_config_params: dict): ...@@ -152,6 +173,7 @@ def _main(router_fqdn: str, app_config_params: dict):
netconf_doc = juniper.get_interface_info_ncrpc(router_fqdn) netconf_doc = juniper.get_interface_info_ncrpc(router_fqdn)
netconf_timestamp = datetime.now() netconf_timestamp = datetime.now()
influx_params = app_config_params['influx']['brian-counters'] influx_params = app_config_params['influx']['brian-counters']
points = _brian_points( points = _brian_points(
router_fqdn=router_fqdn, router_fqdn=router_fqdn,
...@@ -178,7 +200,7 @@ def _main(router_fqdn: str, app_config_params: dict): ...@@ -178,7 +200,7 @@ def _main(router_fqdn: str, app_config_params: dict):
'--config', 'app_config_params', '--config', 'app_config_params',
required=True, required=True,
type=click.File('r'), type=click.File('r'),
help='Config filename', help='config filename',
callback=_validate_config) callback=_validate_config)
@click.option( @click.option(
'--router', 'router_fqdn', '--router', 'router_fqdn',
...@@ -186,8 +208,8 @@ def _main(router_fqdn: str, app_config_params: dict): ...@@ -186,8 +208,8 @@ def _main(router_fqdn: str, app_config_params: dict):
type=click.STRING, type=click.STRING,
help='router fqdn', help='router fqdn',
callback=_validate_hostname) callback=_validate_hostname)
def main(app_config_params: dict): def main(router_fqdn, app_config_params: dict):
_main(app_config_params) _main(router_fqdn, app_config_params)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -71,7 +71,7 @@ def load(config_file): ...@@ -71,7 +71,7 @@ def load(config_file):
config = json.loads(config_file.read()) config = json.loads(config_file.read())
jsonschema.validate(config, CONFIG_SCHEMA) jsonschema.validate(config, CONFIG_SCHEMA)
for db in config['influx']: for db in config['influx'].values():
db.setdefault('ssl', DEFAULT_INFLUX_SSL) db.setdefault('ssl', DEFAULT_INFLUX_SSL)
db.setdefault('port', DEFAULT_INFLUX_PORT) db.setdefault('port', DEFAULT_INFLUX_PORT)
......
...@@ -13,6 +13,7 @@ import tempfile ...@@ -13,6 +13,7 @@ import tempfile
import time import time
from unittest.mock import patch from unittest.mock import patch
from click.testing import CliRunner
import jsonschema import jsonschema
from lxml import etree from lxml import etree
import pytest import pytest
...@@ -219,6 +220,14 @@ def app_config_params(free_host_port): ...@@ -219,6 +220,14 @@ def app_config_params(free_host_port):
} }
@pytest.fixture
def app_config_filename(app_config_params):
with tempfile.NamedTemporaryFile() as f:
f.write(json.dumps(app_config_params).encode('utf-8'))
f.flush()
yield f.name
@pytest.fixture @pytest.fixture
def brian_influx_container_params(app_config_params): def brian_influx_container_params(app_config_params):
...@@ -408,7 +417,7 @@ def _use_docker_compose(): ...@@ -408,7 +417,7 @@ def _use_docker_compose():
@pytest.mark.skipif( @pytest.mark.skipif(
not _use_docker_compose(), not _use_docker_compose(),
reason='docker compose not found or disabled') reason='docker compose not found or disabled')
def test_e2e(app_config_params, ifc_netconf_rpc, testenv_containers): def test_e2e(app_config_params, app_config_filename, ifc_netconf_rpc, testenv_containers):
""" """
load all router interfaces into a tmp influx container, check that load all router interfaces into a tmp influx container, check that
all potential counter fields are populated all potential counter fields are populated
...@@ -427,7 +436,15 @@ def test_e2e(app_config_params, ifc_netconf_rpc, testenv_containers): ...@@ -427,7 +436,15 @@ def test_e2e(app_config_params, ifc_netconf_rpc, testenv_containers):
with patch('brian_polling_manager.inventory.load_interfaces') as inventory: with patch('brian_polling_manager.inventory.load_interfaces') as inventory:
inventory.return_value = poller_interfaces() inventory.return_value = poller_interfaces()
for router_fqdn in managed_routers: for router_fqdn in managed_routers:
cli._main(router_fqdn=router_fqdn, app_config_params=app_config_params) # cli._main(router_fqdn=router_fqdn, app_config_params=app_config_params)
runner = CliRunner()
result = runner.invoke(
cli.main,
[
'--config', app_config_filename,
'--router', router_fqdn
])
assert result.exit_code == 0, str(result)
def verify_influx_content(influx_config, expected_fields): def verify_influx_content(influx_config, expected_fields):
# for each expected field, verify that there's # 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