From e82a9a578a640386452be24067362ca2b9fced8a Mon Sep 17 00:00:00 2001 From: Erik Reid <erik.reid@geant.org> Date: Fri, 4 Sep 2020 17:36:16 +0200 Subject: [PATCH] updated unit tests --- inventory_provider/routes/poller.py | 4 +- test/per_router/test_poller_routes.py | 90 ++++++++++++++------------- test/test_general_poller_routes.py | 58 ++++++++++++++++- 3 files changed, 104 insertions(+), 48 deletions(-) diff --git a/inventory_provider/routes/poller.py b/inventory_provider/routes/poller.py index 51bfa7d9..583f2a04 100644 --- a/inventory_provider/routes/poller.py +++ b/inventory_provider/routes/poller.py @@ -17,7 +17,7 @@ def after_request(resp): def _load_snmp_indexes(hostname=None): result = dict() - key_pattern = f'snmp-interfaces:{hostname}' \ + key_pattern = f'snmp-interfaces:{hostname}*' \ if hostname else 'snmp-interfaces:*' for doc in common.load_json_docs( @@ -85,7 +85,7 @@ def _load_services(hostname=None): return result def _load_interfaces(hostname): - key_pattern = f'netconf:{hostname}' if hostname else 'netconf:*' + key_pattern = f'netconf:{hostname}*' if hostname else 'netconf:*' for doc in common.load_xml_docs( config_params=current_app.config['INVENTORY_PROVIDER_CONFIG'], key_pattern=key_pattern, diff --git a/test/per_router/test_poller_routes.py b/test/per_router/test_poller_routes.py index fe0a91cb..8d380553 100644 --- a/test/per_router/test_poller_routes.py +++ b/test/per_router/test_poller_routes.py @@ -6,61 +6,63 @@ DEFAULT_REQUEST_HEADERS = { "Accept": ["application/json"] } +INTERFACE_LIST_SCHEMA = { + '$schema': 'http://json-schema.org/draft-07/schema#', -def test_router_interfaces(router, client): - interfaces_list_schema = { - "$schema": "http://json-schema.org/draft-07/schema#", - - "definitions": { - "circuit": { - "type": "object", - "properties": { - "name": {"type": "string"}, - "status": {"type": "string"}, - "type": {"type": "string"}, - "id": {"type": "integer"} - }, - "required": ["name", "status", "type", "id"], - "additionalProperties": False - } + 'definitions': { + 'service': { + 'type': 'object', + 'properties': { + 'id': {'type': 'integer'}, + 'name': {'type': 'string'}, + 'type': {'type': 'string'}, + 'status': {'type': 'string'}, + }, + 'required': ['id', 'name', 'type', 'status'], + 'additionalProperties': False }, - - "type": "array", - "items": { - "type": "object", - "properties": { - "circuits": { - "type": "array", - "items": {"$ref": "#/definitions/circuit"} + 'interface': { + 'type': 'object', + 'properties': { + 'router': {'type': 'string'}, + 'name': {'type': 'string'}, + 'description': {'type': 'string'}, + 'snmp-index': { + 'type': 'integer', + 'minimum': 1 }, - "bundle": { - "type": "array", - "items": {"type": "string"} + 'bundle': { + 'type' : 'array', + 'items': {'type': 'string'} }, - "bundle-parents": { - "type": "array", - "items": {"type": "string"} + 'bundle-parents': { + 'type': 'array', + 'items': {'type': 'string'} }, - "description": {"type": "string"}, - "name": {"type": "string"}, - "snmp-index": {"type": "integer"} + 'circuits': { + 'type': 'array', + 'items': {'$ref': '#/definitions/service'} + } }, - "required": [ - "circuits", - "bundle", - "bundle-parents", - "description", - "name", - "snmp-index"], - "additionalProperties": False - } - } + 'required': [ + 'router', 'name', 'description', + 'snmp-index', 'bundle', 'bundle-parents', + 'circuits'], + 'additionalProperties': False + }, + }, + + 'type': 'array', + 'items': {'$ref': '#/definitions/interface'} +} + +def test_router_interfaces(router, client): rv = client.post( "/poller/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) + jsonschema.validate(response, INTERFACE_LIST_SCHEMA) assert response # at least shouldn't be empty diff --git a/test/test_general_poller_routes.py b/test/test_general_poller_routes.py index 166696f0..e14491ab 100644 --- a/test/test_general_poller_routes.py +++ b/test/test_general_poller_routes.py @@ -11,7 +11,7 @@ DEFAULT_REQUEST_HEADERS = { } -INTERFACE_LIST_SCHEMA = { +SCHEMA_INTERFACE_LIST_SCHEMA = { '$schema': 'http://json-schema.org/draft-07/schema#', 'definitions': { @@ -33,6 +33,56 @@ INTERFACE_LIST_SCHEMA = { } +INTERFACE_LIST_SCHEMA = { + '$schema': 'http://json-schema.org/draft-07/schema#', + + 'definitions': { + 'service': { + 'type': 'object', + 'properties': { + 'id': {'type': 'integer'}, + 'name': {'type': 'string'}, + 'type': {'type': 'string'}, + 'status': {'type': 'string'}, + }, + 'required': ['id', 'name', 'type', 'status'], + 'additionalProperties': False + }, + 'interface': { + 'type': 'object', + 'properties': { + 'router': {'type': 'string'}, + 'name': {'type': 'string'}, + 'description': {'type': 'string'}, + 'snmp-index': { + 'type': 'integer', + 'minimum': 1 + }, + 'bundle': { + 'type' : 'array', + 'items': {'type': 'string'} + }, + 'bundle-parents': { + 'type': 'array', + 'items': {'type': 'string'} + }, + 'circuits': { + 'type': 'array', + 'items': {'$ref': '#/definitions/service'} + } + }, + 'required': [ + 'router', 'name', 'description', + 'snmp-index', 'bundle', 'bundle-parents', + 'circuits'], + 'additionalProperties': False + }, + }, + + 'type': 'array', + 'items': {'$ref': '#/definitions/interface'} +} + @pytest.mark.parametrize('category', ['mdvpn', 'lhcone', 'MDVpn', 'LHCONE']) def test_service_category(client, mocked_worker_module, category): worker._build_service_category_interface_list() @@ -42,7 +92,7 @@ def test_service_category(client, mocked_worker_module, category): assert rv.status_code == 200 assert rv.is_json response_data = json.loads(rv.data.decode('utf-8')) - jsonschema.validate(response_data, INTERFACE_LIST_SCHEMA) + jsonschema.validate(response_data, SCHEMA_INTERFACE_LIST_SCHEMA) assert response_data, 'expected a non-empty list' @@ -60,3 +110,7 @@ def test_get_all_interfaces(client): f'/poller/interfaces', headers=DEFAULT_REQUEST_HEADERS) assert rv.status_code == 200 + assert rv.is_json + response_data = json.loads(rv.data.decode('utf-8')) + jsonschema.validate(response_data, INTERFACE_LIST_SCHEMA) + assert response_data, 'expected a non-empty list' -- GitLab