Files
qbitcheck/test_enhanced_logging.py
2025-11-10 06:56:40 -08:00

65 lines
1.9 KiB
Python

#!/usr/bin/env python3
"""
Test script to verify the enhanced logging for different failure scenarios
"""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from checker import ConnectionMonitor
import logging
# Configure logging for testing
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
def test_dns_failure():
"""Test DNS resolution failure scenario"""
print("Testing DNS failure scenario...")
monitor = ConnectionMonitor(
qbittorrent_url='http://nonexistent-host-that-will-fail:8080',
nomad_url='http://127.0.0.1:4646',
tracker_name='test-tracker',
consul_url='http://127.0.0.1:8500'
)
result = monitor.get_connection_status()
print(f"DNS failure result: {result}")
# Test state determination
state = monitor._determine_connection_state(result)
print(f"Connection state: {state}")
return True
def test_invalid_response():
"""Test scenario where API returns invalid response"""
print("\nTesting invalid response scenario...")
monitor = ConnectionMonitor(
qbittorrent_url='http://127.0.0.1:8080', # This might work but return bad data
nomad_url='http://127.0.0.1:4646',
tracker_name='test-tracker',
consul_url='http://127.0.0.1:8500'
)
# Simulate a bad response (no connection_status or dht_nodes)
bad_response = {'some_other_field': 'value'}
state = monitor._determine_connection_state(bad_response)
print(f"Invalid response state: {state}")
# Simulate connected but no DHT nodes
no_dht_response = {'connection_status': 'connected', 'dht_nodes': 0}
state = monitor._determine_connection_state(no_dht_response)
print(f"Connected but no DHT state: {state}")
return True
if __name__ == '__main__':
test_dns_failure()
test_invalid_response()