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.
142 lines
5.4 KiB
PHTML
142 lines
5.4 KiB
PHTML
|
2 weeks ago
|
<?php
|
||
|
|
|
||
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
||
|
|
|
||
|
|
use App\Models\CloudInstanceBackup;
|
||
|
|
use App\Models\CloudJob;
|
||
|
|
use App\Services\Cloud\CloudBackupService;
|
||
|
|
use App\Services\Cloud\CloudError;
|
||
|
|
use App\Services\Cloud\CloudPortableRestoreCompatibility;
|
||
|
|
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_instance_backups', static function (Blueprint $table): void {
|
||
|
|
$table->increments('id');
|
||
|
|
$table->string('backup_key')->unique();
|
||
|
|
$table->integer('source_instance_id');
|
||
|
|
$table->integer('source_host_id');
|
||
|
|
$table->integer('user_id');
|
||
|
|
$table->string('source_uuid');
|
||
|
|
$table->string('name');
|
||
|
|
$table->string('state');
|
||
|
|
$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('object_etag')->default('');
|
||
|
|
$table->string('object_version_id')->default('');
|
||
|
|
$table->text('source_portable_restore_contract_json')->default('{}');
|
||
|
|
$table->string('source_portable_restore_contract_sha256', 64)->default('');
|
||
|
|
$table->text('spec_json');
|
||
|
|
$table->text('sealed_storage_runtime');
|
||
|
|
$table->text('sealed_credential')->nullable();
|
||
|
|
$table->integer('storage_revision')->default(1);
|
||
|
|
$table->integer('active_job_id')->nullable();
|
||
|
|
$table->string('last_operation_id')->nullable();
|
||
|
|
$table->string('error_code')->default('');
|
||
|
|
$table->integer('ready_at')->nullable();
|
||
|
|
$table->integer('created_at');
|
||
|
|
$table->integer('updated_at');
|
||
|
|
});
|
||
|
|
$schema->create('cloud_jobs', static function (Blueprint $table): void {
|
||
|
|
$table->increments('id');
|
||
|
|
$table->string('kind');
|
||
|
|
$table->integer('host_id');
|
||
|
|
$table->integer('instance_id')->nullable();
|
||
|
|
$table->integer('backup_id')->nullable();
|
||
|
|
$table->integer('user_id')->nullable();
|
||
|
|
$table->string('operation_id')->unique();
|
||
|
|
});
|
||
|
|
|
||
|
|
$payload = [
|
||
|
|
'contract' => 1, 'architecture' => 'x86_64', 'machine' => 'q35',
|
||
|
|
'firmware' => 'uefi', 'secure_boot' => false, 'disk_format' => 'qcow2',
|
||
|
|
'system_disk_only' => true, 'public_network_contract' => 1,
|
||
|
|
'vpc_inherited' => false,
|
||
|
|
];
|
||
|
|
$contract = CloudPortableRestoreCompatibility::fromPayload($payload);
|
||
|
|
$now = time();
|
||
|
|
$create = static function (int $id, string $key, string $operation) use (
|
||
|
|
$capsule, $now
|
||
|
|
): CloudJob {
|
||
|
|
$capsule->table('cloud_instance_backups')->insert([
|
||
|
|
'id' => $id, 'backup_key' => $key,
|
||
|
|
'source_instance_id' => 100 + $id, 'source_host_id' => 3,
|
||
|
|
'user_id' => 9,
|
||
|
|
'source_uuid' => sprintf('00000000-0000-4000-8000-%012d', $id),
|
||
|
|
'name' => 'portable-' . $id, 'state' => 'creating',
|
||
|
|
'consistency' => 'crash',
|
||
|
|
'object_key' => 'vm-backups/' . $key . '/system.qcow2',
|
||
|
|
'spec_json' => '{}', 'sealed_storage_runtime' => 'sealed',
|
||
|
|
'active_job_id' => $id, 'last_operation_id' => $operation,
|
||
|
|
'created_at' => $now, 'updated_at' => $now,
|
||
|
|
]);
|
||
|
|
$capsule->table('cloud_jobs')->insert([
|
||
|
|
'id' => $id, 'kind' => 'backup_create', 'host_id' => 3,
|
||
|
|
'instance_id' => 100 + $id, 'backup_id' => $id,
|
||
|
|
'operation_id' => $operation,
|
||
|
|
]);
|
||
|
|
return CloudJob::findOrFail($id);
|
||
|
|
};
|
||
|
|
$result = static function (string $key) use ($payload): array {
|
||
|
|
return [
|
||
|
|
'backup_key' => $key, 'size_bytes' => 1024,
|
||
|
|
'virtual_size_bytes' => 20 * 1073741824,
|
||
|
|
'sha256' => str_repeat('a', 64), 'object_etag' => 'etag',
|
||
|
|
'object_version_id' => '', 'consistency' => 'crash',
|
||
|
|
'cleanup_pending' => false, 'source_contract' => $payload,
|
||
|
|
];
|
||
|
|
};
|
||
|
|
$finalizerPayload = static function (string $key) use ($contract): array {
|
||
|
|
return [
|
||
|
|
'owner_id' => 9, 'backup_key' => $key,
|
||
|
|
'maximum_size_bytes' => 21 * 1073741824,
|
||
|
|
'source_portable_restore_contract_sha256' => $contract->sha256(),
|
||
|
|
];
|
||
|
|
};
|
||
|
|
|
||
|
|
$key = str_repeat('1', 32);
|
||
|
|
CloudBackupService::finishJobLocked(
|
||
|
|
$create(1, $key, str_repeat('2', 32)),
|
||
|
|
'succeeded', $result($key), $finalizerPayload($key)
|
||
|
|
);
|
||
|
|
$ready = CloudInstanceBackup::findOrFail(1);
|
||
|
|
$assert((string) $ready->state === 'ready'
|
||
|
|
&& (string) $ready->source_portable_restore_contract_json
|
||
|
|
=== $contract->json()
|
||
|
|
&& (string) $ready->source_portable_restore_contract_sha256
|
||
|
|
=== $contract->sha256()
|
||
|
|
&& $ready->sourcePortableRestoreCompatibility() !== null,
|
||
|
|
'Valid Agent source contract was not frozen with the ready backup.');
|
||
|
|
|
||
|
|
$missingKey = str_repeat('3', 32);
|
||
|
|
$missing = $result($missingKey);
|
||
|
|
unset($missing['source_contract']);
|
||
|
|
try {
|
||
|
|
CloudBackupService::finishJobLocked(
|
||
|
|
$create(2, $missingKey, str_repeat('4', 32)),
|
||
|
|
'succeeded', $missing, $finalizerPayload($missingKey)
|
||
|
|
);
|
||
|
|
throw new RuntimeException('Missing source contract was accepted.');
|
||
|
|
} catch (CloudError $error) {
|
||
|
|
$assert($error->httpStatus === 502,
|
||
|
|
'Missing source contract returned the wrong status.');
|
||
|
|
}
|
||
|
|
$assert((string) CloudInstanceBackup::findOrFail(2)->state === 'creating',
|
||
|
|
'Missing source contract promoted a backup to ready.');
|
||
|
|
|
||
|
|
echo "Portable backup source contract finalizer passed.\n";
|