Set-OPS-Public/scripts/tests/test_index_reprise.py

98 lines
4.8 KiB
Python
Raw Normal View History

#!/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.
"""
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 instances # noqa: E402
import inventory_rules # noqa: E402
import reprise # 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), [])
if __name__ == "__main__":
unittest.main()