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

180 lines
6.1 KiB
PHTML

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\CloudInstance;
use App\Models\CloudInstanceMigration;
use App\Models\CloudInstanceSnapshot;
use App\Models\CloudPeerMigrationArtifact;
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudPeerMigrationArtifactService;
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');
$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('vpc_port_id')->nullable();
$table->integer('source_host_id');
$table->integer('target_host_id');
});
$schema->create('cloud_peer_migration_artifacts', static function (
Blueprint $table
): void {
$table->increments('id');
$table->integer('migration_id');
$table->string('artifact_id');
$table->string('role');
$table->integer('source_snapshot_id')->nullable();
$table->string('snapshot_key')->nullable();
$table->string('name')->default('');
$table->string('consistency');
$table->integer('object_size_bytes')->default(0);
$table->integer('virtual_size_bytes');
$table->string('sha256')->default('');
$table->string('state');
$table->integer('snapshot_created_at')->nullable();
$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 = CloudPeerMigrationArtifactService::freezeSnapshotsLocked($instance);
$assert(count($frozen) === 2
&& CloudPeerMigrationArtifactService::additionalDiskGib($frozen) === 50,
'PeerMigration 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();
CloudPeerMigrationArtifactService::persistLocked(
$migration,
$snapshots,
40 * 1073741824
);
$rows = CloudPeerMigrationArtifact::where('migration_id', 20)
->orderBy('id')->get();
$assert($rows->count() === 3
&& $rows->where('role', 'root')->count() === 1
&& $rows->where('role', 'snapshot')->count() === 2,
'PeerMigration did not persist one root and the complete snapshot catalog.');
$receipts = [];
foreach ($rows as $index => $row) {
$receipts[] = [
'id' => (string) $row->artifact_id,
'role' => (string) $row->role,
'snapshot_key' => (string) ($row->snapshot_key ?? ''),
'name' => (string) $row->name,
'consistency' => (string) $row->consistency,
'size_bytes' => 1000 + $index,
'virtual_size_bytes' => (int) $row->virtual_size_bytes,
'sha256' => str_repeat(dechex($index + 1), 64),
'created_at' => $row->source_snapshot_id === null
? $now : (int) CloudInstanceSnapshot::findOrFail(
(int) $row->source_snapshot_id
)->created_at,
];
}
CloudPeerMigrationArtifactService::finishPrepareLocked($migration, [
'source_stopped' => true,
'source_autostart_disabled' => true,
'artifacts' => $receipts,
]);
$assert(CloudPeerMigrationArtifact::where('migration_id', 20)
->where('state', 'ready')->count() === 3,
'PeerMigration did not seal the complete path-free source manifest.');
try {
CloudPeerMigrationArtifactService::finishReceiveLocked($migration, [
'status' => 'running',
'staged_powered_off' => false,
'artifacts' => $receipts,
]);
throw new RuntimeException(
'PeerMigration accepted a target before powered-off staging was proven.'
);
} catch (CloudError $error) {
$assert($error->httpStatus === 502,
'PeerMigration 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.');
CloudPeerMigrationArtifactService::finishReceiveLocked($migration, [
'status' => 'shutoff',
'staged_powered_off' => true,
'artifacts' => $receipts,
]);
$assert(CloudInstanceSnapshot::where('host_id', 2)->count() === 2
&& CloudPeerMigrationArtifact::where('migration_id', 20)
->where('state', 'restored')->count() === 3,
'PeerMigration did not atomically rebind the complete snapshot catalog.');
echo "PeerMigration V3 path-free artifact and snapshot behavior passed.\n";