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.
383 lines
16 KiB
PHP
383 lines
16 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\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_instance_migrations',
|
|
static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->string('migration_key');
|
|
$table->integer('instance_id');
|
|
$table->integer('backup_id');
|
|
$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('backup_job_id');
|
|
$table->integer('restore_job_id');
|
|
$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->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, 'backup_id' => 41, '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,
|
|
'backup_job_id' => 91, 'restore_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',
|
|
]);
|
|
$finalize = new ReflectionMethod(
|
|
CloudInstanceMigrationService::class,
|
|
'finalizePlacementLocked'
|
|
);
|
|
$finalize->invoke(null, 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, 'backup_id' => 42, '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,
|
|
'backup_job_id' => 191, 'restore_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,
|
|
]);
|
|
$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']
|
|
);
|
|
$rolledBack = CloudInstanceMigration::findOrFail(2);
|
|
$assert((string) $rolledBack->state === 'rolled_back'
|
|
&& (string) $rolledBack->error_code === 'migration_restore_failed'
|
|
&& $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_instance_migrations')->insert([
|
|
'id' => 3, 'migration_key' => str_repeat('1', 32),
|
|
'instance_id' => 13, 'backup_id' => 43, '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,
|
|
'backup_job_id' => 291, 'restore_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']
|
|
);
|
|
$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.');
|
|
|
|
echo "Maintenance migration logical-service and placement finalizer contracts passed.\n";
|