Update dependencies, improve CI workflow, and enhance code formatting
- Updated the `sientia-dataops-library` dependency version from 1.4.3 to 1.4.6 in `requirements.txt`. - Modified the GitHub Actions workflow to install development and runtime dependencies separately, improving clarity and organization. - Added code formatting and linting checks using Ruff, along with type checking using mypy, to ensure code quality. - Updated `.gitignore` to include additional cache directories and log files. - Refactored code in various files for consistency in string formatting and improved logging messages.
This commit is contained in:
@@ -1,66 +1,68 @@
|
||||
import json
|
||||
from datetime import datetime
|
||||
from unittest.mock import AsyncMock, MagicMock, call, patch
|
||||
from pytest import fixture, mark
|
||||
from asyncua.crypto.security_policies import SecurityPolicyBasic256
|
||||
|
||||
import pytest
|
||||
from ingestor.managers.opc_manager import OpcManager
|
||||
from asyncua.crypto.security_policies import SecurityPolicyBasic256
|
||||
from pytest import fixture, mark
|
||||
from sientia_do.notifications.models import NotificationLevel
|
||||
|
||||
from ingestor.managers.opc_manager import OpcManager
|
||||
|
||||
tags = {
|
||||
"ns=3;i=1001": {
|
||||
"aggregation_function": "LTS",
|
||||
"frequency": 1000,
|
||||
"max_value": 100,
|
||||
"min_value": 0,
|
||||
"tag_name": "Counter",
|
||||
'ns=3;i=1001': {
|
||||
'aggregation_function': 'LTS',
|
||||
'frequency': 1000,
|
||||
'max_value': 100,
|
||||
'min_value': 0,
|
||||
'tag_name': 'Counter',
|
||||
},
|
||||
"ns=3;i=1003": {
|
||||
"aggregation_function": "AVG",
|
||||
"frequency": 1000,
|
||||
"max_value": 100,
|
||||
"min_value": 0,
|
||||
"tag_name": "Random",
|
||||
'ns=3;i=1003': {
|
||||
'aggregation_function': 'AVG',
|
||||
'frequency': 1000,
|
||||
'max_value': 100,
|
||||
'min_value': 0,
|
||||
'tag_name': 'Random',
|
||||
},
|
||||
"ns=3;i=1004": {
|
||||
"aggregation_function": "MDN",
|
||||
"frequency": 1000,
|
||||
"max_value": 100,
|
||||
"min_value": 0,
|
||||
"tag_name": "Sawtooth",
|
||||
'ns=3;i=1004': {
|
||||
'aggregation_function': 'MDN',
|
||||
'frequency': 1000,
|
||||
'max_value': 100,
|
||||
'min_value': 0,
|
||||
'tag_name': 'Sawtooth',
|
||||
},
|
||||
}
|
||||
|
||||
metadata = {
|
||||
"metadata": {
|
||||
"model_id": "test_model",
|
||||
"model_name": "test_model",
|
||||
"workflow_name": "test_workflow",
|
||||
"schema_name": "test_schedule",
|
||||
'metadata': {
|
||||
'model_id': 'test_model',
|
||||
'model_name': 'test_model',
|
||||
'workflow_name': 'test_workflow',
|
||||
'schema_name': 'test_schedule',
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@fixture
|
||||
@patch("ingestor.managers.opc_manager.metrics")
|
||||
@patch('ingestor.managers.opc_manager.metrics')
|
||||
def raw_opc_manager(mock_metrics):
|
||||
return OpcManager(
|
||||
name="TestConnector",
|
||||
url="opc.tcp://localhost:4840",
|
||||
name='TestConnector',
|
||||
url='opc.tcp://localhost:4840',
|
||||
data_manager=MagicMock(),
|
||||
logger=MagicMock(),
|
||||
server_uri="opc.tcp://localhost:4840",
|
||||
server_uri='opc.tcp://localhost:4840',
|
||||
notification_handler=MagicMock(),
|
||||
metadata=metadata["metadata"],
|
||||
metadata=metadata['metadata'],
|
||||
)
|
||||
|
||||
|
||||
@fixture
|
||||
def opc_manager(raw_opc_manager):
|
||||
raw_opc_manager.client = AsyncMock()
|
||||
raw_opc_manager.cert_path = "cert.pem"
|
||||
raw_opc_manager.private_key_path = "private_key.pem"
|
||||
raw_opc_manager.server_cert_path = "server_cert.pem"
|
||||
raw_opc_manager.cert_path = 'cert.pem'
|
||||
raw_opc_manager.private_key_path = 'private_key.pem'
|
||||
raw_opc_manager.server_cert_path = 'server_cert.pem'
|
||||
raw_opc_manager.send_notification = MagicMock()
|
||||
|
||||
return raw_opc_manager
|
||||
@@ -68,7 +70,7 @@ def opc_manager(raw_opc_manager):
|
||||
|
||||
@fixture
|
||||
def opc_manager_subscribed(opc_manager):
|
||||
opc_manager.subscriptions["sub1"] = AsyncMock()
|
||||
opc_manager.subscriptions['sub1'] = AsyncMock()
|
||||
|
||||
return opc_manager
|
||||
|
||||
@@ -76,7 +78,7 @@ def opc_manager_subscribed(opc_manager):
|
||||
def test___str__(opc_manager):
|
||||
assert (
|
||||
str(opc_manager)
|
||||
== "OpcManager(name=TestConnector, url=opc.tcp://localhost:4840, server_uri=opc.tcp://localhost:4840)\nnodes={}, subscriptions={}"
|
||||
== 'OpcManager(name=TestConnector, url=opc.tcp://localhost:4840, server_uri=opc.tcp://localhost:4840)\nnodes={}, subscriptions={}'
|
||||
)
|
||||
|
||||
|
||||
@@ -91,21 +93,18 @@ async def test_shutdown_success(opc_manager):
|
||||
|
||||
@mark.asyncio
|
||||
async def test_shutdown_error(opc_manager):
|
||||
opc_manager.disconnect = AsyncMock(side_effect=Exception("Test error"))
|
||||
opc_manager.disconnect = AsyncMock(side_effect=Exception('Test error'))
|
||||
|
||||
await opc_manager.shutdown()
|
||||
|
||||
opc_manager.logger.error.assert_called_once_with(
|
||||
"Error during cleanup: Test error")
|
||||
opc_manager.logger.error.assert_called_once_with('Error during cleanup: Test error')
|
||||
|
||||
|
||||
@mark.asyncio
|
||||
async def test_set_security_success(opc_manager):
|
||||
await opc_manager.set_security()
|
||||
|
||||
opc_manager.client.set_application_uri.assert_called_once_with(
|
||||
opc_manager.server_uri
|
||||
)
|
||||
opc_manager.client.set_application_uri.assert_called_once_with(opc_manager.server_uri)
|
||||
|
||||
opc_manager.client.set_security.assert_called_once_with(
|
||||
SecurityPolicyBasic256,
|
||||
@@ -114,8 +113,7 @@ async def test_set_security_success(opc_manager):
|
||||
server_certificate=opc_manager.server_cert_path,
|
||||
)
|
||||
|
||||
opc_manager.client.set_secure_channel_timeout.assert_called_once_with(
|
||||
10000000)
|
||||
opc_manager.client.set_secure_channel_timeout.assert_called_once_with(10000000)
|
||||
opc_manager.client.set_session_timeout.assert_called_once_with(10000000)
|
||||
|
||||
|
||||
@@ -127,19 +125,16 @@ async def test_set_security_no_cert(opc_manager):
|
||||
try:
|
||||
await opc_manager.set_security()
|
||||
except ValueError as e:
|
||||
assert (
|
||||
str(e)
|
||||
== "Certificate and private key paths must be provided for secure connection."
|
||||
)
|
||||
assert str(e) == 'Certificate and private key paths must be provided for secure connection.'
|
||||
else:
|
||||
assert False, "ValueError not raised"
|
||||
assert False, 'ValueError not raised'
|
||||
|
||||
assert opc_manager.client.set_security.call_count == 0
|
||||
|
||||
|
||||
@mark.asyncio
|
||||
@patch("ingestor.managers.opc_manager.metrics")
|
||||
@patch("ingestor.managers.opc_manager.Client")
|
||||
@patch('ingestor.managers.opc_manager.metrics')
|
||||
@patch('ingestor.managers.opc_manager.Client')
|
||||
async def test_connect_no_security(client, mock_metrics, raw_opc_manager):
|
||||
raw_opc_manager.set_security = AsyncMock()
|
||||
client.return_value = AsyncMock()
|
||||
@@ -150,26 +145,24 @@ async def test_connect_no_security(client, mock_metrics, raw_opc_manager):
|
||||
raw_opc_manager.client.connect.assert_called_once()
|
||||
raw_opc_manager.set_security.assert_not_called()
|
||||
mock_metrics.OPC_CONNECTIONS_TOTAL.labels.assert_called_once_with(
|
||||
pod_id=raw_opc_manager.pod_id,
|
||||
server_name=raw_opc_manager.name
|
||||
pod_id=raw_opc_manager.pod_id, server_name=raw_opc_manager.name
|
||||
)
|
||||
mock_metrics.OPC_CONNECTIONS_TOTAL.labels.return_value.inc.assert_called_once()
|
||||
mock_metrics.OPC_CONNECTION_STATUS.labels.assert_called_once_with(
|
||||
pod_id=raw_opc_manager.pod_id,
|
||||
server_name=raw_opc_manager.name,
|
||||
server_url=raw_opc_manager.url
|
||||
server_url=raw_opc_manager.url,
|
||||
)
|
||||
mock_metrics.OPC_CONNECTION_STATUS.labels.return_value.set.assert_called_once_with(
|
||||
1)
|
||||
mock_metrics.OPC_CONNECTION_STATUS.labels.return_value.set.assert_called_once_with(1)
|
||||
mock_metrics.OPC_CONNECTIONS_FAILED.labels.assert_not_called()
|
||||
|
||||
|
||||
@mark.asyncio
|
||||
@patch("ingestor.managers.opc_manager.Client")
|
||||
@patch('ingestor.managers.opc_manager.Client')
|
||||
async def test_connect_with_security(client, raw_opc_manager):
|
||||
raw_opc_manager.cert_path = "cert.pem"
|
||||
raw_opc_manager.private_key_path = "private_key.pem"
|
||||
raw_opc_manager.server_cert_path = "server_cert.pem"
|
||||
raw_opc_manager.cert_path = 'cert.pem'
|
||||
raw_opc_manager.private_key_path = 'private_key.pem'
|
||||
raw_opc_manager.server_cert_path = 'server_cert.pem'
|
||||
raw_opc_manager.set_security = AsyncMock()
|
||||
client.return_value = AsyncMock()
|
||||
|
||||
@@ -181,15 +174,14 @@ async def test_connect_with_security(client, raw_opc_manager):
|
||||
|
||||
|
||||
@mark.asyncio
|
||||
@patch("ingestor.managers.opc_manager.Client")
|
||||
@patch("ingestor.managers.opc_manager.metrics")
|
||||
@patch('ingestor.managers.opc_manager.Client')
|
||||
@patch('ingestor.managers.opc_manager.metrics')
|
||||
async def test_connect_exception_handling_and_metrics(
|
||||
mock_metrics_module, mock_opc_client_class, raw_opc_manager
|
||||
):
|
||||
mock_client_instance = mock_opc_client_class.return_value
|
||||
simulated_error_message = "Erro de conexão simulado"
|
||||
mock_client_instance.connect.side_effect = Exception(
|
||||
simulated_error_message)
|
||||
simulated_error_message = 'Erro de conexão simulado'
|
||||
mock_client_instance.connect.side_effect = Exception(simulated_error_message)
|
||||
|
||||
opc_manager_instance = raw_opc_manager
|
||||
opc_manager_instance.cert_path = None
|
||||
@@ -207,9 +199,7 @@ async def test_connect_exception_handling_and_metrics(
|
||||
server_name=opc_manager_instance.name,
|
||||
server_url=opc_manager_instance.url,
|
||||
)
|
||||
mock_metrics_module.OPC_CONNECTION_STATUS.labels.return_value.set.assert_called_once_with(
|
||||
0
|
||||
)
|
||||
mock_metrics_module.OPC_CONNECTION_STATUS.labels.return_value.set.assert_called_once_with(0)
|
||||
|
||||
mock_metrics_module.OPC_CONNECTIONS_FAILED.labels.assert_called_once_with(
|
||||
pod_id=opc_manager_instance.pod_id, server_name=opc_manager_instance.name
|
||||
@@ -217,50 +207,48 @@ async def test_connect_exception_handling_and_metrics(
|
||||
mock_metrics_module.OPC_CONNECTIONS_FAILED.labels.return_value.inc.assert_called_once()
|
||||
|
||||
opc_manager_instance.logger.error.assert_called_once_with(
|
||||
f"Failed to connect to {opc_manager_instance.name}: {simulated_error_message}"
|
||||
f'Failed to connect to {opc_manager_instance.name}: {simulated_error_message}'
|
||||
)
|
||||
|
||||
|
||||
@mark.asyncio
|
||||
async def test_create_subscription_no_client(raw_opc_manager):
|
||||
try:
|
||||
await raw_opc_manager.create_subscription("sub1")
|
||||
await raw_opc_manager.create_subscription('sub1')
|
||||
except ValueError as e:
|
||||
assert str(e) == "Client not connected. Call connect first."
|
||||
assert str(e) == 'Client not connected. Call connect first.'
|
||||
else:
|
||||
assert False, "ValueError not raised"
|
||||
assert False, 'ValueError not raised'
|
||||
|
||||
|
||||
@mark.asyncio
|
||||
async def test_create_subscription_success_has_period(opc_manager):
|
||||
await opc_manager.create_subscription("sub1", 1000)
|
||||
await opc_manager.create_subscription('sub1', 1000)
|
||||
|
||||
opc_manager.client.create_subscription.assert_called_once_with(
|
||||
1000, opc_manager)
|
||||
assert opc_manager.subscriptions["sub1"] is not None
|
||||
opc_manager.client.create_subscription.assert_called_once_with(1000, opc_manager)
|
||||
assert opc_manager.subscriptions['sub1'] is not None
|
||||
|
||||
|
||||
@mark.asyncio
|
||||
async def test_create_subscription_success_no_period(opc_manager):
|
||||
await opc_manager.create_subscription("sub1", None)
|
||||
await opc_manager.create_subscription('sub1', None)
|
||||
|
||||
opc_manager.client.create_subscription.assert_called_once_with(
|
||||
500, opc_manager)
|
||||
assert opc_manager.subscriptions["sub1"] is not None
|
||||
opc_manager.client.create_subscription.assert_called_once_with(500, opc_manager)
|
||||
assert opc_manager.subscriptions['sub1'] is not None
|
||||
|
||||
|
||||
@patch("ingestor.managers.opc_manager.metrics")
|
||||
@patch('ingestor.managers.opc_manager.metrics')
|
||||
@mark.asyncio
|
||||
async def test_create_subscription_with_metrics(metrics, opc_manager):
|
||||
await opc_manager.create_subscription("sub1", 1000)
|
||||
await opc_manager.create_subscription('sub1', 1000)
|
||||
|
||||
metrics.OPC_SUBSCRIPTIONS_CREATED.labels.assert_called_once_with(
|
||||
pod_id=opc_manager.pod_id, server_name=opc_manager.name, slot_name="sub1"
|
||||
pod_id=opc_manager.pod_id, server_name=opc_manager.name, slot_name='sub1'
|
||||
)
|
||||
metrics.OPC_SUBSCRIPTIONS_CREATED.labels.return_value.inc.assert_called_once()
|
||||
|
||||
|
||||
@patch("ingestor.managers.opc_manager.metrics")
|
||||
@patch('ingestor.managers.opc_manager.metrics')
|
||||
@mark.asyncio
|
||||
async def test_create_subscription_exception_during_client_call(
|
||||
mock_metrics_module, raw_opc_manager
|
||||
@@ -268,71 +256,66 @@ async def test_create_subscription_exception_during_client_call(
|
||||
opc_manager_instance = raw_opc_manager
|
||||
opc_manager_instance.client = AsyncMock()
|
||||
|
||||
subscription_name = "test_sub_client_error"
|
||||
subscription_name = 'test_sub_client_error'
|
||||
simulated_period = 750
|
||||
simulated_error_message = "Falha ao criar subscrição no cliente OPC"
|
||||
simulated_error_message = 'Falha ao criar subscrição no cliente OPC'
|
||||
|
||||
opc_manager_instance.client.create_subscription.side_effect = Exception(
|
||||
simulated_error_message
|
||||
)
|
||||
opc_manager_instance.client.create_subscription.side_effect = Exception(simulated_error_message)
|
||||
|
||||
with pytest.raises(Exception, match=simulated_error_message):
|
||||
await opc_manager_instance.create_subscription(
|
||||
subscription_name, period=simulated_period
|
||||
)
|
||||
await opc_manager_instance.create_subscription(subscription_name, period=simulated_period)
|
||||
|
||||
opc_manager_instance.client.create_subscription.assert_called_once_with(
|
||||
simulated_period, opc_manager_instance
|
||||
)
|
||||
|
||||
opc_manager_instance.logger.error.assert_called_once_with(
|
||||
f"Failed to create subscription {subscription_name} on {opc_manager_instance.name}: {simulated_error_message}"
|
||||
f'Failed to create subscription {subscription_name} on {opc_manager_instance.name}: {simulated_error_message}'
|
||||
)
|
||||
|
||||
mock_metrics_module.OPC_SUBSCRIPTIONS_CREATED.labels.assert_not_called()
|
||||
|
||||
|
||||
@patch("ingestor.managers.opc_manager.metrics")
|
||||
@patch('ingestor.managers.opc_manager.metrics')
|
||||
@mark.asyncio
|
||||
async def test_subscribe_no_subscription(metrics, opc_manager):
|
||||
try:
|
||||
await opc_manager.subscribe("sub1", tags, 1000)
|
||||
await opc_manager.subscribe('sub1', tags, 1000)
|
||||
except ValueError as e:
|
||||
assert str(
|
||||
e) == "Subscription not created. Call create_subscription first."
|
||||
assert str(e) == 'Subscription not created. Call create_subscription first.'
|
||||
else:
|
||||
assert False, "ValueError not raised"
|
||||
assert False, 'ValueError not raised'
|
||||
metrics.OPC_TAGS_SUBSCRIBED.labels.assert_not_called()
|
||||
|
||||
|
||||
@mark.asyncio
|
||||
async def test_subscribe_success(opc_manager_subscribed):
|
||||
opc_manager_subscribed.client.get_node = MagicMock()
|
||||
opc_manager_subscribed.nodes = {"ns=3;i=1001": "data"}
|
||||
opc_manager_subscribed.nodes = {'ns=3;i=1001': 'data'}
|
||||
|
||||
await opc_manager_subscribed.subscribe("sub1", tags, 1000)
|
||||
await opc_manager_subscribed.subscribe('sub1', tags, 1000)
|
||||
|
||||
assert opc_manager_subscribed.nodes == tags
|
||||
opc_manager_subscribed.subscriptions["sub1"].subscribe_data_change.assert_called_once_with(
|
||||
opc_manager_subscribed.subscriptions['sub1'].subscribe_data_change.assert_called_once_with(
|
||||
[opc_manager_subscribed.client.get_node(n) for n in tags]
|
||||
)
|
||||
|
||||
|
||||
@mark.asyncio
|
||||
async def test_unsubscribe_no_subscription(opc_manager):
|
||||
await opc_manager.unsubscribe("sub1")
|
||||
await opc_manager.unsubscribe('sub1')
|
||||
|
||||
opc_manager.logger.warning.assert_called_once_with(
|
||||
"Subscription 'sub1' not found. Cannot unsubscribe."
|
||||
)
|
||||
assert opc_manager.subscriptions.get("sub1") is None
|
||||
assert opc_manager.subscriptions.get('sub1') is None
|
||||
|
||||
|
||||
@mark.asyncio
|
||||
async def test_unsubscribe_success(opc_manager_subscribed):
|
||||
await opc_manager_subscribed.unsubscribe("sub1")
|
||||
await opc_manager_subscribed.unsubscribe('sub1')
|
||||
|
||||
opc_manager_subscribed.subscriptions.get("sub1") is None
|
||||
opc_manager_subscribed.subscriptions.get('sub1') is None
|
||||
|
||||
|
||||
@mark.asyncio
|
||||
@@ -341,7 +324,7 @@ async def test_disconnect_success(opc_manager_subscribed):
|
||||
|
||||
await opc_manager_subscribed.disconnect()
|
||||
|
||||
opc_manager_subscribed.subscriptions["sub1"].delete.assert_called_once()
|
||||
opc_manager_subscribed.subscriptions['sub1'].delete.assert_called_once()
|
||||
assert opc_manager_subscribed.client is None
|
||||
|
||||
|
||||
@@ -352,42 +335,38 @@ async def test_disconnect_no_client(opc_manager_subscribed):
|
||||
|
||||
opc_manager_subscribed.logger.warning.assert_has_calls(
|
||||
[
|
||||
call("Client already disconnected."),
|
||||
call('Client already disconnected.'),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
@mark.asyncio
|
||||
async def test_disconnect_error_unsubscribe(opc_manager_subscribed):
|
||||
opc_manager_subscribed.client = MagicMock(
|
||||
disconnect=AsyncMock()
|
||||
)
|
||||
opc_manager_subscribed.subscriptions["sub1"] = MagicMock(
|
||||
delete=AsyncMock(side_effect=Exception("Test error"))
|
||||
opc_manager_subscribed.client = MagicMock(disconnect=AsyncMock())
|
||||
opc_manager_subscribed.subscriptions['sub1'] = MagicMock(
|
||||
delete=AsyncMock(side_effect=Exception('Test error'))
|
||||
)
|
||||
|
||||
await opc_manager_subscribed.disconnect()
|
||||
|
||||
opc_manager_subscribed.subscriptions["sub1"].delete.assert_called_once()
|
||||
opc_manager_subscribed.subscriptions['sub1'].delete.assert_called_once()
|
||||
opc_manager_subscribed.client = None
|
||||
opc_manager_subscribed.logger.error.assert_called_once_with(
|
||||
"Failed to clean up subscription: Test error"
|
||||
'Failed to clean up subscription: Test error'
|
||||
)
|
||||
|
||||
|
||||
@mark.asyncio
|
||||
async def test_disconnect_error(opc_manager_subscribed):
|
||||
opc_manager_subscribed.client = MagicMock()
|
||||
opc_manager_subscribed.client.disconnect = MagicMock(
|
||||
side_effect=Exception("Test error")
|
||||
)
|
||||
opc_manager_subscribed.client.disconnect = MagicMock(side_effect=Exception('Test error'))
|
||||
|
||||
await opc_manager_subscribed.disconnect()
|
||||
|
||||
opc_manager_subscribed.subscriptions["sub1"].delete.assert_called_once()
|
||||
opc_manager_subscribed.subscriptions['sub1'].delete.assert_called_once()
|
||||
opc_manager_subscribed.client = None
|
||||
opc_manager_subscribed.logger.error.assert_called_once_with(
|
||||
"Failed to disconnect from OPC UA server: Test error"
|
||||
'Failed to disconnect from OPC UA server: Test error'
|
||||
)
|
||||
|
||||
|
||||
@@ -400,24 +379,21 @@ async def test_disconnect_metrics_on_successful_path(mock_metrics_module, raw_op
|
||||
raw_opc_manager.client = MagicMock()
|
||||
mock_sub1 = MagicMock()
|
||||
mock_sub2 = MagicMock()
|
||||
raw_opc_manager.subscriptions = {"sub1": mock_sub1, "sub2": mock_sub2}
|
||||
raw_opc_manager.subscriptions = {'sub1': mock_sub1, 'sub2': mock_sub2}
|
||||
|
||||
await raw_opc_manager.disconnect()
|
||||
|
||||
mock_metrics_module.OPC_CONNECTION_STATUS.labels.assert_called_once_with(
|
||||
pod_id=raw_opc_manager.pod_id,
|
||||
server_name=raw_opc_manager.name,
|
||||
server_url=raw_opc_manager.url
|
||||
server_url=raw_opc_manager.url,
|
||||
)
|
||||
mock_metrics_module.OPC_CONNECTION_STATUS.labels.return_value.set.assert_called_once_with(
|
||||
0)
|
||||
mock_metrics_module.OPC_CONNECTION_STATUS.labels.return_value.set.assert_called_once_with(0)
|
||||
|
||||
mock_metrics_module.OPC_TAGS_SUBSCRIBED.labels.assert_called_once_with(
|
||||
pod_id=raw_opc_manager.pod_id,
|
||||
server_name=raw_opc_manager.name
|
||||
pod_id=raw_opc_manager.pod_id, server_name=raw_opc_manager.name
|
||||
)
|
||||
mock_metrics_module.OPC_TAGS_SUBSCRIBED.labels.return_value.set.assert_called_once_with(
|
||||
0)
|
||||
mock_metrics_module.OPC_TAGS_SUBSCRIBED.labels.return_value.set.assert_called_once_with(0)
|
||||
|
||||
|
||||
@patch('ingestor.managers.opc_manager.metrics')
|
||||
@@ -427,76 +403,70 @@ async def test_datachange_notification(metrics, opc_manager_subscribed):
|
||||
monitored_item=MagicMock(
|
||||
Value=MagicMock(
|
||||
Value=MagicMock(Value=42),
|
||||
SourceTimestamp=datetime.strptime(
|
||||
"2021-01-01T00:00:00", "%Y-%m-%dT%H:%M:%S"
|
||||
),
|
||||
SourceTimestamp=datetime.strptime('2021-01-01T00:00:00', '%Y-%m-%dT%H:%M:%S'),
|
||||
)
|
||||
)
|
||||
)
|
||||
opc_manager_subscribed.nodes = {
|
||||
"ns=3;i=1001": {
|
||||
"tag_name": "Counter",
|
||||
"cycle_rule": {"cycle_increment": 1.0, "cycle_count": 2},
|
||||
"topics": ["topic1", "topic2"],
|
||||
'ns=3;i=1001': {
|
||||
'tag_name': 'Counter',
|
||||
'cycle_rule': {'cycle_increment': 1.0, 'cycle_count': 2},
|
||||
'topics': ['topic1', 'topic2'],
|
||||
}
|
||||
}
|
||||
|
||||
metrics.OPC_CYCLES_WITHOUT_DATA.reset_mock()
|
||||
|
||||
await opc_manager_subscribed.datachange_notification("ns=3;i=1001", None, data)
|
||||
await opc_manager_subscribed.datachange_notification('ns=3;i=1001', None, data)
|
||||
|
||||
opc_manager_subscribed.data_manager.publish.assert_any_call(
|
||||
"topic1",
|
||||
'topic1',
|
||||
{
|
||||
"tag": "ns=3;i=1001",
|
||||
"name": "Counter",
|
||||
"timestamp": "2021-01-01 00:00:00-0300",
|
||||
"value": 42,
|
||||
'tag': 'ns=3;i=1001',
|
||||
'name': 'Counter',
|
||||
'timestamp': '2021-01-01 00:00:00-0300',
|
||||
'value': 42,
|
||||
},
|
||||
)
|
||||
opc_manager_subscribed.data_manager.publish.assert_any_call(
|
||||
"topic2",
|
||||
'topic2',
|
||||
{
|
||||
"tag": "ns=3;i=1001",
|
||||
"name": "Counter",
|
||||
"timestamp": "2021-01-01 00:00:00-0300",
|
||||
"value": 42,
|
||||
'tag': 'ns=3;i=1001',
|
||||
'name': 'Counter',
|
||||
'timestamp': '2021-01-01 00:00:00-0300',
|
||||
'value': 42,
|
||||
},
|
||||
)
|
||||
assert opc_manager_subscribed.nodes["ns=3;i=1001"]["cycle_rule"]["cycle_count"] == 0
|
||||
assert opc_manager_subscribed.nodes['ns=3;i=1001']['cycle_rule']['cycle_count'] == 0
|
||||
|
||||
metrics.OPC_CYCLES_WITHOUT_DATA.labels.assert_called_once_with(
|
||||
pod_id=opc_manager_subscribed.pod_id,
|
||||
server_name=opc_manager_subscribed.name
|
||||
pod_id=opc_manager_subscribed.pod_id, server_name=opc_manager_subscribed.name
|
||||
)
|
||||
metrics.OPC_CYCLES_WITHOUT_DATA.labels.return_value.set.assert_called_once_with(
|
||||
0)
|
||||
metrics.OPC_CYCLES_WITHOUT_DATA.labels.return_value.set.assert_called_once_with(0)
|
||||
|
||||
|
||||
def test_check_cycles_no_notification(opc_manager):
|
||||
# Setup: node with cycle_count just below threshold
|
||||
opc_manager.nodes = {
|
||||
"ns=3;i=1001": {
|
||||
"tag_name": "Counter",
|
||||
"cycle_rule": {"cycle_increment": 1.0, "cycle_count": 3.0},
|
||||
'ns=3;i=1001': {
|
||||
'tag_name': 'Counter',
|
||||
'cycle_rule': {'cycle_increment': 1.0, 'cycle_count': 3.0},
|
||||
}
|
||||
}
|
||||
|
||||
opc_manager.check_cycles()
|
||||
|
||||
# After one increment, cycle_count = 4.0, still below threshold
|
||||
assert opc_manager.nodes["ns=3;i=1001"]["cycle_rule"][
|
||||
"cycle_count"
|
||||
] == pytest.approx(4.0)
|
||||
assert opc_manager.nodes['ns=3;i=1001']['cycle_rule']['cycle_count'] == pytest.approx(4.0)
|
||||
opc_manager.send_notification.assert_not_called()
|
||||
|
||||
|
||||
def test_check_cycles_triggers_notification(opc_manager):
|
||||
# Setup: node with cycle_count just below threshold, increment will cross threshold
|
||||
opc_manager.nodes = {
|
||||
"ns=3;i=1001": {
|
||||
"tag_name": "Counter",
|
||||
"cycle_rule": {"cycle_increment": 2.5, "cycle_count": 3.0},
|
||||
'ns=3;i=1001': {
|
||||
'tag_name': 'Counter',
|
||||
'cycle_rule': {'cycle_increment': 2.5, 'cycle_count': 3.0},
|
||||
}
|
||||
}
|
||||
opc_manager.notification_handler.build_and_send_notification = MagicMock()
|
||||
@@ -504,15 +474,13 @@ def test_check_cycles_triggers_notification(opc_manager):
|
||||
opc_manager.check_cycles()
|
||||
|
||||
# After increment, cycle_count = 5.5, should trigger notification
|
||||
assert opc_manager.nodes["ns=3;i=1001"]["cycle_rule"][
|
||||
"cycle_count"
|
||||
] == pytest.approx(5.5)
|
||||
assert opc_manager.nodes['ns=3;i=1001']['cycle_rule']['cycle_count'] == pytest.approx(5.5)
|
||||
opc_manager.send_notification.assert_called_once_with(
|
||||
notification_id="TAG_ns=3;i=1001:Counter_LISTENNING_STOPPED",
|
||||
message="5.5 cycles without receive from ns=3;i=1001:Counter",
|
||||
block="opc_manager",
|
||||
notification_id='TAG_ns=3;i=1001:Counter_LISTENNING_STOPPED',
|
||||
message='5.5 cycles without receive from ns=3;i=1001:Counter',
|
||||
block='opc_manager',
|
||||
level=NotificationLevel.WARNING,
|
||||
metadata=metadata["metadata"],
|
||||
metadata=metadata['metadata'],
|
||||
)
|
||||
|
||||
|
||||
@@ -530,11 +498,11 @@ def test_check_opc_listenning_no_notification(metrics, opc_manager):
|
||||
assert result is False
|
||||
|
||||
metrics.OPC_CYCLES_WITHOUT_DATA.labels.assert_called_once_with(
|
||||
pod_id=opc_manager.pod_id,
|
||||
server_name=opc_manager.name
|
||||
pod_id=opc_manager.pod_id, server_name=opc_manager.name
|
||||
)
|
||||
metrics.OPC_CYCLES_WITHOUT_DATA.labels.return_value.set.assert_called_once_with(
|
||||
opc_manager.non_receive_count)
|
||||
opc_manager.non_receive_count
|
||||
)
|
||||
metrics.OPC_RECONNECTIONS_TOTAL.labels.assert_not_called()
|
||||
|
||||
|
||||
@@ -545,11 +513,11 @@ def test_check_opc_listenning_warning_notification(opc_manager):
|
||||
|
||||
assert opc_manager.non_receive_count == 5
|
||||
opc_manager.send_notification.assert_called_once_with(
|
||||
notification_id=f"OPC_LISTENNING_STOPPED__{opc_manager.name}",
|
||||
message=f"5 cycles without receive from OPC {opc_manager.name}. Tags: {json.dumps(opc_manager.nodes)}",
|
||||
block="opc_manager",
|
||||
notification_id=f'OPC_LISTENNING_STOPPED__{opc_manager.name}',
|
||||
message=f'5 cycles without receive from OPC {opc_manager.name}. Tags: {json.dumps(opc_manager.nodes)}',
|
||||
block='opc_manager',
|
||||
level=NotificationLevel.ERROR,
|
||||
metadata=metadata["metadata"],
|
||||
metadata=metadata['metadata'],
|
||||
)
|
||||
assert result is False
|
||||
|
||||
@@ -569,47 +537,45 @@ def test_check_opc_listenning_error_notification_and_retry(metrics, opc_manager)
|
||||
calls = opc_manager.send_notification.call_args_list
|
||||
# First call: 5 cycles warning
|
||||
assert calls[0].kwargs == {
|
||||
"notification_id": f"OPC_LISTENNING_STOPPED__{opc_manager.name}",
|
||||
"message": f"15 cycles without receive from OPC {opc_manager.name}. Tags: {json.dumps(opc_manager.nodes)}",
|
||||
"block": "opc_manager",
|
||||
"level": NotificationLevel.ERROR,
|
||||
"metadata": metadata["metadata"],
|
||||
'notification_id': f'OPC_LISTENNING_STOPPED__{opc_manager.name}',
|
||||
'message': f'15 cycles without receive from OPC {opc_manager.name}. Tags: {json.dumps(opc_manager.nodes)}',
|
||||
'block': 'opc_manager',
|
||||
'level': NotificationLevel.ERROR,
|
||||
'metadata': metadata['metadata'],
|
||||
}
|
||||
# Second call: 15 cycles retry
|
||||
assert calls[1].kwargs == {
|
||||
"notification_id": f"OPC_CONNECTION_RETRY__{opc_manager.name}",
|
||||
"message": f"Retrying to connect to server {opc_manager.name}",
|
||||
"block": "opc_manager",
|
||||
"level": NotificationLevel.ERROR,
|
||||
"metadata": metadata["metadata"],
|
||||
'notification_id': f'OPC_CONNECTION_RETRY__{opc_manager.name}',
|
||||
'message': f'Retrying to connect to server {opc_manager.name}',
|
||||
'block': 'opc_manager',
|
||||
'level': NotificationLevel.ERROR,
|
||||
'metadata': metadata['metadata'],
|
||||
}
|
||||
assert result is True
|
||||
|
||||
metrics.OPC_CYCLES_WITHOUT_DATA.labels.assert_called_once_with(
|
||||
pod_id=opc_manager.pod_id,
|
||||
server_name=opc_manager.name
|
||||
pod_id=opc_manager.pod_id, server_name=opc_manager.name
|
||||
)
|
||||
metrics.OPC_CYCLES_WITHOUT_DATA.labels.return_value.set.assert_called_once_with(
|
||||
opc_manager.non_receive_count)
|
||||
opc_manager.non_receive_count
|
||||
)
|
||||
|
||||
metrics.OPC_RECONNECTIONS_TOTAL.labels.assert_called_once_with(
|
||||
pod_id=opc_manager.pod_id,
|
||||
server_name=opc_manager.name
|
||||
pod_id=opc_manager.pod_id, server_name=opc_manager.name
|
||||
)
|
||||
metrics.OPC_RECONNECTIONS_TOTAL.labels.return_value.inc.assert_called_once()
|
||||
|
||||
|
||||
@patch("ingestor.managers.opc_manager.metrics")
|
||||
@patch('ingestor.managers.opc_manager.metrics')
|
||||
def test_init_metrics_calls_correct_metric_methods(metrics):
|
||||
|
||||
opc_manager = OpcManager(
|
||||
name="TestInitConnector",
|
||||
url="opc.tcp://init.test:4840",
|
||||
name='TestInitConnector',
|
||||
url='opc.tcp://init.test:4840',
|
||||
data_manager=MagicMock(),
|
||||
logger=MagicMock(),
|
||||
server_uri="opc.tcp://init.test:4840/uri",
|
||||
server_uri='opc.tcp://init.test:4840/uri',
|
||||
notification_handler=MagicMock(),
|
||||
metadata=metadata["metadata"],
|
||||
metadata=metadata['metadata'],
|
||||
)
|
||||
|
||||
metrics.OPC_CONNECTION_STATUS.labels.assert_called_with(
|
||||
@@ -618,13 +584,10 @@ def test_init_metrics_calls_correct_metric_methods(metrics):
|
||||
server_url=opc_manager.url,
|
||||
)
|
||||
|
||||
metrics.OPC_CONNECTION_STATUS.labels.return_value.set.assert_called_once_with(
|
||||
0)
|
||||
metrics.OPC_CONNECTION_STATUS.labels.return_value.set.assert_called_once_with(0)
|
||||
|
||||
metrics.OPC_TAGS_SUBSCRIBED.labels.assert_called_once_with(
|
||||
pod_id=opc_manager.pod_id,
|
||||
server_name=opc_manager.name
|
||||
pod_id=opc_manager.pod_id, server_name=opc_manager.name
|
||||
)
|
||||
|
||||
metrics.OPC_TAGS_SUBSCRIBED.labels.return_value.set.assert_called_once_with(
|
||||
0)
|
||||
metrics.OPC_TAGS_SUBSCRIBED.labels.return_value.set.assert_called_once_with(0)
|
||||
|
||||
Reference in New Issue
Block a user