from pathlib import Path import pytest from exporter.config import Config VALID_FERNET_KEY = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=" # 44 chars base64 -> 32 bytes def test_config_loads_global_fields(tmp_path: Path): yaml_content = f""" global: http_listen: "0.0.0.0:9100" scrape_interval_seconds: 60 netconf_port: 830 connect_timeout_seconds: 5 rpc_timeout_seconds: 10 max_workers: 10 api_token: "changeme" runtime_db_path: "./devices.db" password_secret: "{VALID_FERNET_KEY}" ssh_keepalive_seconds: 30 failure_threshold: 3 max_backoff_factor: 8 shutdown_timeout_seconds: 30 log_level: INFO log_to_stdout: true log_file: "" log_file_max_bytes: 10485760 log_file_backup_count: 5 """ cfg_file = tmp_path / "config.yaml" cfg_file.write_text(yaml_content) cfg = Config.from_file(cfg_file) g = cfg.global_ assert g.http_listen == "0.0.0.0:9100" assert g.scrape_interval_seconds == 60 assert g.rpc_timeout_seconds == 10 assert g.runtime_db_path == "./devices.db" assert g.password_secret == VALID_FERNET_KEY assert g.ssh_keepalive_seconds == 30 assert g.failure_threshold == 3 assert g.max_backoff_factor == 8 assert g.shutdown_timeout_seconds == 30 def test_config_invalid_fernet_key_raises(): data = { "global": { "runtime_db_path": "./devices.db", "password_secret": "too-short", } } with pytest.raises(ValueError, match="Invalid Fernet key"): Config.from_dict(data) def test_shutdown_timeout_too_small_warns(): data = { "global": { "http_listen": "0.0.0.0:9100", "scrape_interval_seconds": 60, "netconf_port": 830, "connect_timeout_seconds": 5, "rpc_timeout_seconds": 20, "max_workers": 10, "api_token": "changeme", "runtime_db_path": "./devices.db", "password_secret": VALID_FERNET_KEY, "ssh_keepalive_seconds": 30, "failure_threshold": 3, "max_backoff_factor": 8, "shutdown_timeout_seconds": 10, "log_level": "INFO", "log_to_stdout": True, "log_file": "", "log_file_max_bytes": 10485760, "log_file_backup_count": 5, } } with pytest.warns(UserWarning): Config.from_dict(data)