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.
267 lines
9.1 KiB
PHP
267 lines
9.1 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');
|
|
$table->string('default_network');
|
|
});
|
|
$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');
|
|
});
|
|
$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');
|
|
});
|
|
$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_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,
|
|
'default_network' => 'bridge:br' . ($id - 1),
|
|
]);
|
|
Capsule::table('cloud_host_agents')->insert([
|
|
'host_id' => $id, 'enabled' => 1, 'revision' => 1,
|
|
'tested_revision' => 1, 'last_test_status' => 'healthy',
|
|
]);
|
|
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,
|
|
]);
|
|
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_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";
|