diff --git a/test/test_worker.py b/test/test_worker.py index 3a4190eb99c83906263c6316fcd847119b71ca5f..34dfd68cd78f8194efb7c2a142bf414678211f9e 100644 --- a/test/test_worker.py +++ b/test/test_worker.py @@ -3,7 +3,8 @@ import json from inventory_provider.tasks import common from inventory_provider.tasks.worker import transform_ims_data, \ extract_ims_data, persist_ims_data, \ - retrieve_and_persist_neteng_managed_device_list + retrieve_and_persist_neteng_managed_device_list, \ + populate_poller_interfaces_cache def test_extract_ims_data(mocker): @@ -324,3 +325,182 @@ def test_retrieve_and_persist_neteng_managed_device_list( result = retrieve_and_persist_neteng_managed_device_list() assert result == device_list assert json.loads(r.get('netdash')) == device_list + + +def test_populate_poller_interfaces_cache( + mocker, data_config, mocked_redis): + r = common._get_redis(data_config) + standard_interfaces = [ + { + "router": "router_a.geant.net", + "name": "interface_a", + "bundle": ["ae_a"], + "bundle-parents": [], + "description": "DESCRIPTION A", + "circuits": [] + }, + { + "router": "router_a.geant.net", + "name": "ae_a", + "bundle": [], + "bundle-parents": [], + "description": "DESCRIPTION B", + "circuits": [] + }, + { + "router": "router_a.geant.net", + "name": "ae_a.123", + "bundle": [], + "bundle-parents": [], + "description": "DESCRIPTION C", + "circuits": [] + }, + ] + + lab_interfaces = [ + { + "router": "lab_router_a.geant.net", + "name": "lab_interface_a", + "bundle": ["ae_c"], + "bundle-parents": [], + "description": "DESCRIPTION C", + "circuits": [] + }, + { + "router": "lab_router_a.geant.net", + "name": "ae_c", + "bundle": [], + "bundle-parents": [], + "description": "DESCRIPTION D", + "circuits": [] + }, + ] + + bundles = { + "router_z.geant.net": {"ae_1": ["interface_z"]}, + "lab_router_a.geant.net": {"ae_c": ["lab_interface_a"]}, + "router_a.geant.net": {"ae_a": ["interface_a"]}, + } + + snmp_indexes = { + "router_a.geant.net": { + "ae_a": { + "name": "ae_a", + "index": 1, + "community": "COMMUNITY_A" + }, + "ae_a.123": { + "name": "ae_a.123", + "index": 1231, + "community": "COMMUNITY_A" + }, + "interface_a": { + "name": "interface_a", + "index": 12, + "community": "COMMUNITY_A" + } + }, + "router_b.geant.net": { + "ae_a": { + "name": "ae_a", + "index": 2, + "community": "COMMUNITY_A" + } + }, + "lab_router_a.geant.net": { + "ae_c": { + "name": "ae_c", + "index": 3, + "community": "COMMUNITY_A" + } + }, + } + services = { + "ROUTER_A": { + "AE_A.123": [{ + "id": 321, + "name": "SERVICE A", + "type": "SERVICE TYPE", + "status": "operational" + }], + "AE_A.456": [{ + "id": 654, + "name": "SERVICE B", + "type": "SERVICE TYPE", + "status": "operational" + }] + } + } + + no_lab_res = [ + { + "router": "router_a.geant.net", + "name": "interface_a", + "bundle": ["ae_a"], + "bundle-parents": [], + "description": "DESCRIPTION A", + "circuits": [], + "snmp-index": 12, + "dashboards": [] + }, + { + "router": "router_a.geant.net", + "name": "ae_a", + "bundle": [], + "bundle-parents": ["interface_a"], + "description": "DESCRIPTION B", + "circuits": [], + "snmp-index": 1, + "dashboards": [] + }, + { + "router": "router_a.geant.net", + "name": "ae_a.123", + "bundle": [], + "bundle-parents": ["interface_a"], + "description": "DESCRIPTION C", + "circuits": [{ + "id": 321, + "name": "SERVICE A", + "type": "SERVICE TYPE", + "status": "operational" + }], + "snmp-index": 1231, + "dashboards": [] + }, + ] + lab_res = [ + { + "router": "lab_router_a.geant.net", + "name": "ae_c", + "bundle": [], + "bundle-parents": ["lab_interface_a"], + "description": "DESCRIPTION D", + "circuits": [], + "snmp-index": 3, + "dashboards": [] + }, + ] + + mocker.patch('inventory_provider.tasks.worker._load_interfaces', + side_effect=[standard_interfaces, lab_interfaces]) + mocker.patch('inventory_provider.tasks.worker._load_interface_bundles', + return_value=bundles) + mocker.patch('inventory_provider.tasks.worker.load_snmp_indexes', + return_value=snmp_indexes) + mocker.patch('inventory_provider.tasks.worker._load_services', + return_value=services) + mocker.patch( + 'inventory_provider.tasks.worker.InventoryTask.config' + ) + mocker.patch('inventory_provider.tasks.worker.get_next_redis', + return_value=r) + + populate_poller_interfaces_cache() + assert r.exists("classifier-cache:poller-interfaces:no-lab") + assert r.exists("classifier-cache:poller-interfaces:all") + no_lab = r.get("classifier-cache:poller-interfaces:no-lab").decode("utf-8") + all = r.get("classifier-cache:poller-interfaces:all").decode("utf-8") + assert json.loads(no_lab) == no_lab_res + all_res = no_lab_res + lab_res + assert json.loads(all) == all_res