feat(simulation): upload real gaussian source bundles

This commit is contained in:
DCCONSTRUCTIONS
2026-08-26 00:54:28 +03:00
parent 147a623df9
commit 2920fc7579
3 changed files with 351 additions and 81 deletions
+126 -54
View File
@@ -13,6 +13,7 @@ from k1link.simulation.gaussian_pipeline_gateway import (
GaussianPipelineGateway,
GaussianPipelineGatewayError,
GaussianPipelineIntegrityError,
_discover_bundle_members,
)
@@ -22,9 +23,9 @@ def _token_file(tmp_path: Path) -> Path:
return token
def test_gateway_uploads_with_tus_and_reads_provider_contract(tmp_path: Path) -> None:
uploaded = bytearray()
metadata: dict[str, str] = {}
def test_gateway_uploads_lcc_bundle_with_tus_and_reads_provider_contract(tmp_path: Path) -> None:
uploads: dict[str, bytearray] = {}
metadata: dict[str, dict[str, str]] = {}
def handler(request: httpx.Request) -> httpx.Response:
assert request.headers["authorization"] == f"Bearer {'t' * 64}"
@@ -35,29 +36,47 @@ def test_gateway_uploads_with_tus_and_reads_provider_contract(tmp_path: Path) ->
"schema_version": "gaussian-pipeline.capabilities/v1",
"service": "ndc-gaussian-pipeline",
"api_version": "gaussian-pipeline.api/v1",
"upload_protocol": "tus/1.0.0",
"source_transport": "tus-bundle/v1",
"provider": {"id": "playcanvas-splat-transform", "version": "3.3.3"},
"runtime": {
"source_revision": "a" * 40,
"image_digest": f"sha256:{'b' * 64}",
},
"max_source_bytes": 1024 * 1024,
"max_source_files": 10_000,
},
)
if request.method == "POST" and request.url.path == "/v1/uploads":
upload_id = f"upload-{len(uploads) + 1:03d}"
uploads[upload_id] = bytearray()
item_metadata: dict[str, str] = {}
for item in request.headers["upload-metadata"].split(","):
key, encoded = item.split(" ", 1)
metadata[key] = base64.b64decode(encoded).decode("utf-8")
return httpx.Response(201, headers={"Location": "/v1/uploads/upload-001"})
if request.method == "HEAD" and request.url.path == "/v1/uploads/upload-001":
return httpx.Response(200, headers={"Upload-Offset": str(len(uploaded))})
if request.method == "PATCH" and request.url.path == "/v1/uploads/upload-001":
assert int(request.headers["upload-offset"]) == len(uploaded)
uploaded.extend(request.content)
return httpx.Response(204, headers={"Upload-Offset": str(len(uploaded))})
item_metadata[key] = base64.b64decode(encoded).decode("utf-8")
metadata[upload_id] = item_metadata
return httpx.Response(201, headers={"Location": f"/v1/uploads/{upload_id}"})
upload_id = request.url.path.rsplit("/", 1)[-1]
if request.method == "HEAD" and upload_id in uploads:
return httpx.Response(200, headers={"Upload-Offset": str(len(uploads[upload_id]))})
if request.method == "PATCH" and upload_id in uploads:
assert int(request.headers["upload-offset"]) == len(uploads[upload_id])
uploads[upload_id].extend(request.content)
return httpx.Response(
204,
headers={"Upload-Offset": str(len(uploads[upload_id]))},
)
return httpx.Response(404)
source = tmp_path / "yard.lcc2"
source.write_bytes(b"portable-gaussian-source")
digest = hashlib.sha256(source.read_bytes()).hexdigest()
bundle = tmp_path / "bundle"
bundle.mkdir()
(bundle / "yard.lcc").write_text(
json.dumps({"fileType": "Quality", "attributes": []}),
encoding="utf-8",
)
(bundle / "index.bin").write_bytes(b"index")
(bundle / "data.bin").write_bytes(b"data")
(bundle / "shcoef.bin").write_bytes(b"sh")
with GaussianPipelineGateway(
"http://gaussian.test",
_token_file(tmp_path),
@@ -65,49 +84,101 @@ def test_gateway_uploads_with_tus_and_reads_provider_contract(tmp_path: Path) ->
transport=httpx.MockTransport(handler),
) as gateway:
assert gateway.capabilities()["service"] == "ndc-gaussian-pipeline"
descriptor = gateway.upload_source(
source,
filename="yard.lcc2",
source_format="lcc2",
sha256=digest,
descriptor = gateway.upload_source_bundle(
bundle,
entrypoint="yard.lcc",
source_format="lcc",
)
assert bytes(uploaded) == source.read_bytes()
assert metadata == {"filename": "yard.lcc2", "format": "lcc2", "sha256": digest}
assert descriptor.to_dict() == {
"upload_id": "upload-001",
"filename": "yard.lcc2",
"format": "lcc2",
"sha256": digest,
"byte_length": len(uploaded),
paths = [member.logical_path for member in descriptor.members]
assert paths == ["data.bin", "index.bin", "shcoef.bin", "yard.lcc"]
assert [metadata[f"upload-{index:03d}"]["logical_path"] for index in range(1, 5)] == paths
for index, member in enumerate(descriptor.members, start=1):
assert bytes(uploads[f"upload-{index:03d}"]) == (bundle / member.logical_path).read_bytes()
assert metadata[f"upload-{index:03d}"]["sha256"] == member.sha256
identity = {
"format": "lcc",
"entrypoint": "yard.lcc",
"members": [
{
"logical_path": member.logical_path,
"sha256": member.sha256,
"byte_length": member.byte_length,
}
for member in descriptor.members
],
}
assert descriptor.bundle_sha256 == hashlib.sha256(
json.dumps(identity, ensure_ascii=False, separators=(",", ":")).encode()
).hexdigest()
assert descriptor.total_byte_length == sum(member.byte_length for member in descriptor.members)
def test_gateway_rejects_local_source_digest_mismatch(tmp_path: Path) -> None:
source = tmp_path / "yard.lcc"
source.write_bytes(b"source")
def test_gateway_rejects_incomplete_lcc_bundle(tmp_path: Path) -> None:
bundle = tmp_path / "bundle"
bundle.mkdir()
(bundle / "yard.lcc").write_text('{"fileType":"Portable"}', encoding="utf-8")
with (
GaussianPipelineGateway(
"http://gaussian.test",
_token_file(tmp_path),
transport=httpx.MockTransport(lambda _request: httpx.Response(500)),
) as gateway,
pytest.raises(GaussianPipelineIntegrityError, match="digest does not match"),
pytest.raises(GaussianPipelineIntegrityError, match="member is unavailable"),
):
gateway.upload_source(
source,
filename="yard.lcc",
gateway.upload_source_bundle(
bundle,
entrypoint="yard.lcc",
source_format="lcc",
sha256="a" * 64,
)
def test_gateway_rejects_upload_location_outside_provider(tmp_path: Path) -> None:
source = tmp_path / "yard.lcc"
source.write_bytes(b"source")
digest = hashlib.sha256(source.read_bytes()).hexdigest()
def test_gateway_discovers_nested_lcc2_chunks_with_trailing_commas(tmp_path: Path) -> None:
bundle = tmp_path / "bundle"
(bundle / "scene" / "chunks").mkdir(parents=True)
(bundle / "scene" / "meta.lcc2").write_text(
"""
{
"root": {"splatFiles": ["chunks/0.sog", "chunks/1.spz",],},
}
""",
encoding="utf-8",
)
(bundle / "scene" / "chunks" / "0.sog").write_bytes(b"sog")
(bundle / "scene" / "chunks" / "1.spz").write_bytes(b"spz")
def handler(_request: httpx.Request) -> httpx.Response:
assert _discover_bundle_members(bundle, "scene/meta.lcc2", "lcc2") == {
"scene/meta.lcc2",
"scene/chunks/0.sog",
"scene/chunks/1.spz",
}
def test_gateway_rejects_upload_location_outside_provider(tmp_path: Path) -> None:
bundle = tmp_path / "bundle"
bundle.mkdir()
(bundle / "yard.lcc").write_text('{"fileType":"Portable"}', encoding="utf-8")
(bundle / "data.bin").write_bytes(b"data")
(bundle / "index.bin").write_bytes(b"index")
def handler(request: httpx.Request) -> httpx.Response:
if request.method == "GET":
return httpx.Response(
200,
json={
"schema_version": "gaussian-pipeline.capabilities/v1",
"service": "ndc-gaussian-pipeline",
"api_version": "gaussian-pipeline.api/v1",
"upload_protocol": "tus/1.0.0",
"source_transport": "tus-bundle/v1",
"runtime": {
"source_revision": "a" * 40,
"image_digest": f"sha256:{'b' * 64}",
},
"max_source_bytes": 1024,
"max_source_files": 10_000,
},
)
return httpx.Response(
201,
headers={"Location": "https://attacker.invalid/v1/uploads/stolen"},
@@ -121,19 +192,20 @@ def test_gateway_rejects_upload_location_outside_provider(tmp_path: Path) -> Non
) as gateway,
pytest.raises(GaussianPipelineGatewayError, match="escaped the provider"),
):
gateway.upload_source(
source,
filename="yard.lcc",
gateway.upload_source_bundle(
bundle,
entrypoint="yard.lcc",
source_format="lcc",
sha256=digest,
)
def test_gateway_rejects_symlink_source_and_token(tmp_path: Path) -> None:
source = tmp_path / "source.lcc"
source.write_bytes(b"source")
source_link = tmp_path / "source-link.lcc"
source_link.symlink_to(source)
bundle = tmp_path / "bundle"
bundle.mkdir()
(bundle / "source.lcc").write_text('{"fileType":"Portable"}', encoding="utf-8")
(bundle / "real-data.bin").write_bytes(b"source")
(bundle / "data.bin").symlink_to(bundle / "real-data.bin")
(bundle / "index.bin").write_bytes(b"index")
token = _token_file(tmp_path)
token_link = tmp_path / "token-link"
token_link.symlink_to(token)
@@ -141,20 +213,18 @@ def test_gateway_rejects_symlink_source_and_token(tmp_path: Path) -> None:
with pytest.raises(GaussianPipelineGatewayError, match="token must be one regular file"):
GaussianPipelineGateway("http://gaussian.test", token_link)
digest = hashlib.sha256(source.read_bytes()).hexdigest()
with (
GaussianPipelineGateway(
"http://gaussian.test",
token,
transport=httpx.MockTransport(lambda _request: httpx.Response(500)),
) as gateway,
pytest.raises(GaussianPipelineIntegrityError, match="one regular file"),
pytest.raises(GaussianPipelineIntegrityError, match="contains a symlink"),
):
gateway.upload_source(
source_link,
filename="source.lcc",
gateway.upload_source_bundle(
bundle,
entrypoint="source.lcc",
source_format="lcc",
sha256=digest,
)
@@ -223,6 +293,8 @@ def test_gateway_rejects_capabilities_without_runtime_provenance(tmp_path: Path)
"schema_version": "gaussian-pipeline.capabilities/v1",
"service": "ndc-gaussian-pipeline",
"api_version": "gaussian-pipeline.api/v1",
"upload_protocol": "tus/1.0.0",
"source_transport": "tus-bundle/v1",
},
)