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

135 lines
4.7 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\CloudHost;
use App\Models\CloudInstance;
use App\Models\CloudInstanceBackup;
use App\Services\Cloud\CloudBackupService;
use App\Services\Cloud\CloudError;
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_hosts', static function (Blueprint $table): void {
$table->increments('id');
$table->string('name');
$table->string('region');
});
$schema->create('cloud_instances', static function (Blueprint $table): void {
$table->increments('id');
$table->string('name');
$table->string('status');
$table->integer('user_id');
});
$schema->create('cloud_instance_backups',
static function (Blueprint $table): void {
$table->increments('id');
$table->string('backup_key');
$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');
$table->integer('virtual_size_bytes');
$table->string('sha256');
$table->text('spec_json');
$table->text('sealed_storage_runtime');
$table->text('sealed_credential')->nullable();
$table->integer('storage_revision');
$table->integer('active_job_id')->nullable();
$table->string('last_operation_id')->nullable();
$table->string('error_code');
$table->integer('ready_at')->nullable();
$table->integer('created_at');
$table->integer('updated_at');
});
$host = new CloudHost();
$host->name = 'source-host';
$host->region = 'HKG';
$host->save();
$source = new CloudInstance();
$source->name = 'deleted-source';
$source->status = 'deleted';
$source->user_id = 77;
$source->save();
for ($index = 1; $index <= 21; $index++) {
$backup = new CloudInstanceBackup();
$backup->backup_key = str_pad((string) $index, 32, '0', STR_PAD_LEFT);
$backup->source_instance_id = (int) $source->id;
$backup->source_host_id = (int) $host->id;
$backup->user_id = 77;
$backup->source_uuid = '11111111-1111-4111-8111-111111111111';
$backup->name = 'backup-' . $index;
$backup->state = 'failed';
$backup->consistency = 'crash';
$backup->object_key = 'virt/vm-backups/' . $index . '/system.qcow2';
$backup->object_size_bytes = 0;
$backup->virtual_size_bytes = 0;
$backup->sha256 = '';
$backup->spec_json = '{}';
$backup->sealed_storage_runtime = 'sealed';
$backup->sealed_credential = null;
$backup->storage_revision = 1;
$backup->active_job_id = null;
$backup->last_operation_id = null;
$backup->error_code = 'backup_failed';
$backup->ready_at = null;
$backup->created_at = $backup->updated_at = 1700000000 + $index;
$backup->save();
}
$readOnly = [
'user_id' => 9,
'is_admin' => true,
'is_super_admin' => false,
'can_manage' => false,
];
$first = CloudBackupService::library($readOnly,
['page' => '1', 'per_page' => '20']);
$assert(count($first['items']) === 20
&& $first['pagination']['total'] === 21
&& $first['pagination']['total_pages'] === 2
&& $first['can_manage'] === false,
'Global backup library pagination is not bounded or read-only.');
$item = $first['items'][0];
$assert(($item['source']['deleted'] ?? null) === true
&& ($item['source']['status'] ?? null) === 'deleted'
&& ($item['source']['name'] ?? null) === 'deleted-source'
&& ($item['can_delete'] ?? null) === false,
'A deleted source VM made its retained backup disappear or become writable.');
$second = CloudBackupService::library($readOnly,
['page' => '2', 'per_page' => '20']);
$assert(count($second['items']) === 1
&& $second['pagination']['from'] === 21
&& $second['pagination']['to'] === 21,
'Backup library second-page bounds are incorrect.');
try {
CloudBackupService::library([
'user_id' => 77, 'is_admin' => false,
], []);
throw new RuntimeException('Non-administrator read was accepted.');
} catch (CloudError $error) {
$assert($error->httpStatus === 403,
'Non-administrator backup-library read returned the wrong status.');
}
echo "Portable backup global library pagination and deleted-source behavior passed.\n";