Files
aitbc/apps/monitor/tests/test_edge_cases_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

217 lines
7.8 KiB
Python

"""Edge case and error handling 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_json_decode_error_handling():
"""Test JSON decode error is handled correctly"""
with patch('monitor.logging') as mock_logging, \
patch('monitor.time.sleep', side_effect=[None, KeyboardInterrupt]), \
patch('monitor.Path') as mock_path, \
patch('builtins.open', mock_open(read_data='invalid json{')):
# 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 error was logged
error_calls = [call for call in logger.error.call_args_list if 'JSONDecodeError' in str(call)]
assert len(error_calls) > 0
@pytest.mark.unit
def test_file_not_found_error_handling():
"""Test FileNotFoundError is handled correctly"""
with patch('monitor.logging') as mock_logging, \
patch('monitor.time.sleep', side_effect=[None, KeyboardInterrupt]), \
patch('monitor.Path') as mock_path, \
patch('builtins.open', side_effect=FileNotFoundError("File not found")):
# 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 error was logged
error_calls = [call for call in logger.error.call_args_list if 'FileNotFoundError' in str(call)]
assert len(error_calls) > 0
@pytest.mark.unit
def test_permission_error_handling():
"""Test PermissionError is handled correctly"""
with patch('monitor.logging') as mock_logging, \
patch('monitor.time.sleep', side_effect=[None, KeyboardInterrupt]), \
patch('monitor.Path') as mock_path, \
patch('builtins.open', side_effect=PermissionError("Permission denied")):
# 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 error was logged
error_calls = [call for call in logger.error.call_args_list if 'PermissionError' in str(call)]
assert len(error_calls) > 0
@pytest.mark.unit
def test_io_error_handling():
"""Test IOError is handled correctly"""
with patch('monitor.logging') as mock_logging, \
patch('monitor.time.sleep', side_effect=[None, KeyboardInterrupt]), \
patch('monitor.Path') as mock_path, \
patch('builtins.open', side_effect=IOError("I/O error")):
# 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 error was logged
error_calls = [call for call in logger.error.call_args_list if 'IOError' in str(call) or 'OSError' in str(call)]
assert len(error_calls) > 0
@pytest.mark.unit
def test_psutil_error_handling():
"""Test psutil.Error is handled correctly"""
with patch('monitor.logging') as mock_logging, \
patch('monitor.time.sleep', side_effect=[None, KeyboardInterrupt]), \
patch('monitor.psutil.cpu_percent', side_effect=PsutilError("psutil error")):
logger = mock_logging.getLogger.return_value
mock_logging.basicConfig.return_value = None
try:
monitor.main()
except KeyboardInterrupt:
pass
# Verify error was logged
error_calls = [call for call in logger.error.call_args_list if 'psutil error' in str(call)]
assert len(error_calls) > 0
@pytest.mark.unit
def test_empty_blocks_array():
"""Test handling of empty blocks array in blockchain data"""
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": []}')):
# 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 with 0 blocks
blockchain_calls = [call for call in logger.info.call_args_list if 'Blockchain' in str(call)]
assert len(blockchain_calls) > 0
assert '0 blocks' in str(blockchain_calls[0])
@pytest.mark.unit
def test_missing_blocks_key():
"""Test handling of missing blocks key in blockchain data"""
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='{"height": 100}')):
# 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 with 0 blocks (default)
blockchain_calls = [call for call in logger.info.call_args_list if 'Blockchain' in str(call)]
assert len(blockchain_calls) > 0
assert '0 blocks' in str(blockchain_calls[0])