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

316 lines
11 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\CloudInstanceMigration;
use App\Services\Cloud\CloudInstanceMigrationService;
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);
}
};
$rejects = static function (callable $callback, string $message): void {
try {
$callback();
} catch (Throwable) {
return;
}
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');
});
$schema->create('cloud_resource_pools', static function (
Blueprint $table
): void {
$table->increments('id');
$table->integer('line_id');
});
$schema->create('cloud_instances', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('user_id');
$table->integer('resource_pool_id');
});
$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');
});
$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->string('allocation_cursor')->nullable();
$table->integer('max_allocations');
});
$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->binary('address_binary');
$table->integer('prefix_length');
$table->string('state');
$table->integer('reservation_id')->nullable();
$table->integer('reservation_ip_claim_id')->nullable();
$table->integer('instance_id')->nullable();
$table->string('operation_id')->nullable();
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_resource_reservations', static function (
Blueprint $table
): void {
$table->increments('id');
$table->string('operation_id');
$table->integer('order_id')->nullable();
$table->integer('backup_restore_id')->nullable();
$table->string('backup_restore_attempt_key')->nullable();
$table->integer('migration_source_debt_id')->nullable();
$table->integer('offer_id')->nullable();
$table->integer('resource_pool_id');
$table->integer('host_id');
$table->integer('user_id');
$table->integer('instance_id')->nullable();
$table->integer('network_attachment_id')->nullable();
$table->integer('public_ip_pool_id')->nullable();
$table->integer('public_ip_id')->nullable();
$table->string('state');
$table->integer('expires_at')->nullable();
$table->integer('released_at')->nullable();
$table->string('release_reason')->default('');
$table->integer('updated_at');
});
$schema->create('cloud_resource_reservation_ips', static function (
Blueprint $table
): void {
$table->increments('id');
$table->integer('reservation_id');
$table->integer('address_family');
$table->integer('claim_generation');
$table->integer('active_claim')->nullable();
$table->integer('network_attachment_id');
$table->integer('public_ip_pool_id');
$table->integer('public_ip_id');
$table->string('operation_id');
$table->string('state');
$table->integer('released_at')->nullable();
$table->string('release_reason')->default('');
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_backup_restores', static function (
Blueprint $table
): void {
$table->increments('id');
$table->integer('target_host_id');
$table->integer('reservation_id');
$table->string('operation_id');
$table->string('state');
$table->string('error_code')->default('');
$table->integer('updated_at');
});
$now = time();
$operationId = str_repeat('a', 32);
Capsule::table('cloud_hosts')->insert(['id' => 2]);
Capsule::table('cloud_resource_pools')->insert(['id' => 11, 'line_id' => 7]);
Capsule::table('cloud_instances')->insert([
'id' => 31,
'user_id' => 9,
'resource_pool_id' => 11,
]);
Capsule::table('cloud_host_network_attachments')->insert([
'id' => 21,
'host_id' => 2,
'line_id' => 7,
'network_selector' => 'bridge:br0',
]);
Capsule::table('cloud_public_ip_pools')->insert([
[
'id' => 41,
'pool_key' => str_repeat('4', 32),
'host_id' => 2,
'network_attachment_id' => 21,
'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"]',
'reserved_ips_json' => '[]',
'allocation_start' => null,
'allocation_end' => null,
'allocation_cursor' => null,
'max_allocations' => 0,
],
[
'id' => 42,
'pool_key' => str_repeat('6', 32),
'host_id' => 2,
'network_attachment_id' => 21,
'address_family' => 6,
'cidr' => '2404:c140:156:3a::/64',
'gateway' => '2404:c140:156:3a::1',
'gateway_on_link' => 0,
'guest_prefix_length' => 64,
'dns_servers_json' => '["2606:4700:4700::1111"]',
'reserved_ips_json' => '[]',
'allocation_start' => '2404:c140:156:3a::10',
'allocation_end' => '2404:c140:156:3a::ff',
'allocation_cursor' => null,
'max_allocations' => 8,
],
]);
Capsule::table('cloud_resource_reservations')->insert([
'id' => 51,
'operation_id' => $operationId,
'order_id' => null,
'backup_restore_id' => 61,
'backup_restore_attempt_key' => null,
'migration_source_debt_id' => null,
'offer_id' => null,
'resource_pool_id' => 11,
'host_id' => 2,
'user_id' => 9,
'instance_id' => null,
'network_attachment_id' => null,
'public_ip_pool_id' => null,
'public_ip_id' => null,
'state' => 'uncertain',
'expires_at' => null,
'released_at' => null,
'release_reason' => '',
'updated_at' => $now,
]);
Capsule::table('cloud_backup_restores')->insert([
'id' => 61,
'target_host_id' => 2,
'reservation_id' => 51,
'operation_id' => $operationId,
'state' => 'reserved',
'error_code' => '',
'updated_at' => $now,
]);
$addresses = [
4 => [41, 71, '8.8.8.20', 24],
6 => [42, 72, '2404:c140:156:3a::20', 64],
];
foreach ($addresses as $family => [$poolId, $ipId, $address, $prefix]) {
$claimId = $family === 4 ? 81 : 82;
Capsule::table('cloud_public_ips')->insert([
'id' => $ipId,
'host_id' => 2,
'public_ip_pool_id' => $poolId,
'address_family' => $family,
'address' => $address,
'address_binary' => inet_pton($address),
'prefix_length' => $prefix,
'state' => 'uncertain',
'reservation_id' => 51,
'reservation_ip_claim_id' => $claimId,
'instance_id' => null,
'operation_id' => $operationId,
'created_at' => $now,
'updated_at' => $now,
]);
Capsule::table('cloud_resource_reservation_ips')->insert([
'id' => $claimId,
'reservation_id' => 51,
'address_family' => $family,
'claim_generation' => 1,
'active_claim' => 1,
'network_attachment_id' => 21,
'public_ip_pool_id' => $poolId,
'public_ip_id' => $ipId,
'operation_id' => $operationId,
'state' => 'uncertain',
'released_at' => null,
'release_reason' => '',
'created_at' => $now,
'updated_at' => $now,
]);
}
$migration = new CloudInstanceMigration();
$migration->instance_id = 31;
$migration->restore_id = 61;
$migration->target_host_id = 2;
$migration->target_reservation_id = 51;
$release = new ReflectionMethod(
CloudInstanceMigrationService::class,
'releasePreparedTargetLocked'
);
$rejects(static fn () => $release->invoke(
null,
$migration,
'migration_transfer_aborted'
), 'An uncertain target bundle was released without target-absence proof.');
$assert((string) Capsule::table('cloud_resource_reservations')
->where('id', 51)->value('state') === 'uncertain'
&& Capsule::table('cloud_resource_reservation_ips')
->where('reservation_id', 51)->where('state', 'uncertain')
->count() === 2
&& Capsule::table('cloud_public_ips')->where('reservation_id', 51)
->where('state', 'uncertain')->count() === 2,
'Rejecting an uncertain release changed part of the target bundle.');
Capsule::table('cloud_resource_reservations')->where('id', 51)->update([
'state' => 'held',
]);
Capsule::table('cloud_resource_reservation_ips')
->where('reservation_id', 51)->update(['state' => 'reserved']);
Capsule::table('cloud_public_ips')
->where('reservation_id', 51)->update(['state' => 'reserved']);
$release->invoke(null, $migration, 'migration_transfer_aborted');
$reservation = Capsule::table('cloud_resource_reservations')->find(51);
$restore = Capsule::table('cloud_backup_restores')->find(61);
$assert((string) $reservation->state === 'released'
&& (string) $reservation->release_reason
=== 'maintenance_migration_target_not_applied'
&& (string) $restore->state === 'failed'
&& (string) $restore->error_code === 'migration_transfer_aborted'
&& Capsule::table('cloud_resource_reservation_ips')
->where('reservation_id', 51)->whereNull('active_claim')
->where('state', 'released')->count() === 2
&& Capsule::table('cloud_public_ips')->whereIn('id', [71, 72])
->where('state', 'available')->whereNull('reservation_id')
->whereNull('reservation_ip_claim_id')->whereNull('operation_id')
->count() === 2,
'A proven target absence did not release both address families and restore intent.');
$release->invoke(null, $migration, 'migration_transfer_aborted');
$assert(Capsule::table('cloud_resource_reservation_ips')
->where('reservation_id', 51)->count() === 2,
'An idempotent target release created or rewrote address claims.');
echo "Maintenance migration target outcome tests passed.\n";