|
| 1 | +from typing import AsyncGenerator, Generator, Tuple |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +import httpx |
| 5 | +import pytest |
| 6 | +from fastapi import FastAPI |
| 7 | + |
| 8 | +from dstack._internal.gateway.deps import BaseGatewayDependencyInjector |
| 9 | +from dstack._internal.gateway.repos.base import BaseGatewayRepo, Project, Replica, Service |
| 10 | +from dstack._internal.gateway.repos.memory import InMemoryGatewayRepo |
| 11 | +from dstack._internal.gateway.routers.service_proxy import router |
| 12 | + |
| 13 | + |
| 14 | +def make_app(repo: BaseGatewayRepo) -> FastAPI: |
| 15 | + class DependencyInjector(BaseGatewayDependencyInjector): |
| 16 | + async def get_repo(self) -> AsyncGenerator[BaseGatewayRepo, None]: |
| 17 | + yield repo |
| 18 | + |
| 19 | + app = FastAPI() |
| 20 | + app.state.gateway_dependency_injector = DependencyInjector() |
| 21 | + app.include_router(router, prefix="/gateway/services") |
| 22 | + return app |
| 23 | + |
| 24 | + |
| 25 | +def make_client(app: FastAPI) -> httpx.AsyncClient: |
| 26 | + return httpx.AsyncClient(transport=httpx.ASGITransport(app=app)) |
| 27 | + |
| 28 | + |
| 29 | +def make_app_client(repo: BaseGatewayRepo) -> Tuple[FastAPI, httpx.AsyncClient]: |
| 30 | + app = make_app(repo) |
| 31 | + client = make_client(app) |
| 32 | + return app, client |
| 33 | + |
| 34 | + |
| 35 | +def make_project(name: str) -> Project: |
| 36 | + return Project(name=name, ssh_private_key="secret") |
| 37 | + |
| 38 | + |
| 39 | +def make_service(run_name: str) -> Service: |
| 40 | + return Service( |
| 41 | + id="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
| 42 | + run_name=run_name, |
| 43 | + auth=False, |
| 44 | + app_port=80, |
| 45 | + replicas=[ |
| 46 | + Replica( |
| 47 | + id="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
| 48 | + ssh_destination="ubuntu@server", |
| 49 | + ssh_port=22, |
| 50 | + ssh_proxy=None, |
| 51 | + ) |
| 52 | + ], |
| 53 | + ) |
| 54 | + |
| 55 | + |
| 56 | +MOCK_REPLICA_CLIENT_TIMEOUT = 8 |
| 57 | + |
| 58 | + |
| 59 | +@pytest.fixture |
| 60 | +def mock_replica_client_httpbin(httpbin) -> Generator[None, None, None]: |
| 61 | + with patch( |
| 62 | + "dstack._internal.gateway.services.service_proxy.get_replica_client" |
| 63 | + ) as get_replica_client_mock: |
| 64 | + get_replica_client_mock.return_value = httpx.AsyncClient( |
| 65 | + base_url=httpbin.url, timeout=MOCK_REPLICA_CLIENT_TIMEOUT |
| 66 | + ) |
| 67 | + yield |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.asyncio |
| 71 | +@pytest.mark.parametrize("method", ["get", "post", "put", "patch", "delete"]) |
| 72 | +async def test_proxy(mock_replica_client_httpbin, method: str) -> None: |
| 73 | + methods_without_body = "get", "delete" |
| 74 | + repo = InMemoryGatewayRepo() |
| 75 | + await repo.add_project(make_project("test-proj")) |
| 76 | + await repo.add_service(project_name="test-proj", service=make_service("httpbin")) |
| 77 | + _, client = make_app_client(repo) |
| 78 | + req_body = "." * 20 * 2**20 if method not in methods_without_body else None |
| 79 | + resp = await client.request( |
| 80 | + method, |
| 81 | + f"http://test-host:8888/gateway/services/test-proj/httpbin/{method}?a=b&c=", |
| 82 | + headers={"User-Agent": "test-ua", "Connection": "keep-alive"}, |
| 83 | + content=req_body, |
| 84 | + ) |
| 85 | + assert resp.status_code == 200 |
| 86 | + assert resp.headers["server"].startswith("Pytest-HTTPBIN") |
| 87 | + resp_body = resp.json() |
| 88 | + assert resp_body["url"] == f"http://test-host:8888/{method}?a=b&c=" |
| 89 | + assert resp_body["args"] == {"a": "b", "c": ""} |
| 90 | + assert resp_body["headers"]["Host"] == "test-host:8888" |
| 91 | + assert resp_body["headers"]["User-Agent"] == "test-ua" |
| 92 | + assert resp_body["headers"]["Connection"] == "keep-alive" |
| 93 | + if method not in methods_without_body: |
| 94 | + assert resp_body["data"] == req_body |
| 95 | + |
| 96 | + |
| 97 | +@pytest.mark.asyncio |
| 98 | +async def test_proxy_method_head(mock_replica_client_httpbin) -> None: |
| 99 | + repo = InMemoryGatewayRepo() |
| 100 | + await repo.add_project(make_project("test-proj")) |
| 101 | + await repo.add_service(project_name="test-proj", service=make_service("httpbin")) |
| 102 | + _, client = make_app_client(repo) |
| 103 | + url = "http://test-host/gateway/services/test-proj/httpbin/" |
| 104 | + get_resp = await client.get(url) |
| 105 | + head_resp = await client.head(url) |
| 106 | + assert get_resp.status_code == head_resp.status_code == 200 |
| 107 | + assert head_resp.headers["Content-Length"] == get_resp.headers["Content-Length"] |
| 108 | + assert int(head_resp.headers["Content-Length"]) > 0 |
| 109 | + assert head_resp.content == b"" |
| 110 | + |
| 111 | + |
| 112 | +@pytest.mark.asyncio |
| 113 | +async def test_proxy_method_options(mock_replica_client_httpbin) -> None: |
| 114 | + repo = InMemoryGatewayRepo() |
| 115 | + await repo.add_project(make_project("test-proj")) |
| 116 | + await repo.add_service(project_name="test-proj", service=make_service("httpbin")) |
| 117 | + _, client = make_app_client(repo) |
| 118 | + resp = await client.options("http://test-host/gateway/services/test-proj/httpbin/get") |
| 119 | + assert resp.status_code == 200 |
| 120 | + assert set(resp.headers["Allow"].split(", ")) == {"HEAD", "GET", "OPTIONS"} |
| 121 | + assert resp.content == b"" |
| 122 | + |
| 123 | + |
| 124 | +@pytest.mark.asyncio |
| 125 | +@pytest.mark.parametrize("code", [204, 304, 418, 503]) |
| 126 | +async def test_proxy_status_codes(mock_replica_client_httpbin, code: int) -> None: |
| 127 | + repo = InMemoryGatewayRepo() |
| 128 | + await repo.add_project(make_project("test-proj")) |
| 129 | + await repo.add_service(project_name="test-proj", service=make_service("httpbin")) |
| 130 | + _, client = make_app_client(repo) |
| 131 | + resp = await client.get(f"http://test-host/gateway/services/test-proj/httpbin/status/{code}") |
| 132 | + assert resp.status_code == code |
| 133 | + |
| 134 | + |
| 135 | +@pytest.mark.asyncio |
| 136 | +async def test_proxy_not_leaks_cookies(mock_replica_client_httpbin) -> None: |
| 137 | + repo = InMemoryGatewayRepo() |
| 138 | + await repo.add_project(make_project("test-proj")) |
| 139 | + await repo.add_service(project_name="test-proj", service=make_service("httpbin")) |
| 140 | + app = make_app(repo) |
| 141 | + client1 = make_client(app) |
| 142 | + client2 = make_client(app) |
| 143 | + cookies_url = "http://test-host/gateway/services/test-proj/httpbin/cookies" |
| 144 | + await client1.get(cookies_url + "/set?a=1") |
| 145 | + await client1.get(cookies_url + "/set?b=2") |
| 146 | + await client2.get(cookies_url + "/set?a=3") |
| 147 | + resp1 = await client1.get(cookies_url) |
| 148 | + resp2 = await client2.get(cookies_url) |
| 149 | + assert resp1.json()["cookies"] == {"a": "1", "b": "2"} |
| 150 | + assert resp2.json()["cookies"] == {"a": "3"} |
| 151 | + |
| 152 | + |
| 153 | +@pytest.mark.asyncio |
| 154 | +async def test_proxy_gateway_timeout(mock_replica_client_httpbin) -> None: |
| 155 | + repo = InMemoryGatewayRepo() |
| 156 | + await repo.add_project(make_project("test-proj")) |
| 157 | + await repo.add_service(project_name="test-proj", service=make_service("httpbin")) |
| 158 | + _, client = make_app_client(repo) |
| 159 | + assert MOCK_REPLICA_CLIENT_TIMEOUT < 10 |
| 160 | + resp = await client.get("http://test-host/gateway/services/test-proj/httpbin/delay/10") |
| 161 | + assert resp.status_code == 504 |
| 162 | + assert resp.json()["detail"] == "Gateway Timeout" |
| 163 | + |
| 164 | + |
| 165 | +@pytest.mark.asyncio |
| 166 | +async def test_proxy_run_not_found(mock_replica_client_httpbin) -> None: |
| 167 | + repo = InMemoryGatewayRepo() |
| 168 | + await repo.add_project(make_project("test-proj")) |
| 169 | + await repo.add_service(project_name="test-proj", service=make_service("test-run")) |
| 170 | + _, client = make_app_client(repo) |
| 171 | + resp = await client.get("http://test-host/gateway/services/test-proj/unknown/") |
| 172 | + assert resp.status_code == 404 |
| 173 | + assert resp.json()["detail"] == "Service test-proj/unknown not found" |
| 174 | + |
| 175 | + |
| 176 | +@pytest.mark.asyncio |
| 177 | +async def test_proxy_project_not_found(mock_replica_client_httpbin) -> None: |
| 178 | + _, client = make_app_client(InMemoryGatewayRepo()) |
| 179 | + resp = await client.get("http://test-host/gateway/services/unknown/test-run/") |
| 180 | + assert resp.status_code == 404 |
| 181 | + assert resp.json()["detail"] == "Service unknown/test-run not found" |
| 182 | + |
| 183 | + |
| 184 | +@pytest.mark.asyncio |
| 185 | +async def test_redirect_to_service_root(mock_replica_client_httpbin) -> None: |
| 186 | + repo = InMemoryGatewayRepo() |
| 187 | + await repo.add_project(make_project("test-proj")) |
| 188 | + await repo.add_service(project_name="test-proj", service=make_service("httpbin")) |
| 189 | + _, client = make_app_client(repo) |
| 190 | + url = "http://test-host/gateway/services/test-proj/httpbin" |
| 191 | + resp = await client.get(url, follow_redirects=False) |
| 192 | + assert resp.status_code == 308 |
| 193 | + assert resp.headers["Location"] == url + "/" |
| 194 | + resp = await client.get(url, follow_redirects=True) |
| 195 | + assert resp.status_code == 200 |
| 196 | + assert resp.request.url == url + "/" |
0 commit comments