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

137 lines
4.4 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudAdminFabricService;
use App\Services\Cloud\CloudError;
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$method = new ReflectionMethod(CloudAdminFabricService::class, 'operation');
$invoke = static fn ($value, bool $node): ?array =>
$method->invoke(null, $value, $node);
$root = $invoke([
'id' => str_repeat('a', 32),
'kind' => 'apply',
'status' => 'running',
'started_at' => 1791501000,
'updated_at' => 1791501010,
'finished_at' => null,
'stderr' => 'must-not-cross-boundary',
'command' => 'must-not-cross-boundary',
], false);
$assert($root === [
'id' => str_repeat('a', 32),
'status' => 'running',
'started_at' => 1791501000,
'updated_at' => 1791501010,
'finished_at' => null,
'kind' => 'apply',
], 'Root operation projection is not exact or leaked an unreviewed field.');
$node = $invoke([
'id' => str_repeat('a', 32),
'status' => 'running',
'phase' => 'apply',
'checkpoint' => 'future_checkpoint:/root/private',
'reason' => 'future_reason --private-key',
'started_at' => 1791501000,
'updated_at' => 1791501010,
'finished_at' => null,
], true);
$assert(($node['checkpoint'] ?? null) === 'unknown'
&& ($node['reason'] ?? null) === 'unknown'
&& !in_array('future_checkpoint:/root/private', $node, true)
&& !in_array('future_reason --private-key', $node, true),
'Unknown node diagnostics were not replaced by fixed markers.');
foreach ([
'journal_dispatch',
'journal_artifact_restore',
'journal_nft_restore',
'journal_runtime_restore',
'journal_finalize',
] as $checkpoint) {
$projected = $invoke(array_replace($node, [
'checkpoint' => $checkpoint,
'reason' => 'journal_conflict',
]), true);
$assert(($projected['checkpoint'] ?? null) === $checkpoint
&& ($projected['reason'] ?? null) === 'journal_conflict',
'Reviewed journal checkpoint was hidden: ' . $checkpoint);
}
foreach ([
'preflight' => 'ownership_conflict',
'preflight_sysctl' => 'sysctl_conflict',
'preflight_tools' => 'tool_missing',
'preflight_artifacts' => 'ownership_conflict',
'preflight_firewall' => 'firewall_conflict',
'preflight_vpc' => 'readback_failed',
'preflight_underlay' => 'underlay_conflict',
'preflight_routes' => 'underlay_conflict',
'preflight_persistence' => 'persistence_conflict',
] as $checkpoint => $reason) {
$projected = $invoke(array_replace($node, [
'checkpoint' => $checkpoint,
'reason' => $reason,
'stderr' => 'must-not-cross-boundary',
'command' => 'must-not-cross-boundary',
]), true);
$assert(($projected['checkpoint'] ?? null) === $checkpoint
&& ($projected['reason'] ?? null) === $reason
&& !array_key_exists('stderr', $projected)
&& !array_key_exists('command', $projected),
'Reviewed preflight diagnostics were hidden or leaked raw output: ' . $checkpoint);
}
$invalid = $invoke([
'id' => '../not-an-operation',
'kind' => 'future_kind',
'status' => 'future_status',
'started_at' => true,
'updated_at' => '1791501010',
'finished_at' => -1,
], false);
$assert($invalid === [
'id' => null,
'status' => 'unknown',
'started_at' => null,
'updated_at' => null,
'finished_at' => null,
'kind' => 'unknown',
], 'Malformed root operation values were not safely normalized.');
$mutationMethod = new ReflectionMethod(
CloudAdminFabricService::class, 'mutationOperation'
);
$mutation = static fn ($value, array $kinds): array =>
$mutationMethod->invoke(null, $value, $kinds);
$assert($mutation($root, ['apply']) === $root,
'A valid asynchronous apply receipt was rejected.');
foreach ([
'missing' => null,
'invalid-id' => array_replace($root, ['id' => null]),
'wrong-kind' => array_replace($root, ['kind' => 'reconcile']),
'unknown-status' => array_replace($root, ['status' => 'unknown']),
] as $label => $receipt) {
try {
$mutation($receipt, ['apply']);
} catch (CloudError $error) {
$assert($error->httpStatus === 502,
'Malformed ' . $label . ' receipt returned the wrong status.');
continue;
}
throw new RuntimeException(
'Malformed ' . $label . ' receipt did not fail closed.'
);
}
echo "Administrator Fabric operation projection contracts passed.\n";