|
| 1 | +# Copyright 2025, 2026 Query Farm LLC - https://query.farm |
| 2 | + |
| 3 | +"""``vgi-serve`` wiring of the blob vendor (``VGI_BLOB_VENDOR_URL``). |
| 4 | +
|
| 5 | +With the vendor configured, the HTTP worker externalizes large results to it |
| 6 | +(pointers carry vendor download URLs), vends client upload URLs, and pins |
| 7 | +inbound pointer URLs to the vendor's storage hosts. |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import os |
| 13 | +import subprocess |
| 14 | +import sys |
| 15 | +from collections.abc import Iterator |
| 16 | +from urllib.parse import urlsplit |
| 17 | + |
| 18 | +import pyarrow as pa |
| 19 | +import pytest |
| 20 | +from vgi_rpc.conformance.fake_vendor import FakeVendorServer, serve_fake_vendor |
| 21 | +from vgi_rpc.vendor import VendorStorage |
| 22 | + |
| 23 | +from vgi.serve import resolve_blob_vendor_http_config |
| 24 | + |
| 25 | +pytest.importorskip("aiohttp", reason="external location resolution needs vgi-rpc[external]") |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +def vendor() -> Iterator[FakeVendorServer]: |
| 30 | + """A fake blob vendor on a background thread.""" |
| 31 | + with serve_fake_vendor() as server: |
| 32 | + yield server |
| 33 | + |
| 34 | + |
| 35 | +def test_nothing_configured() -> None: |
| 36 | + """No vendor and no request cap: everything off, as before.""" |
| 37 | + cfg = resolve_blob_vendor_http_config({}) |
| 38 | + assert (cfg.external_location, cfg.upload_url_provider, cfg.max_upload_bytes, cfg.max_request_bytes) == ( |
| 39 | + None, |
| 40 | + None, |
| 41 | + None, |
| 42 | + None, |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +def test_request_cap_without_vendor() -> None: |
| 47 | + """VGI_RPC_MAX_REQUEST_BYTES is honoured on its own (an explicit operator setting).""" |
| 48 | + cfg = resolve_blob_vendor_http_config({"VGI_RPC_MAX_REQUEST_BYTES": "1048576"}) |
| 49 | + assert cfg.max_request_bytes == 1048576 |
| 50 | + assert cfg.external_location is None and cfg.upload_url_provider is None |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.parametrize("bad", ["0", "-1", "big"]) |
| 54 | +def test_malformed_request_cap(bad: str) -> None: |
| 55 | + """A malformed cap stops startup.""" |
| 56 | + with pytest.raises(ValueError, match="VGI_RPC_MAX_REQUEST_BYTES"): |
| 57 | + resolve_blob_vendor_http_config({"VGI_RPC_MAX_REQUEST_BYTES": bad}) |
| 58 | + |
| 59 | + |
| 60 | +def test_vendor_configured(vendor: FakeVendorServer) -> None: |
| 61 | + """The vendor supplies the external config, the upload URL provider and the advertised upload size.""" |
| 62 | + cfg = resolve_blob_vendor_http_config( |
| 63 | + { |
| 64 | + "VGI_BLOB_VENDOR_URL": vendor.url, |
| 65 | + "VGI_CF_DO_TOKEN": "test-token", |
| 66 | + "VGI_RPC_EXTERNALIZE_THRESHOLD_BYTES": "2048", |
| 67 | + "VGI_RPC_MAX_UPLOAD_BYTES": "1073741824", |
| 68 | + "VGI_RPC_MAX_REQUEST_BYTES": "65536", |
| 69 | + } |
| 70 | + ) |
| 71 | + assert cfg.external_location is not None |
| 72 | + assert isinstance(cfg.upload_url_provider, VendorStorage) |
| 73 | + assert cfg.external_location.storage is cfg.upload_url_provider |
| 74 | + assert cfg.external_location.externalize_threshold_bytes == 2048 |
| 75 | + assert cfg.max_upload_bytes == 1073741824 |
| 76 | + assert cfg.max_request_bytes == 65536 |
| 77 | + |
| 78 | + |
| 79 | +def test_vendor_without_token(vendor: FakeVendorServer) -> None: |
| 80 | + """A vendor URL with no token stops startup.""" |
| 81 | + with pytest.raises(ValueError, match="VGI_BLOB_VENDOR_TOKEN"): |
| 82 | + resolve_blob_vendor_http_config({"VGI_BLOB_VENDOR_URL": vendor.url}) |
| 83 | + |
| 84 | + |
| 85 | +def test_vgi_serve_externalizes_results_and_vends_upload_urls(vendor: FakeVendorServer) -> None: |
| 86 | + """End to end: a large scalar result comes back as a vendor pointer and resolves.""" |
| 87 | + from vgi_rpc import ExternalLocationConfig |
| 88 | + from vgi_rpc.http import http_capabilities, http_connect |
| 89 | + from vgi_rpc.metadata import LOCATION_SOURCE_KEY |
| 90 | + from vgi_rpc.rpc import AnnotatedBatch |
| 91 | + from vgi_rpc.vendor import pinned_hosts_validator |
| 92 | + |
| 93 | + from vgi.arguments import Arguments |
| 94 | + from vgi.invocation import FunctionType |
| 95 | + from vgi.protocol import BindRequest, InitRequest, VgiProtocol |
| 96 | + |
| 97 | + env = { |
| 98 | + **os.environ, |
| 99 | + "VGI_BLOB_VENDOR_URL": vendor.url, |
| 100 | + "VGI_BLOB_VENDOR_TOKEN": "test-token", |
| 101 | + "VGI_RPC_EXTERNALIZE_THRESHOLD_BYTES": "16384", |
| 102 | + "VGI_RPC_MAX_REQUEST_BYTES": "262144", |
| 103 | + "VGI_RPC_MAX_UPLOAD_BYTES": "1073741824", |
| 104 | + "PYTHONUNBUFFERED": "1", |
| 105 | + } |
| 106 | + proc = subprocess.Popen( |
| 107 | + [sys.executable, "-m", "vgi.serve", "vgi._test_fixtures.worker:ExampleWorker", "--http"] |
| 108 | + + ["--host", "127.0.0.1", "--port", "0"], |
| 109 | + env=env, |
| 110 | + stdout=subprocess.PIPE, |
| 111 | + stderr=subprocess.DEVNULL, |
| 112 | + text=True, |
| 113 | + ) |
| 114 | + try: |
| 115 | + assert proc.stdout is not None |
| 116 | + line = proc.stdout.readline() |
| 117 | + assert line.startswith("PORT:"), line |
| 118 | + base_url = f"http://127.0.0.1:{line.strip().split(':', 1)[1]}" |
| 119 | + |
| 120 | + caps = http_capabilities(base_url=base_url) |
| 121 | + assert caps.upload_url_support is True |
| 122 | + assert caps.max_request_bytes == 262144 |
| 123 | + assert caps.max_upload_bytes == 1073741824 |
| 124 | + |
| 125 | + payload_bytes = 64 * 1024 |
| 126 | + validator = pinned_hosts_validator([vendor.vendor.host], allow_http=True) |
| 127 | + with http_connect( |
| 128 | + VgiProtocol, # type: ignore[type-abstract] |
| 129 | + base_url=base_url, |
| 130 | + external_location=ExternalLocationConfig(url_validator=validator), |
| 131 | + ) as proxy: |
| 132 | + schema = pa.schema([("dummy", pa.int64())]) |
| 133 | + bind = BindRequest( |
| 134 | + function_name="random_bytes", |
| 135 | + arguments=Arguments(positional=(pa.scalar(7), pa.scalar(payload_bytes))), |
| 136 | + function_type=FunctionType.SCALAR, |
| 137 | + input_schema=schema, |
| 138 | + ) |
| 139 | + bound = proxy.bind(request=bind) |
| 140 | + stream = proxy.init( |
| 141 | + request=InitRequest( |
| 142 | + bind_call=bind, output_schema=bound.output_schema, bind_opaque_data=bound.opaque_data |
| 143 | + ) |
| 144 | + ) |
| 145 | + try: |
| 146 | + out = stream.exchange(AnnotatedBatch(batch=pa.RecordBatch.from_pydict({"dummy": [1]}, schema=schema))) |
| 147 | + finally: |
| 148 | + stream.close() |
| 149 | + assert len(out.batch.column("result")[0].as_py()) == payload_bytes |
| 150 | + assert out.custom_metadata is not None |
| 151 | + source = out.custom_metadata.get(LOCATION_SOURCE_KEY) |
| 152 | + assert isinstance(source, bytes) |
| 153 | + assert urlsplit(source.decode()).netloc == vendor.vendor.host |
| 154 | + assert vendor.vendor.stats().get("/v1/blobs/put", 0) >= 1 |
| 155 | + finally: |
| 156 | + proc.terminate() |
| 157 | + proc.wait(timeout=10) |
0 commit comments