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.
177 lines
6.9 KiB
PHP
177 lines
6.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services {
|
|
final class Config
|
|
{
|
|
public static string $key = '';
|
|
|
|
public static function get($name, $default = null)
|
|
{
|
|
return $name === 'cloud_data_key' ? self::$key : $default;
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace {
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Models\CloudTemplateImportIntent;
|
|
use App\Services\Cloud\CloudError;
|
|
use App\Services\Cloud\CloudTemplateImportStateMachine;
|
|
use App\Services\Cloud\CloudTemplateImportWorkflow;
|
|
use App\Services\Config;
|
|
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);
|
|
}
|
|
};
|
|
|
|
Config::$key = base64_encode(str_repeat('i', 32));
|
|
$capsule = new Capsule();
|
|
$capsule->addConnection([
|
|
'driver' => 'sqlite', 'database' => ':memory:',
|
|
], 'default');
|
|
$capsule->setAsGlobal();
|
|
$capsule->bootEloquent();
|
|
$schema = $capsule->schema('default');
|
|
$schema->create('cloud_template_import_intents',
|
|
static function (Blueprint $table): void {
|
|
$table->string('id')->primary();
|
|
$table->integer('user_id');
|
|
$table->string('request_idempotency_key');
|
|
$table->string('request_fingerprint');
|
|
$table->string('source_kind');
|
|
$table->string('name');
|
|
$table->string('os_family');
|
|
$table->string('os_version');
|
|
$table->string('source_filename')->default('');
|
|
$table->integer('source_size_bytes')->default(0);
|
|
$table->integer('source_last_modified')->default(0);
|
|
$table->string('source_sha256')->default('');
|
|
$table->text('sealed_source');
|
|
$table->string('template_id')->unique();
|
|
$table->string('catalog_key');
|
|
$table->string('version_label');
|
|
$table->string('generation_token')->unique();
|
|
$table->integer('storage_revision');
|
|
$table->text('sealed_storage_runtime');
|
|
$table->string('object_key')->unique();
|
|
$table->string('multipart_upload_id')->default('');
|
|
$table->integer('multipart_part_size_bytes')->default(0);
|
|
$table->integer('multipart_part_count')->default(0);
|
|
$table->string('object_version_id')->default('');
|
|
$table->string('object_etag')->default('');
|
|
$table->string('state');
|
|
$table->integer('active_job_id')->nullable();
|
|
$table->string('error_code')->default('');
|
|
$table->integer('expires_at');
|
|
$table->integer('completed_at')->nullable();
|
|
$table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
$table->unique(['user_id', 'request_idempotency_key']);
|
|
});
|
|
$schema->create('cloud_template_import_parts',
|
|
static function (Blueprint $table): void {
|
|
$table->string('intent_id');
|
|
$table->integer('part_number');
|
|
$table->integer('size_bytes');
|
|
$table->string('etag');
|
|
$table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
$table->primary(['intent_id', 'part_number']);
|
|
});
|
|
|
|
$runtime = [
|
|
'config' => [
|
|
'endpoint' => 'https://objects.example.test',
|
|
'region' => 'ewr',
|
|
'bucket' => 'templates',
|
|
'prefix' => 'virt-cloud',
|
|
'path_style' => true,
|
|
],
|
|
'credentials' => [
|
|
'access_key_id' => 'INTENTACCESS',
|
|
'secret_access_key' => 'intent-secret',
|
|
],
|
|
'revision' => 9,
|
|
];
|
|
$workflow = new CloudTemplateImportWorkflow(
|
|
static fn (): array => $runtime,
|
|
static fn (): int => 2000000000
|
|
);
|
|
$actor = [
|
|
'user_id' => 7,
|
|
'is_admin' => true,
|
|
'is_super_admin' => true,
|
|
'can_manage' => true,
|
|
'request_idempotency_key' => 'template-upload-intent-0001',
|
|
];
|
|
$input = [
|
|
'name' => 'Debian 13 本地版',
|
|
'os_family' => 'Debian',
|
|
'os_version' => '13',
|
|
'filename' => 'debian-13.qcow2',
|
|
'size_bytes' => 64 * 1024 * 1024,
|
|
'last_modified' => 1999999000000,
|
|
];
|
|
|
|
$first = $workflow->beginUpload($input, $actor);
|
|
$second = $workflow->beginUpload($input, $actor);
|
|
$assert($first['created'] === true && $second['created'] === false
|
|
&& $first['intent']['id'] === $second['intent']['id']
|
|
&& $first['intent']['state'] === 'prepared'
|
|
&& $first['intent']['can_resume'] === true
|
|
&& $first['intent']['source_size_bytes'] === 64 * 1024 * 1024,
|
|
'A same-key replay did not return the original template import intent.');
|
|
$stored = CloudTemplateImportIntent::findOrFail(
|
|
(string) $first['intent']['id']
|
|
);
|
|
$assert((int) $stored->storage_revision === 9
|
|
&& str_starts_with((string) $stored->object_key,
|
|
'virt-cloud/cloud-templates/')
|
|
&& !str_contains(json_encode($first['intent'], JSON_THROW_ON_ERROR),
|
|
'intent-secret')
|
|
&& !array_key_exists('sealed_storage_runtime', $first['intent'])
|
|
&& !array_key_exists('multipart_upload_id', $first['intent']),
|
|
'The intent did not freeze its object identity or leaked secrets.');
|
|
|
|
try {
|
|
$workflow->beginUpload(
|
|
array_replace($input, ['name' => 'Different']),
|
|
$actor
|
|
);
|
|
throw new RuntimeException('A same-key different request was accepted.');
|
|
} catch (CloudError $error) {
|
|
$assert($error->httpStatus === 409,
|
|
'A same-key fingerprint conflict returned the wrong status.');
|
|
}
|
|
|
|
$listed = $workflow->active($actor);
|
|
$one = $workflow->one((string) $stored->id, $actor);
|
|
$assert(count($listed['intents']) === 1
|
|
&& $one['intent']['id'] === (string) $stored->id,
|
|
'A refreshed administrator page cannot recover the active intent.');
|
|
|
|
CloudTemplateImportStateMachine::assertIntegrity();
|
|
CloudTemplateImportStateMachine::move($stored, 'uploading');
|
|
CloudTemplateImportStateMachine::move($stored, 'source_verifying');
|
|
CloudTemplateImportStateMachine::move($stored, 'source_ready');
|
|
CloudTemplateImportStateMachine::move($stored, 'distributing');
|
|
CloudTemplateImportStateMachine::move($stored, 'ready');
|
|
$assert($stored->completed_at !== null
|
|
&& CloudTemplateImportStateMachine::terminal('ready'),
|
|
'The import state machine did not mark a terminal intent.');
|
|
try {
|
|
CloudTemplateImportStateMachine::move($stored, 'uploading');
|
|
throw new RuntimeException('A terminal import intent was reopened.');
|
|
} catch (CloudError $error) {
|
|
$assert($error->httpStatus === 409,
|
|
'An invalid terminal transition returned the wrong status.');
|
|
}
|
|
|
|
echo "Cloud-template import intent and state-machine behavior passed.\n";
|
|
}
|