Set-OPS-Public/scripts/tests/test_index_reprise.py
Daniel Allaire 04b3c0a6b5 reprise : le site clone la copie, et sur sa propre fabric
contexte.py --hotes-actifs et --parametres-clonage, par ou LE SITE lit un
locataire pour le materialiser, passaient par le depot frere : vu du site
de TechnoLibre, treize VM seraient nees en 23. Ils passent par
underlay.depot_locataire ; Locataire.charger reste celui de la production.

Le noeud vient du plan du locataire (asgard, juste en production). Le site
place sur sa propre fabric (D-80) : un noeud qu'il ne declare pas
(proxmox_noeuds) cede la place a celui de son gabarit, et il le dit ; un
noeud qu'il declare reste celui du plan. Simulation des treize VM vue de ce
site : 10.43.x, VMID 143..., ponts t43..., toutes sur maurice.

Au site de TechnoLibre, applique : SDN (t43), frontiere, tunnel et
pare-feu Proxmox de la reprise ; les quatre plans disent Rien a faire.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-24 20:00:27 -04:00

152 lines
8.3 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.
"""
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 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)
# 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")
if __name__ == "__main__":
unittest.main()