Add remote autoscaler daemon endpoint support
This commit is contained in:
parent
95021a4253
commit
679b5c8d07
11 changed files with 291 additions and 22 deletions
|
|
@ -8,7 +8,7 @@ from typing import Any
|
|||
from fastapi.testclient import TestClient
|
||||
|
||||
from nix_builder_autoscaler.api import create_app
|
||||
from nix_builder_autoscaler.config import AppConfig, CapacityConfig
|
||||
from nix_builder_autoscaler.config import AppConfig, CapacityConfig, ServerConfig
|
||||
from nix_builder_autoscaler.metrics import MetricsRegistry
|
||||
from nix_builder_autoscaler.models import SlotState
|
||||
from nix_builder_autoscaler.providers.clock import FakeClock
|
||||
|
|
@ -18,12 +18,16 @@ from nix_builder_autoscaler.state_db import StateDB
|
|||
def _make_client(
|
||||
*,
|
||||
reconcile_now: Any = None, # noqa: ANN401
|
||||
auth_token: str = "",
|
||||
) -> tuple[TestClient, StateDB, FakeClock, MetricsRegistry]:
|
||||
clock = FakeClock()
|
||||
db = StateDB(":memory:", clock=clock)
|
||||
db.init_schema()
|
||||
db.init_slots("slot", 3, "x86_64-linux", "all")
|
||||
config = AppConfig(capacity=CapacityConfig(reservation_ttl_seconds=1200))
|
||||
config = AppConfig(
|
||||
server=ServerConfig(auth_token=auth_token),
|
||||
capacity=CapacityConfig(reservation_ttl_seconds=1200),
|
||||
)
|
||||
metrics = MetricsRegistry()
|
||||
app = create_app(db, config, clock, metrics, reconcile_now=reconcile_now)
|
||||
return TestClient(app), db, clock, metrics
|
||||
|
|
@ -245,3 +249,20 @@ def test_admin_reconcile_now_success() -> None:
|
|||
assert response.json()["status"] == "accepted"
|
||||
assert response.json()["triggered"] is True
|
||||
assert called["value"] is True
|
||||
|
||||
|
||||
def test_auth_token_required_for_v1_when_configured() -> None:
|
||||
client, _, _, _ = _make_client(auth_token="test-token")
|
||||
response = client.post("/v1/reservations", json={"system": "x86_64-linux", "reason": "test"})
|
||||
assert response.status_code == 401
|
||||
assert response.json()["error"]["code"] == "unauthorized"
|
||||
|
||||
|
||||
def test_auth_token_allows_v1_when_header_matches() -> None:
|
||||
client, _, _, _ = _make_client(auth_token="test-token")
|
||||
response = client.post(
|
||||
"/v1/reservations",
|
||||
json={"system": "x86_64-linux", "reason": "test"},
|
||||
headers={"Authorization": "Bearer test-token"},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue