#!/usr/bin/env python3 """Tests stdlib (sans pytest) pour inventory_host.parametres_proxmox_hote. Lancer : python3 scripts/tests/test_inventory_host.py """ from __future__ import annotations import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1])) from inventory_host import parametres_proxmox_hote # noqa: E402 def _inventaire_factice() -> dict: return { "all": { "children": { "hotes_planifies": { "hosts": { "app-01": { "ansible_host": "10.0.2.11", "ansible_user": "ansible", "proxmox_cidr": 24, "proxmox_coeurs": 4, "proxmox_disque_taille": "160G", "proxmox_memoire": 2048, "proxmox_noeud": "noeud-a", "proxmox_passerelle": "10.0.2.1", "proxmox_stockage": "stockage-a", "proxmox_vlan": 13, "proxmox_vmid": 93101, }, # app-02 : sans proxmox_noeud (champ optionnel). "app-02": { "ansible_host": "10.0.2.21", "proxmox_cidr": 24, "proxmox_disque_taille": "16G", "proxmox_passerelle": "10.0.2.1", "proxmox_stockage": "stockage-a", "proxmox_vlan": 15, "proxmox_vmid": 95201, }, # incomplet-01 : il manque l'IP (champ requis). "incomplet-01": { "proxmox_vmid": 99901, "proxmox_cidr": 24, "proxmox_passerelle": "10.0.2.1", "proxmox_vlan": 99, }, } }, "serveur_postgresql": {"hosts": {"app-01": {}}}, } } } def test_hote_complet_avec_noeud() -> None: lignes = parametres_proxmox_hote(_inventaire_factice(), "app-01") attendu = { "SETOPS_VMID='93101'", "SETOPS_IP='10.0.2.11'", "SETOPS_CIDR='24'", "SETOPS_PASSERELLE='10.0.2.1'", "SETOPS_VLAN='13'", "SETOPS_STOCKAGE='stockage-a'", "SETOPS_DISQUE='160G'", "SETOPS_NOEUD='noeud-a'", } assert set(lignes) == attendu, lignes def test_hote_sans_noeud_emet_noeud_vide() -> None: lignes = parametres_proxmox_hote(_inventaire_factice(), "app-02") assert "SETOPS_NOEUD=''" in lignes, lignes assert "SETOPS_VMID='95201'" in lignes, lignes def test_hote_absent_refuse() -> None: try: parametres_proxmox_hote(_inventaire_factice(), "inconnu-01") except ValueError as exc: assert "absent" in str(exc), exc else: raise AssertionError("un hote absent doit lever ValueError") def test_champ_requis_manquant_refuse() -> None: try: parametres_proxmox_hote(_inventaire_factice(), "incomplet-01") except ValueError as exc: assert "manquants" in str(exc) and "ansible_host" in str(exc), exc else: raise AssertionError("un champ requis manquant doit lever ValueError") def main() -> int: tests = [ test_hote_complet_avec_noeud, test_hote_sans_noeud_emet_noeud_vide, test_hote_absent_refuse, test_champ_requis_manquant_refuse, ] for test in tests: test() print(f"ok {test.__name__}") print(f"{len(tests)} tests passes.") return 0 if __name__ == "__main__": raise SystemExit(main())