Files
aitbc/apps/monitor/tests/test_unit_monitor.py
aitbc e60cc3226c
Some checks failed
API Endpoint Tests / test-api-endpoints (push) Successful in 9s
Blockchain Synchronization Verification / sync-verification (push) Failing after 1s
CLI Tests / test-cli (push) Failing after 3s
Documentation Validation / validate-docs (push) Successful in 6s
Documentation Validation / validate-policies-strict (push) Successful in 2s
Integration Tests / test-service-integration (push) Successful in 40s
Multi-Node Blockchain Health Monitoring / health-check (push) Successful in 1s
P2P Network Verification / p2p-verification (push) Successful in 2s
Production Tests / Production Integration Tests (push) Successful in 21s
Python Tests / test-python (push) Successful in 13s
Security Scanning / security-scan (push) Failing after 46s
Smart Contract Tests / test-solidity (map[name:aitbc-token path:packages/solidity/aitbc-token]) (push) Successful in 17s
Smart Contract Tests / lint-solidity (push) Successful in 10s
Add sys import to test files and remove obsolete integration tests
- Add sys import to 29 test files across agent-coordinator, blockchain-event-bridge, blockchain-node, and coordinator-api
- Remove apps/blockchain-event-bridge/tests/test_integration.py (obsolete bridge integration tests)
- Remove apps/coordinator-api/tests/test_integration.py (obsolete API integration tests)
- Implement GPU registration in marketplace_gpu.py with GPURegistry model persistence
2026-04-23 16:43:17 +02:00

109 lines
3.9 KiB
Python

"""Unit tests for monitor service"""
import sys
import pytest
import sys
from unittest.mock import Mock, patch, MagicMock, mock_open
from pathlib import Path
import json
# Create a proper psutil mock with Error exception class
class PsutilError(Exception):
pass
mock_psutil = MagicMock()
mock_psutil.cpu_percent = Mock(return_value=45.5)
mock_psutil.virtual_memory = Mock(return_value=MagicMock(percent=60.2))
mock_psutil.Error = PsutilError
sys.modules['psutil'] = mock_psutil
import monitor
@pytest.mark.unit
def test_main_system_stats_logging():
"""Test that system stats are logged correctly"""
with patch('monitor.logging') as mock_logging, \
patch('monitor.time.sleep', side_effect=KeyboardInterrupt), \
patch('monitor.Path') as mock_path:
mock_path.return_value.exists.return_value = False
logger = mock_logging.getLogger.return_value
mock_logging.basicConfig.return_value = None
try:
monitor.main()
except KeyboardInterrupt:
pass
# Verify system stats were logged
assert logger.info.call_count >= 1
system_call = logger.info.call_args_list[0]
assert 'CPU 45.5%' in str(system_call)
assert 'Memory 60.2%' in str(system_call)
@pytest.mark.unit
def test_main_blockchain_stats_logging():
"""Test that blockchain stats are logged when file exists"""
with patch('monitor.logging') as mock_logging, \
patch('monitor.time.sleep', side_effect=KeyboardInterrupt), \
patch('monitor.Path') as mock_path, \
patch('builtins.open', mock_open(read_data='{"blocks": [{"height": 1}, {"height": 2}]}')):
# Mock blockchain file exists
blockchain_path = Mock()
blockchain_path.exists.return_value = True
marketplace_path = Mock()
marketplace_path.exists.return_value = False
mock_path.side_effect = lambda x: blockchain_path if 'blockchain' in str(x) else marketplace_path
logger = mock_logging.getLogger.return_value
mock_logging.basicConfig.return_value = None
try:
monitor.main()
except KeyboardInterrupt:
pass
# Verify blockchain stats were logged
blockchain_calls = [call for call in logger.info.call_args_list if 'Blockchain' in str(call)]
assert len(blockchain_calls) > 0
assert '2 blocks' in str(blockchain_calls[0])
@pytest.mark.unit
def test_main_marketplace_stats_logging():
"""Test that marketplace stats are logged when file exists"""
with patch('monitor.logging') as mock_logging, \
patch('monitor.time.sleep', side_effect=KeyboardInterrupt), \
patch('monitor.Path') as mock_path, \
patch('builtins.open', mock_open(read_data='[{"id": 1, "gpu": "rtx3080"}, {"id": 2, "gpu": "rtx3090"}]')):
# Mock blockchain file doesn't exist, marketplace does
blockchain_path = Mock()
blockchain_path.exists.return_value = False
marketplace_path = Mock()
marketplace_path.exists.return_value = True
listings_file = Mock()
listings_file.exists.return_value = True
listings_file.__truediv__ = Mock(return_value=listings_file)
marketplace_path.__truediv__ = Mock(return_value=listings_file)
mock_path.side_effect = lambda x: listings_file if 'gpu_listings' in str(x) else (marketplace_path if 'marketplace' in str(x) else blockchain_path)
logger = mock_logging.getLogger.return_value
mock_logging.basicConfig.return_value = None
try:
monitor.main()
except KeyboardInterrupt:
pass
# Verify marketplace stats were logged
marketplace_calls = [call for call in logger.info.call_args_list if 'Marketplace' in str(call)]
assert len(marketplace_calls) > 0
assert '2 GPU listings' in str(marketplace_calls[0])