feat(host-agent): add Console task submission with Host self-submission client
Adds a CSRF-protected task submission form to the local Console Tasks page
This commit is contained in:
@@ -11,6 +11,8 @@ from cloud.internal_api.models import AssignmentModel, DeviceSnapshotModel
|
||||
from host_agent.client import (
|
||||
HostAgentClient,
|
||||
HostAgentEnrollmentClient,
|
||||
HostAgentAPIError,
|
||||
HostTaskSubmissionUnknownError,
|
||||
StaleLeaseError,
|
||||
)
|
||||
from host_agent.config import HostAgentConfig
|
||||
@@ -172,6 +174,161 @@ def test_result_report_retries_identical_payload_after_response_loss() -> None:
|
||||
assert payloads[0]["failure_reason"] == "planner unavailable"
|
||||
|
||||
|
||||
def test_submit_self_task_posts_once_and_returns_task_id() -> None:
|
||||
requests: list[httpx.Request] = []
|
||||
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
requests.append(request)
|
||||
return httpx.Response(201, json={"task_id": "task-cloud-1"})
|
||||
|
||||
async def scenario() -> None:
|
||||
async with httpx.AsyncClient(
|
||||
transport=httpx.MockTransport(handler),
|
||||
base_url="https://control.example",
|
||||
) as http_client:
|
||||
client = HostAgentClient(_config(), http_client=http_client)
|
||||
response = await client.submit_self_task(
|
||||
goal="open settings",
|
||||
device_id=None,
|
||||
)
|
||||
|
||||
assert response.task_id == "task-cloud-1"
|
||||
|
||||
asyncio.run(scenario())
|
||||
assert len(requests) == 1
|
||||
assert requests[0].url.path == "/internal/v1/hosts/host-a/tasks"
|
||||
assert requests[0].headers["authorization"] == "Bearer host-secret"
|
||||
payload = json.loads(requests[0].content)
|
||||
assert payload == {
|
||||
"host_id": "host-a",
|
||||
"goal": "open settings",
|
||||
"device_id": None,
|
||||
}
|
||||
|
||||
|
||||
def test_submit_self_task_does_not_retry_transport_failure() -> None:
|
||||
attempts = 0
|
||||
sleeps: list[float] = []
|
||||
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
nonlocal attempts
|
||||
attempts += 1
|
||||
raise httpx.ConnectError("network down", request=request)
|
||||
|
||||
async def sleep(delay: float) -> None:
|
||||
sleeps.append(delay)
|
||||
|
||||
async def scenario() -> None:
|
||||
async with httpx.AsyncClient(
|
||||
transport=httpx.MockTransport(handler),
|
||||
base_url="https://control.example",
|
||||
) as http_client:
|
||||
client = HostAgentClient(_config(), http_client=http_client, sleep=sleep)
|
||||
with pytest.raises(HostTaskSubmissionUnknownError):
|
||||
await client.submit_self_task(goal="open settings")
|
||||
|
||||
asyncio.run(scenario())
|
||||
assert attempts == 1
|
||||
assert sleeps == []
|
||||
|
||||
|
||||
def test_submit_self_task_treats_5xx_as_unknown_outcome_without_retry() -> None:
|
||||
attempts = 0
|
||||
sleeps: list[float] = []
|
||||
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
nonlocal attempts
|
||||
attempts += 1
|
||||
return httpx.Response(502, json={"detail": "bad gateway"})
|
||||
|
||||
async def sleep(delay: float) -> None:
|
||||
sleeps.append(delay)
|
||||
|
||||
async def scenario() -> None:
|
||||
async with httpx.AsyncClient(
|
||||
transport=httpx.MockTransport(handler),
|
||||
base_url="https://control.example",
|
||||
) as http_client:
|
||||
client = HostAgentClient(_config(), http_client=http_client, sleep=sleep)
|
||||
with pytest.raises(HostTaskSubmissionUnknownError):
|
||||
await client.submit_self_task(goal="open settings")
|
||||
|
||||
asyncio.run(scenario())
|
||||
assert attempts == 1
|
||||
assert sleeps == []
|
||||
|
||||
|
||||
def test_submit_self_task_raises_definitive_error_on_4xx_rejection() -> None:
|
||||
attempts = 0
|
||||
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
nonlocal attempts
|
||||
attempts += 1
|
||||
return httpx.Response(
|
||||
403,
|
||||
json={"detail": "Host self-submission is disabled"},
|
||||
)
|
||||
|
||||
async def scenario() -> None:
|
||||
async with httpx.AsyncClient(
|
||||
transport=httpx.MockTransport(handler),
|
||||
base_url="https://control.example",
|
||||
) as http_client:
|
||||
client = HostAgentClient(_config(), http_client=http_client)
|
||||
with pytest.raises(HostAgentAPIError) as error:
|
||||
await client.submit_self_task(goal="open settings")
|
||||
assert "host-secret" not in str(error.value)
|
||||
|
||||
asyncio.run(scenario())
|
||||
assert attempts == 1
|
||||
|
||||
|
||||
def test_submit_self_task_treats_malformed_success_as_unknown() -> None:
|
||||
attempts = 0
|
||||
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
nonlocal attempts
|
||||
attempts += 1
|
||||
return httpx.Response(201, json={"unexpected": "shape"})
|
||||
|
||||
async def scenario() -> None:
|
||||
async with httpx.AsyncClient(
|
||||
transport=httpx.MockTransport(handler),
|
||||
base_url="https://control.example",
|
||||
) as http_client:
|
||||
client = HostAgentClient(_config(), http_client=http_client)
|
||||
with pytest.raises(HostTaskSubmissionUnknownError):
|
||||
await client.submit_self_task(goal="open settings")
|
||||
|
||||
asyncio.run(scenario())
|
||||
assert attempts == 1
|
||||
|
||||
|
||||
def test_submit_self_task_does_not_duplicate_when_response_is_lost() -> None:
|
||||
attempts = 0
|
||||
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
nonlocal attempts
|
||||
attempts += 1
|
||||
raise httpx.ReadError("response lost", request=request)
|
||||
|
||||
async def scenario() -> None:
|
||||
async with httpx.AsyncClient(
|
||||
transport=httpx.MockTransport(handler),
|
||||
base_url="https://control.example",
|
||||
) as http_client:
|
||||
client = HostAgentClient(
|
||||
_config(),
|
||||
http_client=http_client,
|
||||
sleep=lambda delay: asyncio.sleep(0),
|
||||
)
|
||||
with pytest.raises(HostTaskSubmissionUnknownError):
|
||||
await client.submit_self_task(goal="open settings")
|
||||
|
||||
asyncio.run(scenario())
|
||||
assert attempts == 1
|
||||
|
||||
|
||||
def test_bootstrap_client_directly_enrolls_and_enrolls_device() -> None:
|
||||
requests: list[httpx.Request] = []
|
||||
host_attempts = 0
|
||||
|
||||
Reference in New Issue
Block a user