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

175 lines
7.4 KiB
PHP

<?php
$root = dirname(__DIR__);
$schema = file_get_contents($root . '/sql/cloud.sql');
$setup = file_get_contents($root . '/app/Command/CloudSetup.php');
$service = file_get_contents(
$root . '/app/Services/Cloud/CloudPublicIpPoolReplacementBatchService.php'
);
$itemModel = file_get_contents(
$root . '/app/Models/CloudPublicIpPoolReplacementBatchItem.php'
);
$jobs = file_get_contents($root . '/app/Services/Cloud/CloudJobService.php');
$worker = file_get_contents($root . '/app/Services/Cloud/CloudWorkerService.php');
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$method = static function (string $contents, string $start, string $end) use (
$assert
): string {
$offset = strpos($contents, $start);
$limit = $offset === false ? false : strpos($contents, $end, $offset + 1);
$assert($offset !== false && $limit !== false && $limit > $offset,
'Replacement method source is missing: ' . $start);
return substr($contents, $offset, $limit - $offset);
};
$assert(is_string($schema)
&& str_contains($schema,
'CREATE TABLE cloud_public_ip_pool_replacement_batches')
&& str_contains($schema,
'CREATE TABLE cloud_public_ip_pool_replacement_batch_items')
&& str_contains($schema, 'cloud_public_pool_replacement_active')
&& str_contains($schema, 'source_initial_state')
&& str_contains($schema,
"source_initial_state IN ('active','draining')")
&& str_contains($schema, 'source_public_ip_pool_id, active_claim')
&& str_contains($schema, 'target_public_ip_pool_id, active_claim, id')
&& str_contains($schema, 'cloud_public_pool_replacement_child')
&& str_contains($schema, 'address_family TINYINT UNSIGNED NOT NULL')
&& str_contains($schema, 'child_job_id BIGINT UNSIGNED DEFAULT NULL')
&& str_contains($schema,
'chk_cloud_public_pool_replacement_item_family')
&& str_contains($schema, 'address_family IN (4,6)')
&& str_contains($schema,
"state = 'preparing' AND child_job_id IS NULL")
&& str_contains($schema,
"state IN ('queued','succeeded','failed','unknown')")
&& str_contains($schema, 'AND child_job_id IS NOT NULL')
&& str_contains($schema, "state IN ('running','blocked','succeeded','failed')"),
'Public-pool replacement lacks durable batch, child, or active-claim schema.');
$assert(is_string($setup)
&& str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v30-public-network-batch-identity'")
&& str_contains($setup,
"'cloud_public_ip_pool_replacement_batches'")
&& str_contains($setup,
"'cloud_public_ip_pool_replacement_batch_items'")
&& str_contains($setup,
"'chk_cloud_public_pool_replacement_source_initial'")
&& str_contains($setup,
"'chk_cloud_public_pool_replacement_item_family'")
&& str_contains($setup,
"'chk_cloud_public_pool_replacement_claim'"),
'CloudSetup does not enforce the replacement batch schema.');
$assert(is_string($itemModel)
&& str_contains($itemModel, "'address_family' => 'int'")
&& str_contains($itemModel, "'child_job_id' => 'int'"),
'Replacement batch-item model lost family or nullable-child identity.');
$assert(is_string($service)
&& str_contains($service, 'expected_source_revision')
&& str_contains($service, 'expected_target_revision')
&& str_contains($service, 'confirm_replacement')
&& str_contains($service, "->where('active_claim', 1)")
&& str_contains($service,
"'source_public_ip_pool_id', \$replacementPoolIds")
&& str_contains($service,
"'target_public_ip_pool_id', \$replacementPoolIds")
&& str_contains($service, "\$batch->state = 'blocked'")
&& str_contains($service, 'finishResolvedChildLocked')
&& str_contains($service, "self::setPoolStateLocked(\$source, 'disabled')")
&& str_contains($service,
'self::setPoolStateLocked($source, $sourceInitialState)')
&& str_contains($service, "'replacement_locked'")
&& str_contains($service, 'CloudAccess::manage($actor)')
&& str_contains($service, 'self::assertAdminRead($actor)'),
'Replacement service lost revision fencing, overlap locking, or unknown-effect convergence.');
$assert(is_string($jobs)
&& str_contains($jobs,
'enqueuePoolReplacementNetworkChangeLocked')
&& str_contains($jobs,
"'public_pool_replacement_batch_key' => \$batchKey")
&& str_contains($jobs, "'confirm_restart' => true")
&& str_contains($jobs, "\$targetIp->state = 'changing'"),
'Replacement children no longer freeze ordinary network-change identity.');
$assert(is_string($worker)
&& str_contains($worker,
'CloudPublicIpPoolReplacementBatchService::finishChildLocked')
&& str_contains($worker,
"'public_ip_pool_replacement_batch'")
&& str_contains($worker,
"\$preparedRecoveryMarker === null")
&& str_contains($worker, "? \$catalogStatus : 'uncertain'"),
'Worker no longer converges replacement children or excludes the local parent from dispatch.');
$finishChild = $method(
$service,
'public static function finishChildLocked(',
'public static function finishResolvedChildLocked('
);
$start = $method(
$service,
'public static function start(',
'public static function finishChildLocked('
);
$transactionOffset = strpos(
$start,
'->transaction(static function () use ('
);
$assert($transactionOffset !== false,
'Replacement start transaction source is missing.');
$lockedStart = substr($start, $transactionOffset);
$startPositions = array_map(
static fn (string $needle): int|false => strpos($lockedStart, $needle),
[
'$actorUser = User::where(',
'$prior = CloudJob::where(',
'$instances = CloudInstance::whereIn(',
'$host = CloudHost::where(',
'$agent = CloudHostAgent::where(',
'$attachments = CloudHostNetworkAttachment::whereIn(',
'$pools = CloudPublicIpPool::whereIn(',
'$reservations = CloudResourceReservation::whereIn(',
'$ips = CloudPublicIp::whereIn(',
]
);
$sortedStartPositions = $startPositions;
sort($sortedStartPositions, SORT_NUMERIC);
$assert(!in_array(false, $startPositions, true)
&& $startPositions === $sortedStartPositions
&& count(array_unique($startPositions)) === count($startPositions),
'Replacement start reversed actor/job/instance/host/catalog locks.');
$finishResolved = $method(
$service,
'public static function finishResolvedChildLocked(',
'public static function display('
);
$converge = $method(
$service,
'private static function convergeLocked(',
'private static function setPoolStateLocked('
);
$positions = array_map(
static fn (string $needle): int|false => strpos($finishChild, $needle),
[
'$parent = CloudJob::where(',
'$source = CloudPublicIpPool::where(',
'$batch = CloudPublicIpPoolReplacementBatch::where(',
'$items = CloudPublicIpPoolReplacementBatchItem::where(',
'$item = $items->first(',
]
);
$sortedPositions = $positions;
sort($sortedPositions, SORT_NUMERIC);
$assert(!in_array(false, $positions, true)
&& $positions === $sortedPositions
&& count(array_unique($positions)) === count($positions)
&& !str_contains($finishResolved, 'CloudHost::where(')
&& !str_contains($converge, 'lockForUpdate()'),
'Replacement convergence reversed parent/pool/batch locks.');
echo "Cloud public IP pool replacement schema contract tests passed\n";