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

148 lines
8.6 KiB
PHP

<?php
$root = dirname(__DIR__);
$read = static function (string $path): string {
$source = file_get_contents($path);
if (!is_string($source)) {
throw new RuntimeException('Unable to read ' . $path);
}
return $source;
};
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$routes = $read($root . '/config/routes.php');
$apiRoutes = $read($root . '/config/routes_cloud.php');
$middleware = $read($root . '/app/Middleware/CloudApi.php');
$pageController = $read($root . '/app/Controllers/Admin/CloudPageController.php');
$apiController = $read($root . '/app/Controllers/CloudAdminResourceController.php');
$service = $read($root . '/app/Services/Cloud/CloudAdminResourceService.php');
$countryCatalog = $read($root . '/app/Services/Cloud/CloudCountryCatalog.php');
$placement = $read($root . '/app/Services/Cloud/CloudPlacementService.php');
$worker = $read($root . '/app/Services/Cloud/CloudWorkerService.php');
$createFinalizer = $read($root
. '/app/Services/Cloud/CloudCreateFailureFinalizer.php');
$feature = $read($root . '/app/Services/Cloud/CloudNetworkLineFeature.php');
$template = $read($root . '/resources/views/venix/admin/cloud/resources.tpl');
$header = $read($root . '/resources/views/venix/admin/static_header.tpl');
$script = $read($root . '/public/cloud/admin-resources.js');
$styles = $read($root . '/public/theme/venix/css/admin-home.css');
$assert(str_contains($routes, "'/resources', 'App\\Controllers\\Admin\\CloudPageController:resources'")
&& str_contains($pageController, 'CloudAdminResourceService::page(')
&& str_contains($pageController, "fetch('admin/cloud/resources.tpl')")
&& str_contains($pageController, 'JSON_HEX_TAG')
&& str_contains($pageController, "Cache-Control', 'private, no-store"),
'Resource scheduling must use a safe database-owned SSR first paint.');
foreach (['/admin/resources', '/admin/resources/settings', '/admin/regions',
'/admin/network-lines', '/admin/resource-pools', '/admin/resource-reservations'] as $path) {
$assert(str_contains($apiRoutes, "'" . $path), 'Missing resource API route: ' . $path);
}
$assert(!str_contains($apiRoutes, "delete('/admin/regions")
&& !str_contains($apiRoutes, "delete('/admin/network-lines")
&& !str_contains($apiRoutes, "delete('/admin/resource-pools"),
'Region, line and pool lifecycle must use statuses rather than hard deletion.');
$assert(str_contains($middleware, 'resource-reservations')
&& str_contains($apiController, 'CloudAccess::manage($actor)')
&& str_contains($apiController, 'if (!($actor[\'is_admin\'] ?? false))')
&& str_contains($apiController, 'CloudInput::only($body, [\'network_lines_enabled\'])'),
'All administrators may read resources, while mutations require server-side superadmin authorization.');
$assert(str_contains($feature, "CONFIG_KEY = 'cloud_network_lines_enabled'")
&& str_contains($feature, 'return self::$cached = false')
&& str_contains($service, "private const POOL_STATUSES = ['active', 'draining', 'disabled']")
&& str_contains($service, "whereIn('state', ['held', 'committed', 'uncertain'])")
&& str_contains($service, 'active_default_line_count')
&& str_contains($service, 'CloudHostCapacityPolicy')
&& str_contains($service, 'CloudResourceReservation')
&& str_contains($service, 'CloudPanelUsageService')
&& !str_contains($service, 'eligible_host.host')
&& !str_contains($service, 'CloudHostCapacitySnapshot')
&& !str_contains($service, 'CloudEngineClient'),
'Resource projections must be DB-only, use policy and reservation ledgers, and retain a disabled-by-default line feature.');
$assert(str_contains($service, "'country_options' => CloudCountryCatalog::options()")
&& str_contains($service, 'CloudCountryCatalog::normalize(')
&& str_contains($service, "'country_name' => \$country['name']")
&& str_contains($service, "'country_flag' => \$country['flag']")
&& str_contains($countryCatalog, '$code = strtoupper(trim($value));')
&& !str_contains($service, 'strtoupper(self::safeText($countryValue, 2))'),
'Regions must use the shared country catalog and reject rather than truncate invalid input.');
$assert(str_contains($placement,
"->whereIn('state', ['held', 'uncertain', 'committed'])")
&& str_contains($placement, "->orderBy('id')->lockForUpdate()->get([")
&& str_contains($placement, 'CloudPanelUsageService::forHost(')
&& str_contains($placement, 'CloudPanelUsageService::forHosts(')
&& str_contains($placement, "(int) (\$usage['unknown'] ?? 0) > 0")
&& !str_contains($placement, "COALESCE(SUM(cpu_cores)")
&& str_contains($placement,
"CloudResourceReservation::where('order_id', (int) \$order->id)->exists()")
&& !str_contains($placement, 'restoreExisting('),
'Final placement capacity must use a current locking read after the host sentinel, not a repeatable-read aggregate snapshot.');
$assert(str_contains($worker, '$requestedPublicIpCidr')
&& str_contains($worker, '$confirmedPublicIpCidr')
&& str_contains($worker, "'Agent 成功结果与预留公网 IP 不一致,必须人工核查'")
&& str_contains($placement,
'(string) $ip->address . \'/\' . (int) $ip->prefix_length')
&& str_contains($placement, '$confirmedPublicIpCidr'),
'Create reconciliation must match requested, confirmed, and reserved public IPs before committing the ledger.');
$refundStart = strpos($createFinalizer,
'public static function refundAndReleaseLocked(');
$refundEnd = $refundStart === false ? false
: strpos($createFinalizer, 'private static function terminalizeLocked(',
$refundStart);
$refund = $refundStart === false || $refundEnd === false
? '' : substr($createFinalizer, $refundStart, $refundEnd - $refundStart);
$stockPosition = strpos($refund, 'CloudOrderService::releaseStock($order)');
$releasePosition = strpos($refund,
'CloudPlacementService::release($order, $releaseReason)');
$assert($refund !== ''
&& str_contains($worker,
'CloudCreateFailureFinalizer::refundAndReleaseLocked(')
&& $stockPosition !== false && $releasePosition !== false
&& $stockPosition < $releasePosition,
'Create refunds must lock offer stock before host resources to match payment placement lock order.');
foreach (['资源总览', '地区', '线路', '资源池', '资源预留'] as $label) {
$assert(str_contains($template, '>' . $label . '<') || str_contains($template, '>' . $label . '</a>'),
'Resource page is missing a persistent tab: ' . $label);
}
$assert(str_contains($template, '{if $admin_can_manage|default:false}<button')
&& str_contains($template, 'data-resource-create="region"')
&& str_contains($template, 'data-resource-create="line"')
&& str_contains($template, 'data-resource-create="pool"')
&& str_contains($template, 'id="network-line-feature-form"')
&& str_contains($template, 'data-admin-instance-drawer')
&& str_contains($template, '/cloud/admin-resources.js?v={$venix_asset_version}')
&& str_contains($header, 'href="/admin/resources"'),
'Resource management controls, line switch, shared drawer and sidebar link are incomplete.');
$assert(str_contains($script, 'window.VenixDrawerMotion')
&& str_contains($script, "'X-CSRF-Token': window.ADMIN_CSRF_TOKEN")
&& str_contains($script, "method: id ? 'PATCH' : 'POST'")
&& str_contains($script, "method: 'PUT'")
&& !str_contains($script, 'is_super_admin')
&& !str_contains($script, 'data-can-manage'),
'Resource JavaScript must use shared motion and CSRF without duplicating authorization rules.');
$assert(str_contains($template, '<span class="form-label">国家 / 地区</span>')
&& str_contains($template, '{foreach $resource_page.country_options as $country}')
&& str_contains($template, 'id="resource-code-label"')
&& str_contains($template, 'id="resource-code-help"')
&& str_contains($template, 'pattern="[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?"')
&& str_contains($script, "label: '地区标识'")
&& str_contains($script, "label: '线路标识'")
&& str_contains($script, "label: '资源池标识'")
&& str_contains($script, '.value.trim().toLowerCase()'),
'Resource forms must explain identifiers, provide readable countries and normalize codes consistently.');
$assert(str_contains($styles, '[data-admin-resources] .admin-resource-tabs')
&& str_contains($styles, '[data-admin-resource-drawer] .admin-resource-form-fields')
&& !is_file($root . '/public/cloud/admin-resources.css'),
'Resource UI must extend the shared Venix stylesheet without adding a parallel theme file.');
echo "Administrator resource scheduling contracts passed.\n";