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

382 lines
14 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\CloudTemplate;
use App\Services\Cloud\CloudError;
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);
}
};
$expectConflict = static function (callable $callable, string $message) use (
$assert
): void {
try {
$callable();
} catch (CloudError $error) {
$assert($error->httpStatus === 409, $message);
return;
}
throw new RuntimeException($message);
};
$capsule = new Capsule();
$capsule->addConnection([
'driver' => 'sqlite', 'database' => ':memory:',
], 'default');
$capsule->setAsGlobal();
$capsule->bootEloquent();
$schema = $capsule->schema('default');
$schema->create('configurations', static function (Blueprint $table): void {
$table->increments('id');
$table->string('name')->unique();
$table->text('value');
});
$schema->create('shop', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('status');
});
$schema->create('cloud_regions', static function (Blueprint $table): void {
$table->increments('id');
$table->string('status');
});
$schema->create('cloud_network_lines', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('region_id');
$table->integer('is_default');
$table->string('status');
});
$schema->create('cloud_resource_pools', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('line_id');
$table->string('status');
});
$schema->create('cloud_hosts', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('enabled');
});
$schema->create('cloud_host_agents', static function (Blueprint $table): void {
$table->integer('host_id')->primary();
$table->integer('enabled');
$table->integer('revision');
$table->integer('tested_revision');
$table->string('last_test_status');
$table->text('network_selectors_json');
});
$schema->create('cloud_host_storage_configurations', static function (
Blueprint $table
): void {
$table->integer('host_id')->primary();
$table->string('state');
});
$schema->create('cloud_host_resource_pool_memberships', static function (
Blueprint $table
): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('resource_pool_id');
$table->string('scheduler_state');
$table->integer('revision')->default(1);
});
$schema->create('cloud_host_capacity_policies', static function (
Blueprint $table
): void {
$table->integer('host_id')->primary();
$table->integer('configured_cpu_cores');
$table->integer('configured_memory_mib');
$table->integer('configured_disk_gib');
});
$schema->create('cloud_host_network_attachments', static function (
Blueprint $table
): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('line_id');
$table->string('network_selector');
$table->string('state');
$table->string('verification_state');
$table->integer('verified_agent_revision');
$table->integer('last_verified_at');
});
$schema->create('cloud_public_ip_pools', static function (
Blueprint $table
): void {
$table->increments('id');
$table->string('pool_key');
$table->integer('host_id');
$table->integer('network_attachment_id');
$table->string('cidr');
$table->string('gateway');
$table->integer('guest_prefix_length');
$table->text('dns_servers_json');
$table->text('reserved_ips_json');
$table->string('state');
$table->string('sync_state');
$table->integer('desired_revision');
$table->integer('observed_revision');
});
$schema->create('cloud_public_ips', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('public_ip_pool_id');
$table->string('address');
$table->integer('prefix_length');
$table->string('state');
$table->integer('reservation_id')->nullable();
$table->integer('instance_id')->nullable();
$table->string('operation_id')->nullable();
});
$schema->create('cloud_offers', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('plan_id');
$table->integer('resource_pool_id');
$table->integer('status');
$table->string('currency');
});
$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']);
});
Capsule::table('configurations')->insert([
'name' => 'cloud_network_lines_enabled', 'value' => 'false',
]);
Capsule::table('shop')->insert([
['id' => 1, 'status' => 1],
['id' => 2, 'status' => 1],
]);
foreach ([1, 2] as $id) {
Capsule::table('cloud_regions')->insert([
'id' => $id, 'status' => 'active',
]);
Capsule::table('cloud_network_lines')->insert([
'id' => $id, 'region_id' => $id, 'is_default' => 1,
'status' => 'active',
]);
Capsule::table('cloud_resource_pools')->insert([
'id' => $id, 'line_id' => $id, 'status' => 'active',
]);
Capsule::table('cloud_hosts')->insert([
'id' => $id, 'enabled' => 1,
]);
Capsule::table('cloud_host_agents')->insert([
'host_id' => $id, 'enabled' => 1, 'revision' => 1,
'tested_revision' => 1, 'last_test_status' => 'healthy',
'network_selectors_json' => json_encode([
'bridge:public-' . $id,
], JSON_THROW_ON_ERROR),
]);
Capsule::table('cloud_host_storage_configurations')->insert([
'host_id' => $id, 'state' => 'applied',
]);
Capsule::table('cloud_host_resource_pool_memberships')->insert([
'host_id' => $id, 'resource_pool_id' => $id,
'scheduler_state' => 'active',
]);
Capsule::table('cloud_host_capacity_policies')->insert([
'host_id' => $id, 'configured_cpu_cores' => 16,
'configured_memory_mib' => 32768, 'configured_disk_gib' => 512,
]);
$attachmentId = 100 + $id;
Capsule::table('cloud_host_network_attachments')->insert([
'id' => $attachmentId, 'host_id' => $id, 'line_id' => $id,
'network_selector' => 'bridge:public-' . $id,
'state' => 'active', 'verification_state' => 'verified',
'verified_agent_revision' => 1, 'last_verified_at' => time(),
]);
$publicPoolId = 200 + $id;
Capsule::table('cloud_public_ip_pools')->insert([
'id' => $publicPoolId, 'pool_key' => str_repeat((string) $id, 32),
'host_id' => $id, 'network_attachment_id' => $attachmentId,
'cidr' => '203.0.11' . $id . '.0/29',
'gateway' => '203.0.11' . $id . '.1',
'guest_prefix_length' => 29, 'state' => 'active',
'dns_servers_json' => '[]', 'reserved_ips_json' => '[]',
'sync_state' => 'applied', 'desired_revision' => 1,
'observed_revision' => 1,
]);
Capsule::table('cloud_public_ips')->insert([
'host_id' => $id, 'public_ip_pool_id' => $publicPoolId,
'address' => '203.0.11' . $id . '.2', 'prefix_length' => 29,
'state' => 'available', 'reservation_id' => null,
'instance_id' => null, 'operation_id' => null,
]);
Capsule::table('cloud_offers')->insert([
'id' => 10 + $id, 'plan_id' => $id, 'resource_pool_id' => $id,
'status' => 1, 'currency' => 'USD',
]);
}
$now = time();
$generation = str_repeat('a', 32);
$template = new CloudTemplate();
foreach ([
'id' => 'regional-template',
'catalog_key' => 'regional',
'version_label' => 'v1',
'is_default' => 1,
'name' => 'Regional Linux',
'os_family' => 'Linux',
'os_version' => '1',
'image_bytes' => 4096,
'sha256' => str_repeat('b', 64),
'startup_script' => '',
'revision' => 2,
'generation_token' => $generation,
'state' => 'degraded',
'created_at' => $now,
'updated_at' => $now,
] as $field => $value) {
$template->{$field} = $value;
}
$template->save();
Capsule::table('cloud_template_replicas')->insert([
[
'host_id' => 1, 'template_id' => 'regional-template',
'revision' => 2, 'generation_token' => $generation,
'state' => 'active', 'image_bytes' => 4096,
'sha256' => str_repeat('b', 64),
'operation_id' => str_repeat('1', 32), 'job_id' => null,
'error_code' => '', 'created_at' => $now, 'updated_at' => $now,
],
[
'host_id' => 2, 'template_id' => 'regional-template',
'revision' => 1, 'generation_token' => $generation,
'state' => 'active', 'image_bytes' => 4096,
'sha256' => str_repeat('b', 64),
'operation_id' => str_repeat('2', 32), 'job_id' => null,
'error_code' => '', 'created_at' => $now, 'updated_at' => $now,
],
]);
$public = CloudTemplateCatalogService::publicCatalog()['templates'];
$assert(count($public) === 1
&& $public[0]['offer_ids'] === [11]
&& $public[0]['region_ids'] === [1],
'Deployment catalog did not scope a degraded template to its exact replica.');
$assert(count(CloudTemplateCatalogService::publicCatalogForHost(1)['templates'])
=== 1
&& CloudTemplateCatalogService::publicCatalogForHost(2)['templates'] === [],
'Reinstall catalog was not scoped to the current host replica.');
CloudTemplateCatalogService::assertProvisionableForPool(
'regional-template',
1
);
$expectConflict(static fn () =>
CloudTemplateCatalogService::assertProvisionableForPool(
'regional-template',
2
), 'A pool without an exact replica accepted the template.');
$expectConflict(static fn () =>
CloudTemplateCatalogService::applyProvisioningSnapshotForHost([
'template_id' => 'regional-template',
], 2, false), 'A reinstall host without an exact replica was accepted.');
Capsule::table('cloud_template_replicas')->where('host_id', 2)->update([
'revision' => 2,
]);
$public = CloudTemplateCatalogService::publicCatalog()['templates'];
$assert($public[0]['offer_ids'] === [11, 12]
&& $public[0]['region_ids'] === [1, 2]
&& count(CloudTemplateCatalogService::publicCatalogForHost(2)['templates'])
=== 1,
'A newly exact replica did not expose its offer, region and reinstall host.');
Capsule::table('cloud_host_network_attachments')->where('host_id', 2)->update([
'verified_agent_revision' => 0,
]);
$public = CloudTemplateCatalogService::publicCatalog()['templates'];
$assert($public[0]['offer_ids'] === [11]
&& $public[0]['region_ids'] === [1]
&& count(CloudTemplateCatalogService::publicCatalogForHost(2)['templates'])
=== 1,
'Stale network evidence affected reinstall or remained deployable.');
Capsule::table('cloud_host_network_attachments')->where('host_id', 2)->update([
'verified_agent_revision' => 1,
]);
Capsule::table('cloud_public_ip_pools')->where('host_id', 2)->update([
'sync_state' => 'failed',
]);
$public = CloudTemplateCatalogService::publicCatalog()['templates'];
$assert($public[0]['offer_ids'] === [11]
&& count(CloudTemplateCatalogService::publicCatalogForHost(2)['templates'])
=== 1,
'An unapplied public pool remained deployable or blocked reinstall.');
Capsule::table('cloud_public_ip_pools')->where('host_id', 2)->update([
'sync_state' => 'applied',
]);
Capsule::table('cloud_public_ips')->where('host_id', 2)->update([
'state' => 'reserved',
]);
$public = CloudTemplateCatalogService::publicCatalog()['templates'];
$assert($public[0]['offer_ids'] === [11]
&& count(CloudTemplateCatalogService::publicCatalogForHost(2)['templates'])
=== 1,
'A host without an available IP remained deployable or blocked reinstall.');
Capsule::table('cloud_public_ips')->where('host_id', 2)->update([
'state' => 'available',
]);
Capsule::table('cloud_host_resource_pool_memberships')
->where('host_id', 2)->where('resource_pool_id', 2)->update([
'scheduler_state' => 'disabled',
]);
$public = CloudTemplateCatalogService::publicCatalog()['templates'];
$assert($public[0]['offer_ids'] === [11]
&& count(CloudTemplateCatalogService::publicCatalogForHost(2)['templates'])
=== 1,
'An inactive exact membership remained deployable or blocked reinstall.');
Capsule::table('cloud_host_resource_pool_memberships')
->where('host_id', 2)->where('resource_pool_id', 2)->update([
'scheduler_state' => 'active',
]);
Capsule::table('cloud_host_agents')->where('host_id', 2)->update([
'last_test_status' => 'unhealthy',
]);
$public = CloudTemplateCatalogService::publicCatalog()['templates'];
$assert($public[0]['offer_ids'] === [11]
&& $public[0]['region_ids'] === [1]
&& CloudTemplateCatalogService::publicCatalogForHost(2)['templates'] === [],
'An unhealthy host remained visible in deployment or reinstall catalogs.');
echo "Regional template availability contract passed.\n";