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

214 lines
8.3 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\CloudRescueTemplate;
use App\Models\CloudRescueTemplateArtifact;
use App\Services\Cloud\CloudObjectStorageS3Client;
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudRemoteArtifactFetcher;
use App\Services\Cloud\CloudRescueCatalogService;
use App\Services\Config;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
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('r', 32));
$capsule = new Capsule();
$capsule->addConnection([
'driver' => 'sqlite', 'database' => ':memory:',
], 'default');
$capsule->setAsGlobal();
$capsule->bootEloquent();
$capsule->schema('default')->create('cloud_rescue_templates',
static function (Blueprint $table): void {
$table->string('id')->primary();
$table->string('name');
$table->integer('size_bytes');
$table->string('sha256');
$table->integer('revision');
$table->string('generation_token')->unique();
$table->string('state');
$table->integer('active_job_id')->nullable();
$table->integer('created_at');
$table->integer('updated_at');
});
$capsule->schema('default')->create(
'cloud_rescue_template_artifacts',
static function (Blueprint $table): void {
$table->string('template_id')->primary();
$table->integer('storage_revision');
$table->text('sealed_storage_runtime');
$table->string('object_key')->unique();
$table->string('object_version_id')->default('');
$table->string('object_etag')->default('');
$table->integer('size_bytes');
$table->string('sha256');
$table->string('state');
$table->string('error_code')->default('');
$table->string('delete_operation_id')->default('');
$table->integer('delete_generation')->default(0);
$table->integer('delete_cycle_attempts')->default(0);
$table->integer('delete_presence_confirmations')->default(0);
$table->integer('delete_last_checked_at')->default(0);
$table->integer('delete_next_check_at')->default(0);
$table->integer('delete_attention_at')->nullable();
$table->integer('verified_at')->default(0);
$table->integer('created_at');
$table->integer('updated_at');
});
$body = str_repeat('bootable-rescue-iso-', 65536);
$digest = hash('sha256', $body);
$fetchHistory = [];
$fetchStack = HandlerStack::create(new MockHandler([
new Response(200, [
'Content-Length' => (string) strlen($body),
'Content-Encoding' => 'identity',
], $body),
]));
$fetchStack->push(Middleware::history($fetchHistory));
$fetcher = new CloudRemoteArtifactFetcher(
'救援 ISO',
1,
8 * 1024 * 1024 * 1024,
'virt-rescue-test-',
new Client(['handler' => $fetchStack]),
static fn (string $host): array => ['93.184.216.34']
);
$storageHistory = [];
$storageStack = HandlerStack::create(new MockHandler([
new Response(200, [
'ETag' => '"rescue-etag"',
'x-amz-version-id' => 'rescue-version-1',
]),
]));
$storageStack->push(Middleware::history($storageHistory));
$client = new CloudObjectStorageS3Client(new Client([
'handler' => $storageStack,
]));
$runtime = [
'config' => [
'endpoint' => 'https://objects.example.com',
'region' => 'ewr',
'bucket' => 'rescue-bucket',
'prefix' => 'virt-cloud',
'path_style' => true,
],
'credentials' => [
'access_key_id' => 'RESCUEACCESSKEY',
'secret_access_key' => 'rescue-secret',
],
'revision' => 9,
];
$result = CloudRescueCatalogService::importRemote([
'name' => 'SystemRescue',
'url' => 'https://images.example.com/systemrescue.iso',
'sha256' => $digest,
], [
'is_super_admin' => true,
'can_manage' => true,
'user_id' => 1,
], $fetcher, $runtime, $client, [32, 31]);
$template = CloudRescueTemplate::find($result['template_id']);
$artifact = CloudRescueTemplateArtifact::find($result['template_id']);
$request = $storageHistory[0]['request'];
$assert($template !== null && $artifact !== null
&& (string) $template->state === 'validating'
&& (string) $artifact->state === 'validating'
&& (int) $artifact->storage_revision === 9
&& (string) $artifact->object_version_id === 'rescue-version-1'
&& (string) $template->sha256 === $digest
&& (int) $template->size_bytes === strlen($body)
&& $result['validation_host_id'] === 31,
'Rescue import did not freeze the quarantine identity and host order.');
$assert($request->getMethod() === 'PUT'
&& str_contains(
(string) $request->getUri(),
'/rescue-bucket/virt-cloud/rescue-isos/'
. (string) $template->id . '/'
. (string) $template->generation_token . '/rescue.iso'
)
&& $request->getHeaderLine('If-None-Match') === '*'
&& $request->getHeaderLine('X-Amz-Content-Sha256') === $digest,
'Rescue master was not published to an immutable exact object key.');
$assert(count($fetchHistory) === 1 && count($storageHistory) === 1,
'Rescue import unexpectedly replayed a remote side effect.');
$unknownFetcher = new CloudRemoteArtifactFetcher(
'救援 ISO',
1,
8 * 1024 * 1024 * 1024,
'virt-rescue-test-',
new Client(['handler' => HandlerStack::create(new MockHandler([
new Response(200, [
'Content-Length' => (string) strlen($body),
], $body),
]))]),
static fn (string $host): array => ['93.184.216.34']
);
$unknownClient = new CloudObjectStorageS3Client(new Client([
'handler' => HandlerStack::create(new MockHandler([
new ConnectException(
'simulated lost rescue PUT response',
new Request('PUT', 'https://objects.example.com')
),
new ConnectException(
'simulated unavailable rescue HEAD',
new Request('HEAD', 'https://objects.example.com')
),
])),
]));
try {
CloudRescueCatalogService::importRemote([
'name' => 'Unknown Rescue',
'url' => 'https://images.example.com/unknown.iso',
'sha256' => $digest,
], [
'is_super_admin' => true,
'can_manage' => true,
'user_id' => 1,
], $unknownFetcher, $runtime, $unknownClient, [31]);
throw new RuntimeException('Unknown rescue publication succeeded.');
} catch (CloudError $error) {
$assert($error->httpStatus === 502,
'Unknown rescue publication returned an unsafe status.');
}
$unknownTemplate = CloudRescueTemplate::query()
->where('name', 'Unknown Rescue')->first();
$unknownArtifact = $unknownTemplate === null ? null
: CloudRescueTemplateArtifact::find((string) $unknownTemplate->id);
$assert($unknownTemplate !== null && $unknownArtifact !== null
&& (string) $unknownTemplate->state === 'source_unknown'
&& (string) $unknownArtifact->state === 'publish_unknown',
'Unknown rescue PUT effect was not durably fenced for reconciliation.');
echo "Global rescue ISO quarantine import passed.\n";
}