SIENTIAPDE-1163
SIENTIAPDE-1163 Refactor Ingestor and DataManager classes to enhance metadata handling - Updated Ingestor class to include additional metadata fields: 'workflow_name' and 'schema_name'. - Modified DataManager to accept metadata during initialization. - Adjusted unit tests to validate the new metadata structure and ensure proper functionality across various managers. - Removed unused logger initialization in tests for cleaner code.
This commit is contained in:
@@ -1,13 +1,36 @@
|
||||
from unittest.mock import MagicMock, patch
|
||||
from pytest import fixture, raises
|
||||
from sientia_do.notifications.models import NotificationLevel
|
||||
from ingestor.managers.resource_manager import ResourceManager
|
||||
|
||||
metadata = {
|
||||
"metadata": {
|
||||
"model_id": "test_model",
|
||||
"model_name": "test_model",
|
||||
"workflow_name": "test_workflow",
|
||||
"schema_name": "test_schedule",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@fixture
|
||||
@patch("ingestor.managers.resource_manager.Redis")
|
||||
def resource_manager(redis):
|
||||
|
||||
return ResourceManager("localhost", 6379, 10, 10, "pod_id")
|
||||
resource_manager = ResourceManager(
|
||||
host="localhost",
|
||||
port=6379,
|
||||
lease_ttl=10,
|
||||
heartbeat_ttl=10,
|
||||
pod_id="pod_id",
|
||||
metadata=metadata["metadata"],
|
||||
logger=MagicMock(),
|
||||
notification_handler=MagicMock(),
|
||||
)
|
||||
|
||||
resource_manager.send_notification = MagicMock()
|
||||
|
||||
return resource_manager
|
||||
|
||||
|
||||
def test_get_success(resource_manager):
|
||||
@@ -54,7 +77,8 @@ def test_renew_tag_lease_success(resource_manager):
|
||||
result = resource_manager.renew_tag_lease("tag_id")
|
||||
assert result is True
|
||||
resource_manager.redis.get.assert_called_once_with("lease:opc_tags:tag_id")
|
||||
resource_manager.redis.expire.assert_called_once_with("lease:opc_tags:tag_id", 10)
|
||||
resource_manager.redis.expire.assert_called_once_with(
|
||||
"lease:opc_tags:tag_id", 10)
|
||||
|
||||
|
||||
def test_renew_tag_lease_failure(resource_manager):
|
||||
@@ -69,7 +93,8 @@ def test_renew_tag_lease_failure(resource_manager):
|
||||
def test_drop_tag_lease(resource_manager):
|
||||
resource_manager.redis.delete.return_value = True
|
||||
resource_manager.drop_tag_lease("tag_id")
|
||||
resource_manager.redis.delete.assert_called_once_with("lease:opc_tags:tag_id")
|
||||
resource_manager.redis.delete.assert_called_once_with(
|
||||
"lease:opc_tags:tag_id")
|
||||
|
||||
|
||||
def test_get_all_ingestors(resource_manager):
|
||||
@@ -106,7 +131,16 @@ def test_init_connection_failure(monkeypatch):
|
||||
mock_metrics.labels.return_value = mock_status
|
||||
|
||||
with raises(Exception, match="Connection failed"):
|
||||
ResourceManager("localhost", 6379, 10, 10, "pod_id")
|
||||
ResourceManager(
|
||||
host="localhost",
|
||||
port=6379,
|
||||
lease_ttl=10,
|
||||
heartbeat_ttl=10,
|
||||
pod_id="pod_id",
|
||||
metadata=metadata["metadata"],
|
||||
logger=MagicMock(),
|
||||
notification_handler=MagicMock(),
|
||||
)
|
||||
|
||||
mock_metrics.labels.assert_called_once_with(pod_id="pod_id")
|
||||
mock_status.set.assert_called_once_with(0)
|
||||
@@ -118,7 +152,8 @@ def test_init_ping_failure(monkeypatch):
|
||||
mock_redis_instance.ping.side_effect = Exception("Ping failed")
|
||||
|
||||
mock_redis_class = MagicMock(return_value=mock_redis_instance)
|
||||
monkeypatch.setattr("ingestor.managers.resource_manager.Redis", mock_redis_class)
|
||||
monkeypatch.setattr(
|
||||
"ingestor.managers.resource_manager.Redis", mock_redis_class)
|
||||
|
||||
# Test that the exception is raised and metrics are set properly
|
||||
with patch("ingestor.metrics.REDIS_CONNECTION_STATUS") as mock_metrics:
|
||||
@@ -126,7 +161,16 @@ def test_init_ping_failure(monkeypatch):
|
||||
mock_metrics.labels.return_value = mock_status
|
||||
|
||||
with raises(Exception, match="Ping failed"):
|
||||
ResourceManager("localhost", 6379, 10, 10, "pod_id")
|
||||
ResourceManager(
|
||||
host="localhost",
|
||||
port=6379,
|
||||
lease_ttl=10,
|
||||
heartbeat_ttl=10,
|
||||
pod_id="pod_id",
|
||||
metadata=metadata["metadata"],
|
||||
logger=MagicMock(),
|
||||
notification_handler=MagicMock(),
|
||||
)
|
||||
|
||||
mock_metrics.labels.assert_called_once_with(pod_id="pod_id")
|
||||
mock_status.set.assert_called_once_with(0)
|
||||
@@ -172,19 +216,23 @@ def test_execute_redis_op_exception(resource_manager):
|
||||
|
||||
with patch("ingestor.managers.resource_manager.time", return_value=100):
|
||||
with patch("ingestor.metrics.REDIS_OPERATIONS_ERRORS") as mock_errors:
|
||||
with patch("builtins.print") as mock_print:
|
||||
mock_errors_labels = MagicMock()
|
||||
mock_errors.labels.return_value = mock_errors_labels
|
||||
mock_errors_labels = MagicMock()
|
||||
mock_errors.labels.return_value = mock_errors_labels
|
||||
|
||||
# Execute the operation and expect an exception
|
||||
with raises(Exception, match="Operation failed"):
|
||||
resource_manager._execute_redis_op("test_op", mock_func, "arg1")
|
||||
# Execute the operation and expect an exception
|
||||
with raises(Exception, match="Operation failed"):
|
||||
resource_manager._execute_redis_op(
|
||||
"test_op", mock_func, "arg1")
|
||||
|
||||
# Verify metrics and error handling
|
||||
mock_errors.labels.assert_called_once_with(
|
||||
pod_id="pod_id", operation="test_op"
|
||||
)
|
||||
mock_errors_labels.inc.assert_called_once()
|
||||
mock_print.assert_called_once_with(
|
||||
"Error in Redis operation 'test_op': Operation failed"
|
||||
)
|
||||
# Verify metrics and error handling
|
||||
mock_errors.labels.assert_called_once_with(
|
||||
pod_id="pod_id", operation="test_op"
|
||||
)
|
||||
mock_errors_labels.inc.assert_called_once()
|
||||
resource_manager.send_notification.assert_called_once_with(
|
||||
metadata=metadata["metadata"],
|
||||
notification_id="REDIS_OPERATION_ERROR_test_op",
|
||||
message="Error in Redis operation 'test_op': Operation failed",
|
||||
block="redis_manager",
|
||||
level=NotificationLevel.ERROR,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user