Files
inventory-collector/tests/test_config.py
T
soda64 598e5d7397
Build and Publish Release / Build Packages and Publish (push) Successful in 9s
feat: release v1.1.0 - clean YAML, wildcard DNS protection, and tag/group/source filtering
2026-10-03 11:41:25 +02:00

68 lines
2.6 KiB
Python

import os
import unittest
from pathlib import Path
from tempfile import NamedTemporaryFile
from inventory_collector.config import ConfigLoader, AppConfig
class TestConfigLoader(unittest.TestCase):
def test_resolve_env_vars(self):
os.environ["TEST_PVE_HOST"] = "pve.wired.intra"
os.environ["TEST_SECRET"] = "secret123"
template = "https://${TEST_PVE_HOST}:8006/token=${TEST_SECRET}"
resolved = ConfigLoader.resolve_env_vars(template)
self.assertEqual(resolved, "https://pve.wired.intra:8006/token=secret123")
def test_load_config_file(self):
os.environ["ENV_PVE_URL"] = "https://pve-01.test:8006"
raw_config = """
sources:
proxmox:
enabled: true
url: "${ENV_PVE_URL}"
token_id: "root@pam!test"
token_secret: "uuid-secret"
verify_ssl: false
static_files:
- path: "static_hosts.ini"
required: false
destinations:
ansible: "inventory/hosts.yaml"
linux_connector: "~/.config/linux-connector/hosts.yaml"
"""
with NamedTemporaryFile("w+", suffix=".yaml", delete=False) as f:
f.write(raw_config)
cfg_path = Path(f.name)
try:
config = ConfigLoader.load(cfg_path)
self.assertTrue(config.proxmox.enabled)
self.assertEqual(config.proxmox.url, "https://pve-01.test:8006")
self.assertEqual(config.proxmox.token_id, "root@pam!test")
self.assertFalse(config.proxmox.verify_ssl)
self.assertEqual(len(config.static_files), 1)
self.assertEqual(config.static_files[0].path, Path("static_hosts.ini"))
self.assertEqual(config.output_ansible, Path("inventory/hosts.yaml"))
finally:
cfg_path.unlink()
def test_load_env_files(self):
with NamedTemporaryFile("w+", suffix=".env", delete=False) as f:
f.write("TEST_ENV_VAR_COLLECTOR=dynamic_val\nexport TEST_ANOTHER=exported_val\n")
env_file = Path(f.name)
try:
# Test direct parsing through _load_env_files candidate injection
with unittest.mock.patch("inventory_collector.config.Path.is_file") as mock_is_file, \
unittest.mock.patch("inventory_collector.config.Path.read_text") as mock_read:
mock_is_file.return_value = True
mock_read.return_value = "TEST_AUTO_LOADED_KEY=success_val\n"
ConfigLoader._load_env_files()
self.assertEqual(os.environ.get("TEST_AUTO_LOADED_KEY"), "success_val")
finally:
env_file.unlink()
if __name__ == "__main__":
unittest.main()