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

148 lines
6.7 KiB
PHP

<?php
// Static VPC data-boundary checks. This test intentionally requires no
// database, Agent, network namespace or application bootstrap.
$root = dirname(__DIR__);
$schema = file_get_contents($root . '/sql/cloud.sql');
$setup = file_get_contents($root . '/app/Command/CloudSetup.php');
$feature = file_get_contents(
$root . '/app/Services/Cloud/CloudVpcFeature.php'
);
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
foreach ([
'cloud_host_vpc_transports',
'cloud_vpc_cidr_pools',
'cloud_vpcs',
'cloud_vpc_ports',
'cloud_vpc_host_states',
'cloud_vpc_membership_batches',
'cloud_vpc_membership_batch_items',
] as $table) {
$assert(str_contains($schema, 'CREATE TABLE ' . $table . ' ('),
'Missing VPC table: ' . $table);
$assert(str_contains($setup, "'" . $table . "'"),
'cloudInit does not require VPC table: ' . $table);
}
$assert(str_contains($schema,
'transport_address VARCHAR(15) CHARACTER SET ascii COLLATE ascii_bin NOT NULL')
&& str_contains($schema,
'transport_interface VARCHAR(15) CHARACTER SET ascii COLLATE ascii_bin NOT NULL')
&& str_contains($schema,
"transport_mode VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'direct_l3'")
&& str_contains($schema, 'observed_revision <= desired_revision')
&& str_contains($schema, 'cloud_host_vpc_transport_state'),
'Host VPC transport intent or convergence fields are incomplete.');
$assert(str_contains($schema, 'cloud_vpc_cidr_pool_region (region_id, cidr)')
&& str_contains($schema,
'allocation_prefix BETWEEN prefix_length AND 28')
&& str_contains($schema,
'allocation_prefix BETWEEN 16 AND 28')
&& str_contains($schema, 'revision BIGINT UNSIGNED NOT NULL DEFAULT 1')
&& str_contains($schema,
'cloud_vpc_region_cidr (region_id, cidr, active_claim)')
&& str_contains($schema, 'cloud_vpc_pool_state (cidr_pool_id, state, id)'),
'Region-scoped VPC CIDR allocation is not protected.');
$assert(str_contains($schema,
'cloud_vpc_region_cidr (region_id, cidr, active_claim)')
&& str_contains($schema,
'active_claim IS NULL OR active_claim = 1')
&& str_contains($schema,
"state = 'deleted' AND active_claim IS NULL AND deleted_at IS NOT NULL")
&& str_contains($schema,
"state <> 'deleted' AND active_claim = 1 AND deleted_at IS NULL")
&& str_contains($schema,
'cloud_vpc_port_ip_claim (vpc_id, private_ip, active_claim)')
&& str_contains($schema,
'cloud_vpc_port_instance_claim (instance_id, active_claim)')
&& str_contains($schema,
"state = 'migrating' AND migration_host_id IS NOT NULL")
&& str_contains($schema, 'active_claim IS NULL OR active_claim = 1')
&& str_contains($schema,
"state = 'released' AND active_claim IS NULL AND released_at IS NOT NULL")
&& str_contains($schema, 'applied_revision <= desired_revision'),
'VPC port allocation, release or revision invariants are incomplete.');
$assert(str_contains($schema,
'cloud_vpc_host_identity (vpc_id, host_id)')
&& str_contains($schema,
'cloud_vpc_host_sync (host_id, sync_state, updated_at, vpc_id)')
&& str_contains($schema,
'UNIQUE KEY cloud_vpc_host_operation (last_operation_id)')
&& str_contains($schema,
"sync_state IN ('pending','applying','ready','failed','unknown')"),
'Per-host VPC desired/applied convergence ledger is incomplete.');
$jobsStart = strpos($schema, 'CREATE TABLE cloud_jobs (');
$jobsEnd = strpos($schema, ') ENGINE=InnoDB', $jobsStart);
$jobs = $jobsStart === false || $jobsEnd === false ? ''
: substr($schema, $jobsStart, $jobsEnd - $jobsStart);
$assert(str_contains($jobs, 'vpc_id BIGINT UNSIGNED DEFAULT NULL')
&& str_contains($jobs, 'vpc_port_id BIGINT UNSIGNED DEFAULT NULL')
&& str_contains($jobs, 'vpc_batch_id BIGINT UNSIGNED DEFAULT NULL')
&& str_contains($jobs, 'supersedes_job_id BIGINT UNSIGNED DEFAULT NULL')
&& str_contains($jobs,
'vpc_repair_root_job_id BIGINT UNSIGNED DEFAULT NULL')
&& str_contains($jobs, 'KEY cloud_job_vpc (vpc_id, id)')
&& str_contains($jobs, 'KEY cloud_job_vpc_port (vpc_port_id, id)')
&& str_contains($jobs, 'KEY cloud_job_vpc_batch (vpc_batch_id, id)')
&& str_contains($jobs,
'KEY cloud_job_supersedes (supersedes_job_id, id)')
&& str_contains($jobs,
'KEY cloud_job_vpc_repair_root (vpc_repair_root_job_id, id)'),
'Cloud jobs cannot be correlated to VPC or VPC-port work.');
$assert(str_contains($setup,
"private const SCHEMA_VERSION = 'go-agent-wss-v18-rescue-catalog'")
&& str_contains($setup, "'name' => 'cloud_vpc_enabled'")
&& str_contains($setup, "'value' => 'false'")
&& str_contains($setup,
"'cloud_job_vpc', 'cloud_job_vpc_port', 'cloud_job_vpc_batch',")
&& str_contains($setup,
"'cloud_job_supersedes', 'cloud_job_vpc_repair_root'")
&& str_contains($setup, "'chk_cloud_vpc_port_release'"),
'cloudInit does not publish and validate the disabled VPC schema.');
$assert(str_contains($feature,
"public const CONFIG_KEY = 'cloud_vpc_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 VPC feature switch is incomplete.');
$models = [
'CloudHostVpcTransport' => 'cloud_host_vpc_transports',
'CloudVpcCidrPool' => 'cloud_vpc_cidr_pools',
'CloudVpc' => 'cloud_vpcs',
'CloudVpcPort' => 'cloud_vpc_ports',
'CloudVpcHostState' => 'cloud_vpc_host_states',
'CloudVpcMembershipBatch' => 'cloud_vpc_membership_batches',
'CloudVpcMembershipBatchItem' => 'cloud_vpc_membership_batch_items',
];
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 VPC model: ' . $class);
}
$jobModel = file_get_contents($root . '/app/Models/CloudJob.php');
$assert(str_contains($jobModel, "'vpc_id' => 'int'")
&& str_contains($jobModel, "'vpc_port_id' => 'int'")
&& str_contains($jobModel, "'vpc_batch_id' => 'int'")
&& str_contains($jobModel, "'supersedes_job_id' => 'int'")
&& str_contains($jobModel, "'vpc_repair_root_job_id' => 'int'"),
'CloudJob does not cast VPC correlations.');
echo "VPC schema, model and feature-switch contracts passed.\n";