Files
CelerisLab/tests/unit/test_validation_source_provenance.py

81 lines
2.7 KiB
Python

"""Explicit experimental repin and historical-source provenance contracts."""
import hashlib
import json
from pathlib import Path
import subprocess
import sys
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT / "tests/validation"))
from source_provenance import checkout_provenance
import re100_helpers
import run_jet_circle_cv
sys.path.insert(0, str(ROOT / "tests/unit"))
from historical_tree import historical_tree, tracked
ORIGIN = "47b05bc8abfc957fe876b0c63bdb386e3c6cbaa5"
def test_manifests_cover_current_source_and_preserve_origin(tmp_path):
assert re100_helpers.PIN == run_jet_circle_cv.PIN == ORIGIN
manifests = {}
for group, hashes_key, revision_key in (
("dfg_re100", "production_sha256", "commit"),
("jet_circle_cv", "files", "revision"),
):
manifest = json.loads(
(
ROOT / "tests/validation/data" / group / "source_manifest.json"
).read_text()
)
assert manifest[revision_key] == ORIGIN
assert manifest["source_origin_commits"][-1] == ORIGIN
assert (
manifest["historical_checkpoints"]["production"]
== "d79e3f6f0a424f5bcc3a1a6d610313e49daf97a2"
)
assert "not inherited from d79" in manifest["experiment"]
manifests[group] = manifest[hashes_key]
# History-dependent checks last, so a missing commit masks nothing above.
names = tracked(ORIGIN)
origin = historical_tree(ORIGIN, tmp_path)
for group, hashes in manifests.items():
assert set(hashes) == set(names), group
for name, expected in hashes.items():
assert (
hashlib.sha256((origin / name).read_bytes()).hexdigest() == expected
), name
def test_runtime_checkout_identity_is_separate_from_source_origin(tmp_path):
subprocess.run(["git", "init", "-q", str(tmp_path)], check=True)
(tmp_path / "test.txt").write_text("tracked\n")
subprocess.run(["git", "-C", str(tmp_path), "add", "test.txt"], check=True)
subprocess.run(
[
"git",
"-C",
str(tmp_path),
"-c",
"user.name=Test",
"-c",
"user.email=test@example.invalid",
"commit",
"-qm",
"fixture",
],
check=True,
)
expected = subprocess.check_output(
["git", "-C", str(tmp_path), "rev-parse", "HEAD"], text=True
).strip()
assert checkout_provenance(tmp_path) == dict(
checkout_revision=expected, checkout_dirty=False
)
(tmp_path / "test.txt").write_text("changed\n")
assert checkout_provenance(tmp_path) == dict(
checkout_revision=expected, checkout_dirty=True
)