index_attribue suit le chemin de la fiche (presentee, attribution du site, deposee) : sans lui la copie de reprise se regenerait sous l'index de production. site_ou_vit, depot_locataire et racines lisent la production dans parente.yml (underlay.site_de_production). Console et tests suivis. Conception et CHANGELOG 210. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
193 lines
11 KiB
Python
193 lines
11 KiB
Python
#!/usr/bin/env python3
|
|
"""L'index de reprise : la fiche du site attribue, tout suit, et rien ne se percute.
|
|
|
|
Ecosystemes FICTIFS (un site de production, un site de reprise, un locataire). Ces cas gardent :
|
|
1. la nomenclature rend l'index que la FICHE attribue, et garde celui de production a part ;
|
|
sans fiche, l'index de la nomenclature ;
|
|
2. l'hebergeur actif doit attribuer l'index de production ; un site de reprise, un autre ;
|
|
3. un index de reprise est RESERVE : un ecosysteme qui le prendrait est en collision (P21) ;
|
|
4. preparer une reprise chez l'hebergeur actif, ou chez un site qui ne la declare pas, est refuse ;
|
|
5. vu du site de reprise, le locataire se lit dans sa COPIE ; vu de sa production, dans son depot ;
|
|
6. dans la copie, les pairs du tunnel passent dans le reseau de l'index de reprise ;
|
|
7. un site n'equipe que les tenants qui y tournent, ou dont il porte une copie ;
|
|
8. le site place une machine sur SA fabric : un noeud qu'il ne declare pas cede la place a
|
|
celui de son gabarit ; un noeud qu'il declare reste celui du plan ;
|
|
9. la forge du site de reprise enregistre la cle de lecture du locataire qu'il reprend ; un
|
|
site qui ne le declare pas, non.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest import mock
|
|
|
|
import yaml
|
|
|
|
RACINE = Path(__file__).resolve().parents[2]
|
|
sys.path.insert(0, str(RACINE / "scripts"))
|
|
|
|
import contexte # noqa: E402
|
|
import devis_reseau # noqa: E402
|
|
import forge_cles # noqa: E402
|
|
import instances # noqa: E402
|
|
import inventory_rules # noqa: E402
|
|
import reprise # noqa: E402
|
|
import underlay # noqa: E402
|
|
|
|
|
|
def ecrire(chemin: Path, donnees: dict) -> None:
|
|
chemin.parent.mkdir(parents=True, exist_ok=True)
|
|
chemin.write_text(yaml.safe_dump(donnees), encoding="utf-8")
|
|
|
|
|
|
class Fictif(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
self.d = Path(tempfile.mkdtemp())
|
|
ecrire(self.d / "SITE-Prod" / "underlay.yml", {"underlay": {"index": 37, "tenants": {"OPS-X": 23}}})
|
|
ecrire(self.d / "SITE-Reprise" / "underlay.yml", {"underlay": {"index": 31, "tenants": {"OPS-X": 43}}})
|
|
ecrire(self.d / "OPS-X" / "plan" / "nomenclature.yml", {"index": 23, "categories": {1: {"libelle": "Frontiere"}}})
|
|
ecrire(self.d / "OPS-X" / "fiche-site.yml", {"site": "SITE-Prod", "attribue": {"index": 23}})
|
|
ecrire(self.d / "OPS-X" / "parente.yml", {"parente": {"depots": {"hebergeur": {"nom": "SITE-Prod"}}}})
|
|
self.loc = contexte.Locataire(self.d / "OPS-X")
|
|
|
|
def reprise_tenants(self, idx):
|
|
ecrire(self.d / "SITE-Reprise" / "underlay.yml", {"underlay": {"index": 31, "tenants": {"OPS-X": idx}}})
|
|
return contexte.Site(self.d / "SITE-Reprise")
|
|
|
|
# 1. ----------------------------------------------------------------------------------
|
|
def test_la_fiche_attribue_l_index(self):
|
|
copie = self.d / "copie" / "OPS-X"
|
|
ecrire(copie / "plan" / "nomenclature.yml", {"index": 23})
|
|
ecrire(copie / "fiche-site.yml", {"site": "SITE-Reprise", "attribue": {"index": 43}})
|
|
n = inventory_rules.charger_nomenclature(copie / "plan" / "nomenclature.yml")
|
|
self.assertEqual((n["index"], n["index_production"]), (43, 23))
|
|
|
|
def test_sans_fiche_l_index_de_la_nomenclature(self):
|
|
(self.d / "OPS-X" / "fiche-site.yml").unlink()
|
|
self.assertEqual(inventory_rules.charger_nomenclature(self.d / "OPS-X" / "plan" / "nomenclature.yml")["index"], 23)
|
|
|
|
# 10. SANS FICHE DEPOSEE (2026-10-25, §2.5 pas 5) ------------------------------------
|
|
# Le site ne depose plus sa fiche. La copie de reprise se regenerait alors sous l'index de
|
|
# PRODUCTION (23) : `index_attribue` ne lisait que le fichier. Elle tient desormais son index du
|
|
# site de reprise (son chemin le nomme), et la production le sien (sa filiation).
|
|
def test_sans_fiche_la_copie_garde_l_index_de_reprise(self):
|
|
(self.d / "OPS-X" / "fiche-site.yml").unlink()
|
|
copie = self.d / "reprises" / "SITE-Reprise" / "OPS-X"
|
|
ecrire(copie / "plan" / "nomenclature.yml", {"index": 23})
|
|
ecrire(copie / "parente.yml", {"parente": {"depots": {"hebergeur": {"nom": "SITE-Prod"}}}})
|
|
with mock.patch.object(contexte, "DOSSIER_ECOSYSTEMES", self.d):
|
|
n = inventory_rules.charger_nomenclature(copie / "plan" / "nomenclature.yml")
|
|
self.assertEqual((n["index"], n.get("index_production")), (43, 23))
|
|
n = inventory_rules.charger_nomenclature(self.d / "OPS-X" / "plan" / "nomenclature.yml")
|
|
self.assertEqual(n["index"], 23)
|
|
|
|
def test_sans_fiche_la_production_vit_chez_son_hebergeur(self):
|
|
(self.d / "OPS-X" / "fiche-site.yml").unlink()
|
|
self.assertEqual(underlay.site_de_production(self.d / "OPS-X"), "SITE-Prod")
|
|
with mock.patch.object(devis_reseau, "DOSSIER_INSTANCES", self.d):
|
|
self.assertEqual(devis_reseau.site_ou_vit("OPS-X"), "SITE-Prod")
|
|
c = self.copie()
|
|
(c / "fiche-site.yml").unlink(missing_ok=True)
|
|
self.assertEqual(underlay.depot_locataire("OPS-X", self.d, "SITE-Reprise"), c)
|
|
self.assertEqual(underlay.depot_locataire("OPS-X", self.d, "SITE-Prod"), self.d / "OPS-X")
|
|
|
|
# 2. ----------------------------------------------------------------------------------
|
|
def test_l_hebergeur_attribue_la_production(self):
|
|
self.assertEqual(contexte.Site(self.d / "SITE-Prod").ecarts_attribution(self.loc), [])
|
|
ecrire(self.d / "SITE-Prod" / "underlay.yml", {"underlay": {"index": 37, "tenants": {"OPS-X": 24}}})
|
|
self.assertTrue(contexte.Site(self.d / "SITE-Prod").ecarts_attribution(self.loc))
|
|
|
|
def test_la_reprise_attribue_un_autre_index(self):
|
|
self.assertEqual(self.reprise_tenants(43).ecarts_attribution(self.loc), [])
|
|
self.assertTrue(self.reprise_tenants(23).ecarts_attribution(self.loc))
|
|
|
|
# 3. ----------------------------------------------------------------------------------
|
|
def test_l_index_de_reprise_est_reserve(self):
|
|
ecrire(self.d / "OPS-Y" / "plan" / "nomenclature.yml", {"index": 43, "categories": {1: {}}})
|
|
with mock.patch.object(instances, "FRERES", self.d), mock.patch.object(instances, "_actif", return_value=None):
|
|
coll = instances.collisions(instances.decouvrir())
|
|
self.assertIn(43, coll)
|
|
self.assertEqual(len(coll[43]), 2)
|
|
|
|
def test_sans_voisin_pas_de_collision(self):
|
|
with mock.patch.object(instances, "FRERES", self.d), mock.patch.object(instances, "_actif", return_value=None):
|
|
self.assertEqual(instances.collisions(instances.decouvrir()), {})
|
|
|
|
# 4. ----------------------------------------------------------------------------------
|
|
def test_preparer_chez_l_hebergeur_est_refuse(self):
|
|
self.assertTrue(reprise.refus_preparation(contexte.Site(self.d / "SITE-Prod"), self.loc))
|
|
|
|
def test_preparer_chez_un_site_qui_ne_declare_pas(self):
|
|
ecrire(self.d / "SITE-Reprise" / "underlay.yml", {"underlay": {"index": 31, "tenants": {}}})
|
|
self.assertTrue(reprise.refus_preparation(contexte.Site(self.d / "SITE-Reprise"), self.loc))
|
|
|
|
def test_preparer_chez_le_site_de_reprise_passe(self):
|
|
self.assertEqual(reprise.refus_preparation(self.reprise_tenants(43), self.loc), [])
|
|
|
|
|
|
# 5. ----------------------------------------------------------------------------------
|
|
def copie(self):
|
|
c = self.d / "reprises" / "SITE-Reprise" / "OPS-X"
|
|
ecrire(c / "plan" / "nomenclature.yml", {"index": 23})
|
|
ecrire(c / "fiche-site.yml", {"site": "SITE-Reprise", "attribue": {"index": 43}})
|
|
ecrire(c / "plan" / "acces.yml", {"acces_admin_vpn": {"ana-portable": {"cle_publique": "x", "adresse": "10.23.29.2/32"}}})
|
|
return c
|
|
|
|
def test_vu_du_site_de_reprise_la_copie(self):
|
|
c = self.copie()
|
|
self.assertEqual(underlay.depot_locataire("OPS-X", self.d, "SITE-Reprise"), c)
|
|
self.assertEqual(underlay.depot_locataire("OPS-X", self.d, "SITE-Prod"), self.d / "OPS-X")
|
|
|
|
def test_sans_copie_le_depot_frere(self):
|
|
self.assertEqual(underlay.depot_locataire("OPS-X", self.d, "SITE-Reprise"), self.d / "OPS-X")
|
|
|
|
# 6. ----------------------------------------------------------------------------------
|
|
def test_les_pairs_suivent_l_index_de_reprise(self):
|
|
c = self.copie()
|
|
p = inventory_rules.charger_acces(c / "plan" / "acces.yml")["acces_admin_vpn"]["ana-portable"]
|
|
self.assertEqual((p["adresse"], p["adresse_production"]), ("10.43.29.2/32", "10.23.29.2/32"))
|
|
ecrire(self.d / "OPS-X" / "plan" / "acces.yml", {"acces_admin_vpn": {"ana-portable": {"adresse": "10.23.29.2/32"}}})
|
|
p = inventory_rules.charger_acces(self.d / "OPS-X" / "plan" / "acces.yml")["acces_admin_vpn"]["ana-portable"]
|
|
self.assertEqual(p["adresse"], "10.23.29.2/32")
|
|
|
|
# 7. ----------------------------------------------------------------------------------
|
|
def test_on_n_equipe_que_ce_qui_tourne_ici(self):
|
|
tenants = [("OPS-X", "X", {}), ("OPS-Y", "Y", {})]
|
|
vit = {"OPS-X": "SITE-Prod", "OPS-Y": "SITE-Prod"}
|
|
with mock.patch.object(devis_reseau, "decouvrir_du_site", return_value=tenants), \
|
|
mock.patch.object(devis_reseau, "site_ou_vit", side_effect=lambda n: vit[n]), \
|
|
mock.patch.object(devis_reseau, "DOSSIER_INSTANCES", self.d), \
|
|
mock.patch.object(underlay, "site_courant", return_value="SITE-Reprise"):
|
|
self.assertEqual(devis_reseau.heberges_ici(), [])
|
|
self.copie()
|
|
self.assertEqual([t[0] for t in devis_reseau.heberges_ici()], ["OPS-X"])
|
|
|
|
|
|
# 8. ----------------------------------------------------------------------------------
|
|
def test_le_site_place_sur_sa_fabric(self):
|
|
m = {"clonage": {"SETOPS_NOEUD": "asgard", "SETOPS_IP": "10.43.19.11"}, "groupes": []}
|
|
with mock.patch.object(underlay, "noeuds_du_site", return_value=["maurice"]), \
|
|
mock.patch.object(underlay, "gabarit", return_value={"noeud": "maurice"}):
|
|
self.assertEqual(contexte.clonage_rendu(m, "", "infra-dns-01")["SETOPS_NOEUD"], "maurice")
|
|
with mock.patch.object(underlay, "noeuds_du_site", return_value=["asgard", "gandalf"]):
|
|
self.assertEqual(contexte.clonage_rendu(m, "", "infra-dns-01")["SETOPS_NOEUD"], "asgard")
|
|
|
|
|
|
# 9. ----------------------------------------------------------------------------------
|
|
def test_la_forge_de_reprise_enregistre_la_cle(self):
|
|
ecrire(self.d / "OPS-X" / "face-reseau.yml", {"hebergeur": "SITE-Prod",
|
|
"genome": {"depot": "ops-x", "cle_lecture": "ssh-ed25519 AAAAcle runner"}})
|
|
with mock.patch.object(contexte, "DOSSIER_ECOSYSTEMES", self.d):
|
|
# Le depot du locataire ET sa fiche presentee (pas 3, §2.5), avec la meme cle.
|
|
attendu = ["ops-x", "fiches/fiche-ops-x"]
|
|
self.assertEqual([d["depot"] for d in forge_cles.demandes("SITE-Reprise")], attendu)
|
|
self.assertEqual([d["depot"] for d in forge_cles.demandes("SITE-Prod")], attendu)
|
|
ecrire(self.d / "SITE-Reprise" / "underlay.yml", {"underlay": {"index": 31, "tenants": {}}})
|
|
self.assertEqual(forge_cles.demandes("SITE-Reprise"), [])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|