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

308 lines
12 KiB
PHTML

<?php
namespace App\Services\Cloud {
final class NetworkOptionsEmptyJobQuery
{
public function orderByDesc(string $column): self
{
return $this;
}
public function first(array $columns = ['*']): null
{
return null;
}
}
final class CloudJobService
{
public static function blockingQueryForInstance(
int $instanceId
): NetworkOptionsEmptyJobQuery {
return new NetworkOptionsEmptyJobQuery();
}
}
}
namespace {
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\CloudInstance;
use App\Services\Cloud\CloudNetworkChangeOptionsService;
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('name');
$table->string('region');
$table->integer('enabled');
});
$schema->create('cloud_host_agents', static function (
Blueprint $table
): void {
$table->integer('host_id')->primary();
$table->integer('enabled');
$table->integer('revision');
$table->integer('tested_revision');
$table->string('last_test_status');
$table->text('network_selectors_json');
});
$schema->create('cloud_instances', static function (
Blueprint $table
): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('reservation_id');
$table->integer('resource_pool_id');
$table->string('status');
$table->text('spec_json');
});
$schema->create('cloud_resource_pools', static function (
Blueprint $table
): void {
$table->increments('id');
$table->integer('line_id');
});
$schema->create('cloud_resource_reservations', static function (
Blueprint $table
): void {
$table->increments('id');
$table->integer('resource_pool_id');
$table->integer('host_id');
$table->integer('instance_id');
$table->integer('network_attachment_id');
$table->integer('public_ip_pool_id');
$table->integer('public_ip_id');
$table->string('state');
});
$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');
});
$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();
});
$now = time();
Capsule::table('cloud_hosts')->insert([
'id' => 1, 'name' => 'HKG-A', 'region' => 'Hong Kong',
'enabled' => 1,
]);
Capsule::table('cloud_host_agents')->insert([
'host_id' => 1, 'enabled' => 1, 'revision' => 7,
'tested_revision' => 7, 'last_test_status' => 'healthy',
'network_selectors_json' => json_encode([
'bridge:line-a', 'bridge:line-b', 'bridge:line-x',
'bridge:stale',
], JSON_THROW_ON_ERROR),
]);
Capsule::table('cloud_resource_pools')->insert([
'id' => 10, 'line_id' => 1,
]);
Capsule::table('cloud_host_network_attachments')->insert([
[
'id' => 1, 'host_id' => 1, 'line_id' => 1,
'network_selector' => 'bridge:line-a', 'state' => 'active',
'priority' => 10, 'verification_state' => 'verified',
'verified_agent_revision' => 7, 'last_verified_at' => $now,
],
[
'id' => 2, 'host_id' => 1, 'line_id' => 1,
'network_selector' => 'bridge:line-b', 'state' => 'active',
'priority' => 0, 'verification_state' => 'verified',
'verified_agent_revision' => 7, 'last_verified_at' => $now,
],
[
'id' => 3, 'host_id' => 1, 'line_id' => 2,
'network_selector' => 'bridge:line-x', 'state' => 'active',
'priority' => -10, 'verification_state' => 'verified',
'verified_agent_revision' => 7, 'last_verified_at' => $now,
],
[
'id' => 4, 'host_id' => 1, 'line_id' => 1,
'network_selector' => 'bridge:stale', 'state' => 'active',
'priority' => -20, 'verification_state' => 'verified',
'verified_agent_revision' => 6, 'last_verified_at' => $now,
],
]);
$pool = static function (int $id, int $attachmentId, string $key,
string $cidr, string $gateway): array {
return [
'id' => $id, 'pool_key' => str_repeat($key, 32),
'host_id' => 1, 'network_attachment_id' => $attachmentId,
'cidr' => $cidr, 'gateway' => $gateway,
'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_ip_pools')->insert([
$pool(11, 1, '1', '198.51.100.0/29', '198.51.100.1'),
$pool(12, 2, '2', '198.51.100.8/29', '198.51.100.9'),
$pool(13, 3, '3', '198.51.100.16/29', '198.51.100.17'),
$pool(14, 4, '4', '198.51.100.24/29', '198.51.100.25'),
]);
Capsule::table('cloud_public_ips')->insert([
[
'id' => 21, 'host_id' => 1, 'public_ip_pool_id' => 11,
'address' => '198.51.100.2', 'prefix_length' => 29,
'state' => 'assigned', 'reservation_id' => 20,
'instance_id' => 30, 'operation_id' => str_repeat('a', 32),
],
[
'id' => 22, 'host_id' => 1, 'public_ip_pool_id' => 11,
'address' => '198.51.100.3', 'prefix_length' => 29,
'state' => 'available', 'reservation_id' => null,
'instance_id' => null, 'operation_id' => null,
],
[
'id' => 23, 'host_id' => 1, 'public_ip_pool_id' => 12,
'address' => '198.51.100.10', 'prefix_length' => 29,
'state' => 'available', 'reservation_id' => null,
'instance_id' => null, 'operation_id' => null,
],
[
'id' => 24, 'host_id' => 1, 'public_ip_pool_id' => 13,
'address' => '198.51.100.18', 'prefix_length' => 29,
'state' => 'available', 'reservation_id' => null,
'instance_id' => null, 'operation_id' => null,
],
[
'id' => 25, 'host_id' => 1, 'public_ip_pool_id' => 14,
'address' => '198.51.100.26', 'prefix_length' => 29,
'state' => 'available', 'reservation_id' => null,
'instance_id' => null, 'operation_id' => null,
],
]);
Capsule::table('cloud_resource_reservations')->insert([
'id' => 20, 'resource_pool_id' => 10, 'host_id' => 1,
'instance_id' => 30, 'network_attachment_id' => 1,
'public_ip_pool_id' => 11, 'public_ip_id' => 21,
'state' => 'committed',
]);
Capsule::table('cloud_instances')->insert([
'id' => 30, 'host_id' => 1, 'reservation_id' => 20,
'resource_pool_id' => 10, 'status' => 'active',
'spec_json' => json_encode([
'managed' => true, 'os_source' => 'cloud_template',
'public_ip_cidr' => '198.51.100.2/29',
], JSON_THROW_ON_ERROR),
]);
$instance = CloudInstance::find(30);
$options = CloudNetworkChangeOptionsService::forInstance($instance);
$targets = array_column(
$options['available_ip_options'],
'public_ip_cidr'
);
$assert($targets === ['198.51.100.10/29', '198.51.100.3/29']
&& ($options['available_count'] ?? null) === 2
&& ($options['can_change'] ?? null) === true,
'Same-line verified attachments were not merged in priority order.');
$assert(!in_array('198.51.100.18/29', $targets, true)
&& !in_array('198.51.100.26/29', $targets, true),
'A cross-line or stale-evidence attachment leaked into IP choices.');
Capsule::table('cloud_host_network_attachments')->where('id', 2)->update([
'verified_agent_revision' => 6,
]);
$stale = CloudNetworkChangeOptionsService::forInstance($instance);
$assert(array_column(
$stale['available_ip_options'],
'public_ip_cidr'
) === ['198.51.100.3/29'],
'An attachment verified for an old Agent revision remained selectable.');
Capsule::table('cloud_host_network_attachments')->where('id', 2)->update([
'verified_agent_revision' => 7, 'last_verified_at' => 0,
]);
$expiredTimestamp = CloudNetworkChangeOptionsService::forInstance(
$instance
);
$assert(array_column(
$expiredTimestamp['available_ip_options'],
'public_ip_cidr'
) === ['198.51.100.3/29'],
'An attachment without durable verification time remained selectable.');
Capsule::table('cloud_host_network_attachments')->where('id', 2)->update([
'last_verified_at' => $now,
]);
Capsule::table('cloud_host_network_attachments')->where('id', 1)->update([
'state' => 'draining', 'verified_agent_revision' => 6,
]);
Capsule::table('cloud_public_ip_pools')->where('id', 11)->update([
'state' => 'draining', 'sync_state' => 'failed',
]);
$moveFromDraining = CloudNetworkChangeOptionsService::forInstance(
$instance
);
$assert(array_column(
$moveFromDraining['available_ip_options'],
'public_ip_cidr'
) === ['198.51.100.10/29']
&& ($moveFromDraining['can_change'] ?? null) === true,
'A draining source ledger prevented migration to a valid target.');
Capsule::table('cloud_host_agents')->where('host_id', 1)->update([
'revision' => 8, 'tested_revision' => 8,
]);
$expired = CloudNetworkChangeOptionsService::forInstance($instance);
$assert(($expired['available_ip_options'] ?? null) === []
&& ($expired['can_change'] ?? null) === false,
'Expired Agent evidence remained available for a paid IP change.');
echo "Cloud network-change options behavior passed.\n";
}