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/CloudUserISOContractTest.php

107 lines
5.4 KiB
PHP

<?php
$root = dirname(__DIR__);
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$routes = file_get_contents($root . '/config/routes_cloud.php');
$pages = file_get_contents($root . '/config/routes.php');
$controller = file_get_contents(
$root . '/app/Controllers/CloudApiController.php'
);
$feature = file_get_contents(
$root . '/app/Services/Cloud/CloudUserIsoFeature.php'
);
$service = file_get_contents(
$root . '/app/Services/Cloud/CloudUserIsoService.php'
);
$orders = file_get_contents(
$root . '/app/Services/Cloud/CloudOrderService.php'
);
$schema = file_get_contents($root . '/sql/cloud.sql');
$view = file_get_contents($root . '/app/Services/View.php');
$sidebar = file_get_contents(
$root . '/resources/views/venix/user/panel_sidebar.tpl'
);
$hostAdmin = file_get_contents(
$root . '/resources/views/venix/admin/hosts/index.tpl'
);
$adminClient = file_get_contents($root . '/public/cloud/admin.js');
$assert(str_contains($pages, "'/cloud/iso-images'")
&& str_contains($routes, "['GET', 'POST'], '/user-isos'")
&& str_contains($routes, "'/user-isos/import-jobs/{job_id:")
&& str_contains($routes, "'/user-isos/delete-jobs/{id:")
&& str_contains($routes, "'/user-isos/{id:[0-9a-f]{32}}'")
&& !str_contains($routes, "'/user-isos/targets'")
&& str_contains($sidebar, '{if !empty($user_iso_enabled)}')
&& str_contains($sidebar, 'href="/user/cloud/iso-images"'),
'The gated personal ISO page and bounded API routes are incomplete.');
$assert(substr_count($controller, 'CloudUserIsoFeature::assertEnabled();') === 4
&& str_contains($feature,
"public const CONFIG_KEY = 'cloud_user_iso_enabled'")
&& str_contains($feature, 'return self::$cached = false;')
&& str_contains($view, "assign('user_iso_enabled'")
&& str_contains($hostAdmin, 'id="user-iso-enabled"')
&& str_contains($adminClient,
"api('/api/v1/admin/user-iso/settings'")
&& !str_contains($controller, 'userIsoTargets(')
&& !str_contains($controller, 'userIsoHost('),
'User ISO exposure must remain globally gated and fail closed.');
$assert(str_contains($controller, 'CloudUserIsoService::import(')
&& str_contains($controller, 'CloudUserIsoService::listing(')
&& str_contains($controller, 'CloudUserIsoService::job(')
&& str_contains($controller, 'CloudUserIsoService::delete(')
&& str_contains($controller, 'CloudUserIsoService::deleteJob(')
&& str_contains($controller, "'/api/v1/user-isos/import-jobs/'")
&& !str_contains($controller, '用户 ISO 现为宿主机级资源,但当前页面尚未提供目标宿主机选择')
&& !str_contains($controller, '当前宿主机 Agent 尚未实现用户 ISO'),
'User ISO endpoints must execute the PHP catalog/Agent workflow instead of returning 501.');
$assert(str_contains($service, 'final class CloudUserIsoService')
&& str_contains($service, 'CloudAgentSettingsService::enabledHostIds()')
&& str_contains($service, "->request('POST', '/v1/user-isos'")
&& str_contains($service, "->request('GET', '/v1/operations/'")
&& str_contains($service, "'DELETE', '/v1/user-isos/'")
&& str_contains($service, "->where('state', 'delete_dispatch')")
&& str_contains($service, "'state' => 'delete_sending'")
&& str_contains($service, "->where('state', 'delete_sending')")
&& str_contains($service, "'error_code' => 'delete_dispatch_unconfirmed'")
&& str_contains($service, "->where('state', 'delete_running')")
&& str_contains($service, "CloudAgentCatalogDeletion::confirms(")
&& str_contains($service, "'delete_evidence_missing'")
&& str_contains($service, "'status_url' => '/api/v1/user-isos/delete-jobs/'")
&& str_contains($service, "CloudSecrets::encrypt(\n ['url' => \$url], 'user-iso-source'")
&& str_contains($service, "'agent_result_mismatch'")
&& str_contains($service, "(\$result['id'] ?? null) !== (string) \$iso->id")
&& str_contains($service, "(int) (\$result['owner_id'] ?? 0) !== (int) \$iso->user_id")
&& str_contains($service, "preg_match('/^[a-f0-9]{64}\$/D', \$digest)")
&& str_contains($service, 'MAX_ISOS = 5')
&& str_contains($service, 'MAX_TOTAL_BYTES = 10 * 1024 * 1024 * 1024'),
'User ISO service lost its authenticated Agent replication, result validation or quota boundary.');
$runtime = file_get_contents($root . '/public/cloud/runtime.js');
$assert(str_contains($orders, "if (\$source !== 'cloud')")
&& str_contains($orders, '当前只支持从云镜像部署实例')
&& !str_contains($orders, "'iso_id'")
&& !str_contains($orders, "\$payload['user_iso_id']")
&& !str_contains($orders, 'iso_path')
&& str_contains($runtime, "source: 'cloud'")
&& !str_contains($runtime, 'body.iso_id')
&& !str_contains($runtime, "byId('vm-iso')")
&& !str_contains($runtime, 'body.iso_path ='),
'VM provisioning must remain cloud-template-only while personal ISOs stay a separate owner library.');
$assert(str_contains($schema, 'CREATE TABLE IF NOT EXISTS cloud_user_isos')
&& str_contains($schema, 'sealed_source_url LONGTEXT NOT NULL')
&& str_contains($schema, 'CREATE TABLE IF NOT EXISTS cloud_user_iso_replicas')
&& str_contains($schema, 'PRIMARY KEY (host_id, user_iso_id)'),
'Fresh schema must persist PHP-owned ISO identity and per-host Agent replicas.');
echo "User ISO PHP catalog and Agent replication contract passed.\n";