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

139 lines
6.5 KiB
PHTML

<?php
// Static multi-host data-boundary checks. This test needs no database,
// engine, network or application bootstrap.
$root = dirname(__DIR__);
$schema = file_get_contents($root . '/sql/cloud.sql');
$baseSchema = file_get_contents($root . '/sql/ss.sql');
$setup = file_get_contents($root . '/app/Command/CloudSetup.php');
$feature = file_get_contents(
$root . '/app/Services/Cloud/CloudNetworkLineFeature.php'
);
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
foreach ([
'cloud_regions',
'cloud_network_lines',
'cloud_resource_pools',
'cloud_host_capacity_policies',
'cloud_public_ips',
'cloud_offers',
'cloud_resource_reservations',
] as $table) {
$assert(str_contains($schema, 'CREATE TABLE IF NOT EXISTS ' . $table . ' ('),
'Missing multi-host table: ' . $table);
}
$assert(str_contains($schema, 'UNIQUE KEY cloud_region_code (code)')
&& str_contains($schema,
'UNIQUE KEY cloud_network_line_code (region_id, code)')
&& str_contains($schema,
'UNIQUE KEY cloud_resource_pool_code (line_id, code)')
&& str_contains($schema,
'UNIQUE KEY cloud_offer_plan_pool (plan_id, resource_pool_id)')
&& str_contains($schema,
'UNIQUE KEY cloud_resource_reservation_order (order_id)'),
'Region, line, pool, offer or reservation identities are not protected.');
$assert(str_contains($schema,
"scheduler_state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'disabled'")
&& str_contains($schema,
'cloud_host_scheduler (resource_pool_id, scheduler_state, id)'),
'Host scheduling intent is not indexed as the panel authority.');
$assert(!str_contains($schema,
'CREATE TABLE IF NOT EXISTS cloud_host_capacity_snapshots')
&& !str_contains($schema,
'CREATE TABLE IF NOT EXISTS cloud_host_inventory_attempts')
&& !str_contains($schema, 'health_state VARCHAR'),
'Retired host observation tables or health columns remain in the schema.');
$assert(str_contains($schema, 'configured_cpu_cores')
&& str_contains($schema, 'safety_memory_mib')
&& str_contains($schema, 'configured_disk_gib')
&& str_contains($schema, 'max_instances')
&& str_contains($schema, 'cloud_resource_reservation_host')
&& str_contains($schema, "state VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'held'"),
'Panel scheduling policy and reservation-ledger capacity fields are incomplete.');
$assert(str_contains($schema, 'UNIQUE KEY cloud_public_ip_address (address)')
&& str_contains($schema, 'cloud_public_ip_available (host_id, state, id)')
&& str_contains($schema, 'operation_id CHAR(32)')
&& str_contains($schema,
'UNIQUE KEY cloud_public_ip_reservation (reservation_id)')
&& str_contains($schema,
'UNIQUE KEY cloud_public_ip_operation (operation_id)')
&& str_contains($schema,
'UNIQUE KEY cloud_resource_reservation_operation (operation_id)')
&& str_contains($schema, 'public_ip_id BIGINT UNSIGNED DEFAULT NULL'),
'Concrete public IP reservation and uncertain-operation fields are incomplete.');
$assert(str_contains($schema, 'offer_id BIGINT UNSIGNED DEFAULT NULL')
&& str_contains($schema, 'resource_pool_id BIGINT UNSIGNED DEFAULT NULL')
&& str_contains($schema, 'reservation_id BIGINT UNSIGNED DEFAULT NULL')
&& str_contains($schema,
"currency CHAR(3) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'USD'")
&& str_contains($schema, 'price_version INT UNSIGNED NOT NULL DEFAULT 1')
&& str_contains($schema, 'revision INT UNSIGNED NOT NULL DEFAULT 1')
&& str_contains($schema, 'ALTER TABLE cloud_orders MODIFY host_id BIGINT UNSIGNED NULL'),
'Orders, invoices or instances cannot retain offer and placement results.');
$shopStart = strpos($baseSchema, 'CREATE TABLE `shop` (');
$shopEnd = strpos($baseSchema, ') ENGINE=InnoDB', $shopStart);
$shop = $shopStart === false || $shopEnd === false ? ''
: substr($baseSchema, $shopStart, $shopEnd - $shopStart);
$assert(str_contains($shop, '`created_at` bigint(20) unsigned')
&& str_contains($shop, '`updated_at` bigint(20) unsigned'),
'Fresh base-plan rows lack stable audit timestamps.');
$seedStart = strpos($setup, 'private static function ensureMultiHostDefaults');
$seedEnd = strpos($setup, 'private static function upgradeReferralSchema', $seedStart);
$seed = $seedStart === false || $seedEnd === false ? ''
: substr($setup, $seedStart, $seedEnd - $seedStart);
$assert(str_contains($setup, 'self::ensureMultiHostDefaults($connection);')
&& str_contains($seed, "'name' => 'cloud_network_lines_enabled'")
&& str_contains($seed, "'value' => 'false'")
&& str_contains($seed, 'insertOrIgnore')
&& stripos($seed, 'truncate') === false
&& stripos($seed, 'drop(') === false
&& stripos($seed, 'delete(') === false,
'cloudInit must seed a disabled line switch without deleting operator data.');
$assert(str_contains($feature,
"public const CONFIG_KEY = 'cloud_network_lines_enabled'")
&& str_contains($feature, 'public static function enabled(): bool')
&& str_contains($feature, 'return self::$cached = false;')
&& str_contains($feature, 'public static function save(bool $enabled): void'),
'The fail-closed network-line feature switch is incomplete.');
$models = [
'CloudRegion' => 'cloud_regions',
'CloudNetworkLine' => 'cloud_network_lines',
'CloudResourcePool' => 'cloud_resource_pools',
'CloudHostCapacityPolicy' => 'cloud_host_capacity_policies',
'CloudPublicIp' => 'cloud_public_ips',
'CloudOffer' => 'cloud_offers',
'CloudResourceReservation' => 'cloud_resource_reservations',
];
foreach ($models as $class => $table) {
$source = file_get_contents($root . '/app/Models/' . $class . '.php');
$assert($source !== false
&& str_contains($source, 'final class ' . $class . ' extends Model')
&& str_contains($source, "protected \$table = '" . $table . "';")
&& str_contains($source, 'protected $casts = ['),
'Missing or incomplete Eloquent boundary: ' . $class);
}
foreach (['CloudHost', 'Shop', 'CloudOrder', 'CloudInvoice', 'CloudInstance']
as $class) {
$source = file_get_contents($root . '/app/Models/' . $class . '.php');
$assert($source !== false && str_contains($source, 'protected $casts = ['),
'Existing model lacks canonical multi-host casts: ' . $class);
}
echo "Multi-host schema, model and feature-switch contracts passed.\n";