Une VM ne peut pas s'installer sans resoudre des noms. PowerDNS repond UNIQUEMENT pour la zone souveraine et ne recurse pour personne : il manquait un resolveur recursif. `client_unbound` est l'outil ecrit pour ca — il rejoint `client_journal` et `client_metrique` parmi les integrations universelles. `infra-dns-01` est exempte (PowerDNS occupe son port 53), et `serveur_powerdns_listen_addresses` passe de 0.0.0.0 a l'adresse de l'hote pour laisser 127.0.0.1:53 libre. Mais l'appartenance au groupe est AUSSI ce qui ouvre le port 53 a la frontiere : en exemptant la machine, je lui retirais le droit de resoudre. `serveur_powerdns` declare donc son propre flux sortant — il ne recurse pour personne, mais doit resoudre pour lui-meme. Nouvel intrant `dns_amorcage`, derive jusqu'a `make creer-vm`. Cloud-init l'ecrit bien mais sans effet : `dns-nameservers` exige `resolvconf`, absent du gabarit, et installer resolvconf demande apt, qui demande la resolution. `serveur_debian` pose donc le resolveur en pre_tasks, avant le premier apt, avec une garde qui respecte la bascule ulterieure de client_unbound. Defaut corrige en chemin : `_intrants_communs()` lisait `instance/` en dur ; le chemin derive maintenant de l'inventaire recu, et un test le prouve. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
164 lines
5.9 KiB
Python
164 lines
5.9 KiB
Python
#!/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_etiquette_vlan_repli_et_vide_explicite() -> None:
|
|
"""Absent -> repli sur proxmox_vlan ; present et VIDE -> reste vide.
|
|
|
|
C'est la distinction qui protege du double etiquetage : en SDN le VNet porte
|
|
deja le tag, et `proxmox_etiquette_vlan: ""` DIT qu'il ne faut pas en poser un
|
|
second. Un repli naif (`a or b`) ferait revenir l'etiquette et casserait le
|
|
rattachement — panne muette, decouverte au premier paquet.
|
|
"""
|
|
inv = _inventaire_factice()
|
|
hote = inv["all"]["children"]["hotes_actifs"]["hosts"]["app-01"]
|
|
|
|
# 1. Cle absente : on retombe sur proxmox_vlan.
|
|
lignes = parametres_proxmox_hote(inv, "app-01")
|
|
assert "SETOPS_VLAN='13'" in lignes, lignes
|
|
|
|
# 2. Cle presente et vide (SDN) : aucune etiquette, malgre proxmox_vlan renseigne.
|
|
hote["proxmox_etiquette_vlan"] = ""
|
|
hote["proxmox_pont"] = "t17serv"
|
|
lignes = parametres_proxmox_hote(inv, "app-01")
|
|
assert "SETOPS_VLAN=''" in lignes, lignes
|
|
assert "SETOPS_PONT='t17serv'" in lignes, lignes
|
|
|
|
|
|
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_PONT=''",
|
|
"SETOPS_STOCKAGE='stockage-a'",
|
|
"SETOPS_DISQUE='160G'",
|
|
"SETOPS_NOEUD='noeud-a'",
|
|
"SETOPS_COEURS='4'",
|
|
"SETOPS_MEMOIRE='2048'",
|
|
"SETOPS_DNS=''",
|
|
}
|
|
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_intrant_commun_lu_a_cote_de_l_inventaire_recu() -> None:
|
|
"""`dns_amorcage` vit dans group_vars/all, pas dans les vars d'hote.
|
|
|
|
Et il doit etre lu A COTE de l'inventaire passe en argument : lire `instance/` en
|
|
dur ferait dependre le resultat de l'environnement, et un test synthetique irait
|
|
chercher les intrants de la production.
|
|
"""
|
|
import tempfile
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
base = Path(tmp)
|
|
(base / "group_vars" / "all").mkdir(parents=True)
|
|
(base / "group_vars" / "all" / "10-intrants.yml").write_text(
|
|
"dns_amorcage: 9.9.9.9\n", encoding="utf-8")
|
|
inv = base / "hosts.yml"
|
|
inv.write_text("all: {children: {}}\n", encoding="utf-8")
|
|
lignes = parametres_proxmox_hote(_inventaire_factice(), "app-01", inv)
|
|
assert "SETOPS_DNS='9.9.9.9'" in lignes, lignes
|
|
# Sans inventaire, aucun intrant commun n'est devine.
|
|
assert "SETOPS_DNS=''" in parametres_proxmox_hote(_inventaire_factice(), "app-01")
|
|
|
|
|
|
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_intrant_commun_lu_a_cote_de_l_inventaire_recu,
|
|
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())
|