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

130 lines
6.2 KiB
PHTML

<?php
use App\Services\Cloud\CloudAdminResourceService;
use App\Services\Cloud\CloudError;
require dirname(__DIR__) . '/vendor/autoload.php';
$root = dirname(__DIR__);
$source = file_get_contents(
$root . '/app/Services/Cloud/CloudAdminResourceService.php'
);
if (!is_string($source)) {
throw new RuntimeException('Unable to read CloudAdminResourceService.php');
}
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$assert(str_contains(
$source,
"private const VIEWS = ['topology', 'capacity', 'reservations']"
)
&& str_contains($source, "'topology' => self::topology(")
&& str_contains($source, "'effective_state' => \$effective['state']")
&& str_contains($source, "'effective_state_label' => self::effectiveStateLabel(")
&& str_contains($source, "'blockers' => \$effective['blockers']")
&& str_contains($source, "'placement_capacity' => \$placementCapacity")
&& str_contains($source, "'default_line_id' =>"),
'Resource projection must expose the new topology, health and human-state contract.');
$assert(str_contains($source, 'CloudHostAgent::whereIn(')
&& str_contains($source, 'CloudHostStorageConfiguration::whereIn(')
&& str_contains($source, '$agent !== null && $agent->isReady()')
&& str_contains($source, '$storage !== null && $storage->isApplied()')
&& str_contains($source, 'CloudNetworkSelector::isValid(')
&& str_contains($source, "'placement_ready_host_count'"),
'Placement readiness must include Agent, storage, selector, network and capacity gates.');
$assert(str_contains($source, "'regions_total' =>")
&& str_contains($source, "'lines_total' =>")
&& str_contains($source, "'pools_total' =>")
&& str_contains($source, "'placement_ready_pools' =>")
&& str_contains($source, "'placement_ready_hosts' =>")
&& str_contains($source, "'uncertain_reservations' =>")
&& !str_contains($source, "'active_pools' =>")
&& !str_contains($source, "'schedulable_hosts' =>")
&& !str_contains($source, "'schedulable_host_count' =>")
&& !str_contains($source, "'sync_state' =>")
&& !str_contains($source, 'locked_reservation'),
'The clean-install projection must not retain misleading or retired field names.');
$settingsLockPosition = strpos($source,
'CloudNetworkLineFeature::lockedEnabled();');
$settingsRegionLockPosition = strpos($source,
"\$regions = CloudRegion::orderBy('id')->lockForUpdate()",
$settingsLockPosition === false ? 0 : $settingsLockPosition);
$assert(str_contains($source, '$isNew = !$region->exists;')
&& str_contains($source,
'$createDefaultLine = !CloudNetworkLineFeature::lockedEnabled();')
&& str_contains($source, '$region->getConnection()->transaction(')
&& str_contains($source, "\$line->code = 'default';")
&& str_contains($source, "\$line->name = '默认线路';")
&& str_contains($source, '$line->is_default = true;')
&& str_contains($source, "\$line->status = 'active';")
&& str_contains($source, 'if (!$enabled && $activeDefaults->count() === 0)')
&& str_contains($source, "\$default->code = 'default';")
&& str_contains($source, 'CloudNetworkLineFeature::save($enabled);')
&& $settingsLockPosition !== false
&& $settingsRegionLockPosition !== false
&& $settingsLockPosition < $settingsRegionLockPosition,
'Hidden-line mode must serialize on the feature row, then atomically create or backfill an active default line for every region.');
$reflection = new ReflectionClass(CloudAdminResourceService::class);
$entityCode = $reflection->getMethod('entityCode');
$newModel = (object) ['exists' => false, 'code' => ''];
$generated = $entityCode->invoke(null, [], $newModel, 'region', '东京', 32,
'地区系统标识');
$assert(preg_match('/^region-[a-f0-9]{8}$/D', $generated) === 1,
'Non-ASCII names must receive a safe server-generated system identifier.');
$custom = $entityCode->invoke(null, ['code' => 'Tokyo-Core'], $newModel,
'region', '东京', 32, '地区系统标识');
$assert($custom === 'tokyo-core',
'A custom create-time system identifier must be normalized once.');
$existingModel = (object) ['exists' => true, 'code' => 'tokyo-core'];
$assert($entityCode->invoke(null, [], $existingModel, 'region', '东京', 32,
'地区系统标识') === 'tokyo-core',
'Reading an existing immutable system identifier must preserve it.');
$immutableRejected = false;
try {
$entityCode->invoke(null, ['code' => 'replacement'], $existingModel,
'region', '东京', 32, '地区系统标识');
} catch (CloudError $error) {
$immutableRejected = $error->httpStatus === 409;
}
$assert($immutableRejected,
'PATCH-style writes must reject system identifier replacement.');
$health = [
'host_count' => 1,
'enabled_host_count' => 1,
'network_selector_ready_host_count' => 1,
'scheduler_active_host_count' => 1,
'agent_ready_host_count' => 1,
'storage_ready_host_count' => 1,
'network_ready_host_count' => 1,
'capacity_ready_host_count' => 1,
'placement_ready_host_count' => 1,
];
$poolState = $reflection->getMethod('poolEffectiveState');
$line = (object) ['status' => 'active', 'is_default' => true];
$region = (object) ['status' => 'active'];
$ready = $poolState->invoke(null, 'active', $line, $region, false, $health);
$assert($ready['state'] === 'ready' && $ready['blockers'] === [],
'A pool is ready only when one host passes every base placement gate.');
$effectiveStateLabel = $reflection->getMethod('effectiveStateLabel');
$assert($effectiveStateLabel->invoke(null, 'ready') === '基础就绪',
'Base health must not claim request-specific offer and template readiness.');
$health['agent_ready_host_count'] = 0;
$health['placement_ready_host_count'] = 0;
$blocked = $poolState->invoke(null, 'active', $line, $region, false, $health);
$blockerCodes = array_column($blocked['blockers'], 'code');
$assert($blocked['state'] === 'blocked'
&& in_array('agent_not_ready', $blockerCodes, true)
&& in_array('no_placement_ready_hosts', $blockerCodes, true),
'A pool with an unavailable Agent must explain why it cannot place instances.');
echo "Administrator resource projection contracts passed.\n";