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

132 lines
4.2 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Services {
final class Config
{
public static function get($name, $default = null)
{
return $name === 'cloud_engine_url' ? 'http://127.0.0.1:8000' : $default;
}
}
}
namespace App\Services\Cloud {
final class CloudSecrets
{
public static function key(string $purpose): string
{
return str_repeat('k', 32);
}
}
}
namespace GuzzleHttp {
final class VpcErrorTestResponse
{
public function __construct(private int $status, private string $body)
{
}
public function getStatusCode(): int
{
return $this->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";
}