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

122 lines
5.1 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'
);
$assert(is_string($source) && is_string($billing)
&& is_string($activeIdentity), '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'],
'Stage 3 must expose only the atomic begin seam.');
$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, "\$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.');
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());
$assert(!is_string($contents)
|| !str_contains($contents,
'CloudPublicNetworkTransitionWorkflow::begin('),
'Stage 3 workflow was opened through a production caller: '
. $file->getPathname());
}
}
echo "Public-network transition workflow contract tests passed.\n";