|
| 1 | +import asyncio |
| 2 | +import aiohttp |
1 | 3 | import sys |
| 4 | +import psutil |
2 | 5 | import pytest |
| 6 | +import os |
3 | 7 | import shutil |
4 | 8 | import subprocess |
5 | 9 | import dotenv |
6 | 10 | from pathlib import Path |
| 11 | +from typing import Any |
7 | 12 | from PyQt5.QtCore import QCoreApplication |
8 | 13 |
|
9 | 14 | sys.path.append(str(Path(__file__).parent.parent)) |
|
15 | 20 |
|
16 | 21 | def pytest_addoption(parser): |
17 | 22 | parser.addoption("--test-install", action="store_true") |
18 | | - parser.addoption("--pod-process", action="store_true") |
| 23 | + parser.addoption("--cloud", action="store_true") |
19 | 24 | parser.addoption("--ci", action="store_true") |
20 | 25 | parser.addoption("--benchmark", action="store_true") |
21 | 26 |
|
@@ -95,3 +100,110 @@ def local_download_server(): |
95 | 100 |
|
96 | 101 | if has_local_cloud: |
97 | 102 | dotenv.load_dotenv(root_dir / "service" / "web" / ".env.local") |
| 103 | + |
| 104 | + |
| 105 | +class CloudService: |
| 106 | + def __init__(self, loop: QtTestApp, enabled=True): |
| 107 | + self.loop = loop |
| 108 | + self.dir = root_dir / "service" |
| 109 | + self.log_dir = result_dir / "logs" |
| 110 | + self.log_dir.mkdir(exist_ok=True) |
| 111 | + self.url = os.environ["TEST_SERVICE_URL"] |
| 112 | + self.coord_proc: asyncio.subprocess.Process | None = None |
| 113 | + self.coord_log = None |
| 114 | + self.worker_proc: asyncio.subprocess.Process | None = None |
| 115 | + self.worker_task: asyncio.Task | None = None |
| 116 | + self.worker_log = None |
| 117 | + self.enabled = enabled |
| 118 | + |
| 119 | + async def serve(self, process: asyncio.subprocess.Process, log_file): |
| 120 | + try: |
| 121 | + async for line in util.ensure(process.stdout): |
| 122 | + print(line.decode("utf-8"), end="", file=log_file, flush=True) |
| 123 | + except asyncio.CancelledError: |
| 124 | + pass |
| 125 | + |
| 126 | + async def launch_coordinator(self): |
| 127 | + assert self.coord_proc is None, "Coordinator already running" |
| 128 | + self.coord_log = open(self.log_dir / "api.log", "w", encoding="utf-8") |
| 129 | + npm = shutil.which("npm") |
| 130 | + assert npm is not None, "npm not found in PATH" |
| 131 | + args = [npm, "run", "dev"] |
| 132 | + self.coord_proc = await asyncio.create_subprocess_exec( |
| 133 | + *args, |
| 134 | + cwd=self.dir / "api", |
| 135 | + stdout=self.coord_log, |
| 136 | + stderr=asyncio.subprocess.STDOUT, |
| 137 | + ) |
| 138 | + |
| 139 | + async def launch_worker(self): |
| 140 | + assert self.worker_proc is None, "Worker already running" |
| 141 | + self.worker_log = open(self.log_dir / "worker.log", "w", encoding="utf-8") |
| 142 | + workerpy = str(self.dir / "pod" / "worker.py") |
| 143 | + config = str(self.dir / "pod" / "_var" / "worker.json") |
| 144 | + args = ["-u", "-Xutf8", workerpy, config] |
| 145 | + self.worker_proc = await asyncio.create_subprocess_exec( |
| 146 | + sys.executable, |
| 147 | + *args, |
| 148 | + cwd=self.dir / "pod", |
| 149 | + stdout=subprocess.PIPE, |
| 150 | + stderr=subprocess.STDOUT, |
| 151 | + ) |
| 152 | + assert self.worker_proc.stdout is not None |
| 153 | + async for line in self.worker_proc.stdout: |
| 154 | + text = line.decode("utf-8") |
| 155 | + print(text[:80], end="", file=self.worker_log, flush=True) |
| 156 | + if "Uvicorn running" in text: |
| 157 | + break |
| 158 | + |
| 159 | + self.worker_task = asyncio.create_task(self.serve(self.worker_proc, self.worker_log)) |
| 160 | + |
| 161 | + async def start(self): |
| 162 | + if not self.enabled or not has_local_cloud: |
| 163 | + return |
| 164 | + try: |
| 165 | + await self.launch_coordinator() |
| 166 | + await self.launch_worker() |
| 167 | + except Exception as e: |
| 168 | + await self.stop() |
| 169 | + raise e |
| 170 | + |
| 171 | + async def stop(self): |
| 172 | + if self.worker_task: |
| 173 | + self.worker_task.cancel() |
| 174 | + await self.worker_task |
| 175 | + if self.worker_proc: |
| 176 | + self.worker_proc.terminate() |
| 177 | + await self.worker_proc.wait() |
| 178 | + if self.coord_proc: |
| 179 | + children = psutil.Process(self.coord_proc.pid).children(recursive=True) |
| 180 | + for child in children: |
| 181 | + child.terminate() |
| 182 | + self.coord_proc.terminate() |
| 183 | + await self.coord_proc.wait() |
| 184 | + |
| 185 | + async def create_user(self, username: str) -> dict[str, Any]: |
| 186 | + assert self.enabled, "Cloud service is not enabled" |
| 187 | + async with aiohttp.ClientSession() as session: |
| 188 | + async with session.post( |
| 189 | + f"{self.url}/admin/user/create", |
| 190 | + json={"name": username}, |
| 191 | + ) as response: |
| 192 | + response.raise_for_status() |
| 193 | + result = await response.json() |
| 194 | + if "error" in result: |
| 195 | + raise Exception(result["error"]) |
| 196 | + return result |
| 197 | + |
| 198 | + def __enter__(self): |
| 199 | + self.loop.run(self.start()) |
| 200 | + return self |
| 201 | + |
| 202 | + def __exit__(self, exc_type, exc, tb): |
| 203 | + self.loop.run(self.stop()) |
| 204 | + |
| 205 | + |
| 206 | +@pytest.fixture(scope="session") |
| 207 | +def cloud_service(qtapp, pytestconfig): |
| 208 | + with CloudService(qtapp, pytestconfig.getoption("--cloud")) as service: |
| 209 | + yield service |
0 commit comments