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

94 lines
5.2 KiB
PHTML

<?php
// Static host-location contract: no application boot, database or Agent connection.
$root = dirname(__DIR__);
$schema = file_get_contents($root . '/sql/cloud.sql');
$controller = file_get_contents($root . '/app/Controllers/CloudAdminController.php');
$pageController = file_get_contents($root . '/app/Controllers/Admin/CloudPageController.php');
$legacyPageController = file_get_contents($root . '/app/Controllers/Admin/HostController.php');
$catalog = file_get_contents($root . '/app/Services/Cloud/CloudCountryCatalog.php');
$template = file_get_contents($root . '/resources/views/venix/admin/hosts/index.tpl');
$client = file_get_contents($root . '/public/cloud/admin-hosts.js');
$hostWriteStart = strpos($controller, 'public function hosts(');
$hostWriteEnd = strpos($controller, 'public function hostScheduling(', $hostWriteStart);
$hostWrite = $hostWriteStart === false || $hostWriteEnd === false ? ''
: substr($controller, $hostWriteStart, $hostWriteEnd - $hostWriteStart);
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$assert(str_contains($schema, "country_code CHAR(2) NOT NULL DEFAULT ''")
&& str_contains($schema,
"default_network VARCHAR(128) NOT NULL DEFAULT 'bridge:br0'")
&& !preg_match('/\b(?:ALTER|DROP)\s+TABLE\b/i', $schema),
'Fresh cloud_hosts schema must contain structured location and the bridge:br0 default without upgrade DDL.');
$assert(str_contains($controller, "'region', 'country_code'")
&& str_contains($controller,
"CloudCountryCatalog::normalize(\$body['country_code'] ?? null)")
&& !str_contains($controller, 'HOST_COUNTRY_CODES')
&& str_contains($controller, '$host->country_code = $countryCode;')
&& $hostWrite !== ''
&& !str_contains($hostWrite,
'invalidateForNetworkSelectorChange')
&& !str_contains($hostWrite, '->request('),
'Host writes must persist country_code without making compatibility metadata an Agent readiness authority.');
$assert(str_contains($catalog, "'CN' => ['name' => '中国'")
&& str_contains($catalog, "'HK' => ['name' => '中国香港'")
&& str_contains($catalog, "'JP' => ['name' => '日本'")
&& str_contains($catalog, "'US' => ['name' => '美国'")
&& str_contains($catalog, "'SG' => ['name' => '新加坡'")
&& str_contains($catalog, '$code = strtoupper(trim($value));')
&& !str_contains($catalog, 'safeText'),
'The shared country catalog must normalize complete values without truncation.');
$assert(str_contains($pageController,
"assign('cloud_country_options', CloudCountryCatalog::options())")
&& str_contains($legacyPageController,
"assign('cloud_country_options', CloudCountryCatalog::options())")
&& str_contains($template, '{foreach $cloud_country_options as $country}')
&& str_contains($template, '{$country.name|escape}')
&& str_contains($template, '({$country.code|escape})'),
'Host country selector must render the shared readable country catalog.');
$assert(str_contains($template, 'id="host-country-code"')
&& preg_match('/id="host-region"[^>]*required[^>]*maxlength="64"/', $template) === 1
&& str_contains($controller, '具体地区需为 1 至 64 字符'),
'Host editor must require a country and an exact region within the panel limit.');
$assert(str_contains($client, "'host-country-code': h.country_code || ''")
&& str_contains($client,
"country_code: byId('host-country-code').value"),
'Host editor must round-trip country_code through the admin API.');
$assert(str_contains($hostWrite, 'CloudNetworkSelector::isValid($network)')
&& str_contains($template, 'id="host-network-type"')
&& str_contains($template, 'value="bridge" selected')
&& str_contains($template, 'value="network"')
&& str_contains($template, 'id="host-network-name"')
&& str_contains($template, 'value="br0"')
&& str_contains($template, 'id="host-network-warning"')
&& !str_contains($template, 'Agent 网络选择器 key'),
'Host network input must select an existing bridge or libvirt network with an explicit legacy warning.');
$legacyPublicInputs = [
'host-public-pool', 'host-gateway', 'host-dns', 'host-reserved',
'host-vm-prefix',
];
foreach ($legacyPublicInputs as $inputId) {
$assert(!str_contains($template, 'id="' . $inputId . '"'),
'The main host form still exposes the retired single-pool input: '
. $inputId);
}
$assert(!str_contains($client, 'function hostPublicNetworkPayload()')
&& !str_contains($controller, 'normalizeHostPublicNetwork')
&& str_contains($template, 'id="host-network-pools-drawer"')
&& !str_contains($hostWrite, 'public_pool_cidr')
&& !str_contains($hostWrite, 'public_dns_servers'),
'Public pools must use the dedicated panel-owned drawer, separate from host metadata writes.');
$assert(str_contains($template, 'href="/admin/agents"')
&& str_contains($template, '配置宿主机 Agent API')
&& !str_contains($template, 'host-private-key')
&& !str_contains($client, 'private_key')
&& !str_contains($controller, 'engineHost'),
'Host metadata must not retain old SSH/engine credentials; Agent API settings are managed on their dedicated page.');
echo "Structured cloud host country and region contracts passed.\n";