You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/CloudAgentActionCutoverCont...

84 lines
4.7 KiB
PHP

<?php
// Dependency-free source contract for the first mutating page cutover. The
// full integration test belongs to the panel CI (PHP/Composer is not part of
// this development shell); this guard ensures the action endpoint cannot
// silently fall back to the legacy queue or transport.
$root = dirname(__DIR__);
$read = static function (string $path) use ($root): string {
$value = file_get_contents($root . '/' . $path);
if (!is_string($value)) {
throw new RuntimeException('Missing Agent action cutover source: ' . $path);
}
return $value;
};
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$controller = $read('app/Controllers/CloudApiController.php');
$service = $read('app/Services/Cloud/CloudAgentActionService.php');
$identity = $read('app/Services/Cloud/CloudAgentOperationIdentity.php');
$gateway = $read('app/Services/Cloud/CloudAgentVmGateway.php');
$repository = $read('app/Services/Cloud/CloudAgentOperationRepository.php');
$runtime = $read('public/cloud/runtime.js');
$actionOffset = strpos($controller, 'public function actions(');
$actionBody = $actionOffset === false ? '' : substr($controller, $actionOffset);
$actionEnd = $actionBody === '' ? false : strpos($actionBody, "\n public function reinstall(");
if ($actionEnd !== false) {
$actionBody = substr($actionBody, 0, $actionEnd);
}
$assert($actionOffset !== false
&& $actionEnd !== false
&& str_contains($actionBody, 'agentActionService()->dispatch')
&& str_contains($actionBody, 'CloudAgentDispatchUncertain')
&& str_contains($actionBody, "'/api/v1/jobs/'"),
'Action endpoint is not wired to the Agent operation/status boundary.');
foreach (['CloudJobService::enqueue', 'CloudEngineClient', 'ssh', 'SSH', 'shell_exec', 'curl_exec'] as $forbidden) {
$assert(!str_contains($actionBody, $forbidden),
'Action endpoint still contains a legacy transport/queue reference: ' . $forbidden);
}
$assert(str_contains($service, 'CloudAgentOperationIdentity::uuidForIdempotencyKey')
&& str_contains($service, '重试必须同时提供 operation_id 和幂等键')
&& !str_contains($service, 'idempotencyKeyForOperationId')
&& str_contains($service, 'CloudAgentVmGateway')
&& str_contains($service, "'action'")
&& str_contains($service, "'instance_id' => (string)"),
'Action service lost typed identity, gateway dispatch, or browser DTO projection.');
$assert(str_contains($identity, 'random_bytes(16)')
&& str_contains($identity, 'uuidForIdempotencyKey')
&& str_contains($identity, 'validateIdempotencyKey')
&& !str_contains($identity, 'idempotencyKeyForOperationId'),
'Agent operation identity must use UUIDs and bounded idempotency keys without deriving a key from an opaque operation UUID.');
$assert(str_contains($gateway, "'poweroff' => 'destroy'")
&& str_contains($gateway, 'isReducedFailure')
&& str_contains($gateway, 'isDefinitiveReducedRejection')
&& str_contains($gateway, 'agent_operation_transport_uncertain'),
'Gateway lost the panel-to-Agent power mapping or reduced-error handling.');
$assert(str_contains($repository, 'lockInstanceState')
&& str_contains($repository, "'instance_busy'")
&& str_contains($repository, "whereIn('status', ['pending', 'running', 'uncertain'])")
&& str_contains($repository, 'in_array($request[\'kind\'], self::MUTATING_KINDS, true)'),
'Agent mutations lost the per-instance durable serialization gate.');
$assert(str_contains($actionBody, 'bodyIdempotency !== $headerIdempotency')
&& str_contains($actionBody, 'bodyOperationId !== $headerOperationId')
&& str_contains($actionBody, '重试必须同时提供 operation_id 和幂等键')
&& str_contains($actionBody, 'CloudAgentOperationException'),
'Action endpoint must reject split identity headers and preserve deterministic repository errors.');
$assert(str_contains($runtime, 'POWER_OPERATION_STORAGE_PREFIX')
&& str_contains($runtime, 'powerOperationStorageKey(vmId, action, vmUuid = \'\')')
&& str_contains($runtime, 'powerOperationMemory')
&& str_contains($runtime, "body: { action, idempotency_key: idempotencyKey }")
&& str_contains($runtime, "headers: { 'Idempotency-Key': idempotencyKey }")
&& str_contains($runtime, 'allowUncertain: true')
&& str_contains($runtime, 'reconcile: true')
&& str_contains($runtime, '${path}/reconcile')
&& str_contains($runtime, '结果待核对')
&& str_contains($runtime, 'clearPowerOperationKey'),
'Power UI must persist one bounded idempotency key and surface uncertain Agent results without retrying.');
fwrite(STDOUT, "Cloud Agent action cutover contract passed.\n");