diff --git a/test/storage/test_external_inventory.py b/test/storage/test_external_inventory.py new file mode 100644 index 0000000000000000000000000000000000000000..30c13e0bc7287fa2b118b875b05b707482028857 --- /dev/null +++ b/test/storage/test_external_inventory.py @@ -0,0 +1,150 @@ +from inventory_provider.storage import external_inventory + + +def test_update_services_to_monitor(mocker): + mocked_redis = mocker.patch( + "inventory_provider.storage.external_inventory.db.get_redis") + mocked_hset = mocked_redis.return_value.hset + + services = [ + {"circuit_type": "path", "id": "test_id_0", "other": "stuff"}, + {"circuit_type": "service", "id": "test_id_1", "other": "stuff"}, + {"circuit_type": "l2circuit", "id": "test_id_2", "other": "stuff"}, + {"circuit_type": "spam", "id": "test_id_4", "other": "stuff"} + ] + external_inventory.update_services_to_monitor(services) + mocked_hset.assert_any_call( + external_inventory.services_key, "test_id_0", + "{\"circuit_type\": \"path\"," + " \"id\": \"test_id_0\"," + " \"other\": \"stuff\"}") + mocked_hset.assert_any_call( + external_inventory.services_key, "test_id_1", + "{\"circuit_type\": \"service\"," + " \"id\": \"test_id_1\"," + " \"other\": \"stuff\"}") + mocked_hset.assert_any_call( + external_inventory.services_key, "test_id_2", + "{\"circuit_type\": \"l2circuit\"," + " \"id\": \"test_id_2\"," + " \"other\": \"stuff\"}") + assert mocked_hset.call_count == 3 + + +def test_update_interfaces_to_services(mocker): + mocked_redis = mocker.patch( + "inventory_provider.storage.external_inventory.db.get_redis") + mocked_hset = mocked_redis.return_value.hset + services = [ + {"equipment": "eq_0", "interface_name": "if_0"}, + {"equipment": "eq_1", "interface_name": "if_1"}, + {"equipment": "eq_2", "interface_name": "if_2"}, + {"equipment": "eq_3", "interface_name": "if_3"}, + {"equipment": "eq_3", "interface_name": "if_3", "extra": "stuff"}, + {"equipment": "eq_4", "interface_name": "if_4"} + ] + unique_keys = set(s["equipment"] + "::" + s["interface_name"] + for s in services) + + external_inventory.update_interfaces_to_services(services) + assert mocked_hset.call_count == len(unique_keys) + mocked_hset.assert_any_call(external_inventory.interfaces_key, + "eq_2::if_2", + "[{\"equipment\": \"eq_2\"," + " \"interface_name\": \"if_2\"}]") + mocked_hset.assert_any_call(external_inventory.interfaces_key, + "eq_3::if_3", + "[{\"equipment\": \"eq_3\"," + " \"interface_name\": \"if_3\"}," + " {\"equipment\": \"eq_3\"," + " \"interface_name\": \"if_3\"," + " \"extra\": \"stuff\"}]") + + +def test_update_service_hierarchy(mocker): + mocked_redis = mocker.patch( + "inventory_provider.storage.external_inventory.db.get_redis") + mocked_hset = mocked_redis.return_value.hset + data = [ + {"parent_circuit": "PC_1", + "parent_circuit_id": "1001", + "parent_circuit_status": "operational", + "child_circuit": "CC_1", + "child_circuit_id": "2001", + "segment_group": 1}, + {"parent_circuit": "PC_2", + "parent_circuit_id": "1002", + "parent_circuit_status": "operational", + "child_circuit": "CC_1", + "child_circuit_id": "2001", + "segment_group": 1}, + {"parent_circuit": "PC_3", + "parent_circuit_id": "1003", + "parent_circuit_status": "operational", + "child_circuit": "CC_2", + "child_circuit_id": "2002", + "segment_group": 1}, + {"parent_circuit": "PC_3", + "parent_circuit_id": "1003", + "parent_circuit_status": "operational", + "child_circuit": "CC_3", + "child_circuit_id": "2003", + "segment_group": 1} + ] + external_inventory.update_service_hierarchy(data) + u_parent_keys = set([d["parent_circuit_id"] for d in data]) + u_child_keys = set([d["child_circuit_id"] for d in data]) + assert mocked_hset.call_count == len(u_parent_keys) + len(u_child_keys) + mocked_hset.assert_any_call( + external_inventory.service_child_to_parents_key, + "2001", + "[{\"parent_circuit\": \"PC_1\"," + " \"parent_circuit_id\": \"1001\"," + " \"parent_circuit_status\": \"operational\"," + " \"child_circuit\": \"CC_1\"," + " \"child_circuit_id\": \"2001\"," + " \"segment_group\": 1}," + " {\"parent_circuit\": \"PC_2\"," + " \"parent_circuit_id\": \"1002\"," + " \"parent_circuit_status\": \"operational\"," + " \"child_circuit\": \"CC_1\"," + " \"child_circuit_id\": \"2001\"," + " \"segment_group\": 1}]" + ) + mocked_hset.assert_any_call( + external_inventory.service_child_to_parents_key, + "2002", + "[{\"parent_circuit\": \"PC_3\"," + " \"parent_circuit_id\": \"1003\"," + " \"parent_circuit_status\": \"operational\"," + " \"child_circuit\": \"CC_2\"," + " \"child_circuit_id\": \"2002\"," + " \"segment_group\": 1}]" + ) + + mocked_hset.assert_any_call( + external_inventory.service_parent_to_children_key, + "1003", + "[{\"parent_circuit\": \"PC_3\"," + " \"parent_circuit_id\": \"1003\"," + " \"parent_circuit_status\": \"operational\"," + " \"child_circuit\": \"CC_2\"," + " \"child_circuit_id\": \"2002\"," + " \"segment_group\": 1}," + " {\"parent_circuit\": \"PC_3\"," + " \"parent_circuit_id\": \"1003\"," + " \"parent_circuit_status\": \"operational\"," + " \"child_circuit\": \"CC_3\"," + " \"child_circuit_id\": \"2003\"," + " \"segment_group\": 1}]" + ) + mocked_hset.assert_any_call( + external_inventory.service_parent_to_children_key, + "1002", + "[{\"parent_circuit\": \"PC_2\"," + " \"parent_circuit_id\": \"1002\"," + " \"parent_circuit_status\": \"operational\"," + " \"child_circuit\": \"CC_1\"," + " \"child_circuit_id\": \"2001\"," + " \"segment_group\": 1}]" + )