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

200 lines
7.6 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudAdminPublicNetworkCapacityProjection;
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_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->integer('host_id');
$table->integer('line_id');
$table->string('network_selector');
$table->string('state');
$table->string('verification_state');
$table->integer('verified_agent_revision');
$table->integer('last_verified_at');
});
$schema->create('cloud_public_ip_pools',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('network_attachment_id');
$table->string('pool_key');
$table->integer('address_family');
$table->string('cidr');
$table->integer('guest_prefix_length');
$table->integer('max_allocations');
$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('cloud_host_agents')->insert([
'host_id' => 1,
'enabled' => 1,
'revision' => 1,
'tested_revision' => 1,
'last_test_status' => 'healthy',
'network_selectors_json' => json_encode([
'bridge:a', 'bridge:b', 'bridge:c', 'bridge:d',
], JSON_THROW_ON_ERROR),
'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, 'host_id' => 1, 'line_id' => 100,
'network_selector' => 'bridge:a', 'state' => 'active',
'verification_state' => 'verified', 'verified_agent_revision' => 1,
'last_verified_at' => 100],
['id' => 11, 'host_id' => 1, 'line_id' => 100,
'network_selector' => 'bridge:b', 'state' => 'active',
'verification_state' => 'verified', 'verified_agent_revision' => 1,
'last_verified_at' => 100],
['id' => 12, 'host_id' => 1, 'line_id' => 101,
'network_selector' => 'bridge:c', 'state' => 'active',
'verification_state' => 'verified', 'verified_agent_revision' => 1,
'last_verified_at' => 100],
['id' => 13, 'host_id' => 1, 'line_id' => 100,
'network_selector' => 'bridge:d', 'state' => 'active',
'verification_state' => 'verified', 'verified_agent_revision' => 1,
'last_verified_at' => 100],
]);
$pools = [];
$poolId = 100;
foreach ([
[10, 4, '8.8.8.0/29', 29, 0],
[10, 6, '2404:c140:156:3a::/64', 64, 5],
[11, 4, '9.9.9.0/29', 29, 0],
[11, 6, '2404:c140:156:3b::/64', 64, 0],
[12, 4, '1.1.1.0/29', 29, 0],
[12, 6, '2404:c140:156:3c::/64', 64, 1],
[13, 4, '8.8.4.0/29', 29, 0],
[13, 6, '2404:c140:156:3d::/64', 64, 1],
] as $index => [$attachmentId, $family, $cidr, $prefix, $maximum]) {
$id = ++$poolId;
$pools[$attachmentId][$family] = $id;
Capsule::table('cloud_public_ip_pools')->insert([
'id' => $id,
'host_id' => 1,
'network_attachment_id' => $attachmentId,
'pool_key' => str_repeat(dechex(($index % 15) + 1), 32),
'address_family' => $family,
'cidr' => $cidr,
'guest_prefix_length' => $prefix,
'max_allocations' => $maximum,
'state' => 'active',
'sync_state' => 'applied',
'observed_revision' => 1,
'desired_revision' => 1,
]);
}
$addresses = [];
$addressId = 0;
$add = static function (int $attachmentId, int $family, string $address,
string $state, ?int $instanceId = null
) use (&$addresses, &$addressId, $pools): void {
$addresses[] = [
'id' => ++$addressId,
'host_id' => 1,
'public_ip_pool_id' => $pools[$attachmentId][$family],
'address_family' => $family,
'address' => $address,
'prefix_length' => $family === 4 ? 29 : 64,
'state' => $state,
'reservation_id' => null,
'instance_id' => $instanceId,
'operation_id' => null,
];
};
// Attachment 10 owns one complete allocation and four concrete IPv4 slots.
$add(10, 4, '8.8.8.1', 'assigned', 9);
$add(10, 6, '2404:c140:156:3a::1', 'assigned', 9);
foreach (['8.8.8.2', '8.8.8.3', '8.8.8.4', '8.8.8.5'] as $address) {
$add(10, 4, $address, 'available');
}
$add(10, 4, '8.8.8.6', 'disabled');
$add(10, 6, '2404:c140:156:3a::2', 'disabled');
// Identity 42 crosses lines; identity 43 stays in one line but crosses
// attachments. Neither may become a complete dual-stack allocation.
$add(11, 4, '9.9.9.1', 'assigned', 42);
$add(12, 6, '2404:c140:156:3c::1', 'assigned', 42);
$add(11, 4, '9.9.9.2', 'assigned', 43);
$add(13, 6, '2404:c140:156:3d::1', 'assigned', 43);
Capsule::table('cloud_public_ips')->insert($addresses);
$projection = CloudAdminPublicNetworkCapacityProjection::forHosts([1]);
$aggregate = $projection[1]['aggregate'];
$attachment = $projection[1]['attachments'][10];
$assert($attachment['bundles']['available'] === 4,
'A disabled sparse IPv6 hole incorrectly reduced concurrent capacity.');
$assert($attachment['bundles']['allocated'] === 1
&& $attachment['bundles']['total'] === 5,
'Complete same-attachment dual-stack identities were not projected once.');
$assert($attachment['disabled_addresses'] === [
'unit' => 'address', 'ipv4' => 1, 'ipv6' => 1, 'total' => 2,
],
'Disabled concrete addresses were mixed into bundle accounting.');
$assert($aggregate['bundles']['allocated'] === 1
&& $aggregate['bundles']['available'] === 4
&& $aggregate['incomplete_claims']['allocated'] === 4,
'Allocation identities were paired across an attachment or line boundary.');
$assert($projection[1]['attachments'][11]['bundles']['allocated'] === 0
&& $projection[1]['attachments'][12]['bundles']['allocated'] === 0
&& $projection[1]['attachments'][13]['bundles']['allocated'] === 0,
'A split allocation identity became a complete attachment bundle.');
$assert($aggregate['bundles']['unit'] === 'dual_stack_bundle'
&& $aggregate['incomplete_claims']['unit'] === 'allocation_identity'
&& $aggregate['disabled_addresses']['unit'] === 'address',
'Capacity projection lost its explicit, non-mixable units.');
echo "Administrator public dual-stack capacity projection passed.\n";