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

154 lines
7.5 KiB
PHTML

<?php
$root = dirname(__DIR__);
$read = static function (string $path) use ($root): string {
$contents = file_get_contents($root . '/' . $path);
if (!is_string($contents)) {
throw new RuntimeException('Unable to read ' . $path);
}
return $contents;
};
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$service = $read('app/Services/Cloud/CloudAdminHostSchedulingService.php');
$capacityProjection = $read(
'app/Services/Cloud/CloudAdminPublicNetworkCapacityProjection.php'
);
$projectionService = $read(
'app/Services/Cloud/CloudAdminHostProjectionService.php'
);
$membershipService = $read(
'app/Services/Cloud/CloudHostPoolMembershipService.php'
);
$controller = $read('app/Controllers/CloudAdminController.php');
$routes = $read('config/routes_cloud.php');
$middleware = $read('app/Middleware/CloudApi.php');
$template = $read('resources/views/venix/admin/hosts/index.tpl');
$script = $read('public/cloud/admin-hosts.js');
$assets = $read('resources/views/venix/layout_assets.tpl');
$assert(str_contains($routes, "'/admin/hosts/{id:[0-9]+}/scheduling'")
&& str_contains($routes,
"'/admin/hosts/{id:[0-9]+}/scheduling/memberships/{resource_pool_id:[1-9][0-9]*}'")
&& !str_contains($routes, '/capacity-sync'),
'Host capacity and independent membership routes are incomplete.');
$assert(str_contains($middleware, '/scheduling')
&& str_contains($middleware, 'memberships')
&& !str_contains($middleware, 'capacity-sync')
&& str_contains($controller, '$this->actor($request, $write)')
&& str_contains($controller, '$this->actor($request, true)'),
'Scheduling reads and writes lost their database-only authorization boundary.');
$hostSchedulingStart = strpos($controller,
'public function hostScheduling');
$hostSchedulingEnd = strpos($controller,
'public function hostStorageConfiguration', $hostSchedulingStart);
$hostScheduling = $hostSchedulingStart === false
|| $hostSchedulingEnd === false ? '' : substr(
$controller,
$hostSchedulingStart,
$hostSchedulingEnd - $hostSchedulingStart
);
$assert(str_contains($controller,
'CloudAdminHostSchedulingService::saveCapacity')
&& str_contains($controller,
'CloudAdminHostSchedulingService::saveMembership')
&& str_contains($controller,
'CloudAdminHostSchedulingService::deleteMembership')
&& str_contains($controller, "'expected_revision'")
&& $hostScheduling !== ''
&& !str_contains($hostScheduling, 'public_ip'),
'Controller dispatch does not keep capacity and membership mutations separate.');
$assert(str_contains($projectionService,
'CloudHostPoolMembershipService::primary(')
&& !str_contains($projectionService, '$host->resource_pool_id')
&& !str_contains($projectionService, '$host->scheduler_state'),
'Host projection must derive compact placement from membership authority.');
$assert(str_contains($service, 'public static function saveCapacity(')
&& str_contains($service, 'public static function saveMembership(')
&& str_contains($service, 'public static function deleteMembership(')
&& str_contains($service,
'CloudHostPoolMembershipService::upsertLocked(')
&& str_contains($service,
'CloudHostPoolMembershipService::removeLocked(')
&& str_contains($service,
'同一宿主机的启用或排空资源池成员必须属于同一地区')
&& str_contains($service,
'该资源池成员仍有实例或资源预留,不能停用或删除'),
'Scheduling service lost independent multi-membership CAS behavior.');
$assert(str_contains($membershipService, 'public static function activeRows(')
&& str_contains($membershipService, 'public static function primary(')
&& str_contains($membershipService, 'public static function upsertLocked(')
&& str_contains($membershipService, 'public static function removeLocked(')
&& str_contains($membershipService, 'expectedRevision')
&& !str_contains($membershipService, '$host->resource_pool_id'),
'Membership authority does not expose independent revisioned rows.');
$assert(!str_contains($service, 'CloudHostInventorySyncService')
&& !str_contains($service, 'syncOne(')
&& str_contains($service, 'CloudPanelUsageService::forHost(')
&& str_contains($service,
'CloudAdminPublicNetworkCapacityProjection::forHosts(')
&& !str_contains($service, 'hostUsage(')
&& str_contains($capacityProjection, 'network_attachment.line_id')
&& str_contains($capacityProjection,
'$attachmentState[$hostId][$lineId][$attachmentId]'),
'Scheduling capacity or IP ownership regressed to host inventory/default-network coupling.');
$assert(str_contains($template, 'data-admin-host-scheduling-drawer')
&& str_contains($template, 'id="host-membership-create-form"')
&& str_contains($template, 'id="host-membership-pool"')
&& str_contains($template, 'id="host-membership-list"')
&& str_contains($template, 'id="host-scheduling-form"')
&& str_contains($template, 'id="host-panel-capacity"')
&& str_contains($template, '保存全局容量')
&& !str_contains($template, 'id="host-resource-pool"')
&& !str_contains($template, 'id="host-scheduler-state"')
&& !str_contains($template, 'host-capacity-sync'),
'Host drawer did not replace its single placement selector with membership rows.');
$capacityBodyStart = strpos($script, 'function schedulingBody()');
$capacityBodyEnd = strpos($script, 'function syncCachedHostScheduling',
$capacityBodyStart);
$capacityBody = $capacityBodyStart === false || $capacityBodyEnd === false
? '' : substr($script, $capacityBodyStart,
$capacityBodyEnd - $capacityBodyStart);
$assert($capacityBody !== ''
&& str_contains($capacityBody, 'configured_cpu_cores')
&& str_contains($capacityBody, 'max_instances')
&& !str_contains($capacityBody, 'resource_pool_id')
&& !str_contains($capacityBody, 'scheduler_state'),
'Global capacity PUT still contains a membership mutation.');
$assert(str_contains($script, 'function renderHostMemberships(')
&& str_contains($script, 'dataset.hostMembershipForm')
&& str_contains($script, 'item?.region_name')
&& str_contains($script, 'item?.line_name')
&& str_contains($script, 'item?.pool_name')
&& str_contains($script, 'item.priority')
&& str_contains($script, 'item.weight')
&& str_contains($script, 'item.revision')
&& str_contains($script,
'/scheduling/memberships/${encodeURIComponent(poolId)}')
&& str_contains($script, "method: 'PUT'")
&& str_contains($script, "method: 'DELETE'")
&& str_contains($script, 'expected_revision: 0')
&& str_contains($script, 'VenixAdminDangerDialog.confirm')
&& str_contains($script,
'window.VenixDrawerMotion.openDrawerWithMotion')
&& str_contains($script,
'window.VenixDrawerMotion.closeDrawerWithMotion')
&& !str_contains($script, '/capacity-sync'),
'Membership rendering or independent PUT/DELETE behavior is incomplete.');
$assert(preg_match("/venix_asset_version value='([0-9]+)'/", $assets,
$match) === 1
&& (int) $match[1] >= 2026110204,
'Shared asset version was not bumped for the scheduling UI change.');
$assert(file_exists($root . '/tests/AdminHostSchedulingBehaviorTest.mjs')
&& !file_exists($root . '/public/cloud/admin-host-scheduling.css'),
'Scheduling UI must retain shared styling and have a DOM behavior test.');
echo "Administrator host multi-membership scheduling contracts passed.\n";