Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 13,228 Bytes
24e70ef ea2cc30 24e70ef ea2cc30 24e70ef ea2cc30 24e70ef ea2cc30 24e70ef ea2cc30 24e70ef ea2cc30 a71d599 ea2cc30 a71d599 ea2cc30 a71d599 b831fb6 a71d599 ea2cc30 a71d599 ea2cc30 a71d599 ea2cc30 a71d599 ea2cc30 a71d599 ea2cc30 a71d599 ea2cc30 a71d599 ea2cc30 a71d599 ea2cc30 a71d599 ea2cc30 a71d599 ea2cc30 a71d599 df8ea5c a71d599 ea2cc30 24e70ef ea2cc30 24e70ef ea2cc30 24e70ef ea2cc30 24e70ef ea2cc30 24e70ef ea2cc30 75da13b ea2cc30 75da13b ea2cc30 75da13b ea2cc30 75da13b c7c36da ea2cc30 c7c36da ea2cc30 c7c36da ea2cc30 c7c36da ea2cc30 c7c36da ea2cc30 c7c36da ea2cc30 c7c36da 24e70ef ea2cc30 24e70ef | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 | from __future__ import annotations
import json
from typing import cast
import httpx
import pytest
from typer.testing import CliRunner
from harbor_hf.cli import app
runner = CliRunner()
def response(status: int, body: dict[str, object]) -> httpx.Response:
return httpx.Response(
status, json=body, request=httpx.Request("GET", "https://control.example/api")
)
def configure(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("HARBOR_HF_CONTROL_URL", "https://control.example")
monkeypatch.setenv("HARBOR_HF_CONTROL_BEARER_TOKEN", "test-token")
def test_cli_does_not_forward_the_local_hugging_face_token(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setenv("HARBOR_HF_CONTROL_URL", "https://control.example")
monkeypatch.setenv("HF_TOKEN", "local-token-must-not-be-forwarded")
monkeypatch.delenv("HARBOR_HF_CONTROL_BEARER_TOKEN", raising=False)
result = runner.invoke(app, ["status"])
assert result.exit_code != 0
assert "purpose-scoped control credential" in result.output
assert "local-token-must-not-be-forwarded" not in result.output
def test_status_reads_control_api(monkeypatch: pytest.MonkeyPatch) -> None:
configure(monkeypatch)
calls: list[tuple[str, str]] = []
def request(method: str, url: str, **_kwargs: object) -> httpx.Response:
calls.append((method, url))
return response(200, {"write_mode": "enabled"})
monkeypatch.setattr("harbor_hf.cli.httpx.request", request)
result = runner.invoke(app, ["status"])
assert result.exit_code == 0
assert json.loads(result.stdout) == {"write_mode": "enabled"}
assert calls == [("GET", "https://control.example/api/v1/system")]
def test_run_submit_sends_profile_references(
monkeypatch: pytest.MonkeyPatch,
) -> None:
configure(monkeypatch)
observed: dict[str, object] = {}
def request(method: str, url: str, **kwargs: object) -> httpx.Response:
observed.update({"method": method, "url": url, **kwargs})
return response(202, {"run_id": "run-one", "action_id": "action-one"})
monkeypatch.setattr("harbor_hf.cli.httpx.request", request)
result = runner.invoke(
app,
[
"run",
"submit",
"--benchmark",
"control-smoke",
"--model",
"control-smoke",
"--harness",
"control-smoke",
"--deployment",
"hf-cpu-smoke",
"--launch-policy",
"control-smoke",
"--ceiling-microusd",
"0",
"--idempotency-key",
"request-key-0001",
"--yes",
],
)
assert result.exit_code == 0
assert json.loads(result.stdout)["run_id"] == "run-one"
assert observed["method"] == "POST"
assert observed["url"] == "https://control.example/api/v1/runs"
assert observed["json"] == {
"benchmark": "control-smoke",
"model": "control-smoke",
"harness": "control-smoke",
"deployment": "hf-cpu-smoke",
"launch_policy": "control-smoke",
"ceiling_microusd": 0,
"confirmed": True,
}
headers = cast(dict[str, str], observed["headers"])
assert headers["Idempotency-Key"] == "request-key-0001"
def test_run_submit_can_start_paused(
monkeypatch: pytest.MonkeyPatch,
) -> None:
configure(monkeypatch)
observed: dict[str, object] = {}
def request(method: str, url: str, **kwargs: object) -> httpx.Response:
observed.update({"method": method, "url": url, **kwargs})
return response(202, {"run_id": "run-one", "action_id": "action-one"})
monkeypatch.setattr("harbor_hf.cli.httpx.request", request)
result = runner.invoke(
app,
[
"run",
"submit",
"--benchmark",
"control-smoke",
"--model",
"control-smoke",
"--harness",
"control-smoke",
"--deployment",
"hf-cpu-smoke",
"--launch-policy",
"control-smoke",
"--ceiling-microusd",
"0",
"--idempotency-key",
"request-key-0002",
"--start-paused",
"--yes",
],
)
assert result.exit_code == 0
payload = cast(dict[str, object], observed["json"])
assert payload["start_paused"] is True
def test_historical_run_continuation_uses_stable_idempotency(
monkeypatch: pytest.MonkeyPatch,
) -> None:
configure(monkeypatch)
observed: dict[str, object] = {}
def request(method: str, url: str, **kwargs: object) -> httpx.Response:
observed.update({"method": method, "url": url, **kwargs})
return response(
202,
{
"run_id": "run-one",
"continuation_id": "continuation-one",
"adopted": False,
},
)
monkeypatch.setattr("harbor_hf.cli.httpx.request", request)
result = runner.invoke(
app,
[
"run",
"continue-historical",
"run-one",
"--reason",
"finish unresolved tasks",
"--idempotency-key",
"continuation-key-0001",
"--yes",
],
)
assert result.exit_code == 0
assert observed["method"] == "POST"
assert observed["url"] == "https://control.example/api/v1/runs/run-one/continuation"
assert observed["json"] == {
"reason": "finish unresolved tasks",
"confirmed": True,
}
headers = cast(dict[str, str], observed["headers"])
assert headers["Idempotency-Key"] == "continuation-key-0001"
@pytest.mark.parametrize(
("arguments", "expected"),
[
(
["pause", "run-one", "--reason", "canary boundary", "--yes"],
{
"action": "pause",
"task_id": None,
"reason": "canary boundary",
"confirmed": True,
},
),
(
[
"retry-infrastructure",
"run-one",
"--task",
"task-one",
"--reason",
"transient infrastructure failure",
"--yes",
],
{
"action": "retry_infrastructure",
"task_id": "task-one",
"reason": "transient infrastructure failure",
"confirmed": True,
},
),
(
[
"retry-infrastructure",
"run-one",
"--all-eligible",
"--reason",
"retry eligible infrastructure failures",
"--yes",
],
{
"action": "retry_infrastructure",
"task_id": None,
"reason": "retry eligible infrastructure failures",
"confirmed": True,
},
),
(
[
"resume",
"run-one",
"--task-limit",
"1",
"--reason",
"run canary",
"--yes",
],
{
"action": "resume",
"task_id": None,
"reason": "run canary",
"confirmed": True,
"task_limit": 1,
},
),
(
[
"supersede",
"run-one",
"--publication",
"publication-old",
"--reason",
"valid replacement",
"--yes",
],
{
"action": "supersede",
"task_id": None,
"reason": "valid replacement",
"confirmed": True,
"publication_id": "publication-old",
},
),
],
)
def test_run_lifecycle_actions_use_control_api(
monkeypatch: pytest.MonkeyPatch,
arguments: list[str],
expected: dict[str, object],
) -> None:
configure(monkeypatch)
observed: dict[str, object] = {}
def request(method: str, url: str, **kwargs: object) -> httpx.Response:
observed.update({"method": method, "url": url, **kwargs})
return response(202, {"run_id": "run-one", "action_id": "action-one"})
monkeypatch.setattr("harbor_hf.cli.httpx.request", request)
result = runner.invoke(app, ["run", *arguments])
assert result.exit_code == 0
assert observed["method"] == "POST"
assert observed["url"] == ("https://control.example/api/v1/runs/run-one/actions")
assert observed["json"] == expected
def test_retry_infrastructure_requires_task_or_all_eligible(
monkeypatch: pytest.MonkeyPatch,
) -> None:
configure(monkeypatch)
result = runner.invoke(
app,
["run", "retry-infrastructure", "run-one", "--yes"],
)
assert result.exit_code != 0
assert json.loads(result.output) == {
"error": "provide exactly one of --task or --all-eligible"
}
def test_run_pause_endpoint_uses_confirmed_control_action(
monkeypatch: pytest.MonkeyPatch,
) -> None:
configure(monkeypatch)
observed: dict[str, object] = {}
def request(method: str, url: str, **kwargs: object) -> httpx.Response:
observed.update({"method": method, "url": url, **kwargs})
return response(202, {"run_id": "run-one", "action_id": "action-one"})
monkeypatch.setattr("harbor_hf.cli.httpx.request", request)
result = runner.invoke(
app,
[
"run",
"pause-endpoint",
"run-one",
"--reason",
"terminal cleanup",
"--yes",
],
)
assert result.exit_code == 0
assert observed["method"] == "POST"
assert observed["url"] == ("https://control.example/api/v1/runs/run-one/actions")
assert observed["json"] == {
"action": "pause_endpoint",
"task_id": None,
"reason": "terminal cleanup",
"confirmed": True,
}
def test_run_rejects_retired_action_correction_command(
monkeypatch: pytest.MonkeyPatch,
) -> None:
configure(monkeypatch)
result = runner.invoke(
app,
["run", "correct-action-dispositions", "run-one"],
)
assert result.exit_code == 2
assert "No such command" in result.output
def test_run_help_omits_retired_action_correction_command() -> None:
result = runner.invoke(app, ["run", "--help"])
assert result.exit_code == 0
assert "correct-action-dispositions" not in result.output
def test_capacity_reads_control_api(monkeypatch: pytest.MonkeyPatch) -> None:
configure(monkeypatch)
calls: list[tuple[str, str]] = []
def request(method: str, url: str, **_kwargs: object) -> httpx.Response:
calls.append((method, url))
return response(200, {"max_active_jobs": 16, "configured": True})
monkeypatch.setattr("harbor_hf.cli.httpx.request", request)
result = runner.invoke(app, ["capacity"])
assert result.exit_code == 0
assert json.loads(result.stdout) == {
"configured": True,
"max_active_jobs": 16,
}
assert calls == [("GET", "https://control.example/api/v1/capacity")]
def test_capacity_set_sends_confirmed_cap(
monkeypatch: pytest.MonkeyPatch,
) -> None:
configure(monkeypatch)
observed: dict[str, object] = {}
def request(method: str, url: str, **kwargs: object) -> httpx.Response:
observed.update({"method": method, "url": url, **kwargs})
return response(200, {"max_active_jobs": 128, "configured": True})
monkeypatch.setattr("harbor_hf.cli.httpx.request", request)
result = runner.invoke(
app,
["capacity", "set", "--max-jobs", "128", "--yes"],
)
assert result.exit_code == 0
assert json.loads(result.stdout)["max_active_jobs"] == 128
assert observed["method"] == "POST"
assert observed["url"] == "https://control.example/api/v1/capacity"
assert observed["json"] == {
"max_active_jobs": 128,
"confirmed": True,
}
headers = cast(dict[str, str], observed["headers"])
assert "Idempotency-Key" in headers
def test_cli_reports_safe_api_error(monkeypatch: pytest.MonkeyPatch) -> None:
configure(monkeypatch)
monkeypatch.setattr(
"harbor_hf.cli.httpx.request",
lambda *_args, **_kwargs: response(
503,
{
"error": {
"code": "control_not_ready",
"message": "projection is rebuilding",
}
},
),
)
result = runner.invoke(app, ["run", "list"])
assert result.exit_code == 1
assert "projection is rebuilding" in result.stderr
assert "test-token" not in result.stderr
def test_cli_rejects_insecure_remote_url(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("HARBOR_HF_CONTROL_URL", "http://control.example")
monkeypatch.setenv("HARBOR_HF_CONTROL_BEARER_TOKEN", "test-token")
result = runner.invoke(app, ["status"])
assert result.exit_code != 0
assert "must use HTTPS" in result.output
|