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

93 lines
5.0 KiB
PHP

<?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');
$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.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]+}/capacity-sync'"),
'Host scheduling and capacity sync routes must be registered.');
$assert(str_contains($middleware, '(?:scheduling|capacity-sync)')
&& str_contains($controller, '$this->actor($request, $write)')
&& str_contains($controller, '$this->actor($request, true)'),
'Scheduling reads must allow administrators while writes stay super-admin only.');
$assert(str_contains($controller, "'resource_pool_id', 'scheduler_state'")
&& !str_contains(substr($controller,
strpos($controller, 'public function hostScheduling'),
strpos($controller, 'public function syncHostCapacity')
- strpos($controller, 'public function hostScheduling')), 'public_ip'),
'The scheduling allowlist must not accept manual public IP fields.');
$saveStart = strpos($service, 'public static function save');
$syncStart = strpos($service, 'public static function sync', $saveStart);
$save = $saveStart === false || $syncStart === false ? ''
: substr($service, $saveStart, $syncStart - $saveStart);
$hostLock = strpos($save, "CloudHost::where('id', \$hostId)->lockForUpdate()");
$membershipLock = strpos($save, 'CloudHostPoolMembershipService::lockedRows');
$policyLock = strpos($save, "CloudHostCapacityPolicy::where('host_id', \$hostId)");
$assert($save !== '' && $hostLock !== false && $membershipLock !== false
&& $policyLock !== false && $hostLock < $membershipLock
&& $membershipLock < $policyLock
&& str_contains($service, '->lockForUpdate()->first();'),
'Scheduling writes must lock host, membership and capacity policy in order.');
$assert(str_contains($service, 'CloudHostPoolMembershipService::replaceLocked(')
&& str_contains($service, "'memberships' => \$memberships->map")
&& str_contains($membershipService,
'CloudHostResourcePoolMembership::where(\'host_id\'')
&& str_contains($membershipService, 'if ($rows->isEmpty()'
. ' && $host->resource_pool_id !== null)')
&& str_contains($membershipService, 'public const SAFE_PRIORITY = 0;')
&& str_contains($membershipService, 'public const SAFE_WEIGHT = 100;')
&& str_contains($membershipService, '$row->delete();')
&& str_contains($membershipService,
'宿主机存在多个启用中的资源池成员关系'),
'Membership authority, legacy backfill or first-release single-row policy is missing.');
$assert(str_contains($service, "\$schedulerState === 'active'")
&& str_contains($service, 'CloudNetworkLineFeature::enabled()')
&& str_contains($service, "(string) \$pool->status !== 'active'")
&& str_contains($service, "(string) \$region->status !== 'active'")
&& str_contains($service, '$safety >= $configured'),
'Active scheduling must validate the host, pool hierarchy and safety reserves.');
$assert(str_contains($service, 'CloudHostInventorySyncService())->syncOne($hostId)')
&& str_contains($service, '可以保存配置,但调度器会拒绝放置实例'),
'Manual sync and non-blocking health/staleness warnings must remain explicit.');
$assert(str_contains($template, 'data-admin-host-scheduling-drawer')
&& str_contains($template, 'id="host-capacity-sync"')
&& str_contains($template, '{if $admin_can_manage|default:false}')
&& str_contains($template, '公网 IP 仅由宿主机盘点维护'),
'The host drawer must expose server-rendered write controls without IP editing.');
$assert(str_contains($script, 'window.VenixDrawerMotion.openDrawerWithMotion')
&& str_contains($script, 'window.VenixDrawerMotion.closeDrawerWithMotion')
&& str_contains($script, '/capacity-sync')
&& str_contains($script, '/scheduling'),
'The host drawer must reuse shared motion and scheduling endpoints.');
$assert(preg_match("/venix_asset_version value='([0-9]+)'/", $assets, $match) === 1
&& (int) $match[1] >= 2026100313,
'Shared asset version must be bumped for admin.js changes.');
$assert(!file_exists($root . '/public/cloud/admin-host-scheduling.css'),
'Host scheduling must not introduce a standalone CSS file.');
echo "Administrator host scheduling contract checks passed.\n";