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/CloudAgentOperationReconcil...

77 lines
3.3 KiB
PHP

<?php
// Source-level contract for the explicit Agent operation read-back endpoint.
// The panel development shell may not have PHP/Composer, so the executable
// integration test belongs in panel CI; these assertions still prevent a
// future change from turning reconciliation into a mutation replay or an
// unauthorised generic Agent proxy.
$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 reconcile contract source: ' . $path);
}
return $value;
};
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$service = $read('app/Services/Cloud/CloudAgentOperationReconcileService.php');
$controller = $read('app/Controllers/CloudApiController.php');
$handle = $read('app/Services/Cloud/CloudAgentHandle.php');
$routes = $read('config/routes_cloud.php');
$middleware = $read('app/Middleware/CloudApi.php');
foreach ([
'CloudAgentOperationEnvelope::fromResponse',
'getOperation(',
'->reconcile(',
"'reconcile'",
'CloudAgentOperationRepository',
'->finalize(',
'agent_reconcile_result_invalid',
'hash_equals',
'resolveReadOnly',
"->where('host_id'",
"->where('id'",
'instance_uuid',
"record['target']",
] as $required) {
$assert(str_contains($service, $required),
'Reconcile service is missing a required safety boundary: ' . $required);
}
foreach ([
'CloudEngineClient', 'CloudJobService', 'CloudWorkerService',
'shell_exec', 'curl_exec', 'CloudAgentVmGateway', '->operation(',
] as $forbidden) {
$assert(!str_contains($service, $forbidden),
'Reconcile service crossed a legacy or mutation boundary: ' . $forbidden);
}
$uuid = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
$assert(str_contains($routes, "/jobs/{id:{$uuid}}/reconcile")
&& str_contains($routes, 'CloudApiController:reconcileJob'),
'The explicit UUID reconcile route is missing.');
$assert(str_contains($middleware, '/api/v1/jobs/[0-9a-f]{8}-[0-9a-f]{4}')
&& str_contains($middleware, '/reconcile$'),
'The reconcile route is not classified as database-only by CloudApi middleware.');
$methodOffset = strpos($controller, 'public function reconcileJob(');
$assert($methodOffset !== false, 'CloudApiController has no reconcileJob endpoint.');
$method = substr($controller, $methodOffset);
$assert(str_contains($method, 'CloudAgentOperationIdentity::validateUuid')
&& str_contains($method, 'CloudAgentOperationReconcileService')
&& str_contains($method, 'CloudAgentOperationStatusService::forActor')
&& str_contains($method, "=== 'uncertain' ? 202 : 200"),
'reconcileJob does not validate, authorize through the service, and project the safe status DTO.');
$assert(str_contains($handle, 'public function reconcile(')
&& str_contains($handle, 'typed read-back endpoint')
&& !str_contains($handle, "if (\$this->readOnly) {\n throw new CloudError('降级宿主机 Agent 仅允许只读观察', 503);\n }\n return \$this->client->reconcile"),
'A degraded read-only Agent handle must keep the non-replaying reconcile endpoint available.');
fwrite(STDOUT, "Cloud Agent operation reconcile contract passed.\n");