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.
241 lines
9.4 KiB
PHP
241 lines
9.4 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\Cloud {
|
|
final class CloudDaemonActor
|
|
{
|
|
public static function resolve(string $sessionId): ?array
|
|
{
|
|
return [
|
|
'user_id' => 1,
|
|
'is_admin' => true,
|
|
'is_super_admin' => true,
|
|
'can_manage' => true,
|
|
'session_id' => $sessionId,
|
|
];
|
|
}
|
|
}
|
|
|
|
final class TemplateWorkerFakeAgent
|
|
{
|
|
public array $requests = [];
|
|
public array $results = [];
|
|
|
|
public function request(string $method, string $target, array $actor,
|
|
?array $payload = null): array
|
|
{
|
|
$this->requests[] = compact('method', 'target', 'actor', 'payload');
|
|
if (preg_match('#^/v1/operations/([A-Za-z0-9_-]+)$#D',
|
|
$target, $match) === 1 && $method === 'GET') {
|
|
$operationId = rawurldecode($match[1]);
|
|
return [
|
|
'operation_id' => $operationId,
|
|
'status' => 'uncertain',
|
|
'error' => ['code' => 'operation_uncertain'],
|
|
];
|
|
}
|
|
if (preg_match('#^/v1/operations/([A-Za-z0-9_-]+)/reconcile$#D',
|
|
$target, $match) === 1 && $method === 'POST') {
|
|
$operationId = rawurldecode($match[1]);
|
|
return [
|
|
'operation_id' => $operationId,
|
|
'status' => 'succeeded',
|
|
'result' => $this->results[$operationId],
|
|
];
|
|
}
|
|
throw new \RuntimeException('Template worker replayed an import');
|
|
}
|
|
}
|
|
|
|
final class CloudAgentSettingsService
|
|
{
|
|
public static ?TemplateWorkerFakeAgent $client = null;
|
|
|
|
public static function clientForHost(int $hostId,
|
|
float $timeoutSeconds = 20.0): TemplateWorkerFakeAgent
|
|
{
|
|
if ($hostId !== 11 || self::$client === null) {
|
|
throw new \RuntimeException('Missing fake Agent');
|
|
}
|
|
return self::$client;
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace {
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Models\CloudTemplate;
|
|
use App\Models\CloudTemplateReplica;
|
|
use App\Services\Cloud\CloudAgentSettingsService;
|
|
use App\Services\Cloud\CloudWorkerService;
|
|
use App\Services\Cloud\TemplateWorkerFakeAgent;
|
|
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_jobs', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->string('kind');
|
|
$table->string('status');
|
|
$table->integer('available_at');
|
|
$table->integer('lease_until');
|
|
});
|
|
$schema->create('cloud_templates', static function (Blueprint $table): void {
|
|
$table->string('id')->primary();
|
|
$table->string('name');
|
|
$table->string('os_family');
|
|
$table->string('os_version');
|
|
$table->string('source_type');
|
|
$table->text('sealed_source_url');
|
|
$table->integer('image_bytes')->default(0);
|
|
$table->string('sha256')->default('');
|
|
$table->text('startup_script')->default('');
|
|
$table->integer('revision');
|
|
$table->string('generation_token');
|
|
$table->string('state');
|
|
$table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
});
|
|
$schema->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']);
|
|
});
|
|
|
|
$now = time();
|
|
$digest = str_repeat('c', 64);
|
|
$operationA = str_repeat('a', 32);
|
|
$operationB = str_repeat('b', 32);
|
|
$generationA = str_repeat('1', 32);
|
|
$generationB = str_repeat('2', 32);
|
|
$insertTemplate = static function (string $id, string $generation,
|
|
string $digest, string $state, int $updatedAt) use ($now): void {
|
|
Capsule::table('cloud_templates')->insert([
|
|
'id' => $id,
|
|
'name' => $id,
|
|
'os_family' => 'Debian',
|
|
'os_version' => '13',
|
|
'source_type' => 'remote',
|
|
'sealed_source_url' => 'test-sealed-source',
|
|
'image_bytes' => 0,
|
|
'sha256' => $digest,
|
|
'startup_script' => '',
|
|
'revision' => 1,
|
|
'generation_token' => $generation,
|
|
'state' => $state,
|
|
'created_at' => $now,
|
|
'updated_at' => $updatedAt,
|
|
]);
|
|
};
|
|
$insertReplica = static function (string $id, string $generation,
|
|
string $digest, string $operationId, string $state,
|
|
int $updatedAt): void {
|
|
Capsule::table('cloud_template_replicas')->insert([
|
|
'host_id' => 11,
|
|
'template_id' => $id,
|
|
'revision' => 1,
|
|
'generation_token' => $generation,
|
|
'state' => $state,
|
|
'image_bytes' => $state === 'active' ? 8192 : 0,
|
|
'sha256' => $digest,
|
|
'operation_id' => $operationId,
|
|
'error_code' => '',
|
|
'created_at' => $updatedAt,
|
|
'updated_at' => $updatedAt,
|
|
]);
|
|
};
|
|
|
|
$insertTemplate('debian-a', $generationA, $digest, 'importing', $now - 30);
|
|
$insertReplica('debian-a', $generationA, $digest, $operationA,
|
|
'pending', $now - 30);
|
|
$insertTemplate('debian-b', $generationB, $digest, 'importing', $now - 20);
|
|
$insertReplica('debian-b', $generationB, $digest, $operationB,
|
|
'pending', $now - 20);
|
|
|
|
$agent = new TemplateWorkerFakeAgent();
|
|
$agent->results[$operationA] = [
|
|
'id' => 'debian-a',
|
|
'revision' => 1,
|
|
'generation_token' => $generationA,
|
|
'size_bytes' => 8192,
|
|
'sha256' => $digest,
|
|
];
|
|
CloudAgentSettingsService::$client = $agent;
|
|
|
|
$policy = new CloudWorkerService(null, true);
|
|
$assert($policy->tick() === false && $agent->requests === [],
|
|
'The policy-only worker consumed a template import.');
|
|
|
|
$worker = new CloudWorkerService();
|
|
$assert($worker->tick() === true && $worker->observationErrors() === [],
|
|
'The ordinary idle worker did not converge one template import.');
|
|
$templateA = CloudTemplate::find('debian-a');
|
|
$replicaA = CloudTemplateReplica::query()->where('host_id', 11)
|
|
->where('template_id', 'debian-a')->first();
|
|
$templateB = CloudTemplate::find('debian-b');
|
|
$replicaB = CloudTemplateReplica::query()->where('host_id', 11)
|
|
->where('template_id', 'debian-b')->first();
|
|
$assert((string) $templateA->state === 'active'
|
|
&& (int) $templateA->image_bytes === 8192
|
|
&& (string) $templateA->sha256 === $digest
|
|
&& (string) $replicaA->state === 'active'
|
|
&& (string) $templateB->state === 'importing'
|
|
&& (string) $replicaB->state === 'pending',
|
|
'One bounded worker pass changed the wrong import or lost image size.');
|
|
$assert(count($agent->requests) === 2
|
|
&& $agent->requests[0]['method'] === 'GET'
|
|
&& $agent->requests[0]['target'] === '/v1/operations/' . $operationA
|
|
&& $agent->requests[1]['method'] === 'POST'
|
|
&& $agent->requests[1]['target']
|
|
=== '/v1/operations/' . $operationA . '/reconcile'
|
|
&& ($agent->requests[0]['actor']['session_id'] ?? null)
|
|
=== 'cloud-template-import-worker'
|
|
&& ($agent->requests[0]['actor']['operation_id'] ?? null)
|
|
=== $operationA,
|
|
'Worker lookup/reconciliation did not preserve the saved operation ID.');
|
|
foreach ($agent->requests as $request) {
|
|
$assert($request['target'] !== '/v1/templates',
|
|
'The worker replayed the original template import POST.');
|
|
}
|
|
|
|
Capsule::table('cloud_template_replicas')->where('template_id', 'debian-b')
|
|
->update(['state' => 'failed', 'error_code' => 'test_terminal']);
|
|
Capsule::table('cloud_templates')->where('id', 'debian-b')
|
|
->update(['state' => 'error']);
|
|
$generationC = str_repeat('3', 32);
|
|
$insertTemplate('debian-c', $generationC, $digest, 'importing', $now - 10);
|
|
$insertReplica('debian-c', $generationC, $digest, str_repeat('d', 32),
|
|
'active', $now - 10);
|
|
$requestsBeforeAggregateRepair = count($agent->requests);
|
|
$assert($worker->tick() === true
|
|
&& (string) CloudTemplate::find('debian-c')->state === 'active'
|
|
&& count($agent->requests) === $requestsBeforeAggregateRepair,
|
|
'Worker did not repair an all-active template aggregate locally.');
|
|
$assert($worker->tick() === false,
|
|
'A terminal failed template kept the idle worker in a busy loop.');
|
|
|
|
echo "Idle worker template import convergence and no-replay tests passed.\n";
|
|
}
|