From 5be403d25ea4419d2c694aceb4fe5931ee94c082 Mon Sep 17 00:00:00 2001 From: Erik Reid <erik.reid@geant.org> Date: Sun, 24 Jan 2021 13:46:36 +0100 Subject: [PATCH] moved peer-info, juniper-link-info response schemas from test to source --- .../routes/classifier_schema.py | 330 +++++++++++++++++ test/test_classifier_routes.py | 335 +----------------- 2 files changed, 337 insertions(+), 328 deletions(-) create mode 100644 inventory_provider/routes/classifier_schema.py diff --git a/inventory_provider/routes/classifier_schema.py b/inventory_provider/routes/classifier_schema.py new file mode 100644 index 00000000..8d132b14 --- /dev/null +++ b/inventory_provider/routes/classifier_schema.py @@ -0,0 +1,330 @@ + +_common_locations_schema_definitions = { + "location-endpoint": { + "type": "object", + "properties": { + "equipment": {"type": "string"}, + "name": {"type": "string"}, + "abbreviation": {"type": "string"} + }, + "required": ["equipment", "name", "abbreviation"], + "additionalProperties": False + }, + "location": { + "type": "object", + "properties": { + "a": {"$ref": "#/definitions/location-endpoint"}, + "b": {"$ref": "#/definitions/location-endpoint"} + }, + "required": ["a"], + "additionalProperties": False + }, + "locations-list": { + "type": "array", + "items": {"$ref": "#/definitions/location"} + } +} + +_juniper_link_response_schema_definitions = { + "ip-address": { + "type": "string", + "oneOf": [ + {"pattern": r'^(\d+\.){3}\d+$'}, + {"pattern": r'^([a-f\d]{4}:){7}[a-f\d]{4}$'} + ] + }, + "ipv4-interface-address": { + "type": "string", + "pattern": r'^(\d+\.){3}\d+/\d+$' + }, + "ipv6-interface-address": { + "type": "string", + "pattern": r'^[a-f\d:]+/\d+$' + }, + "snmp-info": { + "type": "object", + "properties": { + "community": {"type": "string"}, + "index": {"type": "integer"} + }, + "required": ["community", "index"], + "additionalProperties": False + }, + "interface-info": { + "type": "object", + "properties": { + "name": {"type": "string"}, + "description": {"type": "string"}, + "ipv4": { + "type": "array", + "items": {"$ref": "#/definitions/ipv4-interface-address"} + }, + "ipv6": { + "type": "array", + "items": {"$ref": "#/definitions/ipv6-interface-address"} + }, + + # TODO: check what's changed: added to make tests pass + 'bundle': {"type": "array"}, + 'bundle_members': {"type": "array"}, + 'snmp': {"$ref": "#/definitions/snmp-info"} + }, + "required": ["name", "description", "ipv4", "ipv6"], + "additionalProperties": False + }, + "service-info": { + "type": "object", + "properties": { + "id": {"type": "integer"}, + "name": {"type": "string"}, + "status": { + "type": "string", + "enum": ["operational", "installed", "planned", "ordered"] + }, + "circuit_type": { + "type": "string", + "enum": ["path", "service", "l2circuit"] + }, + "service_type": {"type": "string"}, + "project": {"type": "string"}, + "equipment": {"type": "string"}, + "pop": {"type": "string"}, + "pop_abbreviation": {"type": "string"}, + + "other_end_pop": {"type": "string"}, + "other_end_pop_abbreviation": {"type": "string"}, + "other_end_equipment": {"type": "string"}, + "port": {"type": "string"}, + "other_end_port": {"type": "string"}, + "logical_unit": { + "oneOf": [ + {"type": "integer"}, + {"type": "string", "maxLength": 0} + ] + }, + "other_end_logical_unit": { + "oneOf": [ + {"type": "integer"}, + {"type": "string", "maxLength": 0} + ] + }, + "manufacturer": { + "type": "string", + "enum": ["juniper", "coriant", "infinera", + "cisco", "hewlett packard", + "corsa", "graham smith uk ltd", + "unknown", ""] + }, + "card_id": {"type": "string"}, + "other_end_card_id": {"type": "string"}, + "interface_name": {"type": "string"}, + "other_end_interface_name": {"type": "string"}, + + # TODO: check what's changed: added to make tests pass + 'other_end_pop_name': {"type": "string"}, + 'pop_name': {"type": "string"} + }, + # TODO: modify service-info so that "" entries are just omitted + # (... rather than requiring 'oneOf') + # TODO: put 'other_end_*' params in a sub dictionary + # "required": [ + # "id", "name", "status", + # "circuit_type", "service_type", + # "project", "port", "manufacturer", + # "equipment", "logical_unit", "card_id", "interface_name" + # ], + "additionalProperties": False + }, + "related-service-info": { + "type": "object", + "properties": { + "name": {"type": "string"}, + "status": { + "type": "string", + "enum": ["operational", "installed", "planned", "ordered"] + }, + "circuit_type": { + "type": "string", + "enum": ["path", "service", "l2circuit"] + }, + "project": {"type": "string"} + }, + "required": ["name", "status", "circuit_type", "project"], + "additionalProperties": False + } +} + +JUNIPER_LINK_RESPONSE_SCHEMA = { + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "object", + + "definitions": { + **_juniper_link_response_schema_definitions, + **_common_locations_schema_definitions + }, + + "type": "object", + "properties": { + "services": { + "type": "array", + "items": {"$ref": "#/definitions/service-info"} + }, + "interface": {"$ref": "#/definitions/interface-info"}, + "related-services": { + "type": "array", + "items": {"$ref": "#/definitions/related-service-info"} + }, + "locations": {"$ref": "#/definitions/locations-list"} + }, + "required": ["interface", "locations"], + "additionalProperties": False +} + + +_peer_info_response_schema_definitions = { + "ip-address": { + "type": "string", + "oneOf": [ + {"pattern": r'^(\d+\.){3}\d+$'}, + {"pattern": r'^([a-f\d]{4}:){7}[a-f\d]{4}$'} + ] + }, + "interface-address": { + "type": "string", + "oneOf": [ + {"pattern": r'^(\d+\.){3}\d+/\d+$'}, + {"pattern": r'^[a-f\d:]+/\d+$'} + ] + }, + "vpn-rr-peer": { + "type": "object", + "properties": { + "name": {"$ref": "#/definitions/ip-address"}, + "description": {"type": "string"}, + "peer-as": {"type": "integer"}, + "router": {"type": "string"} + }, + "required": ["name", "description"], + "additionalProperties": False + }, + "ix-public-peer": { + "type": "object", + "properties": { + "name": {"$ref": "#/definitions/ip-address"}, + "description": {"type": "string"}, + "router": {"type": "string"}, + "as": { + "type": "object", + "properties": { + "local": {"type": "integer"}, + "peer": {"type": "integer"}, + }, + "required": ["local", "peer"], + "additionalProperties": False + } + }, + "required": ["name", "description", "as"], + "additionalProperties": False + }, + "ix-public-peer-list": { + "type": "array", + "items": {"$ref": "#/definitions/ip-address"} + }, + "ix-public-peer-info": { + "type": "object", + "properties": { + "peer": {"$ref": "#/definitions/ix-public-peer"}, + "group": {"$ref": "#/definitions/ix-public-peer-list"}, + "router": {"$ref": "#/definitions/ix-public-peer-list"} + }, + "required": ["peer", "group", "router"], + "additionalProperties": False + }, + "interface-info": { + "type": "object", + "properties": { + "name": {"$ref": "#/definitions/ip-address"}, + "interface address": { + "$ref": "#/definitions/interface-address"}, + "interface name": {"type": "string"}, + "router": {"type": "string"} + }, + "required": [ + "name", "interface address", "interface name", "router"], + "additionalProperties": False + }, + "service-info": { + "type": "object" + }, + "interface-lookup-info": { + "type": "object", + "properties": { + "interface": {"$ref": "#/definitions/interface-info"}, + "services": { + "type": "array", + "items": {"$ref": "#/definitions/service-info"} + } + } + }, + "snmp-info": { + "type": "object", + "properties": { + "hostname": {"type": "string"}, + "oid": {"type": "string"}, + "community": {"type": "string"} + }, + "required": ["oid", "community", "hostname"], + "additionalProperties": False + }, + "asn-group-member": { + "type": "object", + "properties": { + "router": {"type": "string"}, + "address": {"type": "string"}, + "group": {"type": "string"} + }, + "required": ["router", "address", "group"], + "additionalProperties": False + }, + "asn-group": { + "type": "object", + "properties": { + "asn": {"type": "integer"}, + "peers": { + "type": "array", + "items": {"$ref": "#/definitions/asn-group-member"} + } + }, + "required": ["asn", "peers"], + "additionalProperties": False + } +} + + +PEER_INFO_RESPONSE_SCHEMA = { + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "object", + + "definitions": { + **_peer_info_response_schema_definitions, + **_common_locations_schema_definitions + }, + + "type": "object", + "properties": { + "ix-public-peer-info": { + "$ref": "#/definitions/ix-public-peer-info"}, + "vpn-rr-peer-info": {"$ref": "#/definitions/vpn-rr-peer"}, + "interfaces": { + "type": "array", + "items": {"$ref": "#/definitions/interface-lookup-info"} + }, + "locations": {"$ref": "#/definitions/locations-list"}, + "snmp": { + "type": "array", + "items": {"$ref": "#/definitions/snmp-info"} + }, + "asn": {"$ref": "#/definitions/asn-group"} + }, + "additionalProperties": False +} diff --git a/test/test_classifier_routes.py b/test/test_classifier_routes.py index 8b76c1b5..01192f31 100644 --- a/test/test_classifier_routes.py +++ b/test/test_classifier_routes.py @@ -3,191 +3,14 @@ import json import jsonschema import pytest +from inventory_provider.routes.classifier_schema \ + import JUNIPER_LINK_RESPONSE_SCHEMA, PEER_INFO_RESPONSE_SCHEMA + DEFAULT_REQUEST_HEADERS = { "Content-type": "application/json", "Accept": ["application/json"] } -LOCATIONS_DEFINITIONS = { - "location-endpoint": { - "type": "object", - "properties": { - "equipment": {"type": "string"}, - "name": {"type": "string"}, - "abbreviation": {"type": "string"} - }, - "required": ["equipment", "name", "abbreviation"], - "additionalProperties": False - }, - "location": { - "type": "object", - "properties": { - "a": {"$ref": "#/definitions/location-endpoint"}, - "b": {"$ref": "#/definitions/location-endpoint"} - }, - "required": ["a"], - "additionalProperties": False - }, - "locations-list": { - "type": "array", - "items": {"$ref": "#/definitions/location"} - } -} - -JUNIPER_LINK_METADATA_DEFINITIONS = { - "ip-address": { - "type": "string", - "oneOf": [ - {"pattern": r'^(\d+\.){3}\d+$'}, - {"pattern": r'^([a-f\d]{4}:){7}[a-f\d]{4}$'} - ] - }, - "ipv4-interface-address": { - "type": "string", - "pattern": r'^(\d+\.){3}\d+/\d+$' - }, - "ipv6-interface-address": { - "type": "string", - "pattern": r'^[a-f\d:]+/\d+$' - }, - "snmp-info": { - "type": "object", - "properties": { - "community": {"type": "string"}, - "index": {"type": "integer"} - }, - "required": ["community", "index"], - "additionalProperties": False - }, - "interface-info": { - "type": "object", - "properties": { - "name": {"type": "string"}, - "description": {"type": "string"}, - "ipv4": { - "type": "array", - "items": {"$ref": "#/definitions/ipv4-interface-address"} - }, - "ipv6": { - "type": "array", - "items": {"$ref": "#/definitions/ipv6-interface-address"} - }, - - # TODO: check what's changed: added to make tests pass - 'bundle': {"type": "array"}, - 'bundle_members': {"type": "array"}, - 'snmp': {"$ref": "#/definitions/snmp-info"} - }, - "required": ["name", "description", "ipv4", "ipv6"], - "additionalProperties": False - }, - "service-info": { - "type": "object", - "properties": { - "id": {"type": "integer"}, - "name": {"type": "string"}, - "status": { - "type": "string", - "enum": ["operational", "installed", "planned", "ordered"] - }, - "circuit_type": { - "type": "string", - "enum": ["path", "service", "l2circuit"] - }, - "service_type": {"type": "string"}, - "project": {"type": "string"}, - "equipment": {"type": "string"}, - "pop": {"type": "string"}, - "pop_abbreviation": {"type": "string"}, - - "other_end_pop": {"type": "string"}, - "other_end_pop_abbreviation": {"type": "string"}, - "other_end_equipment": {"type": "string"}, - "port": {"type": "string"}, - "other_end_port": {"type": "string"}, - "logical_unit": { - "oneOf": [ - {"type": "integer"}, - {"type": "string", "maxLength": 0} - ] - }, - "other_end_logical_unit": { - "oneOf": [ - {"type": "integer"}, - {"type": "string", "maxLength": 0} - ] - }, - "manufacturer": { - "type": "string", - "enum": ["juniper", "coriant", "infinera", - "cisco", "hewlett packard", - "corsa", "graham smith uk ltd", - "unknown", ""] - }, - "card_id": {"type": "string"}, - "other_end_card_id": {"type": "string"}, - "interface_name": {"type": "string"}, - "other_end_interface_name": {"type": "string"}, - - # TODO: check what's changed: added to make tests pass - 'other_end_pop_name': {"type": "string"}, - 'pop_name': {"type": "string"} - }, - # TODO: modify service-info so that "" entries are just omitted - # (... rather than requiring 'oneOf') - # TODO: put 'other_end_*' params in a sub dictionary - # "required": [ - # "id", "name", "status", - # "circuit_type", "service_type", - # "project", "port", "manufacturer", - # "equipment", "logical_unit", "card_id", "interface_name" - # ], - "additionalProperties": False - }, - "related-service-info": { - "type": "object", - "properties": { - "name": {"type": "string"}, - "status": { - "type": "string", - "enum": ["operational", "installed", "planned", "ordered"] - }, - "circuit_type": { - "type": "string", - "enum": ["path", "service", "l2circuit"] - }, - "project": {"type": "string"} - }, - "required": ["name", "status", "circuit_type", "project"], - "additionalProperties": False - } -} - -JUNIPER_LINK_METADATA = { - "$schema": "http://json-schema.org/draft-07/schema#", - "type": "object", - - "definitions": { - **JUNIPER_LINK_METADATA_DEFINITIONS, **LOCATIONS_DEFINITIONS - }, - - "type": "object", - "properties": { - "services": { - "type": "array", - "items": {"$ref": "#/definitions/service-info"} - }, - "interface": {"$ref": "#/definitions/interface-info"}, - "related-services": { - "type": "array", - "items": {"$ref": "#/definitions/related-service-info"} - }, - "locations": {"$ref": "#/definitions/locations-list"} - }, - "required": ["interface", "locations"], - "additionalProperties": False -} - def test_juniper_link_info(client): rv = client.get( @@ -196,7 +19,7 @@ def test_juniper_link_info(client): assert rv.status_code == 200 assert rv.is_json response_data = json.loads(rv.data.decode('utf-8')) - jsonschema.validate(response_data, JUNIPER_LINK_METADATA) + jsonschema.validate(response_data, JUNIPER_LINK_RESPONSE_SCHEMA) def test_juniper_link_info_not_found(client): @@ -207,7 +30,7 @@ def test_juniper_link_info_not_found(client): assert rv.status_code == 200 assert rv.is_json response_data = json.loads(rv.data.decode('utf-8')) - jsonschema.validate(response_data, JUNIPER_LINK_METADATA) + jsonschema.validate(response_data, JUNIPER_LINK_RESPONSE_SCHEMA) assert response_data == { 'interface': { 'name': 'unknown-interface-name', @@ -234,7 +57,7 @@ def test_juniper_link_unknown_router(client): assert rv.status_code == 200 assert rv.is_json response_data = json.loads(rv.data.decode('utf-8')) - jsonschema.validate(response_data, JUNIPER_LINK_METADATA) + jsonschema.validate(response_data, JUNIPER_LINK_RESPONSE_SCHEMA) assert response_data == { 'interface': { 'name': 'unknown-interface-name', @@ -266,150 +89,6 @@ IX_PUBLIC_PEER_INFO_KEYS = { ]) def test_peer_info( client, peer_address, expected_response_keys): - response_schema_definitions = { - "ip-address": { - "type": "string", - "oneOf": [ - {"pattern": r'^(\d+\.){3}\d+$'}, - {"pattern": r'^([a-f\d]{4}:){7}[a-f\d]{4}$'} - ] - }, - "interface-address": { - "type": "string", - "oneOf": [ - {"pattern": r'^(\d+\.){3}\d+/\d+$'}, - {"pattern": r'^[a-f\d:]+/\d+$'} - ] - }, - "vpn-rr-peer": { - "type": "object", - "properties": { - "name": {"$ref": "#/definitions/ip-address"}, - "description": {"type": "string"}, - "peer-as": {"type": "integer"}, - "router": {"type": "string"} - }, - "required": ["name", "description"], - "additionalProperties": False - }, - "ix-public-peer": { - "type": "object", - "properties": { - "name": {"$ref": "#/definitions/ip-address"}, - "description": {"type": "string"}, - "router": {"type": "string"}, - "as": { - "type": "object", - "properties": { - "local": {"type": "integer"}, - "peer": {"type": "integer"}, - }, - "required": ["local", "peer"], - "additionalProperties": False - } - }, - "required": ["name", "description", "as"], - "additionalProperties": False - }, - "ix-public-peer-list": { - "type": "array", - "items": {"$ref": "#/definitions/ip-address"} - }, - "ix-public-peer-info": { - "type": "object", - "properties": { - "peer": {"$ref": "#/definitions/ix-public-peer"}, - "group": {"$ref": "#/definitions/ix-public-peer-list"}, - "router": {"$ref": "#/definitions/ix-public-peer-list"} - }, - "required": ["peer", "group", "router"], - "additionalProperties": False - }, - "interface-info": { - "type": "object", - "properties": { - "name": {"$ref": "#/definitions/ip-address"}, - "interface address": { - "$ref": "#/definitions/interface-address"}, - "interface name": {"type": "string"}, - "router": {"type": "string"} - }, - "required": [ - "name", "interface address", "interface name", "router"], - "additionalProperties": False - }, - "service-info": { - "type": "object" - }, - "interface-lookup-info": { - "type": "object", - "properties": { - "interface": {"$ref": "#/definitions/interface-info"}, - "services": { - "type": "array", - "items": {"$ref": "#/definitions/service-info"} - } - } - }, - "snmp-info": { - "type": "object", - "properties": { - "hostname": {"type": "string"}, - "oid": {"type": "string"}, - "community": {"type": "string"} - }, - "required": ["oid", "community", "hostname"], - "additionalProperties": False - }, - "asn-group-member": { - "type": "object", - "properties": { - "router": {"type": "string"}, - "address": {"type": "string"}, - "group": {"type": "string"} - }, - "required": ["router", "address", "group"], - "additionalProperties": False - }, - "asn-group": { - "type": "object", - "properties": { - "asn": {"type": "integer"}, - "peers": { - "type": "array", - "items": {"$ref": "#/definitions/asn-group-member"} - } - }, - "required": ["asn", "peers"], - "additionalProperties": False - } - } - response_schema = { - "$schema": "http://json-schema.org/draft-07/schema#", - "type": "object", - - "definitions": { - **response_schema_definitions, **LOCATIONS_DEFINITIONS - }, - - "type": "object", - "properties": { - "ix-public-peer-info": { - "$ref": "#/definitions/ix-public-peer-info"}, - "vpn-rr-peer-info": {"$ref": "#/definitions/vpn-rr-peer"}, - "interfaces": { - "type": "array", - "items": {"$ref": "#/definitions/interface-lookup-info"} - }, - "locations": {"$ref": "#/definitions/locations-list"}, - "snmp": { - "type": "array", - "items": {"$ref": "#/definitions/snmp-info"} - }, - "asn": {"$ref": "#/definitions/asn-group"} - }, - "additionalProperties": False - } rv = client.get( '/classifier/peer-info/%s' % peer_address, @@ -417,7 +96,7 @@ def test_peer_info( assert rv.status_code == 200 assert rv.is_json response_data = json.loads(rv.data.decode('utf-8')) - jsonschema.validate(response_data, response_schema) + jsonschema.validate(response_data, PEER_INFO_RESPONSE_SCHEMA) assert set(response_data.keys()) == expected_response_keys -- GitLab