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

450 lines
19 KiB
PHTML

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\CloudHost;
use App\Services\Cloud\CloudAdminHostProjectionService;
use App\Services\Cloud\CloudAdminResourceService;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$capsule = new Capsule();
$capsule->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
], 'default');
$capsule->setAsGlobal();
$capsule->bootEloquent();
$schema = $capsule->schema('default');
$schema->create('configurations', static function (Blueprint $table): void {
$table->increments('id');
$table->string('name')->unique();
$table->text('value');
});
$schema->create('cloud_hosts', static function (Blueprint $table): void {
$table->increments('id');
$table->string('name');
$table->string('region');
$table->string('country_code', 2);
$table->string('default_network');
$table->boolean('enabled');
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_host_resource_pool_memberships',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('resource_pool_id');
$table->string('scheduler_state');
$table->integer('priority');
$table->integer('weight');
$table->integer('revision');
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_regions', static function (Blueprint $table): void {
$table->increments('id');
$table->string('code');
$table->string('name');
$table->string('country_code', 2);
$table->string('status');
$table->integer('sort');
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_network_lines', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('region_id');
$table->string('code');
$table->string('name');
$table->boolean('is_default');
$table->string('status');
$table->integer('sort');
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_resource_pools', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('line_id');
$table->string('code');
$table->string('name');
$table->string('status');
$table->string('scheduling_policy');
$table->integer('sort');
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_offers', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('resource_pool_id');
});
$schema->create('cloud_host_capacity_policies',
static function (Blueprint $table): void {
$table->integer('host_id')->primary();
$table->integer('configured_cpu_cores');
$table->integer('configured_memory_mib');
$table->integer('configured_disk_gib');
$table->integer('safety_cpu_cores');
$table->integer('safety_memory_mib');
$table->integer('safety_disk_gib');
$table->integer('max_instances');
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_host_agents', static function (Blueprint $table): void {
$table->integer('host_id')->primary();
$table->text('sealed_api_secret');
$table->boolean('enabled');
$table->integer('revision');
$table->integer('tested_revision');
$table->string('last_test_status');
$table->string('last_error_code');
$table->string('last_agent_version');
$table->text('network_selectors_json');
$table->text('public_network_capabilities_json');
$table->integer('last_tested_at');
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_host_storage_configurations',
static function (Blueprint $table): void {
$table->integer('host_id')->primary();
$table->integer('revision');
$table->string('state');
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_resource_reservations',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('resource_pool_id');
$table->integer('host_id');
$table->string('state');
$table->integer('instance_id')->nullable();
$table->integer('cpu_cores');
$table->integer('memory_mib');
$table->integer('disk_gib');
});
$schema->create('cloud_instances', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->string('status');
$table->text('spec_json');
});
$schema->create('cloud_instance_snapshots',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('instance_id');
$table->integer('host_id');
$table->string('state');
$table->integer('size_bytes')->nullable();
});
$schema->create('cloud_host_network_attachments',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('line_id');
$table->string('network_selector');
$table->string('state');
$table->string('sync_state');
$table->integer('verified_agent_revision');
$table->string('verification_state');
$table->integer('last_verified_at');
$table->integer('observed_revision');
$table->integer('desired_revision');
});
$schema->create('cloud_public_ip_pools',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('network_attachment_id');
$table->integer('address_family');
$table->string('pool_key');
$table->string('cidr');
$table->integer('guest_prefix_length');
$table->integer('max_allocations')->default(0);
$table->string('state');
$table->string('sync_state');
$table->integer('observed_revision');
$table->integer('desired_revision');
});
$schema->create('cloud_public_ips', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('public_ip_pool_id');
$table->integer('address_family');
$table->string('address');
$table->integer('prefix_length');
$table->string('state');
$table->integer('reservation_id')->nullable();
$table->integer('instance_id')->nullable();
$table->string('operation_id')->nullable();
});
Capsule::table('configurations')->insert([
'name' => 'cloud_network_lines_enabled',
'value' => 'true',
]);
Capsule::table('cloud_hosts')->insert([
'id' => 1,
'name' => 'HKG-A',
'region' => 'Hong Kong',
'country_code' => 'HK',
'default_network' => 'bridge:retired-default',
'enabled' => 1,
'created_at' => 1,
'updated_at' => 1,
]);
Capsule::table('cloud_regions')->insert([
'id' => 1, 'code' => 'hkg', 'name' => 'Hong Kong',
'country_code' => 'HK', 'status' => 'active', 'sort' => 1,
'created_at' => 1, 'updated_at' => 1,
]);
Capsule::table('cloud_network_lines')->insert([
['id' => 10, 'region_id' => 1, 'code' => 'premium',
'name' => 'Premium', 'is_default' => 0, 'status' => 'active',
'sort' => 10, 'created_at' => 1, 'updated_at' => 1],
['id' => 11, 'region_id' => 1, 'code' => 'standard',
'name' => 'Standard', 'is_default' => 1, 'status' => 'active',
'sort' => 20, 'created_at' => 1, 'updated_at' => 1],
]);
Capsule::table('cloud_resource_pools')->insert([
['id' => 20, 'line_id' => 10, 'code' => 'premium-pool',
'name' => 'Premium Pool', 'status' => 'active',
'scheduling_policy' => 'balanced', 'sort' => 10,
'created_at' => 1, 'updated_at' => 1],
['id' => 21, 'line_id' => 11, 'code' => 'standard-pool',
'name' => 'Standard Pool', 'status' => 'active',
'scheduling_policy' => 'balanced', 'sort' => 20,
'created_at' => 1, 'updated_at' => 1],
]);
Capsule::table('cloud_host_resource_pool_memberships')->insert([
['id' => 30, 'host_id' => 1, 'resource_pool_id' => 20,
'scheduler_state' => 'active', 'priority' => 20, 'weight' => 100,
'revision' => 1, 'created_at' => 1, 'updated_at' => 1],
['id' => 31, 'host_id' => 1, 'resource_pool_id' => 21,
'scheduler_state' => 'active', 'priority' => 10, 'weight' => 100,
'revision' => 2, 'created_at' => 1, 'updated_at' => 1],
]);
Capsule::table('cloud_host_capacity_policies')->insert([
'host_id' => 1, 'configured_cpu_cores' => 16,
'configured_memory_mib' => 32768, 'configured_disk_gib' => 800,
'safety_cpu_cores' => 2, 'safety_memory_mib' => 1024,
'safety_disk_gib' => 50, 'max_instances' => 50,
'created_at' => 1, 'updated_at' => 1,
]);
Capsule::table('cloud_host_agents')->insert([
'host_id' => 1, 'sealed_api_secret' => 'sealed', 'enabled' => 1,
'revision' => 1, 'tested_revision' => 1,
'last_test_status' => 'healthy', 'last_error_code' => '',
'last_agent_version' => 'v3', 'last_tested_at' => 1,
'network_selectors_json' => json_encode([
'bridge:br-premium', 'bridge:br-standard',
], JSON_THROW_ON_ERROR),
'public_network_capabilities_json' =>
'{"contract":1,"dual_stack_required":true,"project_owned_filter":true,"ndp_guard":true}',
'created_at' => 1, 'updated_at' => 1,
]);
Capsule::table('cloud_host_storage_configurations')->insert([
'host_id' => 1, 'revision' => 1, 'state' => 'applied',
'created_at' => 1, 'updated_at' => 1,
]);
Capsule::table('cloud_host_network_attachments')->insert([
['id' => 40, 'host_id' => 1, 'line_id' => 10,
'network_selector' => 'bridge:br-premium', 'state' => 'active',
'sync_state' => 'never', 'verified_agent_revision' => 1,
'verification_state' => 'verified', 'observed_revision' => 0,
'last_verified_at' => 1, 'desired_revision' => 2],
['id' => 41, 'host_id' => 1, 'line_id' => 11,
'network_selector' => 'bridge:br-standard', 'state' => 'active',
'sync_state' => 'never', 'verified_agent_revision' => 1,
'verification_state' => 'verified', 'observed_revision' => 0,
'last_verified_at' => 1, 'desired_revision' => 3],
]);
Capsule::table('cloud_public_ip_pools')->insert([
['id' => 50, 'host_id' => 1, 'network_attachment_id' => 40,
'address_family' => 4,
'pool_key' => str_repeat('a', 32), 'cidr' => '8.8.8.0/29',
'guest_prefix_length' => 29, 'max_allocations' => 0,
'state' => 'active', 'sync_state' => 'applied',
'observed_revision' => 2, 'desired_revision' => 2],
['id' => 51, 'host_id' => 1, 'network_attachment_id' => 41,
'address_family' => 4,
'pool_key' => str_repeat('b', 32), 'cidr' => '9.9.9.0/29',
'guest_prefix_length' => 29, 'max_allocations' => 0,
'state' => 'active', 'sync_state' => 'applied',
'observed_revision' => 3, 'desired_revision' => 3],
['id' => 52, 'host_id' => 1, 'network_attachment_id' => 40,
'address_family' => 6,
'pool_key' => str_repeat('c', 32),
'cidr' => '2404:c140:156:3a::/64',
'guest_prefix_length' => 64, 'max_allocations' => 100,
'state' => 'active', 'sync_state' => 'applied',
'observed_revision' => 1, 'desired_revision' => 1],
['id' => 53, 'host_id' => 1, 'network_attachment_id' => 41,
'address_family' => 6,
'pool_key' => str_repeat('d', 32),
'cidr' => '2404:c140:156:3b::/64',
'guest_prefix_length' => 64, 'max_allocations' => 100,
'state' => 'active', 'sync_state' => 'applied',
'observed_revision' => 1, 'desired_revision' => 1],
]);
Capsule::table('cloud_public_ips')->insert([
['id' => 60, 'host_id' => 1, 'public_ip_pool_id' => 50,
'address_family' => 4,
'address' => '8.8.8.2', 'prefix_length' => 29,
'state' => 'available', 'reservation_id' => null,
'instance_id' => null, 'operation_id' => null],
['id' => 61, 'host_id' => 1, 'public_ip_pool_id' => 51,
'address_family' => 4,
'address' => '9.9.9.2', 'prefix_length' => 29,
'state' => 'available', 'reservation_id' => null,
'instance_id' => null, 'operation_id' => null],
]);
$projection = CloudAdminHostProjectionService::listing();
$assert(count($projection['hosts']) === 1,
'Expected exactly one physical host projection.');
$host = $projection['hosts'][0];
$assert($host['resource_pool_id'] === 21
&& $host['scheduler_state'] === 'active'
&& $host['active_memberships'] === 2
&& $host['draining_memberships'] === 0,
'Compact fields were not a deterministic primary-membership summary.');
$assert(array_column($host['memberships'], 'resource_pool_id') === [20, 21]
&& count(array_filter($host['memberships'],
static fn (array $row): bool => $row['placement_ready'])) === 2
&& array_column($host['lines'], 'id') === [10, 11]
&& $host['readiness']['network_ready'] === true
&& $host['readiness']['placement_ready'] === true,
'The host did not expose every exact membership and verified target line.');
$assert($host['capacity']['cpu']['configured'] === 16
&& $host['capacity']['ip']['bundles']['available'] === 2
&& $projection['summary']['capacity']['cpu']['configured'] === 16,
'Physical capacity was duplicated across active memberships.');
Capsule::table('cloud_host_agents')->where('host_id', 1)->update([
'public_network_capabilities_json' => '{}',
]);
$missingPublicNetworkContract =
CloudAdminHostProjectionService::listing()['hosts'][0];
$missingPublicNetworkResources = CloudAdminResourceService::page();
$assert($missingPublicNetworkContract['readiness']['network_ready'] === false
&& $missingPublicNetworkContract['capacity']['ip']['bundles']['available'] === 0
&& count(array_filter(
$missingPublicNetworkResources['hosts'],
static fn (array $row): bool => $row['network_ready']
)) === 0,
'Host projection accepted missing public-network capability evidence.');
Capsule::table('cloud_host_agents')->where('host_id', 1)->update([
'public_network_capabilities_json' =>
'{"contract":1,"dual_stack_required":true,"project_owned_filter":true,"ndp_guard":true}',
]);
Capsule::table('cloud_host_network_attachments')->where('id', 40)->update([
'last_verified_at' => 0,
]);
$staleTimestamp = CloudAdminHostProjectionService::listing()['hosts'][0];
$staleTimestampResources = CloudAdminResourceService::page();
$assert(count(array_filter(
$staleTimestamp['memberships'],
static fn (array $row): bool => (int) $row['resource_pool_id'] === 20
&& $row['network_ready'] === true
)) === 0
&& count(array_filter(
$staleTimestampResources['hosts'],
static fn (array $row): bool => (int) $row['resource_pool_id'] === 20
&& $row['network_ready'] === true
)) === 0,
'An attachment without a verification timestamp remained placement-ready.');
Capsule::table('cloud_host_network_attachments')->where('id', 40)->update([
'last_verified_at' => 1,
]);
Capsule::table('cloud_public_ip_pools')->where('id', 53)->update([
'state' => 'disabled',
]);
$oneIncompletePair = CloudAdminHostProjectionService::listing()['hosts'][0];
$assert($oneIncompletePair['capacity']['ip']['bundles']['available'] === 1
&& count(array_filter(
$oneIncompletePair['memberships'],
static fn (array $row): bool => $row['network_ready']
)) === 1,
'A line without an active IPv6 companion pool remained placement-ready.');
$oneIncompletePairResources = CloudAdminResourceService::page();
$readyResourceMemberships = array_values(array_filter(
$oneIncompletePairResources['hosts'],
static fn (array $row): bool => $row['network_ready']
));
$assert(count($readyResourceMemberships) === 1
&& (int) $readyResourceMemberships[0]['resource_pool_id'] === 20,
'Resource capacity accepted a line without its IPv6 companion pool.');
Capsule::table('cloud_public_ip_pools')->where('id', 53)->update([
'state' => 'active',
]);
$resources = CloudAdminResourceService::page();
$pools = [];
foreach ($resources['pools'] as $pool) {
$pools[(int) $pool['id']] = $pool;
}
$assert(count($resources['hosts']) === 2
&& array_column($resources['hosts'], 'resource_pool_id') === [20, 21]
&& $pools[20]['health']['host_count'] === 1
&& $pools[21]['health']['host_count'] === 1,
'Resource catalog did not map each membership to its exact pool.');
$assert($pools[20]['capacity_scope'] === 'shared_host'
&& $pools[21]['capacity_shared'] === true
&& $pools[20]['capacity']['cpu']['configured'] === 16
&& $pools[21]['capacity']['cpu']['configured'] === 16
&& $resources['summary']['hosts_total'] === 1
&& $resources['summary']['placement_ready_hosts'] === 1
&& $resources['summary']['capacity']['cpu']['configured'] === 16
&& $resources['summary']['placement_capacity']['cpu']['configured'] === 16,
'Shared pool views or platform totals duplicated physical host capacity.');
$single = CloudAdminHostProjectionService::one(CloudHost::find(1));
$assert(count($single['memberships']) === 2 && count($single['lines']) === 2,
'Single-host projection drifted from the multi-membership list DTO.');
Capsule::table('cloud_host_agents')->where('host_id', 1)->update([
'network_selectors_json' => '[]',
]);
$missingSelector = CloudAdminHostProjectionService::listing()['hosts'][0];
$assert($missingSelector['readiness']['network_ready'] === false
&& $missingSelector['readiness']['placement_ready'] === false
&& $missingSelector['capacity']['ip']['bundles']['available'] === 0,
'A selector missing from current Agent evidence was advertised as ready.');
$missingSelectorResources = CloudAdminResourceService::page();
$assert(count(array_filter(
$missingSelectorResources['hosts'],
static fn (array $row): bool => $row['network_ready']
)) === 0
&& $missingSelectorResources['summary']['placement_ready_hosts'] === 0
&& $missingSelectorResources['summary']['placement_capacity']['cpu']
['configured'] === 0,
'Resource catalog accepted an attachment absent from Agent evidence.');
Capsule::table('cloud_host_agents')->where('host_id', 1)->update([
'network_selectors_json' => json_encode([
'bridge:br-premium', 'bridge:br-standard',
], JSON_THROW_ON_ERROR),
]);
Capsule::table('cloud_host_network_attachments')->update([
'verified_agent_revision' => 0,
]);
$staleRevision = CloudAdminHostProjectionService::listing()['hosts'][0];
$assert($staleRevision['readiness']['network_ready'] === false
&& $staleRevision['capacity']['ip']['bundles']['available'] === 0,
'Stale attachment verification was advertised as ready.');
echo "Administrator host multi-membership projection passed.\n";