111 lines
4.1 KiB
Python
111 lines
4.1 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
"""`make publier` sert la forge de CHAQUE site, pas celle du seul site actif.
|
||
|
|
|
||
|
|
Mesure du 2026-10-22 : `publier` disait « Publie » quand la forge de TechnoLibre avait cinq
|
||
|
|
commits de retard sur quatre depots. Il ne lisait que le genome du site du symlink.
|
||
|
|
|
||
|
|
Ces cas gardent :
|
||
|
|
1. chaque site decouvert est lu sous SON `SETOPS_UNDERLAY` ;
|
||
|
|
2. un depot porte par deux sites ne part qu'une fois sur eregion ;
|
||
|
|
3. un site dont le genome ne porte pas le depot demande n'a rien a recevoir ;
|
||
|
|
4. un site en echec n'empeche pas de servir le suivant, et la commande le dit (code != 0) ;
|
||
|
|
5. un site illisible est nomme, et le code le dit aussi.
|
||
|
|
"""
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import subprocess
|
||
|
|
import sys
|
||
|
|
import tempfile
|
||
|
|
import unittest
|
||
|
|
from pathlib import Path
|
||
|
|
from unittest import mock
|
||
|
|
|
||
|
|
RACINE = Path(__file__).resolve().parents[2]
|
||
|
|
sys.path.insert(0, str(RACINE / "scripts"))
|
||
|
|
|
||
|
|
import publier # noqa: E402
|
||
|
|
|
||
|
|
GENOMES = {
|
||
|
|
"SITE-A": [{"depot": "moteur", "dest": "Moteur"}, {"depot": "site-a", "dest": "SITE-A"}],
|
||
|
|
"SITE-B": [{"depot": "moteur", "dest": "Moteur"}, {"depot": "site-b", "dest": "SITE-B"}],
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
def _genome(env: dict) -> list[dict]:
|
||
|
|
nom = Path(env["SETOPS_UNDERLAY"]).parent.name
|
||
|
|
if nom not in GENOMES:
|
||
|
|
raise SystemExit("illisible")
|
||
|
|
return GENOMES[nom]
|
||
|
|
|
||
|
|
|
||
|
|
class Fictif(unittest.TestCase):
|
||
|
|
|
||
|
|
def setUp(self):
|
||
|
|
self.tmp = Path(tempfile.mkdtemp())
|
||
|
|
for d in ("Moteur", "SITE-A", "SITE-B"):
|
||
|
|
(self.tmp / d / ".git").mkdir(parents=True)
|
||
|
|
for s in ("SITE-A", "SITE-B"):
|
||
|
|
(self.tmp / s / "underlay.yml").write_text("underlay: {}\n", encoding="utf-8")
|
||
|
|
self.poussees: list[str] = []
|
||
|
|
self.forges: list[str] = []
|
||
|
|
patches = [
|
||
|
|
mock.patch.object(publier, "DEPOTS", self.tmp),
|
||
|
|
mock.patch.object(publier, "genome", side_effect=_genome),
|
||
|
|
mock.patch.object(publier, "git", side_effect=self._git),
|
||
|
|
]
|
||
|
|
for p in patches:
|
||
|
|
p.start()
|
||
|
|
self.addCleanup(p.stop)
|
||
|
|
|
||
|
|
def _git(self, depot: Path, *args: str):
|
||
|
|
if args[:1] == ("push",):
|
||
|
|
self.poussees.append(depot.name)
|
||
|
|
sortie = {"branch": "main\n", "rev-list": "0\n"}.get(args[0], "")
|
||
|
|
return subprocess.CompletedProcess(args, 0, sortie, "")
|
||
|
|
|
||
|
|
def _make(self, echoue_pour: str = ""):
|
||
|
|
def run(cmd, cwd=None, env=None, capture_output=False, text=False):
|
||
|
|
site = Path(env["SETOPS_UNDERLAY"]).parent.name
|
||
|
|
if "genome-pousser" in cmd:
|
||
|
|
self.forges.append(site)
|
||
|
|
rc = 1 if (site == echoue_pour and "genome-pousser" in cmd) else 0
|
||
|
|
return subprocess.CompletedProcess(cmd, rc, "", "")
|
||
|
|
return mock.patch.object(publier.subprocess, "run", side_effect=run)
|
||
|
|
|
||
|
|
def _main(self, *argv: str) -> int:
|
||
|
|
with mock.patch.object(sys, "argv", ["publier.py", *argv]), \
|
||
|
|
mock.patch("builtins.print"):
|
||
|
|
return publier.main()
|
||
|
|
|
||
|
|
def test_chaque_site_est_servi(self):
|
||
|
|
with self._make():
|
||
|
|
self.assertEqual(self._main(), 0)
|
||
|
|
self.assertEqual(self.forges, ["SITE-A", "SITE-B"])
|
||
|
|
|
||
|
|
def test_un_depot_partage_part_une_fois_sur_eregion(self):
|
||
|
|
with self._make():
|
||
|
|
self._main()
|
||
|
|
self.assertEqual(sorted(self.poussees), ["Moteur", "SITE-A", "SITE-B"])
|
||
|
|
|
||
|
|
def test_un_site_sans_le_depot_ne_recoit_rien(self):
|
||
|
|
with self._make():
|
||
|
|
self.assertEqual(self._main("--depot", "site-b"), 0)
|
||
|
|
self.assertEqual(self.forges, ["SITE-B"])
|
||
|
|
self.assertEqual(self.poussees, ["SITE-B"])
|
||
|
|
|
||
|
|
def test_un_echec_n_arrete_pas_le_site_suivant(self):
|
||
|
|
with self._make(echoue_pour="SITE-A"):
|
||
|
|
self.assertEqual(self._main(), 3)
|
||
|
|
self.assertEqual(self.forges, ["SITE-A", "SITE-B"])
|
||
|
|
|
||
|
|
def test_un_site_illisible_est_compte(self):
|
||
|
|
(self.tmp / "SITE-C").mkdir()
|
||
|
|
(self.tmp / "SITE-C" / "underlay.yml").write_text("underlay: {}\n", encoding="utf-8")
|
||
|
|
with self._make():
|
||
|
|
self.assertEqual(self._main(), 3)
|
||
|
|
self.assertEqual(self.forges, ["SITE-A", "SITE-B"])
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
unittest.main()
|