status; } public function getBody(): string { return $this->body; } } class Client { public function __construct(private array $responses = []) { } public function request(string $method, string $url, array $options): VpcErrorTestResponse { $response = array_shift($this->responses); if (!$response instanceof VpcErrorTestResponse) { throw new \RuntimeException('No fake engine response was queued.'); } return $response; } } } namespace { use App\Services\Cloud\CloudEngineClient; use App\Services\Cloud\CloudError; use GuzzleHttp\Client; use GuzzleHttp\VpcErrorTestResponse; require dirname(__DIR__) . '/app/Services/Cloud/CloudError.php'; require dirname(__DIR__) . '/app/Services/Cloud/CloudEngineClient.php'; $assert = static function ($condition, string $message): void { if (!$condition) { throw new RuntimeException($message); } }; $actor = ['user_id' => 1, 'is_admin' => false, 'can_manage' => false]; $deletePath = '/internal/v1/vpcs/' . str_repeat('a', 32) . '/delete'; $restorePath = $deletePath . '/restore'; $safeMessage = 'VPC 宿主机清理尚未全部确认,请稍后重新核对'; $request = static function (string $path, int $status, string $errorCode, string $method = 'POST') use ($actor): CloudError { $client = new CloudEngineClient(new Client([ new VpcErrorTestResponse($status, json_encode([ 'error_code' => $errorCode, 'error' => 'Traceback: host=/root/private key=secret', ], JSON_UNESCAPED_UNICODE)), ])); try { $client->request($method, $path, $actor, []); } catch (CloudError $error) { return $error; } throw new RuntimeException('The fake VPC engine failure did not raise CloudError.'); }; $reviewed = $request($deletePath, 503, 'vpc_delete_incomplete'); $assert($reviewed->getMessage() === $safeMessage && $reviewed->httpStatus === 503 && $reviewed->errorCode === 'vpc_delete_incomplete' && !str_contains($reviewed->getMessage(), '/root') && !str_contains($reviewed->getMessage(), 'private'), 'The reviewed delete-incomplete response lost its fixed message or machine code.'); foreach ([ [$deletePath, 503, 'vpc_unknown_failure'], ['/internal/v1/vpcs/apply', 503, 'vpc_delete_incomplete'], ['/internal/v1/vpcs/' . str_repeat('a', 32) . '/reconcile', 503, 'vpc_delete_incomplete'], ['/internal/v1/vpcs/' . str_repeat('a', 32), 503, 'vpc_delete_incomplete'], [$deletePath . '?unexpected=true', 503, 'vpc_delete_incomplete'], [$deletePath, 500, 'vpc_delete_incomplete'], [$deletePath, 502, 'vpc_delete_incomplete'], ] as [$path, $status, $errorCode]) { $generic = $request($path, $status, $errorCode); $assert($generic->getMessage() === '虚拟化操作未成功(引擎状态 ' . $status . ')' && $generic->httpStatus === 502 && $generic->errorCode === null && !str_contains($generic->getMessage(), '/root') && !str_contains($generic->getMessage(), 'private'), 'A non-allowlisted VPC server error crossed the generic trust boundary.'); } foreach (['GET', 'PUT'] as $method) { $generic = $request( $deletePath, 503, 'vpc_delete_incomplete', $method ); $assert($generic->getMessage() === '虚拟化操作未成功(引擎状态 503)' && $generic->httpStatus === 502 && $generic->errorCode === null, 'A non-POST VPC delete error crossed the typed 503 trust boundary.'); } foreach ([ 'vpc_delete_already_completed', 'vpc_delete_restore_conflict', 'vpc_revision_conflict', 'vpc_delete_restore_identity_mismatch', 'vpc_delete_restore_manifest_mismatch', 'vpc_delete_restore_host_set_mismatch', ] as $errorCode) { $reviewedRestore = $request($restorePath, 409, $errorCode); $assert($reviewedRestore->httpStatus === 409 && $reviewedRestore->errorCode === $errorCode && !str_contains($reviewedRestore->getMessage(), '/root') && !str_contains($reviewedRestore->getMessage(), 'private'), 'A reviewed delete-restore error lost its typed safe boundary.'); } $restoreClient = new CloudEngineClient(new Client([ new VpcErrorTestResponse(200, json_encode([ 'vpc_id' => str_repeat('a', 32), ])), ])); $restoreResult = $restoreClient->vpc('POST', $restorePath, $actor, []); $assert(($restoreResult['vpc_id'] ?? null) === str_repeat('a', 32), 'CloudEngineClient::vpc rejected the exact delete-restore resource path.'); foreach ([ $restorePath . '/extra', '/internal/v1/vpcs/' . str_repeat('g', 32) . '/delete/restore', '/internal/v1/vpcs/' . str_repeat('a', 32) . '/restore', ] as $invalidRestorePath) { try { (new CloudEngineClient(new Client()))->vpc( 'POST', $invalidRestorePath, $actor, [] ); throw new RuntimeException('An invalid delete-restore path was accepted.'); } catch (CloudError $error) { $assert($error->httpStatus === 422, 'Invalid delete-restore path returned the wrong local status.'); } } echo "Cloud VPC engine error-code contract passed.\n"; }