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

674 lines
29 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\CloudInstance;
use App\Models\CloudInstanceMigration;
use App\Models\CloudJob;
use App\Services\Cloud\CloudInstanceMigrationSettlement;
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);
}
};
$capsule = new Capsule();
$capsule->addConnection([
'driver' => 'sqlite', 'database' => ':memory:',
], 'default');
$capsule->setAsGlobal();
$capsule->bootEloquent();
$schema = $capsule->schema('default');
$schema->create('cloud_instances', static function (Blueprint $table): void {
$table->increments('id');
$table->string('uuid');
$table->integer('host_id');
$table->integer('user_id');
$table->integer('order_id');
$table->integer('plan_id');
$table->integer('offer_id');
$table->integer('resource_pool_id');
$table->integer('reservation_id');
$table->integer('current_period_id');
$table->integer('expires_at');
$table->decimal('renewal_price', 12, 2);
$table->integer('renewal_days');
$table->integer('renewal_traffic_bytes');
$table->integer('traffic_quota_bytes');
$table->integer('lifetime_download_bytes');
$table->integer('lifetime_upload_bytes');
$table->string('name');
$table->string('status');
$table->string('service_status');
$table->string('network_status');
$table->text('spec_json');
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_orders', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('user_id');
$table->integer('plan_id');
$table->integer('offer_id');
$table->integer('resource_pool_id');
$table->integer('host_id');
$table->integer('reservation_id');
$table->decimal('amount', 12, 2);
$table->integer('instance_id');
$table->string('status');
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_resource_reservations',
static function (Blueprint $table): void {
$table->increments('id');
$table->string('reservation_key');
$table->string('operation_id');
$table->integer('order_id')->nullable();
$table->integer('backup_restore_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->integer('cpu_cores');
$table->integer('memory_mib');
$table->integer('disk_gib');
$table->string('state');
$table->integer('expires_at')->nullable();
$table->integer('released_at')->nullable();
$table->string('release_reason');
$table->integer('created_at');
$table->integer('updated_at');
});
$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();
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_hosts', static function (Blueprint $table): void {
$table->increments('id');
});
$schema->create('cloud_host_network_attachments',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->string('network_selector');
});
$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->string('cidr');
$table->string('gateway');
$table->text('dns_servers_json');
});
$schema->create('cloud_backup_restores', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('target_host_id')->nullable();
$table->string('operation_id')->nullable();
$table->integer('reservation_id')->nullable();
$table->integer('job_id')->nullable();
$table->string('state');
$table->string('error_code')->default('');
$table->integer('updated_at')->nullable();
});
$schema->create('cloud_instance_migrations',
static function (Blueprint $table): void {
$table->increments('id');
$table->string('migration_key');
$table->integer('instance_id');
$table->integer('vpc_port_id')->nullable();
$table->integer('restore_id');
$table->integer('source_host_id');
$table->string('source_uuid');
$table->integer('source_reservation_id');
$table->integer('target_host_id');
$table->string('target_uuid');
$table->integer('target_reservation_id');
$table->integer('source_prepare_job_id')->nullable();
$table->integer('target_receive_job_id')->nullable();
$table->integer('firewall_job_id')->nullable();
$table->integer('activate_job_id')->nullable();
$table->integer('rollback_job_id')->nullable();
$table->integer('cleanup_job_id')->nullable();
$table->string('cleanup_mode');
$table->string('state');
$table->string('recovery_action')->default('');
$table->integer('retry_generation')->default(0);
$table->string('source_endpoint')->default('["https://192.0.2.10:9443"]');
$table->string('peer_trust_id')->default('');
$table->string('manifest_digest')->default('');
$table->boolean('source_was_running')->nullable();
$table->boolean('source_autostart_enabled')->nullable();
$table->string('source_service_status');
$table->string('source_network_status');
$table->string('error_code');
$table->integer('cutover_at')->nullable();
$table->integer('completed_at')->nullable();
$table->integer('created_at');
$table->integer('updated_at');
});
$now = time();
$sourceUuid = '11111111-1111-4111-8111-111111111111';
$targetUuid = '22222222-2222-4222-8222-222222222222';
$capsule->table('cloud_instances')->insert([
'id' => 11, 'uuid' => $targetUuid, 'host_id' => 2, 'user_id' => 9,
'order_id' => 21, 'plan_id' => 31, 'offer_id' => 41,
'resource_pool_id' => 102, 'reservation_id' => 62,
'current_period_id' => 71, 'expires_at' => $now + 86400,
'renewal_price' => '19.95', 'renewal_days' => 30,
'renewal_traffic_bytes' => 2_000_000,
'traffic_quota_bytes' => 1_500_000,
'lifetime_download_bytes' => 800_000,
'lifetime_upload_bytes' => 300_000,
'name' => 'same-service', 'status' => 'active',
'service_status' => 'active', 'network_status' => 'active',
'spec_json' => '{}', 'created_at' => $now - 1000,
'updated_at' => $now,
]);
$capsule->table('cloud_orders')->insert([
'id' => 21, 'user_id' => 9, 'plan_id' => 31, 'offer_id' => 41,
'resource_pool_id' => 101, 'host_id' => 1, 'reservation_id' => 61,
'amount' => '12.34', 'instance_id' => 11, 'status' => 'active',
'created_at' => $now - 1000, 'updated_at' => $now,
]);
$capsule->table('cloud_resource_reservations')->insert([
[
'id' => 61, 'reservation_key' => str_repeat('a', 32),
'operation_id' => str_repeat('b', 32), 'order_id' => 21,
'backup_restore_id' => null, 'offer_id' => 41,
'resource_pool_id' => 101, 'host_id' => 1, 'user_id' => 9,
'instance_id' => 11, 'network_attachment_id' => 81,
'public_ip_pool_id' => 91, 'public_ip_id' => 101,
'cpu_cores' => 2, 'memory_mib' => 2048, 'disk_gib' => 90,
'state' => 'committed', 'expires_at' => null,
'released_at' => null, 'release_reason' => '',
'created_at' => $now - 1000, 'updated_at' => $now,
],
[
'id' => 62, 'reservation_key' => str_repeat('c', 32),
'operation_id' => str_repeat('d', 32), 'order_id' => null,
'backup_restore_id' => 51, 'offer_id' => null,
'resource_pool_id' => 102, 'host_id' => 2, 'user_id' => 9,
'instance_id' => 11, 'network_attachment_id' => 82,
'public_ip_pool_id' => 92, 'public_ip_id' => 102,
'cpu_cores' => 2, 'memory_mib' => 2048, 'disk_gib' => 40,
'state' => 'committed', 'expires_at' => null,
'released_at' => null, 'release_reason' => '',
'created_at' => $now - 100, 'updated_at' => $now,
],
]);
$capsule->table('cloud_public_ips')->insert([
[
'id' => 101, 'host_id' => 1, 'public_ip_pool_id' => 91,
'address' => '198.51.100.10', 'prefix_length' => 24,
'state' => 'assigned', 'reservation_id' => 61,
'instance_id' => 11, 'operation_id' => str_repeat('b', 32),
'created_at' => $now - 1000, 'updated_at' => $now,
],
[
'id' => 102, 'host_id' => 2, 'public_ip_pool_id' => 92,
'address' => '203.0.113.20', 'prefix_length' => 24,
'state' => 'assigned', 'reservation_id' => 62,
'instance_id' => 11, 'operation_id' => str_repeat('d', 32),
'created_at' => $now - 100, 'updated_at' => $now,
],
]);
$capsule->table('cloud_instance_migrations')->insert([
'id' => 1, 'migration_key' => str_repeat('e', 32),
'instance_id' => 11, 'restore_id' => 51,
'source_host_id' => 1, 'source_uuid' => $sourceUuid,
'source_reservation_id' => 61, 'target_host_id' => 2,
'target_uuid' => $targetUuid, 'target_reservation_id' => 62,
'source_prepare_job_id' => 91, 'target_receive_job_id' => 92,
'firewall_job_id' => null, 'activate_job_id' => null,
'rollback_job_id' => null,
'cleanup_job_id' => 93,
'cleanup_mode' => 'delete', 'state' => 'cleanup_running',
'source_was_running' => true, 'source_autostart_enabled' => true,
'source_service_status' => 'active',
'source_network_status' => 'active', 'error_code' => '',
'cutover_at' => $now - 10, 'completed_at' => null,
'created_at' => $now - 200, 'updated_at' => $now,
]);
$before = CloudInstance::findOrFail(11)->only([
'id', 'user_id', 'order_id', 'plan_id', 'offer_id',
'current_period_id', 'expires_at', 'renewal_price', 'renewal_days',
'renewal_traffic_bytes', 'traffic_quota_bytes',
'lifetime_download_bytes', 'lifetime_upload_bytes', 'created_at',
]);
CloudInstanceMigrationSettlement::finalizeSourceDeletionLocked(
CloudInstanceMigration::findOrFail(1)
);
$instance = CloudInstance::findOrFail(11);
$after = $instance->only(array_keys($before));
$source = $capsule->table('cloud_resource_reservations')->find(61);
$target = $capsule->table('cloud_resource_reservations')->find(62);
$sourceIp = $capsule->table('cloud_public_ips')->find(101);
$targetIp = $capsule->table('cloud_public_ips')->find(102);
$order = $capsule->table('cloud_orders')->find(21);
$assert($before === $after,
'Maintenance migration modified identity, billing, expiry, period, or traffic ownership fields.');
$assert((int) $instance->reservation_id === 61
&& (int) $instance->resource_pool_id === 102
&& (int) $source->host_id === 2
&& (int) $source->resource_pool_id === 102
&& (int) $source->public_ip_id === 102
&& (int) $source->network_attachment_id === 82
&& (int) $source->disk_gib === 40
&& (string) $source->state === 'committed',
'The original paid reservation did not take ownership of target placement.');
$assert((string) $sourceIp->state === 'available'
&& $sourceIp->reservation_id === null
&& $sourceIp->instance_id === null
&& (string) $targetIp->state === 'assigned'
&& (int) $targetIp->reservation_id === 61
&& (int) $targetIp->instance_id === 11,
'Maintenance migration did not release the old IP and transfer the new IP atomically.');
$assert((string) $target->state === 'released'
&& $target->instance_id === null
&& $target->public_ip_id === null
&& (string) $target->release_reason
=== 'maintenance_migration_transferred',
'Temporary restore reservation remained billable after placement transfer.');
$assert((int) $order->host_id === 2
&& (int) $order->resource_pool_id === 102
&& (int) $order->reservation_id === 61
&& (int) $order->plan_id === 31
&& (int) $order->offer_id === 41
&& (string) $order->amount === '12.34',
'Order placement changed without preserving its commercial terms.');
$capsule->table('cloud_instances')->insert([
'id' => 12, 'uuid' => $sourceUuid, 'host_id' => 1, 'user_id' => 9,
'order_id' => 22, 'plan_id' => 31, 'offer_id' => 41,
'resource_pool_id' => 101, 'reservation_id' => 63,
'current_period_id' => 72, 'expires_at' => $now + 86400,
'renewal_price' => '19.95', 'renewal_days' => 30,
'renewal_traffic_bytes' => 2_000_000,
'traffic_quota_bytes' => 1_500_000,
'lifetime_download_bytes' => 100, 'lifetime_upload_bytes' => 50,
'name' => 'rollback-service', 'status' => 'active',
'service_status' => 'active', 'network_status' => 'active',
'spec_json' => '{}', 'created_at' => $now - 1000,
'updated_at' => $now,
]);
$capsule->table('cloud_instance_migrations')->insert([
'id' => 2, 'migration_key' => str_repeat('f', 32),
'instance_id' => 12, 'restore_id' => 52,
'source_host_id' => 1, 'source_uuid' => $sourceUuid,
'source_reservation_id' => 63, 'target_host_id' => 2,
'target_uuid' => $targetUuid, 'target_reservation_id' => 64,
'source_prepare_job_id' => 191, 'target_receive_job_id' => 192,
'firewall_job_id' => null, 'activate_job_id' => null,
'rollback_job_id' => 200,
'cleanup_job_id' => null, 'cleanup_mode' => 'delete',
'state' => 'rollback_running', 'source_was_running' => true,
'source_autostart_enabled' => true,
'source_service_status' => 'active',
'source_network_status' => 'active',
'error_code' => 'migration_restore_failed', 'cutover_at' => null,
'completed_at' => null, 'created_at' => $now - 200,
'updated_at' => $now,
]);
$capsule->table('cloud_backup_restores')->insert([
'id' => 52,
'state' => 'failed',
]);
$rollbackJob = new CloudJob();
$rollbackJob->id = 200;
$rollbackJob->instance_id = 12;
$rollbackJob->host_id = 1;
$finishRollback = new ReflectionMethod(
CloudInstanceMigrationService::class,
'finishRollbackLocked'
);
$finishRollback->invoke(
null,
CloudInstanceMigration::findOrFail(2),
$rollbackJob,
'succeeded',
[
'uuid' => $sourceUuid,
'state' => 'running',
'running' => true,
'autostart_enabled' => true,
],
[]
);
$rolledBack = CloudInstanceMigration::findOrFail(2);
$assert((string) $rolledBack->state === 'rolled_back'
&& (string) $rolledBack->error_code === 'migration_transfer_aborted'
&& $rolledBack->completed_at !== null
&& (int) CloudInstance::findOrFail(12)->host_id === 1,
'A failed target restore did not confirm recovery of the original source service.');
$capsule->table('cloud_instances')->insert([
'id' => 13, 'uuid' => $targetUuid, 'host_id' => 2, 'user_id' => 9,
'order_id' => 23, 'plan_id' => 31, 'offer_id' => 41,
'resource_pool_id' => 102, 'reservation_id' => 65,
'current_period_id' => 73, 'expires_at' => $now + 86400,
'renewal_price' => '19.95', 'renewal_days' => 30,
'renewal_traffic_bytes' => 2_000_000,
'traffic_quota_bytes' => 1_500_000,
'lifetime_download_bytes' => 100, 'lifetime_upload_bytes' => 50,
'name' => 'powered-off-service', 'status' => 'active',
'service_status' => 'suspended_expired',
'network_status' => 'blocked_expired',
'spec_json' => '{"guest_hostname":"retained.example"}',
'created_at' => $now - 1000, 'updated_at' => $now,
]);
$capsule->table('cloud_orders')->insert([
'id' => 23, 'user_id' => 9, 'plan_id' => 31, 'offer_id' => 41,
'resource_pool_id' => 101, 'host_id' => 1, 'reservation_id' => 66,
'amount' => '19.95', 'instance_id' => 13, 'status' => 'active',
'created_at' => $now - 1000, 'updated_at' => $now,
]);
$capsule->table('cloud_resource_reservations')->insert([
[
'id' => 66, 'reservation_key' => str_repeat('6', 32),
'operation_id' => str_repeat('7', 32), 'order_id' => 23,
'backup_restore_id' => null, 'offer_id' => 41,
'resource_pool_id' => 101, 'host_id' => 1, 'user_id' => 9,
'instance_id' => 13, 'network_attachment_id' => 81,
'public_ip_pool_id' => 91, 'public_ip_id' => 103,
'cpu_cores' => 2, 'memory_mib' => 2048, 'disk_gib' => 40,
'state' => 'committed', 'expires_at' => null,
'released_at' => null, 'release_reason' => '',
'created_at' => $now - 1000, 'updated_at' => $now,
],
[
'id' => 65, 'reservation_key' => str_repeat('8', 32),
'operation_id' => str_repeat('9', 32), 'order_id' => null,
'backup_restore_id' => 53, 'offer_id' => null,
'resource_pool_id' => 102, 'host_id' => 2, 'user_id' => 9,
'instance_id' => 13, 'network_attachment_id' => 82,
'public_ip_pool_id' => 92, 'public_ip_id' => 104,
'cpu_cores' => 2, 'memory_mib' => 2048, 'disk_gib' => 40,
'state' => 'committed', 'expires_at' => null,
'released_at' => null, 'release_reason' => '',
'created_at' => $now - 100, 'updated_at' => $now,
],
]);
$capsule->table('cloud_public_ips')->insert([
[
'id' => 103, 'host_id' => 1, 'public_ip_pool_id' => 91,
'address' => '198.51.100.30', 'prefix_length' => 24,
'state' => 'assigned', 'reservation_id' => 66,
'instance_id' => 13, 'operation_id' => str_repeat('7', 32),
'created_at' => $now - 1000, 'updated_at' => $now,
],
[
'id' => 104, 'host_id' => 2, 'public_ip_pool_id' => 92,
'address' => '203.0.113.40', 'prefix_length' => 24,
'state' => 'assigned', 'reservation_id' => 65,
'instance_id' => 13, 'operation_id' => str_repeat('9', 32),
'created_at' => $now - 100, 'updated_at' => $now,
],
]);
$capsule->table('cloud_instance_migrations')->insert([
'id' => 3, 'migration_key' => str_repeat('1', 32),
'instance_id' => 13, 'restore_id' => 53,
'source_host_id' => 1, 'source_uuid' => $sourceUuid,
'source_reservation_id' => 66, 'target_host_id' => 2,
'target_uuid' => $targetUuid, 'target_reservation_id' => 65,
'source_prepare_job_id' => 291, 'target_receive_job_id' => 292,
'firewall_job_id' => null, 'activate_job_id' => 300,
'rollback_job_id' => null, 'cleanup_job_id' => null,
'cleanup_mode' => 'retain', 'state' => 'activation_running',
'source_was_running' => false, 'source_autostart_enabled' => true,
'source_service_status' => 'suspended_expired',
'source_network_status' => 'blocked_expired', 'error_code' => '',
'cutover_at' => $now - 10, 'completed_at' => null,
'created_at' => $now - 200, 'updated_at' => $now,
]);
$activationJob = new CloudJob();
$activationJob->id = 300;
$activationJob->instance_id = 13;
$activationJob->host_id = 2;
$activationJob->user_id = 9;
$finishActivation = new ReflectionMethod(
CloudInstanceMigrationService::class,
'finishActivationLocked'
);
$finishActivation->invoke(
null,
CloudInstanceMigration::findOrFail(3),
$activationJob,
'succeeded',
[
'uuid' => $targetUuid,
'state' => 'shutoff',
'running' => false,
'autostart_enabled' => true,
],
[]
);
$poweredOff = CloudInstance::findOrFail(13);
$activated = CloudInstanceMigration::findOrFail(3);
$assert((string) $activated->state === 'source_retained'
&& (string) $poweredOff->service_status === 'suspended_expired'
&& (string) $poweredOff->network_status === 'blocked_expired'
&& str_contains((string) $poweredOff->spec_json,
'retained.example'),
'Maintenance migration did not retain powered-off, disconnected, or hostname state.');
$previousTransferOperation = str_repeat('4', 32);
$capsule->table('cloud_backup_restores')->insert([
'id' => 54, 'operation_id' => $previousTransferOperation,
'reservation_id' => 67, 'job_id' => 401, 'state' => 'reserved',
'error_code' => 'peer_migration_transfer_failed', 'updated_at' => $now,
]);
$capsule->table('cloud_resource_reservations')->insert([
'id' => 67, 'reservation_key' => str_repeat('5', 32),
'operation_id' => $previousTransferOperation, 'order_id' => null,
'backup_restore_id' => 54, 'offer_id' => null,
'resource_pool_id' => 102, 'host_id' => 2, 'user_id' => 9,
'instance_id' => 13, 'network_attachment_id' => 82,
'public_ip_pool_id' => 92, 'public_ip_id' => 105,
'cpu_cores' => 2, 'memory_mib' => 2048, 'disk_gib' => 40,
'state' => 'held', 'expires_at' => $now + 3600,
'released_at' => null, 'release_reason' => '',
'created_at' => $now, 'updated_at' => $now,
]);
$capsule->table('cloud_public_ips')->insert([
'id' => 105, 'host_id' => 2, 'public_ip_pool_id' => 92,
'address' => '203.0.113.50', 'prefix_length' => 24,
'state' => 'reserved', 'reservation_id' => 67,
'instance_id' => null, 'operation_id' => $previousTransferOperation,
'created_at' => $now, 'updated_at' => $now,
]);
$retryMigration = new CloudInstanceMigration();
$retryMigration->restore_id = 54;
$retryMigration->target_reservation_id = 67;
$retryInstance = CloudInstance::findOrFail(13);
$rotateTransfer = new ReflectionMethod(
CloudInstanceMigrationService::class,
'rotateTargetTransferOperationLocked'
);
$rotateTransfer->invoke(null, $retryMigration, $retryInstance);
$rotatedRestore = $capsule->table('cloud_backup_restores')->find(54);
$rotatedReservation = $capsule->table('cloud_resource_reservations')->find(67);
$rotatedIp = $capsule->table('cloud_public_ips')->find(105);
$assert(is_string($rotatedRestore->operation_id)
&& preg_match('/^[a-f0-9]{32}$/', $rotatedRestore->operation_id) === 1
&& $rotatedRestore->operation_id !== $previousTransferOperation
&& $rotatedRestore->operation_id === $rotatedReservation->operation_id
&& $rotatedRestore->operation_id === $rotatedIp->operation_id
&& $rotatedRestore->job_id === null
&& (string) $rotatedRestore->error_code === '',
'A confirmed failed transfer retry did not rotate every target ledger to a fresh operation ID.');
// A prepare-stage abort keeps the target fence for failed/unknown rollback
// results and releases it only after the Agent proves the source was restored.
$prepareAbortUuid = '33333333-3333-4333-8333-333333333333';
$prepareAbortTargetUuid = '44444444-4444-4444-8444-444444444444';
$prepareAbortOperation = str_repeat('a', 32);
$capsule->table('cloud_hosts')->insert(['id' => 2]);
$capsule->table('cloud_host_network_attachments')->insert([
'id' => 83, 'host_id' => 2, 'network_selector' => 'bridge:br0',
]);
$capsule->table('cloud_public_ip_pools')->insert([
'id' => 93, 'host_id' => 2, 'network_attachment_id' => 83,
'pool_key' => str_repeat('b', 32), 'cidr' => '203.0.113.0/24',
'gateway' => '203.0.113.1', 'dns_servers_json' => '["1.1.1.1"]',
]);
$capsule->table('cloud_instances')->insert([
'id' => 14, 'uuid' => $prepareAbortUuid, 'host_id' => 1,
'user_id' => 9, 'order_id' => 24, 'plan_id' => 31, 'offer_id' => 41,
'resource_pool_id' => 101, 'reservation_id' => 68,
'current_period_id' => 74, 'expires_at' => $now + 86400,
'renewal_price' => '19.95', 'renewal_days' => 30,
'renewal_traffic_bytes' => 2_000_000,
'traffic_quota_bytes' => 1_500_000,
'lifetime_download_bytes' => 100, 'lifetime_upload_bytes' => 50,
'name' => 'prepare-abort-service', 'status' => 'active',
'service_status' => 'active', 'network_status' => 'active',
'spec_json' => '{}', 'created_at' => $now - 1000,
'updated_at' => $now,
]);
$capsule->table('cloud_backup_restores')->insert([
'id' => 55, 'target_host_id' => 2,
'operation_id' => $prepareAbortOperation,
'reservation_id' => 69, 'job_id' => null, 'state' => 'reserved',
'error_code' => '', 'updated_at' => $now,
]);
$capsule->table('cloud_resource_reservations')->insert([
'id' => 69, 'reservation_key' => str_repeat('c', 32),
'operation_id' => $prepareAbortOperation, 'order_id' => null,
'backup_restore_id' => 55, 'offer_id' => null,
'resource_pool_id' => 102, 'host_id' => 2, 'user_id' => 9,
'instance_id' => 14, 'network_attachment_id' => 83,
'public_ip_pool_id' => 93, 'public_ip_id' => 107,
'cpu_cores' => 2, 'memory_mib' => 2048, 'disk_gib' => 40,
'state' => 'held', 'expires_at' => null, 'released_at' => null,
'release_reason' => '', 'created_at' => $now, 'updated_at' => $now,
]);
$capsule->table('cloud_public_ips')->insert([
'id' => 107, 'host_id' => 2, 'public_ip_pool_id' => 93,
'address' => '203.0.113.70', 'prefix_length' => 24,
'state' => 'reserved', 'reservation_id' => 69,
'instance_id' => null, 'operation_id' => $prepareAbortOperation,
'created_at' => $now, 'updated_at' => $now,
]);
$capsule->table('cloud_instance_migrations')->insert([
'id' => 4, 'migration_key' => str_repeat('d', 32),
'instance_id' => 14, 'restore_id' => 55,
'source_host_id' => 1, 'source_uuid' => $prepareAbortUuid,
'source_reservation_id' => 68, 'target_host_id' => 2,
'target_uuid' => $prepareAbortTargetUuid, 'target_reservation_id' => 69,
'source_prepare_job_id' => 391, 'target_receive_job_id' => null,
'firewall_job_id' => null, 'activate_job_id' => null,
'rollback_job_id' => 400, 'cleanup_job_id' => null,
'cleanup_mode' => 'delete', 'state' => 'rollback_running',
'source_was_running' => true, 'source_autostart_enabled' => true,
'source_service_status' => 'active',
'source_network_status' => 'active',
'error_code' => 'migration_prepare_aborted', 'cutover_at' => null,
'completed_at' => null, 'created_at' => $now - 200,
'updated_at' => $now,
]);
$prepareAbortRollbackJob = new CloudJob();
$prepareAbortRollbackJob->id = 400;
$prepareAbortRollbackJob->instance_id = 14;
$prepareAbortRollbackJob->host_id = 1;
$finishRollback->invoke(
null,
CloudInstanceMigration::findOrFail(4),
$prepareAbortRollbackJob,
'uncertain',
null,
[]
);
$uncertainAbort = CloudInstanceMigration::findOrFail(4);
$assert((string) $uncertainAbort->state === 'attention_required'
&& (string) $uncertainAbort->recovery_action === 'reconcile_rollback'
&& (string) $capsule->table('cloud_resource_reservations')
->where('id', 69)->value('state') === 'held'
&& (string) $capsule->table('cloud_public_ips')
->where('id', 107)->value('state') === 'reserved',
'An uncertain prepare-abort rollback released its target fence.');
$capsule->table('cloud_instance_migrations')->where('id', 4)->update([
'state' => 'rollback_running', 'recovery_action' => '',
'error_code' => 'migration_prepare_aborted', 'updated_at' => $now,
]);
$finishRollback->invoke(
null,
CloudInstanceMigration::findOrFail(4),
$prepareAbortRollbackJob,
'failed',
null,
[]
);
$failedAbort = CloudInstanceMigration::findOrFail(4);
$assert((string) $failedAbort->state === 'attention_required'
&& (string) $failedAbort->recovery_action === 'retry_rollback'
&& (string) $capsule->table('cloud_resource_reservations')
->where('id', 69)->value('state') === 'held'
&& (string) $capsule->table('cloud_public_ips')
->where('id', 107)->value('state') === 'reserved',
'A failed prepare-abort rollback released its target fence.');
$capsule->table('cloud_instance_migrations')->where('id', 4)->update([
'state' => 'rollback_running', 'recovery_action' => '',
'error_code' => 'migration_prepare_aborted', 'updated_at' => $now,
]);
$finishRollback->invoke(
null,
CloudInstanceMigration::findOrFail(4),
$prepareAbortRollbackJob,
'succeeded',
[
'uuid' => $prepareAbortUuid,
'running' => true,
'autostart_enabled' => true,
],
[]
);
$completedAbort = CloudInstanceMigration::findOrFail(4);
$releasedAbortRestore = $capsule->table('cloud_backup_restores')->find(55);
$releasedAbortReservation = $capsule->table('cloud_resource_reservations')
->find(69);
$releasedAbortIp = $capsule->table('cloud_public_ips')->find(107);
$assert((string) $completedAbort->state === 'rolled_back'
&& (string) $completedAbort->error_code === 'migration_prepare_aborted'
&& $completedAbort->completed_at !== null
&& (string) $releasedAbortRestore->state === 'failed'
&& (string) $releasedAbortRestore->error_code
=== 'migration_prepare_aborted'
&& (string) $releasedAbortReservation->state === 'released'
&& (string) $releasedAbortReservation->release_reason
=== 'backup_restore_not_applied'
&& (string) $releasedAbortIp->state === 'available'
&& $releasedAbortIp->reservation_id === null
&& $releasedAbortIp->instance_id === null
&& $releasedAbortIp->operation_id === null,
'A confirmed prepare-abort rollback did not release the target reservation and IP exactly once.');
echo "Maintenance migration logical-service and placement finalizer contracts passed.\n";