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

264 lines
8.5 KiB
PHTML

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudPlacementNetworkService;
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('cloud_hosts', static function (Blueprint $table): void {
$table->increments('id');
$table->string('default_network')->default('bridge:br0');
$table->string('endpoint')->default('https://host.invalid');
});
$schema->create('cloud_host_agents', static function (Blueprint $table): void {
$table->integer('host_id')->primary();
$table->boolean('enabled');
$table->integer('revision');
$table->integer('tested_revision');
$table->string('last_test_status');
$table->text('network_selectors_json');
$table->text('public_network_capabilities_json');
});
$schema->create('cloud_host_network_attachments', static function (
Blueprint $table
): void {
$table->increments('id');
$table->string('attachment_key');
$table->integer('host_id');
$table->integer('line_id');
$table->string('network_selector');
$table->string('state');
$table->integer('priority');
$table->string('verification_state');
$table->integer('verified_agent_revision');
$table->integer('last_verified_at');
$table->integer('config_revision');
$table->integer('desired_revision');
$table->integer('observed_revision');
});
$schema->create('cloud_public_ip_pools', static function (
Blueprint $table
): void {
$table->increments('id');
$table->string('pool_key');
$table->integer('host_id');
$table->integer('network_attachment_id');
$table->integer('address_family');
$table->string('cidr');
$table->string('gateway');
$table->boolean('gateway_on_link');
$table->integer('guest_prefix_length');
$table->text('dns_servers_json');
$table->text('reserved_ips_json');
$table->string('allocation_start')->nullable();
$table->string('allocation_end')->nullable();
$table->integer('max_allocations');
$table->string('state');
$table->integer('priority');
$table->integer('desired_revision');
$table->integer('observed_revision');
$table->string('sync_state');
});
$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('cloud_hosts')->insert(['id' => 1]);
Capsule::table('cloud_host_agents')->insert([
'host_id' => 1,
'enabled' => 1,
'revision' => 5,
'tested_revision' => 5,
'last_test_status' => 'healthy',
'network_selectors_json' => '["bridge:br0","bridge:br1"]',
'public_network_capabilities_json' => json_encode([
'contract' => 1,
'dual_stack_required' => true,
'project_owned_filter' => true,
'ndp_guard' => true,
], JSON_THROW_ON_ERROR),
]);
Capsule::table('cloud_host_network_attachments')->insert([
[
'id' => 10,
'attachment_key' => str_repeat('1', 32),
'host_id' => 1,
'line_id' => 7,
'network_selector' => 'bridge:br0',
'state' => 'active',
'priority' => 0,
'verification_state' => 'verified',
'verified_agent_revision' => 5,
'last_verified_at' => time(),
'config_revision' => 3,
'desired_revision' => 3,
'observed_revision' => 3,
],
[
'id' => 11,
'attachment_key' => str_repeat('2', 32),
'host_id' => 1,
'line_id' => 7,
'network_selector' => 'bridge:br1',
'state' => 'active',
'priority' => 1,
'verification_state' => 'verified',
'verified_agent_revision' => 5,
'last_verified_at' => time(),
'config_revision' => 4,
'desired_revision' => 4,
'observed_revision' => 4,
],
]);
$common = [
'host_id' => 1,
'state' => 'active',
'priority' => 0,
'desired_revision' => 1,
'observed_revision' => 1,
'sync_state' => 'applied',
'reserved_ips_json' => '[]',
];
Capsule::table('cloud_public_ip_pools')->insert([
array_merge($common, [
'id' => 100,
'pool_key' => str_repeat('a', 32),
'network_attachment_id' => 10,
'address_family' => 4,
'cidr' => '8.8.8.0/24',
'gateway' => '8.8.8.1',
'gateway_on_link' => 0,
'guest_prefix_length' => 24,
'dns_servers_json' => '["1.1.1.1"]',
'allocation_start' => null,
'allocation_end' => null,
'max_allocations' => 0,
]),
array_merge($common, [
'id' => 200,
'pool_key' => str_repeat('b', 32),
'network_attachment_id' => 11,
'address_family' => 6,
'cidr' => '2404:c140:156:3a::/64',
'gateway' => '2404:c140:156::1',
'gateway_on_link' => 1,
'guest_prefix_length' => 64,
'dns_servers_json' => '["2606:4700:4700::1111"]',
'allocation_start' => '2404:c140:156:3a::10',
'allocation_end' => '2404:c140:156:3a::ff',
'max_allocations' => 2,
]),
]);
Capsule::table('cloud_public_ips')->insert([
'id' => 1,
'host_id' => 1,
'public_ip_pool_id' => 100,
'address_family' => 4,
'address' => '8.8.8.10',
'prefix_length' => 24,
'state' => 'available',
]);
$candidate = collect([
(object) ['id' => 1, 'resource_pool_line_id' => 7],
]);
$assert(CloudPlacementNetworkService::advisoryDualStackAvailableHosts(
$candidate
) === [],
'Pools split across two attachments were advertised as one dual-stack path.');
Capsule::table('cloud_public_ip_pools')->where('id', 200)->update([
'network_attachment_id' => 10,
]);
$connection = Capsule::connection('default');
$connection->flushQueryLog();
$connection->enableQueryLog();
$available = CloudPlacementNetworkService::advisoryDualStackAvailableHosts(
$candidate
);
$queryCount = count($connection->getQueryLog());
$connection->disableQueryLog();
$assert($available === [1 => true] && $queryCount <= 5,
'A sparse IPv6 pool with capacity was not paired with available IPv4.');
Capsule::table('cloud_host_agents')->where('host_id', 1)->update([
'public_network_capabilities_json' => '{}',
]);
$assert(CloudPlacementNetworkService::advisoryDualStackAvailableHosts(
$candidate
) === [],
'A host without current guarded-network capability remained sellable.');
Capsule::table('cloud_host_agents')->where('host_id', 1)->update([
'public_network_capabilities_json' => json_encode([
'contract' => 1,
'dual_stack_required' => true,
'project_owned_filter' => true,
'ndp_guard' => true,
], JSON_THROW_ON_ERROR),
]);
Capsule::table('cloud_public_ip_pools')->where('id', 100)->update([
'observed_revision' => 2,
]);
$assert(CloudPlacementNetworkService::advisoryDualStackAvailableHosts(
$candidate
) === [],
'A pool ahead of its desired revision remained visible as sellable.');
Capsule::table('cloud_public_ip_pools')->where('id', 100)->update([
'observed_revision' => 1,
]);
Capsule::table('cloud_public_ips')->insert([
[
'id' => 2,
'host_id' => 1,
'public_ip_pool_id' => 200,
'address_family' => 6,
'address' => '2404:c140:156:3a::10',
'prefix_length' => 64,
'state' => 'assigned',
'reservation_id' => null,
'instance_id' => 90,
'operation_id' => null,
],
[
'id' => 3,
'host_id' => 1,
'public_ip_pool_id' => 200,
'address_family' => 6,
'address' => '2404:c140:156:3a::11',
'prefix_length' => 64,
'state' => 'changing',
'reservation_id' => 91,
'instance_id' => null,
'operation_id' => str_repeat('c', 32),
],
]);
$assert(CloudPlacementNetworkService::advisoryDualStackAvailableHosts(
$candidate
) === [],
'A changing sparse IPv6 address was omitted from provisioning capacity.');
echo "Dual-stack placement advisory tests passed.\n";