feat(slice-6): implement config loader, CLI check mode, comprehensive documentation, and shell completions
This commit is contained in:
1 parent
869fd241e7
commit
e5784db650
9 files changed
+616
-29
No files matched your search
@@ -0,0 +1,63 @@
|
||||
import json
|
||||
import sys
|
||||
import unittest
|
||||
from io import StringIO
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
from inventory_collector.cli import main
|
||||
|
||||
class TestCliIntegration(unittest.TestCase):
|
||||
def test_cli_dry_run_inspection(self):
|
||||
stdout_capture = StringIO()
|
||||
stderr_capture = StringIO()
|
||||
old_stdout, old_stderr = sys.stdout, sys.stderr
|
||||
try:
|
||||
sys.stdout = stdout_capture
|
||||
sys.stderr = stderr_capture
|
||||
code = main(["--check"])
|
||||
self.assertEqual(code, 0)
|
||||
out = stdout_capture.getvalue()
|
||||
self.assertIn("NAME", out)
|
||||
self.assertIn("IP ADDRESS", out)
|
||||
self.assertIn("k3s-master-0", out)
|
||||
finally:
|
||||
sys.stdout = old_stdout
|
||||
sys.stderr = old_stderr
|
||||
|
||||
def test_cli_dynamic_json_list(self):
|
||||
stdout_capture = StringIO()
|
||||
stderr_capture = StringIO()
|
||||
old_stdout, old_stderr = sys.stdout, sys.stderr
|
||||
try:
|
||||
sys.stdout = stdout_capture
|
||||
sys.stderr = stderr_capture
|
||||
code = main(["--list"])
|
||||
self.assertEqual(code, 0)
|
||||
parsed = json.loads(stdout_capture.getvalue())
|
||||
self.assertIn("_meta", parsed)
|
||||
self.assertIn("hostvars", parsed["_meta"])
|
||||
finally:
|
||||
sys.stdout = old_stdout
|
||||
sys.stderr = old_stderr
|
||||
|
||||
def test_cli_file_output_auto_xml(self):
|
||||
with TemporaryDirectory() as tmpdir:
|
||||
target_xml = Path(tmpdir) / "output.xml"
|
||||
code = main(["-o", str(target_xml)])
|
||||
self.assertEqual(code, 0)
|
||||
self.assertTrue(target_xml.exists())
|
||||
content = target_xml.read_text(encoding="utf-8")
|
||||
self.assertIn("<inventory", content)
|
||||
self.assertIn("<host name=", content)
|
||||
|
||||
def test_cli_file_output_auto_ini(self):
|
||||
with TemporaryDirectory() as tmpdir:
|
||||
target_ini = Path(tmpdir) / "hosts.ini"
|
||||
code = main(["-o", str(target_ini)])
|
||||
self.assertEqual(code, 0)
|
||||
self.assertTrue(target_ini.exists())
|
||||
content = target_ini.read_text(encoding="utf-8")
|
||||
self.assertIn("ansible_host=", content)
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,51 @@
|
||||
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()
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user