176 lines
5.1 KiB
Python
Executable File
176 lines
5.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""
|
|
Test script for the is_hostname_unknown function.
|
|
|
|
This script tests the is_hostname_unknown function with various hostnames
|
|
to ensure it correctly identifies hostnames that match unknown_device_patterns.
|
|
"""
|
|
|
|
import sys
|
|
import logging
|
|
from TasmotaManager import TasmotaDiscovery
|
|
|
|
# Configure logging
|
|
logging.basicConfig(level=logging.INFO,
|
|
format='%(asctime)s - %(levelname)s - %(message)s',
|
|
datefmt='%Y-%m-%d %H:%M:%S')
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Configuration file path
|
|
CONFIG_FILE = "network_configuration.json"
|
|
|
|
def test_with_config_patterns():
|
|
"""Test is_hostname_unknown with patterns from the configuration."""
|
|
logger.info("Testing is_hostname_unknown with patterns from configuration")
|
|
|
|
# Create TasmotaDiscovery instance
|
|
manager = TasmotaDiscovery(debug=True)
|
|
manager.load_config(CONFIG_FILE)
|
|
|
|
# Get the patterns from the configuration for reference
|
|
patterns = []
|
|
network_filters = manager.config['unifi'].get('network_filter', {})
|
|
for network in network_filters.values():
|
|
patterns.extend(network.get('unknown_device_patterns', []))
|
|
|
|
logger.info(f"Patterns from configuration: {patterns}")
|
|
|
|
# Test cases that should match
|
|
match_cases = [
|
|
"tasmota_device123",
|
|
"tasmota-light",
|
|
"esp-abcd1234",
|
|
"ESP-ABCDEF",
|
|
"tasmota_switch_kitchen"
|
|
]
|
|
|
|
# Test cases that should not match
|
|
no_match_cases = [
|
|
"my_device",
|
|
"kitchen_light",
|
|
"living_room_switch",
|
|
"bedroom_lamp",
|
|
"office_fan"
|
|
]
|
|
|
|
# Test matching cases
|
|
logger.info("Testing hostnames that should match:")
|
|
for hostname in match_cases:
|
|
result = manager.is_hostname_unknown(hostname)
|
|
logger.info(f" {hostname}: {result}")
|
|
if not result:
|
|
logger.error(f" ERROR: {hostname} should match but doesn't")
|
|
|
|
# Test non-matching cases
|
|
logger.info("Testing hostnames that should not match:")
|
|
for hostname in no_match_cases:
|
|
result = manager.is_hostname_unknown(hostname)
|
|
logger.info(f" {hostname}: {result}")
|
|
if result:
|
|
logger.error(f" ERROR: {hostname} should not match but does")
|
|
|
|
def test_with_custom_patterns():
|
|
"""Test is_hostname_unknown with custom patterns."""
|
|
logger.info("Testing is_hostname_unknown with custom patterns")
|
|
|
|
# Create TasmotaDiscovery instance
|
|
manager = TasmotaDiscovery(debug=True)
|
|
manager.load_config(CONFIG_FILE)
|
|
|
|
# Define custom patterns
|
|
custom_patterns = [
|
|
"test-*",
|
|
"custom_*",
|
|
"special-device"
|
|
]
|
|
|
|
logger.info(f"Custom patterns: {custom_patterns}")
|
|
|
|
# Test cases that should match
|
|
match_cases = [
|
|
"test-device",
|
|
"custom_light",
|
|
"special-device",
|
|
"test-abcd1234",
|
|
"custom_switch_kitchen"
|
|
]
|
|
|
|
# Test cases that should not match
|
|
no_match_cases = [
|
|
"my_device",
|
|
"kitchen_light",
|
|
"living_room_switch",
|
|
"bedroom_lamp",
|
|
"office_fan"
|
|
]
|
|
|
|
# Test matching cases
|
|
logger.info("Testing hostnames that should match:")
|
|
for hostname in match_cases:
|
|
result = manager.is_hostname_unknown(hostname, custom_patterns)
|
|
logger.info(f" {hostname}: {result}")
|
|
if not result:
|
|
logger.error(f" ERROR: {hostname} should match but doesn't")
|
|
|
|
# Test non-matching cases
|
|
logger.info("Testing hostnames that should not match:")
|
|
for hostname in no_match_cases:
|
|
result = manager.is_hostname_unknown(hostname, custom_patterns)
|
|
logger.info(f" {hostname}: {result}")
|
|
if result:
|
|
logger.error(f" ERROR: {hostname} should not match but does")
|
|
|
|
def test_case_insensitivity():
|
|
"""Test that is_hostname_unknown is case-insensitive."""
|
|
logger.info("Testing case insensitivity")
|
|
|
|
# Create TasmotaDiscovery instance
|
|
manager = TasmotaDiscovery(debug=True)
|
|
manager.load_config(CONFIG_FILE)
|
|
|
|
# Define custom patterns with mixed case
|
|
custom_patterns = [
|
|
"Test-*",
|
|
"CUSTOM_*",
|
|
"Special-Device"
|
|
]
|
|
|
|
logger.info(f"Custom patterns with mixed case: {custom_patterns}")
|
|
|
|
# Test cases with different case
|
|
test_cases = [
|
|
"TEST-DEVICE",
|
|
"test-device",
|
|
"Test-Device",
|
|
"CUSTOM_LIGHT",
|
|
"custom_light",
|
|
"Custom_Light",
|
|
"SPECIAL-DEVICE",
|
|
"special-device",
|
|
"Special-Device"
|
|
]
|
|
|
|
# Test all cases
|
|
logger.info("Testing case insensitivity:")
|
|
for hostname in test_cases:
|
|
result = manager.is_hostname_unknown(hostname, custom_patterns)
|
|
logger.info(f" {hostname}: {result}")
|
|
if not result:
|
|
logger.error(f" ERROR: {hostname} should match but doesn't")
|
|
|
|
def main():
|
|
"""Run all tests."""
|
|
logger.info("Starting tests for is_hostname_unknown function")
|
|
|
|
# Run tests
|
|
test_with_config_patterns()
|
|
print("\n")
|
|
test_with_custom_patterns()
|
|
print("\n")
|
|
test_case_insensitivity()
|
|
|
|
logger.info("All tests completed")
|
|
return 0
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main()) |