feat(slice-2): implement static file collector for YAML/INI and Ansible JSON dynamic inventory exporter
This commit is contained in:
1 parent
68802710b0
commit
951be389a1
11 files changed
+564
-22
No files matched your search
@@ -0,0 +1,85 @@
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from tempfile import NamedTemporaryFile
|
||||
from inventory_collector.collectors.static_file import StaticFileCollector
|
||||
from inventory_collector.models import HostRecord
|
||||
|
||||
class TestStaticFileCollector(unittest.TestCase):
|
||||
def test_parse_ansible_ini_basic(self):
|
||||
ini_content = """
|
||||
[proxmox_nodes]
|
||||
node0 ansible_host=192.168.0.101
|
||||
node1 ansible_host=192.168.0.111
|
||||
|
||||
[proxmox_nodes:vars]
|
||||
ansible_user=root
|
||||
|
||||
[k3s_masters]
|
||||
k3s-master-0 ansible_host=192.168.5.70 ansible_user=lain
|
||||
|
||||
[k3s_workers]
|
||||
k3s-worker-0 ansible_host=192.168.5.80 ansible_user=lain
|
||||
|
||||
[k3s_cluster:children]
|
||||
k3s_masters
|
||||
k3s_workers
|
||||
"""
|
||||
with NamedTemporaryFile("w+", suffix=".ini", delete=False) as f:
|
||||
f.write(ini_content)
|
||||
temp_path = Path(f.name)
|
||||
|
||||
try:
|
||||
collector = StaticFileCollector(temp_path)
|
||||
hosts = collector.collect()
|
||||
|
||||
# Convert to dict by name for inspection
|
||||
hosts_dict = {h.name: h for h in hosts}
|
||||
|
||||
self.assertIn("node0", hosts_dict)
|
||||
self.assertEqual(hosts_dict["node0"].ansible_host, "192.168.0.101")
|
||||
self.assertEqual(hosts_dict["node0"].ansible_user, "root")
|
||||
self.assertIn("proxmox_nodes", hosts_dict["node0"].groups)
|
||||
|
||||
self.assertIn("k3s-master-0", hosts_dict)
|
||||
self.assertEqual(hosts_dict["k3s-master-0"].ansible_host, "192.168.5.70")
|
||||
self.assertEqual(hosts_dict["k3s-master-0"].ansible_user, "lain")
|
||||
self.assertIn("k3s_masters", hosts_dict["k3s-master-0"].groups)
|
||||
self.assertIn("k3s_cluster", hosts_dict["k3s-master-0"].groups)
|
||||
finally:
|
||||
temp_path.unlink()
|
||||
|
||||
def test_parse_static_yaml(self):
|
||||
yaml_content = """
|
||||
hosts:
|
||||
synology-nas:
|
||||
ansible_host: 192.168.0.2
|
||||
ansible_user: soda64
|
||||
groups:
|
||||
- storage
|
||||
- external
|
||||
tags:
|
||||
- nas
|
||||
- btrfs
|
||||
groups:
|
||||
storage:
|
||||
- synology-nas
|
||||
"""
|
||||
with NamedTemporaryFile("w+", suffix=".yaml", delete=False) as f:
|
||||
f.write(yaml_content)
|
||||
temp_path = Path(f.name)
|
||||
|
||||
try:
|
||||
collector = StaticFileCollector(temp_path)
|
||||
hosts = collector.collect()
|
||||
self.assertEqual(len(hosts), 1)
|
||||
nas = hosts[0]
|
||||
self.assertEqual(nas.name, "synology-nas")
|
||||
self.assertEqual(nas.ansible_host, "192.168.0.2")
|
||||
self.assertEqual(nas.ansible_user, "soda64")
|
||||
self.assertIn("storage", nas.groups)
|
||||
self.assertIn("nas", nas.tags)
|
||||
finally:
|
||||
temp_path.unlink()
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user