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.
243 lines
9.2 KiB
PHP
243 lines
9.2 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\Cloud {
|
|
final class TemplateMetadataSyncFakeAgent
|
|
{
|
|
public array $requests = [];
|
|
public string $mode = 'success';
|
|
/** @var null|callable */
|
|
public $beforeResponse = null;
|
|
|
|
public function request(string $method, string $target, array $actor,
|
|
?array $payload = null): array
|
|
{
|
|
$this->requests[] = compact('method', 'target', 'actor', 'payload');
|
|
if (is_callable($this->beforeResponse)) {
|
|
($this->beforeResponse)($method, $target, $actor, $payload);
|
|
}
|
|
$operationId = (string) ($payload['operation_id']
|
|
?? $actor['operation_id'] ?? basename($target));
|
|
if ($method === 'POST') {
|
|
return ['operation_id' => basename(dirname($target)),
|
|
'status' => 'succeeded', 'result' => $this->result];
|
|
}
|
|
if ($this->mode === 'wrong_operation') {
|
|
$operationId = 'wrong-operation-id';
|
|
}
|
|
return [
|
|
'operation_id' => $operationId,
|
|
'status' => $this->mode === 'uncertain'
|
|
? 'uncertain' : 'succeeded',
|
|
'result' => $this->result,
|
|
];
|
|
}
|
|
|
|
public array $result = [];
|
|
}
|
|
|
|
final class CloudAgentSettingsService
|
|
{
|
|
public static ?TemplateMetadataSyncFakeAgent $client = null;
|
|
|
|
public static function clientForHost(int $hostId,
|
|
float $timeoutSeconds = 20.0): TemplateMetadataSyncFakeAgent
|
|
{
|
|
if ($hostId !== 7 || self::$client === null) {
|
|
throw new \RuntimeException('Missing fake Agent');
|
|
}
|
|
return self::$client;
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace {
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Models\CloudTemplateReplica;
|
|
use App\Services\Cloud\CloudAgentSettingsService;
|
|
use App\Services\Cloud\CloudError;
|
|
use App\Services\Cloud\CloudTemplateCatalogService;
|
|
use App\Services\Cloud\TemplateMetadataSyncFakeAgent;
|
|
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);
|
|
}
|
|
};
|
|
$expect = static function (callable $callable, int $status,
|
|
string $message) use ($assert): void {
|
|
try {
|
|
$callable();
|
|
} catch (CloudError $error) {
|
|
$assert($error->httpStatus === $status,
|
|
$message . ' Unexpected status: ' . $error->httpStatus);
|
|
return;
|
|
}
|
|
throw new RuntimeException($message);
|
|
};
|
|
|
|
$capsule = new Capsule();
|
|
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'],
|
|
'default');
|
|
$capsule->setAsGlobal();
|
|
$capsule->bootEloquent();
|
|
$capsule->schema('default')->create('cloud_templates',
|
|
static function (Blueprint $table): void {
|
|
$table->string('id')->primary();
|
|
$table->string('name');
|
|
$table->integer('revision');
|
|
$table->string('generation_token');
|
|
$table->string('state');
|
|
});
|
|
$capsule->schema('default')->create('cloud_template_replicas',
|
|
static function (Blueprint $table): void {
|
|
$table->integer('host_id');
|
|
$table->string('template_id');
|
|
$table->integer('revision');
|
|
$table->string('generation_token');
|
|
$table->string('state');
|
|
$table->integer('image_bytes')->default(0);
|
|
$table->string('sha256')->default('');
|
|
$table->string('operation_id')->default('');
|
|
$table->string('error_code')->default('');
|
|
$table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
$table->primary(['host_id', 'template_id']);
|
|
});
|
|
|
|
$generation = str_repeat('a', 32);
|
|
$id = 'debian-13-metadata-sync';
|
|
$now = time();
|
|
Capsule::table('cloud_templates')->insert([
|
|
'id' => $id,
|
|
'name' => 'Debian 13 renamed',
|
|
'revision' => 2,
|
|
'generation_token' => $generation,
|
|
'state' => 'active',
|
|
]);
|
|
Capsule::table('cloud_template_replicas')->insert([
|
|
'host_id' => 7,
|
|
'template_id' => $id,
|
|
'revision' => 1,
|
|
'generation_token' => $generation,
|
|
'state' => 'active',
|
|
'image_bytes' => 4096,
|
|
'sha256' => str_repeat('b', 64),
|
|
'operation_id' => bin2hex(random_bytes(16)),
|
|
'error_code' => '',
|
|
'created_at' => $now,
|
|
'updated_at' => $now,
|
|
]);
|
|
|
|
$snapshot = static fn (int $revision, string $name): array => [
|
|
'id' => $id,
|
|
'name' => $name,
|
|
'revision' => $revision,
|
|
'generation_token' => $generation,
|
|
];
|
|
$result = static fn (int $revision, string $name): array => [
|
|
'id' => $id,
|
|
'name' => $name,
|
|
'revision' => $revision,
|
|
'generation_token' => $generation,
|
|
];
|
|
$actor = ['user_id' => 1, 'can_manage' => true];
|
|
$setAuthority = static function (int $revision, string $name,
|
|
string $state = 'active') use ($id): void {
|
|
Capsule::table('cloud_templates')->where('id', $id)->update([
|
|
'name' => $name,
|
|
'revision' => $revision,
|
|
'state' => $state,
|
|
]);
|
|
};
|
|
$agent = new TemplateMetadataSyncFakeAgent();
|
|
CloudAgentSettingsService::$client = $agent;
|
|
|
|
$agent->result = $result(2, 'Debian 13 renamed');
|
|
$synced = CloudTemplateCatalogService::synchronizeMetadata(
|
|
$snapshot(2, 'Debian 13 renamed'), $actor
|
|
);
|
|
$row = CloudTemplateReplica::query()->where('host_id', 7)
|
|
->where('template_id', $id)->first();
|
|
$operationId = (string) $row->operation_id;
|
|
$assert($synced['replication_pending'] === false
|
|
&& (int) $row->revision === 2
|
|
&& (string) $row->state === 'active'
|
|
&& ($agent->requests[0]['actor']['operation_id'] ?? null)
|
|
=== $operationId
|
|
&& ($agent->requests[0]['payload']['operation_id'] ?? null)
|
|
=== $operationId,
|
|
'Metadata synchronization did not bind success to its operation ID.');
|
|
|
|
$agent->mode = 'wrong_operation';
|
|
$agent->result = $result(3, 'Debian 13 wrong operation');
|
|
$setAuthority(3, 'Debian 13 wrong operation');
|
|
$mismatch = CloudTemplateCatalogService::synchronizeMetadata(
|
|
$snapshot(3, 'Debian 13 wrong operation'), $actor
|
|
);
|
|
$row->refresh();
|
|
$assert($mismatch['replication_pending'] === true
|
|
&& (int) $row->revision === 2
|
|
&& (string) $row->state === 'stale'
|
|
&& (string) $row->error_code === 'agent_sync_failed',
|
|
'Mismatched metadata operation was accepted as a successful replica.');
|
|
|
|
// A later revision may commit while an older request is still in flight.
|
|
// Neither its late failure nor late success may regress that newer row.
|
|
$agent->beforeResponse = static function () use ($id, $generation): void {
|
|
Capsule::table('cloud_template_replicas')->where('host_id', 7)
|
|
->where('template_id', $id)->update([
|
|
'revision' => 5,
|
|
'generation_token' => $generation,
|
|
'state' => 'active',
|
|
'operation_id' => 'newer-operation-id',
|
|
'error_code' => '',
|
|
'updated_at' => time(),
|
|
]);
|
|
};
|
|
$agent->result = $result(4, 'Debian 13 older response');
|
|
$setAuthority(4, 'Debian 13 older response');
|
|
CloudTemplateCatalogService::synchronizeMetadata(
|
|
$snapshot(4, 'Debian 13 older response'), $actor
|
|
);
|
|
$row->refresh();
|
|
$assert((int) $row->revision === 5
|
|
&& (string) $row->state === 'active'
|
|
&& (string) $row->operation_id === 'newer-operation-id',
|
|
'Late metadata failure regressed a newer replica revision.');
|
|
|
|
$agent->mode = 'uncertain';
|
|
$agent->beforeResponse = null;
|
|
$agent->result = $result(6, 'Debian 13 reconciled');
|
|
$setAuthority(6, 'Debian 13 reconciled');
|
|
$before = count($agent->requests);
|
|
$reconciled = CloudTemplateCatalogService::synchronizeMetadata(
|
|
$snapshot(6, 'Debian 13 reconciled'), $actor
|
|
);
|
|
$requests = array_slice($agent->requests, $before);
|
|
$row->refresh();
|
|
$assert($reconciled['replication_pending'] === false
|
|
&& (int) $row->revision === 6
|
|
&& count($requests) === 2
|
|
&& $requests[0]['method'] === 'PATCH'
|
|
&& $requests[1]['method'] === 'POST'
|
|
&& str_ends_with($requests[1]['target'], '/reconcile'),
|
|
'Uncertain metadata synchronization was replayed or not reconciled.');
|
|
|
|
$setAuthority(7, 'Debian 13 blocked', 'importing');
|
|
$requestsBeforeBlocked = count($agent->requests);
|
|
$operationBeforeBlocked = (string) $row->operation_id;
|
|
$expect(static fn () => CloudTemplateCatalogService::synchronizeMetadata(
|
|
$snapshot(7, 'Debian 13 blocked'), $actor
|
|
), 409, 'A non-active template dispatched metadata synchronization.');
|
|
$row->refresh();
|
|
$assert(count($agent->requests) === $requestsBeforeBlocked
|
|
&& (string) $row->operation_id === $operationBeforeBlocked,
|
|
'Blocked metadata synchronization changed the replica operation ID.');
|
|
|
|
echo "Template metadata operation binding and CAS tests passed.\n";
|
|
}
|