fix governance reqs; allow for sandboxed jupyter notebooks for testing

This commit is contained in:
Roberto Treviño Cervantes
2026-11-13 11:52:51 -06:00
parent ae2310754f
commit 1b45bc61d7
19 changed files with 1340 additions and 171 deletions
+49 -16
View File
@@ -1,4 +1,16 @@
"""What this module covers: the registry's promotion checks, promotion and retirement, the lineage of a version, and
what its run stored (`registry.artifacts`, `registry.artifact`, `registry.routines`).
Fixtures: `production_ready` (from `conftest.py`), a PROD version whose run recorded every routine and its model;
`governance_db`, `cacu_db` and `mlflow_tracking` (from `conftest.py`); `trained_run` and `armado` (from `conftest.py`)
build runs missing a routine or the model.
"""
import io
import json
import zipfile
import pytest
from conftest import armado, trained_run
from core.governance.enums import ArtifactKind, Environment
from models.io.registry import PromotionBlocked, registry
@@ -9,7 +21,7 @@ def test_dev_version_cannot_be_promoted_and_says_why(governance_db, mlflow_track
with pytest.raises(PromotionBlocked) as blocked:
registry.promote("cacu", "tamizaje", version)
failed = {check.code for check in blocked.value.checks if not check.passed}
assert failed == {"ML-02", "ML-03", "MLD-01", "MLD-01b", "MLD-03", "ML-01b"}
assert failed == {"ML-02", "ML-03", "MLD-01", "MLD-01b", "MLD-03", "ML-01a", "ML-01b"}
def test_promotion_retires_the_previous_version_of_the_scenario(production_ready):
@@ -35,10 +47,44 @@ def test_reports_can_be_attached_after_training(production_ready):
registry.attach_reports(domain, scenario, version, metrics="{}")
def test_artifacts_are_logged_to_the_run(production_ready):
def test_lineage_lists_every_stored_file_and_each_routine_record(production_ready):
"""The lineage lists files inside folders (the saved model) and reads back each routine's record. Fails if
`run_files` stops descending into folders or `routines` stops reading `rutinas/`."""
domain, scenario, version = production_ready("v1")
lineage = registry.lineage(domain, scenario, version)
assert any(path.startswith(ArtifactKind.ONNX_CHECKPOINT.value) for path in lineage["artifacts"])
paths = {item["path"] for item in lineage["artifacts"]}
assert "model/MLmodel" in paths and "rutinas/MUESTREO.json" in paths
assert lineage["routines"]["BALANCE_CLASES"] == {"method": "prueba", "n": 8}
@pytest.mark.parametrize("routines, model, failing", [
([ArtifactKind.MUESTREO], True, "ML-01a"),
(None, False, "ML-01b"),
])
def test_promotion_needs_what_the_training_run_itself_recorded(governance_db, cacu_db, mlflow_tracking, routines, model,
failing):
"""A run that skipped routine records, or saved no model, is not promoted, whatever else is in order. Fails if
the ML-01a check stops requiring every kind in `ROUTINE_KINDS` or the ML-01b check stops requiring `modelo_sha`."""
run = trained_run("cacu", "incompleta", armado("huella-x"), {"auc": 0.8}, routines=routines, model=model)
version = registry.register_run(run, technical_validation_report_uri="t", clinical_validation_report_uri="c",
interpretability_report_uri="i")
failed = [check for check in registry.checks("cacu", "incompleta", version) if not check.passed]
assert [check.code for check in failed] == [failing]
if failing == "ML-01a":
assert "DATOS_FALTANTES" in failed[0].detail
def test_an_artifact_downloads_as_a_file_and_a_folder_as_a_zip(production_ready):
"""ML-01b: a stored file comes back as itself; the model folder as a zip of its files. Fails if `run_file`
stops zipping folders or starts accepting a path the run doesn't list."""
domain, scenario, version = production_ready("v1")
name, data = registry.artifact(domain, scenario, version, "rutinas/MUESTREO.json")
assert name == "MUESTREO.json" and json.loads(data)["n"] == 8
name, data = registry.artifact(domain, scenario, version, "model")
assert name == "model.zip" and "model/MLmodel" in zipfile.ZipFile(io.BytesIO(data)).namelist()
for wrong in ("../mlflow.db", "", "rutinas/OTRO.json"):
with pytest.raises(LookupError):
registry.artifact(domain, scenario, version, wrong)
def test_retire_and_lineage(production_ready):
@@ -55,19 +101,6 @@ def test_retire_and_lineage(production_ready):
registry.retire(domain, scenario, second)
def test_invalid_onnx_checkpoint_is_rejected(production_ready):
from models.io.tracking import inspect_onnx_metadata
with pytest.raises(ValueError, match="ONNX"):
import tempfile
from pathlib import Path
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "x.onnx"
path.write_bytes(b"weights")
inspect_onnx_metadata(path)
@pytest.mark.parametrize("domain, scenario", [("cacu' OR '1'='1", None), ("cacu", "x' OR name LIKE '%")])
def test_version_filters_reject_anything_but_known_names(mlflow_tracking, domain, scenario):
with pytest.raises(ValueError):