|
|
<?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.');
|
|
|
};
|
|
|
$coordinationOperation = str_repeat('b', 32);
|
|
|
$vpcApplyRequest = static function (string $method, string $path, int $status,
|
|
|
string $code, ?string $expectedOperation, array $extra = []) use ($actor): CloudError {
|
|
|
$client = new CloudEngineClient(new Client([
|
|
|
new VpcErrorTestResponse($status, json_encode(array_replace([
|
|
|
'error_code' => $code,
|
|
|
'error' => 'Traceback: host=/root/private key=secret',
|
|
|
], $extra), JSON_THROW_ON_ERROR)),
|
|
|
]));
|
|
|
try {
|
|
|
$client->request($method, $path, $actor, [], $expectedOperation);
|
|
|
} catch (CloudError $error) {
|
|
|
return $error;
|
|
|
}
|
|
|
throw new RuntimeException('The fake VPC apply coordination 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 (['/internal/v1/vpcs/apply', $deletePath] as $path) {
|
|
|
$stopped = $request($path, 409, 'vpc_private_remove_requires_stopped');
|
|
|
$assert($stopped->httpStatus === 409
|
|
|
&& $stopped->errorCode === 'vpc_private_remove_requires_stopped'
|
|
|
&& str_contains($stopped->getMessage(), '先关机')
|
|
|
&& !str_contains($stopped->getMessage(), '/root'),
|
|
|
'A reviewed stopped prerequisite lost its durable code or exposed upstream details.');
|
|
|
}
|
|
|
|
|
|
foreach ([
|
|
|
[409, 'coordination_busy', 'VPC 应用协调资源正被占用,请管理员核对协调占用'],
|
|
|
[503, 'coordination_uncertain', 'VPC 应用协调结果未确认,请管理员核对协调记录'],
|
|
|
[503, 'coordination_unavailable', 'VPC 应用协调数据库暂不可用,请管理员核对数据库状态'],
|
|
|
] as [$status, $code, $message]) {
|
|
|
$error = $vpcApplyRequest('POST', '/internal/v1/vpcs/apply', $status,
|
|
|
$code, $coordinationOperation);
|
|
|
$assert($error->httpStatus === $status && $error->errorCode === $code
|
|
|
&& $error->getMessage() === $message
|
|
|
&& !str_contains($error->getMessage(), '/root')
|
|
|
&& !str_contains($error->getMessage(), 'private'),
|
|
|
'A reviewed VPC apply coordination failure lost its exact contract.');
|
|
|
}
|
|
|
|
|
|
foreach ([
|
|
|
['GET', '/internal/v1/vpcs/apply', 409, 'coordination_busy', $coordinationOperation],
|
|
|
['PUT', '/internal/v1/vpcs/apply', 503, 'coordination_uncertain', $coordinationOperation],
|
|
|
['POST', '/internal/v1/vpcs/apply?retry=true', 409, 'coordination_busy', $coordinationOperation],
|
|
|
['POST', '/internal/v1/vpcs/' . str_repeat('a', 32) . '/reconcile', 503,
|
|
|
'coordination_uncertain', $coordinationOperation],
|
|
|
['POST', '/internal/v1/vpcs/apply', 503, 'coordination_busy', $coordinationOperation],
|
|
|
['POST', '/internal/v1/vpcs/apply', 409, 'coordination_uncertain', $coordinationOperation],
|
|
|
['POST', '/internal/v1/vpcs/apply', 409, 'coordination_unavailable', $coordinationOperation],
|
|
|
['POST', '/internal/v1/vpcs/apply', 500, 'coordination_unavailable', $coordinationOperation],
|
|
|
['POST', '/internal/v1/vpcs/apply', 409, 'coordination_busy', null],
|
|
|
['POST', '/internal/v1/vpcs/apply', 503, 'coordination_uncertain', 'invalid'],
|
|
|
] as [$method, $path, $status, $code, $expectedOperation]) {
|
|
|
$error = $vpcApplyRequest($method, $path, $status, $code, $expectedOperation);
|
|
|
$assert($error->httpStatus === 502 && $error->errorCode === null
|
|
|
&& $error->getMessage() === '虚拟化操作未成功(引擎状态 ' . $status . ')',
|
|
|
'A wrong VPC apply endpoint, status or operation identity crossed the coordination boundary.');
|
|
|
}
|
|
|
|
|
|
foreach ([
|
|
|
['operation_id' => $coordinationOperation],
|
|
|
['status' => 'uncertain'],
|
|
|
['safe_to_refund' => false],
|
|
|
['result' => ['state' => 'unknown']],
|
|
|
['mutation_started' => true],
|
|
|
['execution_outcome' => 'unknown'],
|
|
|
] as $extra) {
|
|
|
$error = $vpcApplyRequest('POST', '/internal/v1/vpcs/apply', 503,
|
|
|
'coordination_uncertain', $coordinationOperation, $extra);
|
|
|
$assert($error->httpStatus === 502 && $error->errorCode === null,
|
|
|
'An operation outcome field was mistaken for a bare VPC coordination failure.');
|
|
|
}
|
|
|
$durableVpcOutcome = [
|
|
|
'operation_id' => $coordinationOperation,
|
|
|
'status' => 'uncertain',
|
|
|
'safe_to_refund' => false,
|
|
|
'mutation_started' => true,
|
|
|
'execution_outcome' => 'unknown',
|
|
|
'error_code' => 'coordination_uncertain',
|
|
|
];
|
|
|
$durableVpcClient = new CloudEngineClient(new Client([
|
|
|
new VpcErrorTestResponse(503, json_encode($durableVpcOutcome, JSON_THROW_ON_ERROR)),
|
|
|
]));
|
|
|
$assert($durableVpcClient->request('POST', '/internal/v1/vpcs/apply', $actor, [],
|
|
|
$coordinationOperation) === $durableVpcOutcome + ['_engine_http_status' => 503],
|
|
|
'A durable VPC operation outcome was mistaken for a bare coordination response.');
|
|
|
|
|
|
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.');
|
|
|
}
|
|
|
|
|
|
$powerOperation = str_repeat('c', 32);
|
|
|
$powerPath = '/internal/v1/hosts/2/vms/cbd12dbc-2526-4300-96a5-774a0d431056/actions';
|
|
|
$operationPath = '/internal/v1/operations/' . $powerOperation;
|
|
|
$powerRequest = static function (string $method, string $path, int $status,
|
|
|
string $code, ?string $expectedOperation, array $extra = []) use ($actor): CloudError {
|
|
|
$client = new CloudEngineClient(new Client([
|
|
|
new VpcErrorTestResponse($status, json_encode(array_replace([
|
|
|
'error_code' => $code,
|
|
|
'error' => 'Traceback: host=/root/private key=secret',
|
|
|
], $extra), JSON_THROW_ON_ERROR)),
|
|
|
]));
|
|
|
try {
|
|
|
$client->request($method, $path, $actor,
|
|
|
$method === 'POST' ? ['action' => 'shutdown'] : null, $expectedOperation);
|
|
|
} catch (CloudError $error) {
|
|
|
return $error;
|
|
|
}
|
|
|
throw new RuntimeException('The fake power admission failure did not raise CloudError.');
|
|
|
};
|
|
|
|
|
|
foreach ([['POST', $powerPath], ['GET', $operationPath]] as [$method, $path]) {
|
|
|
$busy = $powerRequest($method, $path, 409, 'coordination_busy', $powerOperation);
|
|
|
$assert($busy->httpStatus === 409 && $busy->errorCode === 'coordination_busy'
|
|
|
&& $busy->getMessage() === '实例操作正在排队,请稍后核对'
|
|
|
&& !str_contains($busy->getMessage(), '/root'),
|
|
|
'A known power admission conflict lost its retryable status or exposed upstream details.');
|
|
|
}
|
|
|
|
|
|
foreach ([
|
|
|
['POST', $powerPath, 503, 'coordination_busy', $powerOperation],
|
|
|
['POST', $powerPath, 409, 'coordination_uncertain', $powerOperation],
|
|
|
['POST', $powerPath, 503, 'coordination_uncertain', $powerOperation],
|
|
|
['POST', $powerPath, 409, 'coordination_unavailable', $powerOperation],
|
|
|
['POST', $powerPath, 403, 'coordination_busy', $powerOperation],
|
|
|
['POST', $powerPath, 409, 'coordination_busy', null],
|
|
|
['POST', $powerPath, 409, 'coordination_busy', 'invalid'],
|
|
|
['POST', $powerPath . '?unexpected=true', 409, 'coordination_busy', $powerOperation],
|
|
|
['PUT', $powerPath, 409, 'coordination_busy', $powerOperation],
|
|
|
['GET', $powerPath, 409, 'coordination_busy', $powerOperation],
|
|
|
['POST', str_replace('/actions', '/network', $powerPath), 409, 'coordination_busy', $powerOperation],
|
|
|
['POST', $operationPath, 409, 'coordination_busy', $powerOperation],
|
|
|
['GET', $operationPath . '?reconcile=false', 409, 'coordination_busy', $powerOperation],
|
|
|
['GET', $operationPath, 409, 'coordination_busy', str_repeat('d', 32)],
|
|
|
['GET', $operationPath, 409, 'coordination_busy', null],
|
|
|
] as [$method, $path, $status, $code, $expectedOperation]) {
|
|
|
$error = $powerRequest($method, $path, $status, $code, $expectedOperation);
|
|
|
$assert($error->httpStatus === 502 && $error->errorCode === null,
|
|
|
'An unreviewed failure became a retryable power admission conflict.');
|
|
|
}
|
|
|
|
|
|
foreach ([
|
|
|
['operation_id' => $powerOperation],
|
|
|
['status' => 'uncertain'],
|
|
|
['result' => ['command_accepted' => true]],
|
|
|
['mutation_started' => true],
|
|
|
['execution_outcome' => 'unknown'],
|
|
|
] as $extra) {
|
|
|
$error = $powerRequest('POST', $powerPath, 409, 'coordination_busy', $powerOperation, $extra);
|
|
|
$assert($error->httpStatus === 502 && $error->errorCode === null,
|
|
|
'An operation outcome was mistaken for a bare admission rejection.');
|
|
|
}
|
|
|
|
|
|
foreach (['failed', 'uncertain'] as $status) {
|
|
|
$outcome = [
|
|
|
'operation_id' => $powerOperation, 'status' => $status,
|
|
|
'safe_to_refund' => false, 'mutation_started' => true,
|
|
|
'execution_outcome' => 'unknown', 'error_code' => 'coordination_busy',
|
|
|
];
|
|
|
$client = new CloudEngineClient(new Client([
|
|
|
new VpcErrorTestResponse(409, json_encode($outcome, JSON_THROW_ON_ERROR)),
|
|
|
]));
|
|
|
$receipt = $client->request('POST', $powerPath, $actor,
|
|
|
['action' => 'shutdown'], $powerOperation);
|
|
|
$assert($receipt === $outcome + ['_engine_http_status' => 409],
|
|
|
'The durable operation outcome lost priority over the admission error policy.');
|
|
|
}
|
|
|
|
|
|
foreach ([
|
|
|
new Client([]),
|
|
|
new Client([new VpcErrorTestResponse(502, '<html>bad gateway</html>')]),
|
|
|
] as $transportClient) {
|
|
|
try {
|
|
|
(new CloudEngineClient($transportClient))->request(
|
|
|
'POST', $powerPath, $actor, ['action' => 'shutdown'], $powerOperation
|
|
|
);
|
|
|
throw new RuntimeException('The fake transport failure did not raise CloudError.');
|
|
|
} catch (CloudError $error) {
|
|
|
$assert($error->httpStatus === 502
|
|
|
&& $error->errorCode === 'engine_transport_uncertain'
|
|
|
&& !str_contains($error->getMessage(), '/root'),
|
|
|
'A local transport failure lost its safe fixed-ID recovery marker.');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
echo "Cloud VPC engine error-code contract passed.\n";
|
|
|
}
|