-
Notifications
You must be signed in to change notification settings - Fork 0
fix: resolve host-relative follow-up links against the origin, not base_url #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sundar-svg
wants to merge
6
commits into
main
Choose a base branch
from
sundar/host-relative-follow-up-links
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+197
−3
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
255429c
fix: resolve host-relative follow-up links against the origin, not ba…
sundar-svg 32be4fe
trim url() comment to the load-bearing why
sundar-svg eb4628b
drop dated/hand-verification notes from test docstrings
sundar-svg 80c06a3
make the gateway e2e workflow model-free (core nodes only)
sundar-svg 52391bd
gateway e2e: allow overriding the test workflow via COMFY_WORKFLOW_FILE
sundar-svg 5c08487
assert output dimensions, not compressed size
sundar-svg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| """Live end-to-end test of the SDK against a serverless gateway deployment: | ||
| upload an input asset, submit a workflow that references it, poll to a | ||
| terminal state, download the output. | ||
|
|
||
| Skipped unless pointed at a live deployment: | ||
|
|
||
| export COMFY_BASE_URL="https://stagingplatformapi.comfy.org/deployment/<dep_id>" | ||
| export COMFY_API_KEY="comfyui-..." | ||
| pytest tests/integration/test_gateway_e2e.py -v | ||
|
|
||
| The default workflow uses only core nodes (no models), so any deployment | ||
| works. Point COMFY_WORKFLOW_FILE at an API-format workflow JSON to run your | ||
| own instead: its first LoadImage node is rewired to the uploaded test input, | ||
| so it must contain one (and its distribution's models must be baked into the | ||
| target deployment). | ||
|
|
||
| First run streams a full multipart upload; reruns hit the by-hash dedup | ||
| fast-path (the input image is deterministic), so both upload paths get | ||
| coverage across two runs. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import os | ||
| import struct | ||
| import zlib | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| from comfy_sdk import Comfy | ||
|
|
||
| BASE_URL = os.environ.get("COMFY_BASE_URL") | ||
| API_KEY = os.environ.get("COMFY_API_KEY") | ||
| INPUT_NAME = "sdk_e2e_input.png" | ||
| WORKFLOW_FILE = os.environ.get("COMFY_WORKFLOW_FILE") | ||
| JOB_TIMEOUT_S = 600 # cold start on a scale-to-zero deployment takes minutes | ||
|
|
||
| pytestmark = pytest.mark.skipif( | ||
| not (BASE_URL and API_KEY), | ||
| reason="set COMFY_BASE_URL and COMFY_API_KEY to run gateway e2e tests", | ||
| ) | ||
|
|
||
|
|
||
| def _gradient_png(width: int = 512, height: int = 512) -> bytes: | ||
| """A deterministic RGB gradient PNG, stdlib-only (no PIL dependency).""" | ||
|
|
||
| def chunk(tag: bytes, data: bytes) -> bytes: | ||
| return ( | ||
| struct.pack(">I", len(data)) | ||
| + tag | ||
| + data | ||
| + struct.pack(">I", zlib.crc32(tag + data) & 0xFFFFFFFF) | ||
| ) | ||
|
|
||
| raw = b"".join( | ||
| b"\x00" | ||
| + bytes( | ||
| v | ||
| for x in range(width) | ||
| for v in (x * 255 // width, y * 255 // height, 128) | ||
| ) | ||
| for y in range(height) | ||
| ) | ||
| ihdr = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0) | ||
| return ( | ||
| b"\x89PNG\r\n\x1a\n" | ||
| + chunk(b"IHDR", ihdr) | ||
| + chunk(b"IDAT", zlib.compress(raw, 6)) | ||
| + chunk(b"IEND", b"") | ||
| ) | ||
|
|
||
|
|
||
| DEFAULT_WORKFLOW = { | ||
| "1": {"class_type": "LoadImage", "inputs": {"image": ""}}, | ||
| "2": {"class_type": "ImageInvert", "inputs": {"image": ["1", 0]}}, | ||
| "3": {"class_type": "SaveImage", "inputs": {"filename_prefix": "sdk_e2e", "images": ["2", 0]}}, | ||
| } | ||
|
|
||
|
|
||
| def _edit_workflow(image_ref: object) -> tuple[dict, str]: | ||
| """The graph to run and the node id of the LoadImage wired to ``image_ref``.""" | ||
| if WORKFLOW_FILE: | ||
| graph = json.loads(Path(WORKFLOW_FILE).read_text()) | ||
| else: | ||
| graph = json.loads(json.dumps(DEFAULT_WORKFLOW)) | ||
| for node_id, node in graph.items(): | ||
| if node.get("class_type") == "LoadImage": | ||
| node["inputs"]["image"] = image_ref | ||
| return graph, node_id | ||
| raise AssertionError("workflow has no LoadImage node to receive the test input") | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def client() -> Comfy: | ||
| c = Comfy(BASE_URL, api_key=API_KEY) | ||
| yield c | ||
| c.close() | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def input_asset(client: Comfy, tmp_path_factory: pytest.TempPathFactory): | ||
| path = tmp_path_factory.mktemp("inputs") / INPUT_NAME | ||
| path.write_bytes(_gradient_png()) | ||
| asset = client.assets.from_file(path) | ||
| asset.commit() | ||
| return asset | ||
|
|
||
|
|
||
| def test_upload_dedup_roundtrip(client: Comfy, input_asset) -> None: | ||
| again = client.assets.from_bytes(_gradient_png(), filename=INPUT_NAME) | ||
| assert again.commit() == input_asset.commit() | ||
|
|
||
|
|
||
| def test_image_edit_by_name(client: Comfy, input_asset, tmp_path) -> None: | ||
| graph, _ = _edit_workflow(INPUT_NAME) | ||
| job = client.run(client.workflows.from_json(graph)).wait(timeout=JOB_TIMEOUT_S) | ||
|
|
||
| assert job.status == "succeeded", f"job {job.id} ended {job.status}: {job.error}" | ||
| assert job.outputs, f"job {job.id} succeeded with no outputs" | ||
|
|
||
| out_path = tmp_path / "sdk_e2e_out" | ||
| job.outputs[0].to_file(out_path) | ||
| data = out_path.read_bytes() | ||
| assert data, "downloaded output is empty" | ||
| if not WORKFLOW_FILE: | ||
| assert data[:8] == b"\x89PNG\r\n\x1a\n" | ||
| width, height = struct.unpack(">II", data[16:24]) | ||
| assert (width, height) == (512, 512) | ||
|
|
||
|
|
||
| @pytest.mark.xfail( | ||
| reason="gateway does not yet resolve core/ASSET references in the graph; " | ||
| "it resolves string filename references only", | ||
| strict=False, | ||
| ) | ||
| def test_image_edit_by_asset_handle(client: Comfy, input_asset, tmp_path) -> None: | ||
| graph, load_node = _edit_workflow(INPUT_NAME) | ||
| wf = client.workflows.from_json(graph) | ||
| wf.set_input(load_node, "image", input_asset) | ||
| job = client.run(wf).wait(timeout=JOB_TIMEOUT_S) | ||
|
|
||
| assert job.status == "succeeded", f"job {job.id} ended {job.status}: {job.error}" | ||
| assert job.outputs, f"job {job.id} succeeded with no outputs" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| """Follow-up links (``job.urls.*``) resolve against the origin, not base_url. | ||
|
|
||
| A server mounts the v2 contract wherever it likes — the serverless gateway | ||
| serves it under ``/deployment/{id}/api/v2`` — and its host-relative follow-up | ||
| links already include that mount prefix. Resolving them against ``base_url`` | ||
| (which carries the same prefix) doubles it and 404s; they must resolve against | ||
| the scheme+authority only. Internal shorthand paths (``/jobs/…``, ``/assets…``) | ||
| keep resolving under ``base_url + /api/v2``. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from comfy_low.transport import _Prepared | ||
|
|
||
| GATEWAY_BASE = "https://stagingplatformapi.comfy.org/deployment/dep_123" | ||
| CLOUD_BASE = "https://api.comfy.org" | ||
|
|
||
|
|
||
| def test_gateway_self_link_resolves_against_origin() -> None: | ||
| p = _Prepared(GATEWAY_BASE, "comfyui-k") | ||
| link = "/deployment/dep_123/api/v2/jobs/j1" | ||
| assert p.url(link) == "https://stagingplatformapi.comfy.org" + link | ||
|
|
||
|
|
||
| def test_gateway_internal_path_keeps_deployment_prefix() -> None: | ||
| p = _Prepared(GATEWAY_BASE, "comfyui-k") | ||
| assert p.url("/jobs/j1") == GATEWAY_BASE + "/api/v2/jobs/j1" | ||
| assert p.url("/assets") == GATEWAY_BASE + "/api/v2/assets" | ||
|
|
||
|
|
||
| def test_cloud_self_link_unchanged() -> None: | ||
| p = _Prepared(CLOUD_BASE, "comfyui-k") | ||
| assert p.url("/api/v2/jobs/j1") == CLOUD_BASE + "/api/v2/jobs/j1" | ||
|
|
||
|
|
||
| def test_absolute_link_passes_through() -> None: | ||
| p = _Prepared(GATEWAY_BASE, "comfyui-k") | ||
| url = "https://elsewhere.example/api/v2/jobs/j1" | ||
| assert p.url(url) == url | ||
|
|
||
|
|
||
| def test_auth_still_attaches_to_origin_resolved_links() -> None: | ||
| p = _Prepared(GATEWAY_BASE, "comfyui-k") | ||
| url = p.url("/deployment/dep_123/api/v2/jobs/j1") | ||
| assert p.headers(url)["Authorization"] == "Bearer comfyui-k" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.