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'; $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.'); } echo "Cloud VPC engine error-code contract passed.\n"; }