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

212 lines
7.7 KiB
PHTML

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\CloudHostAgent;
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');
});
$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');
});
$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->integer('priority');
$table->string('verification_state');
$table->integer('verified_agent_revision');
$table->integer('last_verified_at');
// Deliberately stale: this is controller intent, not Agent evidence.
$table->string('sync_state')->default('never');
});
$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->string('cidr');
$table->string('gateway');
$table->integer('guest_prefix_length');
$table->text('dns_servers_json');
$table->text('reserved_ips_json');
$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->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,
'default_network' => 'bridge:retired',
]);
Capsule::table('cloud_host_agents')->insert([
'host_id' => 1,
'enabled' => 1,
'revision' => 4,
'tested_revision' => 4,
'last_test_status' => 'healthy',
'network_selectors_json' => '["bridge:line-a","bridge:line-b"]',
]);
Capsule::table('cloud_host_network_attachments')->insert([
[
'id' => 10, 'host_id' => 1, 'line_id' => 10,
'network_selector' => 'bridge:line-a', 'state' => 'active',
'priority' => 0, 'verification_state' => 'verified',
'verified_agent_revision' => 4, 'last_verified_at' => time(),
'sync_state' => 'never',
],
[
'id' => 20, 'host_id' => 1, 'line_id' => 20,
'network_selector' => 'bridge:line-b', 'state' => 'active',
'priority' => 0, 'verification_state' => 'verified',
'verified_agent_revision' => 4, 'last_verified_at' => time(),
'sync_state' => 'never',
],
]);
Capsule::table('cloud_public_ip_pools')->insert([
[
'id' => 100, 'pool_key' => str_repeat('a', 32), 'host_id' => 1,
'network_attachment_id' => 10, 'cidr' => '203.0.113.0/29',
'gateway' => '203.0.113.1', 'guest_prefix_length' => 29,
'dns_servers_json' => '["203.0.113.2"]',
'reserved_ips_json' => '["203.0.113.3"]', 'state' => 'active',
'priority' => 0, 'desired_revision' => 2, 'observed_revision' => 2,
'sync_state' => 'applied',
],
[
'id' => 200, 'pool_key' => str_repeat('b', 32), 'host_id' => 1,
'network_attachment_id' => 20, 'cidr' => '198.51.100.8/29',
'gateway' => '198.51.100.9', 'guest_prefix_length' => 29,
'dns_servers_json' => '[]', 'reserved_ips_json' => '[]',
'state' => 'active', 'priority' => 0, 'desired_revision' => 1,
'observed_revision' => 1, 'sync_state' => 'applied',
],
]);
Capsule::table('cloud_public_ips')->insert([
['id' => 1, 'host_id' => 1, 'public_ip_pool_id' => 100,
'address' => '203.0.113.1', 'prefix_length' => 29,
'state' => 'available'],
['id' => 2, 'host_id' => 1, 'public_ip_pool_id' => 100,
'address' => '203.0.113.2', 'prefix_length' => 29,
'state' => 'available'],
['id' => 3, 'host_id' => 1, 'public_ip_pool_id' => 100,
'address' => '203.0.113.3', 'prefix_length' => 29,
'state' => 'available'],
['id' => 4, 'host_id' => 1, 'public_ip_pool_id' => 100,
'address' => '203.0.113.4', 'prefix_length' => 29,
'state' => 'available'],
['id' => 5, 'host_id' => 1, 'public_ip_pool_id' => 200,
'address' => '198.51.100.10', 'prefix_length' => 29,
'state' => 'available'],
]);
$candidateRows = collect([
(object) ['id' => 1, 'resource_pool_line_id' => 10],
(object) ['id' => 1, 'resource_pool_line_id' => 20],
]);
$ready = CloudPlacementNetworkService::advisoryAvailableHostLines(
$candidateRows
);
$assert(($ready[1][10] ?? false) && ($ready[1][20] ?? false),
'One physical host lost one of its exact host-line readiness results.');
$assert(CloudPlacementNetworkService::advisoryAvailableHosts(
collect([(object) ['id' => 1, 'resource_pool_line_id' => 20]])
) === [1 => true],
'The exact-pool compatibility view did not preserve host readiness.');
Capsule::table('cloud_host_network_attachments')->where('id', 20)->update([
'verified_agent_revision' => 3,
]);
$ready = CloudPlacementNetworkService::advisoryAvailableHostLines(
$candidateRows
);
$assert(($ready[1][10] ?? false) && !isset($ready[1][20]),
'A stale line verification affected another line or remained ready.');
Capsule::table('cloud_host_network_attachments')->where('id', 20)->update([
'verified_agent_revision' => 4,
]);
Capsule::table('cloud_host_network_attachments')->where('id', 10)->update([
'last_verified_at' => 0,
]);
$ready = CloudPlacementNetworkService::advisoryAvailableHostLines(
$candidateRows
);
$assert(!isset($ready[1][10]) && ($ready[1][20] ?? false),
'Missing verification time remained ready or poisoned another line.');
Capsule::table('cloud_host_network_attachments')->where('id', 10)->update([
'last_verified_at' => time(),
]);
$selection = Capsule::connection('default')->transaction(static function () {
$agent = CloudHostAgent::find(1);
$candidates = CloudPlacementNetworkService::lockedCandidates(
1,
10,
$agent
);
return CloudPlacementNetworkService::lockAvailableSelection(
1,
$candidates
);
});
$assert($selection !== null
&& (int) $selection['attachment']->id === 10
&& (int) $selection['public_pool']->id === 100
&& (string) $selection['public_ip']->address === '203.0.113.4',
'Payment-time selection used the retired default or admitted a gateway, DNS, or reserved address.');
Capsule::table('cloud_host_agents')->where('host_id', 1)->update([
'network_selectors_json' => '["bridge:line-a"]',
]);
$ready = CloudPlacementNetworkService::advisoryAvailableHostLines(
$candidateRows
);
$assert(($ready[1][10] ?? false) && !isset($ready[1][20]),
'A selector absent from current Agent test evidence remained ready.');
echo "Placement network exact host-line contract passed.\n";