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.
228 lines
7.8 KiB
PHP
228 lines
7.8 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Models\CloudJob;
|
|
use App\Models\CloudTemplate;
|
|
use App\Models\CloudTemplateReplica;
|
|
use App\Services\Cloud\CloudTemplateCatalogService;
|
|
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->integer('id')->primary();
|
|
$table->boolean('enabled');
|
|
$table->string('default_network');
|
|
});
|
|
$schema->create('cloud_host_agents', static function (Blueprint $table): void {
|
|
$table->integer('host_id')->primary();
|
|
$table->boolean('enabled');
|
|
$table->string('last_test_status');
|
|
$table->integer('tested_revision');
|
|
$table->integer('revision');
|
|
});
|
|
$schema->create('cloud_host_storage_configurations',
|
|
static function (Blueprint $table): void {
|
|
$table->integer('host_id')->primary();
|
|
$table->string('state');
|
|
});
|
|
$schema->create('cloud_templates', static function (Blueprint $table): void {
|
|
$table->string('id')->primary();
|
|
$table->string('catalog_key');
|
|
$table->string('version_label');
|
|
$table->boolean('is_default');
|
|
$table->string('name');
|
|
$table->string('os_family');
|
|
$table->string('os_version');
|
|
$table->integer('image_bytes');
|
|
$table->string('sha256');
|
|
$table->text('startup_script');
|
|
$table->integer('revision');
|
|
$table->string('generation_token');
|
|
$table->string('state');
|
|
$table->integer('active_job_id')->nullable();
|
|
$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');
|
|
$table->string('sha256');
|
|
$table->string('operation_id');
|
|
$table->integer('job_id')->nullable();
|
|
$table->string('error_code');
|
|
$table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
$table->primary(['host_id', 'template_id']);
|
|
});
|
|
|
|
foreach ([31, 32] as $hostId) {
|
|
Capsule::table('cloud_hosts')->insert([
|
|
'id' => $hostId, 'enabled' => 1,
|
|
'default_network' => 'bridge:br0',
|
|
]);
|
|
Capsule::table('cloud_host_agents')->insert([
|
|
'host_id' => $hostId, 'enabled' => 1,
|
|
'last_test_status' => 'healthy', 'tested_revision' => 4,
|
|
'revision' => 4,
|
|
]);
|
|
Capsule::table('cloud_host_storage_configurations')->insert([
|
|
'host_id' => $hostId, 'state' => 'applied',
|
|
]);
|
|
}
|
|
$now = time();
|
|
$generation = str_repeat('a', 32);
|
|
$sha = str_repeat('b', 64);
|
|
Capsule::table('cloud_templates')->insert([
|
|
'id' => 'standalone-replica-template',
|
|
'catalog_key' => 'debian-13',
|
|
'version_label' => 'v2',
|
|
'is_default' => 1,
|
|
'name' => 'Debian 13',
|
|
'os_family' => 'Debian',
|
|
'os_version' => '13',
|
|
'image_bytes' => 4096,
|
|
'sha256' => $sha,
|
|
'startup_script' => '',
|
|
'revision' => 2,
|
|
'generation_token' => $generation,
|
|
'state' => 'degraded',
|
|
'active_job_id' => 501,
|
|
'created_at' => $now,
|
|
'updated_at' => $now,
|
|
]);
|
|
Capsule::table('cloud_template_replicas')->insert([
|
|
[
|
|
'host_id' => 31, 'template_id' => 'standalone-replica-template',
|
|
'revision' => 2, 'generation_token' => $generation,
|
|
'state' => 'active', 'image_bytes' => 4096, 'sha256' => $sha,
|
|
'operation_id' => str_repeat('1', 32), 'job_id' => 500,
|
|
'error_code' => '', 'created_at' => $now, 'updated_at' => $now,
|
|
],
|
|
[
|
|
'host_id' => 32, 'template_id' => 'standalone-replica-template',
|
|
'revision' => 2, 'generation_token' => $generation,
|
|
'state' => 'importing', 'image_bytes' => 0, 'sha256' => $sha,
|
|
'operation_id' => str_repeat('2', 32), 'job_id' => 501,
|
|
'error_code' => '', 'created_at' => $now, 'updated_at' => $now,
|
|
],
|
|
]);
|
|
|
|
$job = new CloudJob();
|
|
$job->id = 501;
|
|
$job->host_id = 32;
|
|
$job->kind = 'template_import_replica';
|
|
$job->parent_job_id = null;
|
|
$job->operation_id = str_repeat('2', 32);
|
|
$payload = ['template_id' => 'standalone-replica-template'];
|
|
$result = [
|
|
'id' => 'standalone-replica-template',
|
|
'revision' => 2,
|
|
'generation_token' => $generation,
|
|
'size_bytes' => 4096,
|
|
'sha256' => $sha,
|
|
];
|
|
CloudTemplateCatalogService::finishJobLocked(
|
|
$job,
|
|
'succeeded',
|
|
$result,
|
|
$payload,
|
|
['effect_may_have_applied' => false, 'safe_to_resubmit' => false]
|
|
);
|
|
$template = CloudTemplate::findOrFail('standalone-replica-template');
|
|
$assert((string) $template->state === 'active'
|
|
&& $template->active_job_id === null
|
|
&& (string) CloudTemplateReplica::query()->where('host_id', 32)
|
|
->where('template_id', (string) $template->id)->value('state')
|
|
=== 'active',
|
|
'An exact standalone replica receipt did not restore full availability.');
|
|
|
|
Capsule::table('cloud_templates')->where('id', (string) $template->id)
|
|
->update(['state' => 'degraded', 'active_job_id' => 502]);
|
|
Capsule::table('cloud_template_replicas')->where('host_id', 32)
|
|
->where('template_id', (string) $template->id)->update([
|
|
'state' => 'importing', 'image_bytes' => 0,
|
|
'job_id' => 502, 'operation_id' => str_repeat('3', 32),
|
|
]);
|
|
$job->id = 502;
|
|
$job->operation_id = str_repeat('3', 32);
|
|
CloudTemplateCatalogService::finishJobLocked(
|
|
$job,
|
|
'failed',
|
|
null,
|
|
$payload,
|
|
[
|
|
'effect_may_have_applied' => true,
|
|
'safe_to_resubmit' => false,
|
|
'error_code' => 'agent_timeout',
|
|
]
|
|
);
|
|
$template->refresh();
|
|
$assert((int) $template->active_job_id === 502
|
|
&& (string) $template->state === 'degraded',
|
|
'An unknown standalone replica effect released the template fence.');
|
|
|
|
CloudTemplateCatalogService::finishJobLocked(
|
|
$job,
|
|
'succeeded',
|
|
$result,
|
|
$payload,
|
|
['effect_may_have_applied' => false, 'safe_to_resubmit' => false],
|
|
true
|
|
);
|
|
$template->refresh();
|
|
$assert($template->active_job_id === null
|
|
&& (string) $template->state === 'active',
|
|
'Recovered standalone replica evidence did not release the original fence.');
|
|
|
|
Capsule::table('cloud_templates')->where('id', (string) $template->id)
|
|
->update(['state' => 'degraded', 'active_job_id' => 503]);
|
|
Capsule::table('cloud_template_replicas')->where('host_id', 32)
|
|
->where('template_id', (string) $template->id)->update([
|
|
'state' => 'syncing', 'revision' => 1,
|
|
'job_id' => 503, 'operation_id' => str_repeat('4', 32),
|
|
]);
|
|
$job->id = 503;
|
|
$job->kind = 'template_metadata_replica';
|
|
$job->operation_id = str_repeat('4', 32);
|
|
CloudTemplateCatalogService::finishJobLocked(
|
|
$job,
|
|
'succeeded',
|
|
[
|
|
'id' => 'standalone-replica-template',
|
|
'name' => 'Debian 13',
|
|
'revision' => 2,
|
|
'generation_token' => $generation,
|
|
],
|
|
$payload,
|
|
['effect_may_have_applied' => false, 'safe_to_resubmit' => false]
|
|
);
|
|
$template->refresh();
|
|
$assert($template->active_job_id === null
|
|
&& (string) $template->state === 'active'
|
|
&& (int) CloudTemplateReplica::query()->where('host_id', 32)
|
|
->where('template_id', (string) $template->id)->value('revision') === 2
|
|
&& (string) CloudTemplateReplica::query()->where('host_id', 32)
|
|
->where('template_id', (string) $template->id)->value('state')
|
|
=== 'active',
|
|
'A standalone metadata repair did not publish exact availability.');
|
|
|
|
echo "Template standalone replica finalization behavior passed.\n";
|