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

145 lines
5.0 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudPanelUsageService;
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->integer('host_id');
$table->string('status');
$table->text('spec_json');
});
$schema->create('cloud_resource_reservations',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('instance_id')->nullable();
$table->integer('cpu_cores');
$table->integer('memory_mib');
$table->integer('disk_gib');
$table->string('state');
});
$schema->create('cloud_instance_snapshots',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('instance_id');
$table->integer('host_id');
$table->string('state');
$table->integer('size_bytes')->nullable();
});
$schema->create('cloud_instance_migrations',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('source_host_id');
$table->string('state');
});
$schema->create('cloud_migration_bundle_artifacts',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('migration_id');
$table->integer('virtual_size_bytes');
$table->string('state');
});
$gib = 1073741824;
$capsule->table('cloud_instances')->insert([
'id' => 10,
'host_id' => 1,
'status' => 'active',
'spec_json' => json_encode([
'vcpu' => 2, 'memory_mib' => 2048, 'disk_gib' => 40,
], JSON_THROW_ON_ERROR),
]);
$capsule->table('cloud_resource_reservations')->insert([
[
'id' => 1, 'host_id' => 1, 'instance_id' => 10,
'cpu_cores' => 2, 'memory_mib' => 2048, 'disk_gib' => 40,
'state' => 'committed',
],
[
'id' => 2, 'host_id' => 2, 'instance_id' => 10,
'cpu_cores' => 2, 'memory_mib' => 2048, 'disk_gib' => 90,
'state' => 'held',
],
]);
$capsule->table('cloud_instance_snapshots')->insert([
[
'id' => 11, 'instance_id' => 10, 'host_id' => 1,
'state' => 'ready', 'size_bytes' => 20 * $gib,
],
[
'id' => 12, 'instance_id' => 10, 'host_id' => 1,
'state' => 'ready', 'size_bytes' => 30 * $gib,
],
]);
$capsule->table('cloud_instance_migrations')->insert([
'id' => 20, 'source_host_id' => 1, 'state' => 'restoring',
]);
$capsule->table('cloud_migration_bundle_artifacts')->insert([
[
'id' => 21, 'migration_id' => 20,
'virtual_size_bytes' => 20 * $gib, 'state' => 'ready',
],
[
'id' => 22, 'migration_id' => 20,
'virtual_size_bytes' => 30 * $gib, 'state' => 'ready',
],
]);
$source = CloudPanelUsageService::forHost(1);
$target = CloudPanelUsageService::forHost(2);
$assert($source['disk_sum'] === 90,
'Source capacity did not include its ready snapshot volumes.');
$assert($target['disk_sum'] === 90,
'Target capacity did not retain the complete MigrationBundle claim.');
// After target staging, the logical snapshot catalog moves to the target and
// the temporary target reservation returns to the root-disk size. The source
// copies still exist until their cleanup has been proven, so the source
// reservation temporarily retains the transported snapshot capacity.
$capsule->table('cloud_instance_snapshots')->update(['host_id' => 2]);
$capsule->table('cloud_migration_bundle_artifacts')
->update(['state' => 'restored']);
$capsule->table('cloud_instance_migrations')
->update(['state' => 'cutover']);
$capsule->table('cloud_resource_reservations')->where('id', 2)
->update(['disk_gib' => 40, 'state' => 'committed']);
$capsule->table('cloud_resource_reservations')->where('id', 1)
->update(['disk_gib' => 90]);
$capsule->table('cloud_instances')->where('id', 10)
->update(['host_id' => 2]);
$source = CloudPanelUsageService::forHost(1);
$target = CloudPanelUsageService::forHost(2);
$assert($source['disk_sum'] === 90,
'Source capacity was released before duplicate snapshot cleanup.');
$assert($target['disk_sum'] === 90,
'Target snapshot volumes were double-counted or omitted after cutover.');
$capsule->table('cloud_instance_migrations')
->update(['state' => 'completed']);
$capsule->table('cloud_resource_reservations')->where('id', 1)
->update(['state' => 'released']);
$source = CloudPanelUsageService::forHost(1);
$assert($source['disk_sum'] === 0,
'Source snapshot capacity remained reserved after proven cleanup.');
echo "Maintenance MigrationBundle capacity accounting passed.\n";