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

173 lines
6.2 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\CloudInstance;
use App\Models\CloudInstanceMigration;
use App\Models\CloudInstanceSnapshot;
use App\Models\CloudMigrationBundleArtifact;
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudMigrationBundleService;
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->integer('user_id');
});
$schema->create('cloud_instance_snapshots',
static function (Blueprint $table): void {
$table->increments('id');
$table->string('snapshot_key')->unique();
$table->integer('instance_id');
$table->integer('user_id');
$table->integer('host_id');
$table->string('name');
$table->string('state');
$table->string('consistency');
$table->integer('size_bytes')->nullable();
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_instance_migrations',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('instance_id');
$table->integer('source_host_id');
$table->integer('target_host_id');
});
$schema->create('cloud_migration_bundle_artifacts',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('migration_id');
$table->integer('source_snapshot_id');
$table->string('snapshot_key');
$table->string('name');
$table->string('consistency');
$table->string('object_key');
$table->integer('object_size_bytes')->default(0);
$table->integer('virtual_size_bytes')->default(0);
$table->string('sha256')->default('');
$table->string('state');
$table->integer('snapshot_created_at');
$table->integer('created_at');
$table->integer('updated_at');
});
$now = time();
$instance = new CloudInstance();
$instance->id = 10;
$instance->host_id = 1;
$instance->user_id = 90;
$instance->save();
$snapshots = [];
foreach ([
[11, str_repeat('a', 32), 'before-upgrade', 'filesystem', 20 * 1073741824],
[12, str_repeat('b', 32), 'after-upgrade', 'crash', 30 * 1073741824],
] as [$id, $key, $name, $consistency, $bytes]) {
$snapshot = new CloudInstanceSnapshot();
$snapshot->id = $id;
$snapshot->snapshot_key = $key;
$snapshot->instance_id = 10;
$snapshot->user_id = 90;
$snapshot->host_id = 1;
$snapshot->name = $name;
$snapshot->state = 'ready';
$snapshot->consistency = $consistency;
$snapshot->size_bytes = $bytes;
$snapshot->created_at = $now - $id;
$snapshot->updated_at = $now;
$snapshot->save();
$snapshots[] = $snapshot;
}
$frozen = CloudMigrationBundleService::freezeSnapshotsLocked($instance);
$assert(count($frozen) === 2
&& CloudMigrationBundleService::additionalDiskGib($frozen) === 50,
'MigrationBundle did not freeze and reserve every ready snapshot.');
$migration = new CloudInstanceMigration();
$migration->id = 20;
$migration->instance_id = 10;
$migration->source_host_id = 1;
$migration->target_host_id = 2;
$migration->save();
$prepared = [];
foreach ($snapshots as $snapshot) {
$prepared[] = [
'snapshot' => $snapshot,
'object_key' => 'virt/vm-migrations/bundle/snapshots/'
. $snapshot->snapshot_key . '.qcow2',
'plan' => ['upload_id' => 'upload-' . $snapshot->id],
];
}
CloudMigrationBundleService::persistArtifactsLocked($migration, $prepared);
$receipts = [];
foreach ($snapshots as $index => $snapshot) {
$receipts[] = [
'snapshot_key' => (string) $snapshot->snapshot_key,
'name' => (string) $snapshot->name,
'consistency' => (string) $snapshot->consistency,
'size_bytes' => 1000 + $index,
'virtual_size_bytes' => (int) $snapshot->size_bytes,
'sha256' => str_repeat((string) ($index + 1), 64),
'created_at' => (int) $snapshot->created_at - 30 - $index,
];
}
CloudMigrationBundleService::finishExportLocked($migration, [
'migration_bundle_version' => CloudMigrationBundleService::VERSION,
'snapshot_artifacts' => $receipts,
]);
$assert(CloudMigrationBundleArtifact::where('migration_id', 20)
->where('state', 'ready')->count() === 2
&& (int) CloudMigrationBundleArtifact::where('migration_id', 20)
->orderBy('id')->first()->snapshot_created_at
=== (int) $receipts[0]['created_at'],
'MigrationBundle did not seal every exported snapshot receipt.');
try {
CloudMigrationBundleService::finishRestoreLocked($migration, [
'status' => 'running',
'staged_powered_off' => false,
'migration_bundle_version' => CloudMigrationBundleService::VERSION,
'snapshot_artifacts' => $receipts,
]);
throw new RuntimeException(
'MigrationBundle accepted a target before powered-off staging was proven.'
);
} catch (CloudError $error) {
$assert($error->httpStatus === 502,
'MigrationBundle rejected unsafe staging with the wrong status.');
}
$assert(CloudInstanceSnapshot::where('host_id', 1)->count() === 2,
'Snapshot ownership moved before powered-off target staging was proven.');
CloudMigrationBundleService::finishRestoreLocked($migration, [
'status' => 'shutoff',
'staged_powered_off' => true,
'migration_bundle_version' => CloudMigrationBundleService::VERSION,
'snapshot_artifacts' => $receipts,
]);
$assert(CloudInstanceSnapshot::where('host_id', 2)->count() === 2
&& CloudMigrationBundleArtifact::where('migration_id', 20)
->where('state', 'restored')->count() === 2,
'MigrationBundle did not atomically rebind the complete snapshot catalog.');
echo "Maintenance MigrationBundle snapshot transport behavior passed.\n";