SIENTIAPDE-1174
Refactor unit tests to improve readability and maintainability - Updated test assertions and mock setups in test_app.py for clarity. - Added 'pod_id' to the metadata in test_ingestor.py for consistency. - Introduced a new test for TAG_WRITTEN_COUNT in test_metrics.py to verify its definition. - Enhanced test data structure in test_data_manager.py to include additional fields.
This commit is contained in:
@@ -10,6 +10,7 @@ metadata = {
|
||||
"model_name": "test_model",
|
||||
"workflow_name": "test_workflow",
|
||||
"schema_name": "test_schedule",
|
||||
"pod_id": "localhost",
|
||||
},
|
||||
}
|
||||
|
||||
@@ -258,7 +259,10 @@ def test_publish_no_kafka(data_manager):
|
||||
@patch("ingestor.managers.data_manager.traceback")
|
||||
def test_publish_error(traceback, data_manager):
|
||||
topic = "test_topic"
|
||||
data = {"key": "value"}
|
||||
data = {
|
||||
"key": "value",
|
||||
"name": "test_tag"
|
||||
}
|
||||
|
||||
# Mock the send method of the Kafka producer to raise an exception
|
||||
send_mock = MagicMock(side_effect=Exception("Test error"))
|
||||
|
||||
@@ -44,13 +44,15 @@ def mock_app_env(monkeypatch):
|
||||
monkeypatch.setattr(
|
||||
app.metrics.APP_UP,
|
||||
"labels",
|
||||
MagicMock(return_value=MagicMock(set=mocks["metrics_APP_UP_labels_set"])),
|
||||
MagicMock(return_value=MagicMock(
|
||||
set=mocks["metrics_APP_UP_labels_set"])),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
app.metrics.APP_LOOP_COUNT,
|
||||
"labels",
|
||||
MagicMock(
|
||||
return_value=MagicMock(inc=mocks["metrics_APP_LOOP_COUNT_labels_inc"])
|
||||
return_value=MagicMock(
|
||||
inc=mocks["metrics_APP_LOOP_COUNT_labels_inc"])
|
||||
),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
@@ -66,7 +68,8 @@ def mock_app_env(monkeypatch):
|
||||
app.metrics.APP_ERRORS_TOTAL,
|
||||
"labels",
|
||||
MagicMock(
|
||||
return_value=MagicMock(inc=mocks["metrics_APP_ERRORS_TOTAL_labels_inc"])
|
||||
return_value=MagicMock(
|
||||
inc=mocks["metrics_APP_ERRORS_TOTAL_labels_inc"])
|
||||
),
|
||||
)
|
||||
|
||||
@@ -74,7 +77,8 @@ def mock_app_env(monkeypatch):
|
||||
monkeypatch.setattr(app, "time", mocks["time_time"])
|
||||
monkeypatch.setattr(app, "sleep", mocks["time_sleep"])
|
||||
monkeypatch.setattr(app.signal, "signal", mocks["signal_signal"])
|
||||
monkeypatch.setattr(app.traceback, "print_exc", mocks["traceback_print_exc"])
|
||||
monkeypatch.setattr(app.traceback, "print_exc",
|
||||
mocks["traceback_print_exc"])
|
||||
|
||||
monkeypatch.setattr(app, "exit_signal", mocks["mock_exit_signal"])
|
||||
monkeypatch.setattr(app, "POD_ID", "test_pod")
|
||||
@@ -98,7 +102,7 @@ def test_main_successful_run_one_loop(mock_app_env, capsys):
|
||||
app.main()
|
||||
assert excinfo.value.code == 0
|
||||
|
||||
mock_app_env["start_http_server"].assert_called_once_with(4840)
|
||||
mock_app_env["start_http_server"].assert_called_once_with(9090)
|
||||
app.metrics.APP_UP.labels.assert_any_call(pod_id="test_pod")
|
||||
set_calls = mock_app_env["metrics_APP_UP_labels_set"].call_args_list
|
||||
assert call(1) in set_calls
|
||||
@@ -115,7 +119,8 @@ def test_main_successful_run_one_loop(mock_app_env, capsys):
|
||||
app.metrics.APP_LOOP_COUNT.labels.assert_called_with(pod_id="test_pod")
|
||||
mock_app_env["metrics_APP_LOOP_COUNT_labels_inc"].assert_called_once()
|
||||
|
||||
mock_exit_signal.wait.assert_called_once_with(mock_ingestor_instance.poll_interval)
|
||||
mock_exit_signal.wait.assert_called_once_with(
|
||||
mock_ingestor_instance.poll_interval)
|
||||
|
||||
app.metrics.APP_LOOP_DURATION.labels.assert_called_with(pod_id="test_pod")
|
||||
mock_app_env["metrics_APP_LOOP_DURATION_labels_observe"].assert_called_once_with(
|
||||
@@ -125,15 +130,17 @@ def test_main_successful_run_one_loop(mock_app_env, capsys):
|
||||
mock_ingestor_instance.shutdown.assert_called_once()
|
||||
mock_app_env["time_sleep"].assert_called_once_with(5)
|
||||
# CORREÇÃO APLICADA ABAIXO:
|
||||
mock_ingestor_instance.logger.info.assert_any_call("Main loop exit_signaled.")
|
||||
mock_ingestor_instance.logger.info.assert_any_call(
|
||||
"Main loop exit_signaled.")
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "Prometheus server started on port 4840." in captured.out
|
||||
assert "Prometheus server started on port 9090." in captured.out
|
||||
|
||||
|
||||
def test_main_prometheus_server_fails_to_start(mock_app_env, capsys):
|
||||
"""Test the scenario where starting the Prometheus server fails."""
|
||||
mock_app_env["start_http_server"].side_effect = OSError("Port already in use")
|
||||
mock_app_env["start_http_server"].side_effect = OSError(
|
||||
"Port already in use")
|
||||
|
||||
with pytest.raises(OsExitCalled) as excinfo:
|
||||
app.main()
|
||||
@@ -229,7 +236,8 @@ def test_main_multiple_loop_iterations(mock_app_env):
|
||||
mock_exit_signal = mock_app_env["mock_exit_signal"]
|
||||
|
||||
mock_exit_signal.is_set.side_effect = [False, False, False, True]
|
||||
mock_app_env["time_time"].side_effect = [10.0, 10.1, 10.2, 10.3, 10.4, 10.5]
|
||||
mock_app_env["time_time"].side_effect = [
|
||||
10.0, 10.1, 10.2, 10.3, 10.4, 10.5]
|
||||
|
||||
with pytest.raises(OsExitCalled) as excinfo:
|
||||
app.main()
|
||||
|
||||
@@ -51,6 +51,7 @@ def test___init__(notification_handler, getenv):
|
||||
"model_name": "-",
|
||||
"workflow_name": "OPC_INGESTOR",
|
||||
"schema_name": "OPC_INGESTOR",
|
||||
"pod_id": "localhost1"
|
||||
}
|
||||
|
||||
notification_handler.assert_called_once_with(
|
||||
|
||||
@@ -7,6 +7,15 @@ import ingestor.metrics as metrics
|
||||
# --- Test Functions for Each Metric (Corrected for v0.22.0 _name behavior) ---
|
||||
|
||||
|
||||
def test_ingestor_tag_written_count():
|
||||
"""Verify the definition of INGESTOR_TAG_WRITTEN_COUNT."""
|
||||
assert metrics.TAG_WRITTEN_COUNT is not None
|
||||
assert isinstance(metrics.TAG_WRITTEN_COUNT, Counter)
|
||||
assert metrics.TAG_WRITTEN_COUNT._name == "ingestor_tag_written_count"
|
||||
assert set(metrics.TAG_WRITTEN_COUNT._labelnames) == {
|
||||
"pod_id", "tag_name", "collection_name"}
|
||||
|
||||
|
||||
def test_app_loop_count():
|
||||
"""Verify the definition of APP_LOOP_COUNT."""
|
||||
assert metrics.APP_LOOP_COUNT is not None
|
||||
@@ -118,7 +127,8 @@ def test_opc_connections_total():
|
||||
assert (
|
||||
metrics.OPC_CONNECTIONS_TOTAL._name == "opc_connections_initiated"
|
||||
) # REMOVED _total
|
||||
assert set(metrics.OPC_CONNECTIONS_TOTAL._labelnames) == {"pod_id", "server_name"}
|
||||
assert set(metrics.OPC_CONNECTIONS_TOTAL._labelnames) == {
|
||||
"pod_id", "server_name"}
|
||||
|
||||
|
||||
def test_opc_connections_failed():
|
||||
@@ -128,7 +138,8 @@ def test_opc_connections_failed():
|
||||
assert (
|
||||
metrics.OPC_CONNECTIONS_FAILED._name == "opc_connections_failed"
|
||||
) # REMOVED _total
|
||||
assert set(metrics.OPC_CONNECTIONS_FAILED._labelnames) == {"pod_id", "server_name"}
|
||||
assert set(metrics.OPC_CONNECTIONS_FAILED._labelnames) == {
|
||||
"pod_id", "server_name"}
|
||||
|
||||
|
||||
def test_opc_connection_status():
|
||||
@@ -162,7 +173,8 @@ def test_opc_tags_subscribed():
|
||||
assert metrics.OPC_TAGS_SUBSCRIBED is not None
|
||||
assert isinstance(metrics.OPC_TAGS_SUBSCRIBED, Gauge)
|
||||
assert metrics.OPC_TAGS_SUBSCRIBED._name == "opc_tags_subscribed_current"
|
||||
assert set(metrics.OPC_TAGS_SUBSCRIBED._labelnames) == {"pod_id", "server_name"}
|
||||
assert set(metrics.OPC_TAGS_SUBSCRIBED._labelnames) == {
|
||||
"pod_id", "server_name"}
|
||||
|
||||
|
||||
def test_opc_cycles_without_data():
|
||||
@@ -170,7 +182,8 @@ def test_opc_cycles_without_data():
|
||||
assert metrics.OPC_CYCLES_WITHOUT_DATA is not None
|
||||
assert isinstance(metrics.OPC_CYCLES_WITHOUT_DATA, Gauge)
|
||||
assert metrics.OPC_CYCLES_WITHOUT_DATA._name == "opc_cycles_without_data"
|
||||
assert set(metrics.OPC_CYCLES_WITHOUT_DATA._labelnames) == {"pod_id", "server_name"}
|
||||
assert set(metrics.OPC_CYCLES_WITHOUT_DATA._labelnames) == {
|
||||
"pod_id", "server_name"}
|
||||
|
||||
|
||||
def test_opc_reconnections_total():
|
||||
@@ -180,7 +193,8 @@ def test_opc_reconnections_total():
|
||||
assert (
|
||||
metrics.OPC_RECONNECTIONS_TOTAL._name == "opc_reconnections_tried"
|
||||
) # REMOVED _total
|
||||
assert set(metrics.OPC_RECONNECTIONS_TOTAL._labelnames) == {"pod_id", "server_name"}
|
||||
assert set(metrics.OPC_RECONNECTIONS_TOTAL._labelnames) == {
|
||||
"pod_id", "server_name"}
|
||||
|
||||
|
||||
def test_kafka_messages_sent():
|
||||
@@ -198,7 +212,8 @@ def test_kafka_messages_errors():
|
||||
assert (
|
||||
metrics.KAFKA_MESSAGES_ERRORS._name == "kafka_messages_errors"
|
||||
) # REMOVED _total
|
||||
assert set(metrics.KAFKA_MESSAGES_ERRORS._labelnames) == {"pod_id", "topic"}
|
||||
assert set(metrics.KAFKA_MESSAGES_ERRORS._labelnames) == {
|
||||
"pod_id", "topic"}
|
||||
|
||||
|
||||
def test_kafka_connection_status():
|
||||
@@ -214,7 +229,8 @@ def test_redis_operations_total():
|
||||
assert metrics.REDIS_OPERATIONS_TOTAL is not None
|
||||
assert isinstance(metrics.REDIS_OPERATIONS_TOTAL, Counter)
|
||||
assert metrics.REDIS_OPERATIONS_TOTAL._name == "redis_operations" # REMOVED _total
|
||||
assert set(metrics.REDIS_OPERATIONS_TOTAL._labelnames) == {"pod_id", "operation"}
|
||||
assert set(metrics.REDIS_OPERATIONS_TOTAL._labelnames) == {
|
||||
"pod_id", "operation"}
|
||||
|
||||
|
||||
def test_redis_operations_errors():
|
||||
@@ -224,7 +240,8 @@ def test_redis_operations_errors():
|
||||
assert (
|
||||
metrics.REDIS_OPERATIONS_ERRORS._name == "redis_operations_errors"
|
||||
) # REMOVED _total
|
||||
assert set(metrics.REDIS_OPERATIONS_ERRORS._labelnames) == {"pod_id", "operation"}
|
||||
assert set(metrics.REDIS_OPERATIONS_ERRORS._labelnames) == {
|
||||
"pod_id", "operation"}
|
||||
|
||||
|
||||
def test_redis_operations_duration():
|
||||
@@ -234,7 +251,8 @@ def test_redis_operations_duration():
|
||||
assert (
|
||||
metrics.REDIS_OPERATIONS_DURATION._name == "redis_operations_duration_seconds"
|
||||
)
|
||||
assert set(metrics.REDIS_OPERATIONS_DURATION._labelnames) == {"pod_id", "operation"}
|
||||
assert set(metrics.REDIS_OPERATIONS_DURATION._labelnames) == {
|
||||
"pod_id", "operation"}
|
||||
|
||||
|
||||
def test_redis_connection_status():
|
||||
@@ -250,4 +268,5 @@ def test_notifications_sent():
|
||||
assert metrics.NOTIFICATIONS_SENT is not None
|
||||
assert isinstance(metrics.NOTIFICATIONS_SENT, Counter)
|
||||
assert metrics.NOTIFICATIONS_SENT._name == "notifications_sent" # REMOVED _total
|
||||
assert set(metrics.NOTIFICATIONS_SENT._labelnames) == {"pod_id", "level", "block"}
|
||||
assert set(metrics.NOTIFICATIONS_SENT._labelnames) == {
|
||||
"pod_id", "level", "block"}
|
||||
|
||||
Reference in New Issue
Block a user