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

178 lines
6.0 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\CloudTemplate;
use App\Services\Cloud\CloudTemplateHostBackfillService;
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->string('source_type');
$table->text('sealed_source_url');
$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']);
});
Capsule::table('cloud_hosts')->insert([
['id' => 41, 'enabled' => 1, 'default_network' => 'bridge:br0'],
['id' => 42, 'enabled' => 1, 'default_network' => 'bridge:br0'],
]);
Capsule::table('cloud_host_agents')->insert([
['host_id' => 41, 'enabled' => 1, 'last_test_status' => 'healthy',
'tested_revision' => 3, 'revision' => 3],
['host_id' => 42, 'enabled' => 1, 'last_test_status' => 'healthy',
'tested_revision' => 3, 'revision' => 4],
]);
Capsule::table('cloud_host_storage_configurations')->insert([
['host_id' => 41, 'state' => 'applied'],
['host_id' => 42, 'state' => 'applied'],
]);
$now = time();
$template = static function (
string $id,
string $catalogKey,
bool $default,
string $state,
string $generation,
string $sha
) use ($now): array {
return [
'id' => $id,
'catalog_key' => $catalogKey,
'version_label' => 'v1',
'is_default' => $default ? 1 : 0,
'name' => $id,
'os_family' => 'Debian',
'os_version' => '13',
'source_type' => 'object',
'sealed_source_url' => 'test-only',
'image_bytes' => 4096,
'sha256' => $sha,
'startup_script' => '',
'revision' => 2,
'generation_token' => $generation,
'state' => $state,
'active_job_id' => null,
'created_at' => $now,
'updated_at' => $now,
];
};
$generationA = str_repeat('a', 32);
$generationB = str_repeat('b', 32);
$shaA = str_repeat('c', 64);
$shaB = str_repeat('d', 64);
Capsule::table('cloud_templates')->insert([
$template('debian-current', 'debian-13', true, 'active',
$generationA, $shaA),
$template('ubuntu-current', 'ubuntu-24', true, 'degraded',
$generationB, $shaB),
$template('debian-old', 'debian-13', false, 'active',
str_repeat('e', 32), str_repeat('f', 64)),
$template('retired-current', 'rocky-9', true, 'retired',
str_repeat('1', 32), str_repeat('2', 64)),
]);
Capsule::table('cloud_template_replicas')->insert([
'host_id' => 41,
'template_id' => 'debian-current',
'revision' => 2,
'generation_token' => $generationA,
'state' => 'active',
'image_bytes' => 4096,
'sha256' => $shaA,
'operation_id' => str_repeat('3', 32),
'job_id' => 1,
'error_code' => '',
'created_at' => $now,
'updated_at' => $now,
]);
$plan = CloudTemplateHostBackfillService::planReadyHost(41);
$assert($plan['ready'] === true
&& $plan['considered'] === 2
&& $plan['already_ready'] === ['debian-current']
&& $plan['pending'] === ['ubuntu-current'],
'Ready-host backfill did not select only missing default generations.');
$notReady = CloudTemplateHostBackfillService::planReadyHost(42);
$assert($notReady === [
'host_id' => 42,
'ready' => false,
'considered' => 0,
'already_ready' => [],
'pending' => [],
], 'A stale Agent readiness revision was accepted for template backfill.');
$exact = CloudTemplate::findOrFail('debian-current');
Capsule::table('cloud_template_replicas')
->where('host_id', 41)
->where('template_id', 'debian-current')
->update(['sha256' => str_repeat('9', 64)]);
$changed = CloudTemplateHostBackfillService::planReadyHost(41);
$assert($changed['already_ready'] === []
&& $changed['pending'] === ['debian-current', 'ubuntu-current']
&& (string) $exact->sha256 === $shaA,
'A content-mismatched host replica was treated as exact.');
echo "Template ready-host backfill behavior passed.\n";