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

137 lines
5.5 KiB
PHTML

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Command\CloudTrafficPoolAttention;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$parse = new ReflectionMethod(CloudTrafficPoolAttention::class, 'parse');
$inspection = $parse->invoke(null, [
'xcat', 'cloudTrafficPoolAttention', 'inspect', '--pool-id=42',
]);
$assert($inspection === ['action' => 'inspect', 'pool_id' => 42],
'Inspect did not preserve the exact validated pool identity.');
$uuid = '550e8400-e29b-41d4-a716-446655440000';
$confirm = 'RESOLVE_TRAFFIC_POOL:42:9:' . $uuid;
$resolution = $parse->invoke(null, [
'xcat', 'cloudTrafficPoolAttention', 'resolve',
'--resolution-key=' . $uuid,
'--confirm=' . $confirm,
'--expected-version=9',
'--apply',
'--pool-id=42',
]);
$assert($resolution === [
'action' => 'resolve', 'pool_id' => 42,
'expected_version' => 9, 'resolution_key' => $uuid,
],
'Resolve did not bind the exact pool, version and resolution UUID.');
$resolutionInspection = $parse->invoke(null, [
'xcat', 'cloudTrafficPoolAttention', 'inspect-resolution',
'--resolution-key=' . $uuid,
]);
$assert($resolutionInspection === [
'action' => 'inspect-resolution', 'resolution_key' => $uuid,
],
'Resolution inspection did not bind the exact immutable UUID.');
$invalidCases = [
['inspect'],
['inspect', '--pool-id=0'],
['inspect', '--pool-id=01'],
['inspect', '--pool-id=42', '--extra=true'],
['inspect', '--pool-id=42', '--pool-id=42'],
['inspect-resolution'],
['inspect-resolution', '--resolution-key=invalid'],
['inspect-resolution', '--resolution-key=' . $uuid, '--extra=true'],
['resolve', '--pool-id=42', '--expected-version=9',
'--resolution-key=' . $uuid, '--confirm=' . $confirm],
['resolve', '--pool-id=42', '--expected-version=9',
'--resolution-key=' . $uuid, '--apply', '--confirm=wrong'],
['resolve', '--pool-id=42', '--expected-version=9',
'--resolution-key=' . $uuid, '--apply=true',
'--confirm=' . $confirm],
['resolve', '--pool-id=42', '--expected-version=0',
'--resolution-key=' . $uuid, '--apply', '--confirm=' . $confirm],
['resolve', '--pool-id=42', '--expected-version=9',
'--resolution-key=550e8400-e29b-11d4-a716-446655440000',
'--apply', '--confirm=' . $confirm],
['resolve', '--pool-id=42', '--expected-version=9',
'--resolution-key=' . $uuid, '--apply', '--confirm=' . $confirm,
'--force'],
];
foreach ($invalidCases as $case) {
try {
$parse->invoke(null, array_merge([
'xcat', 'cloudTrafficPoolAttention',
], $case));
throw new RuntimeException(
'Invalid arguments were accepted: ' . implode(' ', $case)
);
} catch (ReflectionException $error) {
throw $error;
} catch (Throwable $error) {
$previous = $error instanceof ReflectionException
? $error : ($error->getPrevious() ?? $error);
$assert($previous instanceof InvalidArgumentException,
'Invalid arguments produced the wrong failure: '
. implode(' ', $case));
}
}
$source = file_get_contents(dirname(__DIR__)
. '/app/Command/CloudTrafficPoolAttention.php');
$accounting = file_get_contents(dirname(__DIR__)
. '/app/Services/Cloud/CloudTrafficPoolAccounting.php');
$xcat = file_get_contents(dirname(__DIR__) . '/app/Command/XCat.php');
$assert(is_string($source) && is_string($accounting) && is_string($xcat),
'Unable to read traffic-pool maintenance sources.');
$assert(str_contains($source,
'CloudTrafficPoolAccounting::resolveAttention(')
&& str_contains($source, 'CloudTrafficPoolAccounting::verifyProjection(')
&& !str_contains($source, 'CloudAgentClient')
&& !str_contains($source, 'CloudJob')
&& !str_contains($source, 'CloudInstanceNetworkRestriction')
&& !str_contains($source, '?callable')
&& !str_contains($source, '->update(')
&& !str_contains($source, '->delete('),
'Maintenance CLI bypasses accounting or acquired a remote/direct mutation.');
$assert(str_contains($accounting,
"'event_key' => 'attention-resolution:' . \$resolutionKey")
&& str_contains($accounting,
'账本核对标识必须是小写规范 UUIDv4'),
'Resolution UUID is not a global accounting-module invariant.');
$assert(str_contains($xcat, "'cloudTrafficPoolAttention'")
&& str_contains($xcat, 'inspect-resolution --resolution-key=UUID')
&& str_contains($xcat, '--confirm=RESOLVE_TRAFFIC_POOL:N:V:UUID'),
'The audited maintenance command is not reachable or documented by XCat.');
$childCode = 'require ' . var_export(
dirname(__DIR__) . '/vendor/autoload.php',
true
) . '; exit((new \\App\\Command\\XCat(['
. "'xcat','cloudTrafficPoolAttention','inspect','--pool-id=bad'"
. ']))->boot());';
$process = proc_open(
[PHP_BINARY, '-r', $childCode],
[1 => ['pipe', 'w'], 2 => ['pipe', 'w']],
$pipes
);
$assert(is_resource($process), 'Unable to start the XCat exit-code probe.');
$childStdout = stream_get_contents($pipes[1]);
$childStderr = stream_get_contents($pipes[2]);
fclose($pipes[1]);
fclose($pipes[2]);
$childStatus = proc_close($process);
$assert($childStatus === 1 && $childStdout === ''
&& str_contains($childStderr, 'pool-id 必须是正整数'),
'Formal XCat routing did not fail closed through stderr and exit code 1.');
echo "Cloud traffic-pool attention command safety contract passed.\n";