cmvr-visual/inference/tests/test_video_edit_deployment.py

201 lines
6.8 KiB
Python
Raw Permalink Normal View History

"""Offline deployment integrity tests; no network or model initialization."""
import hashlib
import importlib.util
import json
import sys
from pathlib import Path
import pytest
SCRIPTS = Path(__file__).parents[1] / "scripts"
sys.path.insert(0, str(SCRIPTS))
def module(name):
spec = importlib.util.spec_from_file_location(name, SCRIPTS / (name + ".py"))
value = importlib.util.module_from_spec(spec)
spec.loader.exec_module(value)
return value
def item(name, data=b"weights"):
return {
"rfilename": name,
"size": len(data),
"lfs": {"sha256": hashlib.sha256(data).hexdigest()},
}
def test_path_traversal_and_symlink_targets_rejected(tmp_path):
d = module("download_video_edit_models")
for name in ("../escape", "/absolute", "C:/outside", "a/../../escape", "a\\escape"):
with pytest.raises(ValueError):
d.child_path(tmp_path, name)
def test_complete_external_onnx_directory_selected_and_hash_required():
d = module("download_video_edit_models")
files = [
item("process_checkpoint/pose2d/vitpose_h_wholebody.onnx/end2end.onnx"),
item("process_checkpoint/pose2d/vitpose_h_wholebody.onnx/tensor.42"),
item("process_checkpoint/det/yolov10m.onnx"),
item("process_checkpoint/sam2/sam2_hiera_large.pt"),
item("unrelated.bin"),
]
assert len(d.select_files(files, "preprocess")) == 4
with pytest.raises(ValueError, match="hash"):
d.checksum(Path("missing"), {"rfilename": "a", "size": 1})
def test_download_refuses_existing_corruption_and_replica_wrong_bytes(tmp_path):
d = module("download_video_edit_models")
expected = item("config.json", b"good")
target = tmp_path / "model"
target.mkdir()
(target / "config.json").write_bytes(b"evil")
with pytest.raises(ValueError, match="existing"):
d.download_file(
"repo/name",
"a" * 40,
expected,
target,
fetch=lambda url, path: path.write_bytes(b"good"),
)
(target / "config.json").unlink()
with pytest.raises(ValueError, match="checksum"):
d.download_file(
"repo/name",
"a" * 40,
expected,
target,
fetch=lambda url, path: path.write_bytes(b"evil"),
)
assert not (target / "config.json").exists()
def test_metadata_revision_and_complete_profile_requirements():
d = module("download_video_edit_models")
with pytest.raises(ValueError, match="revision"):
d.validate_metadata(
{"id": d.MODEL_REPO, "sha": "wrong", "siblings": []},
d.MODEL_REPO,
d.MODEL_REVISION,
"model",
)
with pytest.raises(ValueError, match="required"):
d.validate_metadata(
{
"id": d.MODEL_REPO,
"sha": d.MODEL_REVISION,
"siblings": [item("model_index.json")],
},
d.MODEL_REPO,
d.MODEL_REVISION,
"model",
)
def test_git_blob_checksum_checks_same_size_corruption(tmp_path):
d = module("download_video_edit_models")
path = tmp_path / "config.json"
path.write_bytes(b"good")
entry = {"size": 4, "blobId": hashlib.sha1(b"blob 4\0good").hexdigest()}
assert d.checksum(path, entry)
path.write_bytes(b"evil")
assert not d.checksum(path, entry)
def test_receipt_cannot_certify_without_verified_artifacts(tmp_path):
v = module("verify_video_edit_profile")
with pytest.raises(ValueError, match="evidence"):
v.validate_evidence({"profile": v.PROFILE}, tmp_path / "receipt.json", tmp_path)
def test_receipt_verifies_all_files_and_defaults_unvalidated(tmp_path, monkeypatch):
import download_video_edit_models as d
v = module("verify_video_edit_profile")
metadata = tmp_path / "metadata"
metadata.mkdir()
code = tmp_path / "code"
code.mkdir()
monkeypatch.setattr(
d, "CODE_FILES", {"pose2d.py": hashlib.sha256(b"code").hexdigest()}
)
(code / "pose2d.py").write_bytes(b"code")
for folder, repo, revision, kind, cache in d.SPECS:
names = (
[
"model_index.json",
"transformer/config.json",
"transformer/model.safetensors",
"vae/config.json",
]
if kind == "model"
else [
"process_checkpoint/det/yolov10m.onnx",
"process_checkpoint/sam2/sam2_hiera_large.pt",
"process_checkpoint/pose2d/vitpose_h_wholebody.onnx/end2end.onnx",
"process_checkpoint/pose2d/vitpose_h_wholebody.onnx/tensor.1",
]
)
entries = []
for name in names:
path = tmp_path / "models" / folder / name
path.parent.mkdir(parents=True, exist_ok=True)
path.write_bytes(b"weights")
entries.append(item(name))
(metadata / cache).write_text(
json.dumps({"id": repo, "sha": revision, "siblings": entries})
)
receipt = v.build_receipt(tmp_path, metadata, code)
assert receipt["validated"] is False and len(receipt["files"]) == 9
assert all(len(entry["sha256"]) == 64 for entry in receipt["files"])
extra = tmp_path / "models" / "wan2.2-animate-14b" / "unverified.bin"
extra.write_bytes(b"unknown")
with pytest.raises(ValueError, match="Unverified"):
v.build_receipt(tmp_path, metadata, code)
def test_evidence_is_bound_to_receipt_and_artifact_bytes(tmp_path):
v = module("verify_video_edit_profile")
receipt = tmp_path / "receipt.json"
receipt.write_bytes(b"{}")
artifacts = []
for name, kind in [("job.log", "gpu_job_log"), ("video.mp4", "output_video")]:
path = tmp_path / name
path.write_bytes(b"fixture")
artifacts.append(
{
"path": str(path),
"sha256": hashlib.sha256(b"fixture").hexdigest(),
"kind": kind,
}
)
evidence = {
"profile": v.PROFILE,
"revision": v.d.MODEL_REVISION,
"reviewed_by": "operator",
"checks": dict.fromkeys(v.CHECKS, True),
"receipt_sha256": v.d.hash_file(receipt),
"artifacts": artifacts,
}
v.validate_evidence(evidence, receipt, tmp_path)
(tmp_path / "job.log").write_bytes(b"changed")
with pytest.raises(ValueError, match="artifact"):
v.validate_evidence(evidence, receipt, tmp_path)
def test_download_rejects_symlink_parent_even_inside_workspace(tmp_path):
d = module("download_video_edit_models")
real = tmp_path / "real"
real.mkdir()
link = tmp_path / "link"
try:
link.symlink_to(real, target_is_directory=True)
except OSError:
pytest.skip("Directory symlink creation requires platform permission")
with pytest.raises(ValueError, match="Symlink"):
d.child_path(tmp_path, "link/model.bin")