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

210 lines
11 KiB
PHP

<?php
// Static tenant-page contract. The VPC control plane and host dataplane have
// their own tests; this protects the user-facing page from drifting away from
// the database-only bootstrap and the versioned VPC API.
$root = dirname(__DIR__);
$read = static fn (string $path): string => (string) file_get_contents($root . '/' . $path);
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$controller = $read('app/Controllers/CloudPageController.php');
$pageService = $read('app/Services/Cloud/CloudVpcPageService.php');
$middleware = $read('app/Middleware/CloudApi.php');
$routes = $read('config/routes.php');
$shell = $read('resources/views/venix/cloud/page.tpl');
$template = $read('resources/views/venix/cloud/pages/vpcs.tpl');
$sidebar = $read('resources/views/venix/user/panel_sidebar.tpl');
$app = $read('public/cloud/app.js');
$script = $read('public/cloud/vpc.js');
$styles = $read('public/cloud/vpc.css');
$controllerStart = strpos($controller, 'public function vpcs(');
$controllerEnd = strpos($controller, 'public function detail(', $controllerStart ?: 0);
$controllerSection = $controllerStart !== false && $controllerEnd !== false
? substr($controller, $controllerStart, $controllerEnd - $controllerStart) : '';
$assert($controllerSection !== ''
&& str_contains($controllerSection, 'CloudAccess::actor(false)')
&& str_contains($controllerSection, 'CloudVpcPageService::bootstrap($actor)')
&& str_contains($controllerSection, 'CloudVpcService::listing($actor)')
&& str_contains($controllerSection, "'cloud_vpc_bootstrap_json'")
&& str_contains($controllerSection, '$this->safeJson($payload)'),
'VPC page must use an owner-scoped, safe database bootstrap and the typed listing projection.');
$assert(str_contains($controller, 'use App\\Services\\Cloud\\CloudVpcPageService;')
&& str_contains($controller, "'cloud_vpc_bootstrap_json', '{}'")
&& !str_contains($controllerSection, 'CloudEngineClient')
&& !str_contains($controllerSection, 'CloudHost'),
'VPC first paint must not contact libvirt or expose host inventory.');
$assert(str_contains($pageService, 'CloudRegionFabric')
&& str_contains($pageService, "->where('cloud_region_fabrics.state', 'active')")
&& str_contains($pageService, "->where('cloud_region_fabrics.fabric_revision', '>', 0)")
&& str_contains($pageService, "->where('region.status', 'active')")
&& str_contains($pageService, "->where('user_id', \$userId)")
&& str_contains($pageService, "->where('status', 'active')")
&& str_contains($pageService, "(string) \$row->service_status === 'active'")
&& str_contains($pageService, "(int) \$row->expires_at > time()")
&& str_contains($pageService, '->limit(200)')
&& !str_contains($pageService, 'CloudEngineClient')
&& !str_contains($pageService, "'host_id'"),
'VPC bootstrap must expose only active Fabric regions and bounded owner-scoped instances.');
$assert(str_contains($routes, "\$this->get('/cloud/vpcs', 'App\\Controllers\\CloudPageController:vpcs');"),
'Authenticated user routes must expose the VPC page.');
$assert(str_contains($middleware, "^/api/v1/vpcs(?:/[0-9a-f]{32}(?:/.*)?)?\$#D")
&& str_contains($middleware, 'CloudAccess::actor(!$databaseOnly)'),
'VPC API requests must stay in the PHP/database-only auth lane.');
$assert(str_contains($sidebar, 'href="/user/cloud/vpcs"')
&& str_contains($sidebar, "panel_cloud_active_key == 'vpcs'")
&& str_contains($sidebar, 'VPC 私网'),
'The user cloud navigation must expose and mark the VPC page.');
$assert(str_contains($shell, "cloud_page == 'vpcs'")
&& str_contains($shell, "cloud/pages/vpcs.tpl")
&& str_contains($shell, '/cloud/vpc.css'),
'The cloud shell must load the dedicated VPC view and styles.');
$assert(str_contains($app, "vpcs: '/cloud/vpc.js'"),
'Cloud page module routing must load the VPC module.');
foreach ([
'vpc-create-form', 'vpc-create-name', 'vpc-create-region',
'vpc-list', 'vpc-detail', 'vpc-list-reconcile', 'vpc-bootstrap',
] as $id) {
$assert(str_contains($template, 'id="' . $id . '"'),
'VPC view is missing required control ' . $id . '.');
}
$assert(!str_contains($template, 'id="vpc-create-cidr"')
&& !str_contains($template, 'id="vpc-create-subnet"')
&& !str_contains($template, 'id="vpc-create-max-members"')
&& !str_contains($template, '<label>网络配置</label>')
&& str_contains($template, '分配独立 /24 网段并建立唯一子网'),
'Tenant VPC create form must leave CIDR, subnet and member capacity to platform policy.');
$createStart = strpos($script, 'async function createVpc(');
$createEnd = strpos($script, 'async function submitMember(', $createStart ?: 0);
$createSection = $createStart !== false && $createEnd !== false
? substr($script, $createStart, $createEnd - $createStart) : '';
$assert($createSection !== ''
&& str_contains($createSection, 'name: form.name.value.trim()')
&& str_contains($createSection, 'region_id: Number(form.region_id.value)')
&& str_contains($createSection, 'body.request_key = form.dataset.requestKey')
&& !str_contains($createSection, 'form.cidr')
&& !str_contains($createSection, 'form.subnet_cidr')
&& !str_contains($createSection, 'form.max_members'),
'Tenant create JavaScript must submit only name, region and the idempotency key.');
foreach (['成员与私网 IP', 'Route', 'VPC ACL', '最近操作'] as $label) {
$assert(str_contains($script, $label),
'VPC module is missing section ' . $label . '.');
}
$memberRenderStart = strpos($script, 'function renderMembers(');
$memberRenderEnd = strpos($script, 'function routeRow(', $memberRenderStart ?: 0);
$memberRender = $memberRenderStart !== false && $memberRenderEnd !== false
? substr($script, $memberRenderStart, $memberRenderEnd - $memberRenderStart) : '';
$memberSubmitStart = strpos($script, 'async function submitMember(');
$memberSubmitEnd = strpos($script, 'function readRoutes(', $memberSubmitStart ?: 0);
$memberSubmit = $memberSubmitStart !== false && $memberSubmitEnd !== false
? substr($script, $memberSubmitStart, $memberSubmitEnd - $memberSubmitStart) : '';
$assert(!str_contains($script, 'function renderSubnets(')
&& !str_contains($script, '子网与 CIDR')
&& !str_contains($script, 'value.subnets')
&& !str_contains($template, '查看子网、成员'),
'The platform-managed subnet must not be exposed as a tenant management panel.');
$assert($memberRender !== '' && $memberSubmit !== ''
&& !str_contains($memberRender, "name = 'subnet_key'")
&& !str_contains($memberRender, "name = 'private_ip'")
&& str_contains($memberSubmit, 'instance_id: Number(form.instance_id.value)')
&& str_contains($memberSubmit, "request_key: requestKey('member-add')")
&& !str_contains($memberSubmit, 'subnet_key')
&& !str_contains($memberSubmit, 'private_ip'),
'Tenant member attach must submit only the instance and idempotency key.');
$assert(str_contains($template, '创建 VPC'),
'VPC view is missing the create form.');
$assert(str_contains($script, "'/api/v1/vpcs'")
&& str_contains($script, '/members')
&& str_contains($script, '/reconcile')
&& str_contains($script, "kind === 'routes'")
&& str_contains($script, ": 'acl'")
&& str_contains($script, "'DELETE'")
&& str_contains($script, "'PUT'")
&& !str_contains($script, '/subnets')
&& !str_contains($script, 'submitSubnet(')
&& !str_contains($script, 'removeSubnet(')
&& !str_contains($script, "requestKey('subnet-add')")
&& !str_contains($script, "requestKey('subnet-remove')"),
'VPC module must cover root/member/policy APIs without exposing subnet mutation.');
$assert(str_contains($script, "remove.dataset.vpcAction = 'delete'")
&& str_contains($script, 'async function deleteVpc()')
&& str_contains($script, "requestKey('vpc-delete')")
&& str_contains($script, 'confirmation_name: root.name')
&& str_contains($script, "'DELETE'")
&& str_contains($script, 'selectedRootDeleting()')
&& str_contains($script, 'lockDeletingDetail(container, reconcile)')
&& str_contains($script, 'finishVpcDeletion()')
&& str_contains($script, "rootStatus(item) !== 'deleted'")
&& str_contains($script,
'state.vpcs = state.vpcs.filter(item => item?.vpc_key !== deletedKey)')
&& str_contains($script, "state.selectedKey = ''"),
'VPC root deletion must require an exact-name confirmation, lock mutations and clear completed selections.');
$assert(str_contains($script, 'Absence from it is not a')
&& !str_contains($script, 'const deletionCompleted =')
&& !str_contains($script, "error?.status === 404 && key === state.selectedKey"),
'A bounded list omission or detail 404 must not be treated as a VPC deletion receipt.');
$deletingProjection = strpos($script, "if (raw === 'deleting') return 'deleting';");
$failedProjection = strpos($script, "if (raw === 'degraded' || sync === 'failed')");
$assert($deletingProjection !== false && $failedProjection !== false
&& $deletingProjection < $failedProjection,
'A deleting root must stay a locked tombstone when engine sync is failed or uncertain.');
$assert(str_contains($script, 'X-CSRF-Token')
&& str_contains($script, 'credentials: \'same-origin\'')
&& str_contains($script, 'request_key')
&& str_contains($script, 'function entityKey()')
&& str_contains($script, 'route_key: row.dataset.routeKey')
&& str_contains($script, 'rule_key: row.dataset.aclKey'),
'VPC mutations must use same-origin CSRF protection and separate idempotency/entity keys.');
foreach (['applying', 'degraded', 'uncertain', 'waiting', 'active', 'suspended',
'deleting'] as $status) {
$assert(str_contains($script, $status),
'VPC UI lost state projection for ' . $status . '.');
}
$assert(str_contains($script, 'usable === true')
&& str_contains($script, 'desired_revision')
&& str_contains($script, 'observed_revision')
&& str_contains($script, 'setTimeout')
&& str_contains($script, 'visibilitychange')
&& str_contains($script, 'AbortController')
&& str_contains($script, 'loadDetail(state.selectedKey, true)')
&& str_contains($script, 'state.detailDirty')
&& str_contains($script, 'allowDirty = false'),
'VPC UI must poll desired/observed state, pause hidden tabs and only show active after usable readback.');
$assert(str_contains($script, 'data-vpc-tombstone-state="deleting"')
&& str_contains($script, 'pendingTombstones')
&& str_contains($script, 'sameRegion')
&& str_contains($script, 'alreadyAttached')
&& str_contains($script, 'option.disabled')
&& str_contains($script, 'requestFingerprint')
&& str_contains($script, 'confirmDetailMutation'),
'VPC editors must preserve dirty drafts, block only in-flight tombstones and constrain member choices.');
$assert(!str_contains($script, '/internal/v1/')
&& !str_contains($script, 'host_id')
&& !str_contains($script, 'credential_cipher'),
'Tenant VPC JavaScript must not call private engine endpoints or expose host credentials.');
$assert(str_contains($styles, '.vpc-state-pill')
&& str_contains($styles, '.vpc-columns')
&& str_contains($styles,
'.vpc-member-form{grid-template-columns:repeat(2,minmax(0,1fr))}')
&& !str_contains($styles,
'.vpc-member-form{grid-template-columns:repeat(4,minmax(0,1fr))}')
&& str_contains($styles, '@media'),
'VPC page needs dedicated state/list/detail responsive styling.');
echo "Tenant VPC page, API wiring and state-polling contract passed.\n";