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

176 lines
7.4 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudPublicNetworkTransitionCommand;
use App\Services\Cloud\CloudPublicNetworkTransitionWorkflow;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$root = dirname(__DIR__);
$source = file_get_contents(
$root . '/app/Services/Cloud/CloudPublicNetworkTransitionWorkflow.php'
);
$billing = file_get_contents(
$root . '/app/Services/Cloud/CloudIpChangeBillingService.php'
);
$activeIdentity = file_get_contents(
$root . '/app/Services/Cloud/CloudActivePublicNetworkClaimSet.php'
);
$worker = file_get_contents(
$root . '/app/Services/Cloud/CloudWorkerService.php'
);
$recovery = file_get_contents(
$root . '/app/Services/Cloud/CloudOperationRecoveryService.php'
);
$tenantController = file_get_contents(
$root . '/app/Controllers/CloudApiController.php'
);
$adminController = file_get_contents(
$root . '/app/Controllers/CloudAdminInstanceController.php'
);
$assert(is_string($source) && is_string($billing)
&& is_string($activeIdentity) && is_string($worker)
&& is_string($recovery) && is_string($tenantController)
&& is_string($adminController), 'Transition workflow sources are missing.');
$workflow = new ReflectionClass(CloudPublicNetworkTransitionWorkflow::class);
$public = array_values(array_map(
static fn (ReflectionMethod $method): string => $method->getName(),
array_filter(
$workflow->getMethods(ReflectionMethod::IS_PUBLIC),
static fn (ReflectionMethod $method): bool =>
$method->getDeclaringClass()->getName()
=== CloudPublicNetworkTransitionWorkflow::class
)
));
$assert($workflow->isFinal() && $public === [
'begin',
'markDispatched',
'markJobUncertain',
'recordEvidence',
'finishJobLocked',
'publishPreparedLocked',
'retryPendingFinalization',
], 'Stage 4 transition seam drifted.');
$command = new ReflectionClass(CloudPublicNetworkTransitionCommand::class);
foreach (['userPaidAutomatic', 'adminExact', 'poolReplacement'] as $factory) {
$assert($command->hasMethod($factory)
&& $command->getMethod($factory)->isStatic(),
'Transition command factory is missing: ' . $factory);
}
$ordered = [
"User::where('id', \$command->actorUserId())",
"CloudInstance::where('id', \$command->instanceId())",
"'transition_key', \$command->transitionKey()",
"CloudHost::where('id', (int) \$instance->host_id)",
"CloudHostAgent::where('host_id', (int) \$instance->host_id)",
"CloudResourcePool::where(\n 'id', (int) \$instance->resource_pool_id",
'CloudHostNetworkAttachment::whereIn(',
"CloudPublicIpPool::whereIn('id', \$poolIds)",
"CloudResourceReservation::where(\n 'id', \$reservationId",
"->where('reservation_id', \$reservationId)\n ->orderBy('address_family')",
"CloudPublicIp::where('host_id', (int) \$instance->host_id)",
];
$cursor = -1;
foreach ($ordered as $needle) {
$position = strpos($source, $needle);
$assert($position !== false && $position > $cursor,
'Transition lock order drifted at: ' . $needle);
$cursor = $position;
}
$assert(str_contains($source, "\$transition->state = 'reserved';")
&& str_contains($source, "'active_claim' => null")
&& str_contains($source, "'state' => 'reserved'")
&& str_contains($source, "\$transition->state = 'queued';")
&& strpos($source, "\$transition->state = 'reserved';")
< strpos($source, "\$transition->state = 'queued';"),
'Transition must fence targets before atomically becoming queued.');
$assert(str_contains($source,
'CloudInstanceMigrationService::assertInstanceMutationAvailable(')
&& str_contains($source, "DB::table('cloud_backup_recoveries')")
&& str_contains($source,
'CloudJobService::blockingQueryForInstance('),
'Open migration, recovery, or job work is not fenced before writes.');
$assert(str_contains($source, "(\$spec['managed'] ?? false) !== true")
&& str_contains($source,
"(\$spec['os_source'] ?? null) !== 'cloud_template'"),
'The deep workflow seam does not enforce managed cloud-template admission.');
$assert(str_contains($source, "\$job->kind = 'network_change';")
&& str_contains($source,
"\$job->public_network_transition_id = (int) \$transition->id;")
&& str_contains($source,
"'public_network' => \$targetSnapshot->toArray()")
&& str_contains($source, "'price_cents' => \$command->priceCents()")
&& str_contains($source, "'charged_amount' => \$invoice === null")
&& !str_contains($source, "'source_public_ip_id' =>")
&& !str_contains($source, "'target_public_ip_id' =>"),
'Queued job does not freeze the complete transition contract.');
$assert(str_contains($billing,
'CloudPublicNetworkTransitionCommand $command')
&& str_contains($billing, "\$command->kind() !== 'user_paid'")
&& str_contains($activeIdentity,
'CloudPublicNetworkClaimSetIdentity::fromLedger('),
'Billing or claim-set identity is not delegated through the deep seam.');
$assert(str_contains($worker, 'markDispatched($job)')
&& str_contains($worker, 'recordEvidence(')
&& str_contains($worker, 'finishJobLocked(')
&& str_contains($worker, 'markJobUncertain(')
&& str_contains($recovery, 'publishPreparedLocked('),
'Worker or recovery coordinator bypasses the Stage 4 transition seam.');
$allowedBeginCallers = [
realpath($root . '/app/Controllers/CloudApiController.php'),
realpath($root . '/app/Controllers/CloudAdminInstanceController.php'),
];
$actualBeginCallers = [];
foreach ([$root . '/app', $root . '/config'] as $directory) {
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$directory,
FilesystemIterator::SKIP_DOTS
)
);
foreach ($iterator as $file) {
if (!$file->isFile() || $file->getExtension() !== 'php'
|| $file->getPathname()
=== $root
. '/app/Services/Cloud/'
. 'CloudPublicNetworkTransitionWorkflow.php') {
continue;
}
$contents = file_get_contents($file->getPathname());
if (is_string($contents) && str_contains(
$contents,
'CloudPublicNetworkTransitionWorkflow::begin('
)) {
$actualBeginCallers[] = realpath($file->getPathname());
}
}
}
$actualBeginCallers = array_values(array_unique($actualBeginCallers));
sort($actualBeginCallers);
sort($allowedBeginCallers);
$assert($actualBeginCallers === $allowedBeginCallers,
'Production workflow callers escaped the two Stage 5 controller seams.');
$assert(str_contains($tenantController,
'CloudPublicNetworkTransitionCommand::')
&& str_contains($tenantController, 'userPaidAutomatic(')
&& str_contains($tenantController,
'CloudPublicNetworkTransitionWorkflow::begin($command)')
&& !str_contains($tenantController,
"CloudJobService::enqueue(\n \$instance,\n \$actor,\n 'network_change'")
&& str_contains($adminController,
'CloudPublicNetworkTransitionCommand::adminExact(')
&& str_contains($adminController,
'CloudPublicNetworkTransitionWorkflow::begin($command)'),
'A Stage 5 controller bypasses the command plus transition seam.');
echo "Public-network transition workflow contract tests passed.\n";