feat(cloud): add edge host enrollment

This commit is contained in:
2026-07-13 13:54:16 +08:00
parent cd56facbbf
commit e61dcca801
40 changed files with 2302 additions and 48 deletions
+60 -1
View File
@@ -8,7 +8,11 @@ import httpx
import pytest
from cloud.internal_api.models import AssignmentModel, DeviceSnapshotModel
from host_agent.client import HostAgentClient, StaleLeaseError
from host_agent.client import (
HostAgentClient,
HostAgentEnrollmentClient,
StaleLeaseError,
)
from host_agent.config import HostAgentConfig
@@ -166,3 +170,58 @@ def test_result_report_retries_identical_payload_after_response_loss() -> None:
assert len(payloads) == 2
assert payloads[0] == payloads[1]
assert payloads[0]["failure_reason"] == "planner unavailable"
def test_bootstrap_client_retries_identical_enrollment_and_enrolls_device() -> None:
requests: list[httpx.Request] = []
host_attempts = 0
def handler(request: httpx.Request) -> httpx.Response:
nonlocal host_attempts
requests.append(request)
if request.url.path == "/internal/v1/enrollments":
host_attempts += 1
if host_attempts == 1:
raise httpx.ReadError("response lost", request=request)
return httpx.Response(201, json={"host_id": "host-cloud-a"})
return httpx.Response(201, json={"device_id": "device-cloud-a"})
config = _config(
host_id="",
token="",
enrollment_token="one-time-token",
enrollment_managed=True,
)
with httpx.Client(
transport=httpx.MockTransport(handler),
base_url="https://control.example",
) as http_client:
client = HostAgentEnrollmentClient(
config,
http_client=http_client,
sleep=lambda _delay: None,
)
host = client.enroll_host(
agent_instance_id="agent-instance-a",
host_token="host-token-" + ("x" * 40),
display_name="Edge Mac",
)
client.config = _config(
host_id=host.host_id,
token="host-token-" + ("x" * 40),
enrollment_token="one-time-token",
enrollment_managed=True,
)
device = client.enroll_device(
local_device_id="local-device-a",
driver_type="wda",
name="iPhone",
capability_tags=["ios"],
)
assert host.host_id == "host-cloud-a"
assert device.device_id == "device-cloud-a"
assert len(requests) == 3
assert requests[0].content == requests[1].content
assert requests[0].headers["authorization"] == "Bearer one-time-token"
assert requests[2].headers["authorization"] == ("Bearer host-token-" + ("x" * 40))