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

168 lines
8.4 KiB
PHTML

<?php
// Static contract for the revisioned administrator host public-pool boundary.
$root = dirname(__DIR__);
$routes = file_get_contents($root . '/config/routes_cloud.php');
$controller = file_get_contents(
$root . '/app/Controllers/CloudAdminHostNetworkController.php'
);
$service = file_get_contents(
$root . '/app/Services/Cloud/CloudAdminHostNetworkService.php'
);
$usage = file_get_contents(
$root . '/app/Services/Cloud/CloudPanelUsageService.php'
);
$hostController = file_get_contents(
$root . '/app/Controllers/CloudAdminController.php'
);
$template = file_get_contents(
$root . '/resources/views/venix/admin/hosts/index.tpl'
);
$client = file_get_contents($root . '/public/cloud/admin-hosts.js');
$assets = file_get_contents(
$root . '/resources/views/venix/layout_assets.tpl'
);
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$assert(str_contains($routes,
"map(['GET', 'POST'], '/admin/hosts/{id:[0-9]+}/network-pools'")
&& str_contains($routes,
"'/admin/hosts/{id:[0-9]+}/network-pools/{pool_key:[0-9a-f]{32}}'")
&& str_contains($routes,
"'App\\Controllers\\CloudAdminHostNetworkController:update'")
&& !str_contains($routes,
"delete('/admin/hosts/{id:[0-9]+}/network-pools"),
'Host public pools require bounded GET/POST/PUT routes and no hard-delete route.');
$assert(str_contains($controller, 'CloudAccess::manage($actor);')
&& str_contains($controller, '$write = $method !== \'GET\';')
&& substr_count($controller,
"'name', 'cidr', 'gateway', 'dns_servers', 'reserved_ips'") === 2
&& str_contains($controller,
"'guest_prefix_length', 'state', 'request_key'")
&& str_contains($controller,
"'guest_prefix_length', 'state', 'expected_revision'")
&& str_contains($controller,
"->withHeader('Cache-Control', 'private, no-store')"),
'Pool writes must be super-admin-only and use a strict request/response boundary.');
$assert(str_contains($service, 'private const MAX_POOLS = 32;')
&& str_contains($service,
"private const STATES = ['active', 'draining', 'disabled'];")
&& str_contains($service, "hash_hmac(\n 'sha256'")
&& str_contains($service, "CloudSecrets::key('public-pool-key')")
&& !str_contains($service, 'bin2hex(random_bytes(16))')
&& !str_contains($service, 'CloudAgentClient')
&& str_contains($service, 'CloudHostPoolMembershipService::lockedRows')
&& str_contains($service, 'CloudHostPoolMembershipService::selected')
&& str_contains($service, "\$lockedHost->default_network")
&& str_contains($service,
'CloudNetworkSelector::isValid($selector)')
&& str_contains($service, "\$attachment->network_mode = 'selector';")
&& str_contains($service, 'CloudHostNetworkAttachment')
&& str_contains($service, 'CloudPublicIpPool')
&& str_contains($service, 'desired_revision')
&& str_contains($service, 'observed_revision')
&& str_contains($service, 'sync_state')
&& str_contains($service, 'refreshAttachmentAggregate')
&& str_contains($service,
"'uncertain' => 4")
&& str_contains($service,
'if ($confirmedRevision > 0)')
&& str_contains($service,
'$attachment->observed_revision = $evidenceRevision;'),
'The service must persist host/line attachment desired state and independent pool revisions.');
$assert(str_contains($service,
"preg_match('/^[0-9a-f]{32}$/D', \$requestKey)")
&& str_contains($service, 'sameCreateRequest(')
&& str_contains($service, "'replay' => true")
&& str_contains($service,
'存在尚未核对的地址池创建记录,请刷新后编辑该记录,不能继续创建新地址池')
&& str_contains($service, '$expectedRevision !== $revisionBase')
&& str_contains($service, '公网地址池版本已变化,请刷新后重试')
&& str_contains($service, '->transaction(function () use (')
&& str_contains($service, "\$pool->observed_revision = \$revision;")
&& str_contains($service, "\$pool->sync_state = 'applied';")
&& !str_contains($service, 'dispatch_attempts')
&& str_contains($service, 'materializeIpLedger')
&& str_contains($service, "(string) \$pool->sync_state === 'applied'"),
'Creates must be idempotent, updates CAS-fenced and local commits must atomically materialize the panel IP ledger.');
$assert(str_contains($service, 'assertNoOverlap(')
&& str_contains($service, 'assertNoGlobalOverlap(')
&& str_contains($service, 'catalogCount(')
&& str_contains($service, '一台宿主机最多配置 32 个公网地址池')
&& str_contains($service, 'OCCUPIED_IP_STATES')
&& str_contains($service,
'该地址池仍有实例或任务占用,只能修改名称、运行状态和未占用的保留 / 禁用 IP')
&& str_contains($service,
'已分配或任务占用的公网 IP 不能加入保留 / 禁用列表')
&& str_contains($service, '$occupiedAddressesByPool')
&& str_contains($service, 'occupiedUnsafeRuntimeChanged')
&& str_contains($service, '$originalAttachment')
&& str_contains($service, '$attachmentChanged')
&& str_contains($service, '$attachmentLineChanged')
&& !str_contains($service, '->delete()'),
'Pool limits, overlap checks, occupied runtime locks and soft lifecycle states are required.');
$assert(str_contains($service, 'materializeIpLedger(')
&& str_contains((string) $usage,
"CloudInstance::where('cloud_instances.host_id', \$hostId)")
&& str_contains((string) $usage, 'capacity_snapshots')
&& str_contains((string) $usage, "'unknown'")
&& str_contains((string) $usage, 'spec_json')
&& str_contains((string) $usage, 'managementAddressFromValue'),
'Panel instances must be counted from PHP metadata and block on unknown usage without host discovery.');
$assert(str_contains($service,
'PHP is the sole authority for public-pool configuration and IP allocation')
&& str_contains($service, "'applied'")
&& !str_contains($service, 'CloudAgentClient')
&& !str_contains($service, 'request('),
'PHP-owned pool writes must not depend on a second host-side pool catalog.');
foreach (['host-public-pool', 'host-gateway', 'host-dns', 'host-reserved',
'host-vm-prefix'] as $retiredId) {
$assert(!str_contains($template, 'id="' . $retiredId . '"'),
'The old single-pool host input remains in the main form: ' . $retiredId);
}
$assert(!str_contains($hostController, "'public_pool_cidr', 'public_gateway'")
&& !str_contains($client, 'function hostPublicNetworkPayload()')
&& str_contains($hostController, "'default_network'")
&& !str_contains($hostController, 'public_gateway'),
'The main host writer must not accept pool input or double-write the pool catalog.');
$assert(str_contains($template, 'id="host-network-pools-drawer"')
&& str_contains($template, 'data-admin-host-network-drawer')
&& str_contains($template,
'{if !$admin_can_manage|default:false}disabled{/if}')
&& str_contains($client, 'window.VenixDrawerMotion.openDrawerWithMotion')
&& str_contains($client, 'window.VenixDrawerMotion.closeDrawerWithMotion')
&& str_contains($client, "{ method: 'POST', body }")
&& str_contains($client, "{ method: 'PUT', body }")
&& str_contains($client, 'networkPoolEpoch')
&& str_contains($client, 'networkPoolHostId')
&& str_contains($client, 'networkPoolContextCurrent(hostId, epoch)')
&& str_contains($client, 'globalThis.crypto?.getRandomValues')
&& str_contains($client, 'request_key: requestKey')
&& str_contains($client, 'expected_revision: expectedRevision')
&& str_contains($client,
'const payload = await api(`/api/v1/admin/hosts/${encodeURIComponent(hostId)}/network-pools`);')
&& str_contains($client, 'save.disabled = !canManage')
&& str_contains($client, 'for (const control of [cidr, gateway, prefix, dns]) control.disabled = true;')
&& !str_contains($client, '[cidr, gateway, prefix, dns, reserved]')
&& str_contains($client, '已分配或任务占用的 IP 会被拒绝')
&& !file_exists($root . '/public/cloud/admin-host-network.css'),
'The responsive pool editor must lock network identity while allowing unoccupied disabled-IP edits.');
$assert(preg_match("/venix_asset_version value='([0-9]+)'/", $assets, $match) === 1
&& (int) $match[1] >= 2026100316,
'The shared browser asset version must cover the host pool drawer release.');
echo "Administrator host public-pool contracts passed.\n";