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.
373 lines
16 KiB
PHP
373 lines
16 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Controllers\CloudAdminController;
|
|
use App\Services\Cloud\CloudAgentSettingsService;
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Slim\Http\Environment;
|
|
use Slim\Http\Request;
|
|
use Slim\Http\Response;
|
|
|
|
$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->increments('id');
|
|
$table->string('name');
|
|
$table->string('region');
|
|
$table->string('country_code', 2);
|
|
$table->string('default_network');
|
|
$table->boolean('enabled');
|
|
$table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
});
|
|
$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('priority');
|
|
$table->integer('weight');
|
|
$table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
$table->unique(['host_id', 'resource_pool_id']);
|
|
});
|
|
$schema->create('cloud_regions', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->string('code');
|
|
$table->string('name');
|
|
$table->string('country_code', 2);
|
|
$table->string('status');
|
|
$table->integer('sort');
|
|
$table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
});
|
|
$schema->create('cloud_network_lines', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->integer('region_id');
|
|
$table->string('code');
|
|
$table->string('name');
|
|
$table->boolean('is_default');
|
|
$table->string('status');
|
|
$table->integer('sort');
|
|
$table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
});
|
|
$schema->create('cloud_resource_pools', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->integer('line_id');
|
|
$table->string('code');
|
|
$table->string('name');
|
|
$table->string('status');
|
|
$table->string('scheduling_policy');
|
|
$table->integer('sort');
|
|
$table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
});
|
|
$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');
|
|
$table->integer('safety_cpu_cores');
|
|
$table->integer('safety_memory_mib');
|
|
$table->integer('safety_disk_gib');
|
|
$table->integer('max_instances');
|
|
$table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
});
|
|
$schema->create('cloud_host_agents', static function (Blueprint $table): void {
|
|
$table->integer('host_id')->primary();
|
|
$table->text('sealed_api_secret');
|
|
$table->boolean('enabled');
|
|
$table->integer('revision');
|
|
$table->integer('tested_revision');
|
|
$table->string('last_test_status');
|
|
$table->string('last_error_code');
|
|
$table->string('last_agent_version');
|
|
$table->integer('last_tested_at');
|
|
$table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
});
|
|
$schema->create('cloud_host_storage_configurations',
|
|
static function (Blueprint $table): void {
|
|
$table->integer('host_id')->primary();
|
|
$table->integer('revision');
|
|
$table->text('managed_directory');
|
|
$table->text('template_directory');
|
|
$table->text('seed_directory');
|
|
$table->text('user_iso_directory');
|
|
$table->text('rescue_directory');
|
|
$table->string('state');
|
|
$table->string('operation_id')->nullable();
|
|
$table->integer('job_id')->nullable();
|
|
$table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
});
|
|
$schema->create('cloud_resource_reservations',
|
|
static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->integer('host_id');
|
|
$table->string('state');
|
|
$table->integer('instance_id')->nullable();
|
|
$table->integer('cpu_cores');
|
|
$table->integer('memory_mib');
|
|
$table->integer('disk_gib');
|
|
});
|
|
$schema->create('cloud_instances', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->integer('host_id');
|
|
$table->string('status');
|
|
$table->text('spec_json');
|
|
});
|
|
$schema->create('cloud_instance_snapshots',
|
|
static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->integer('instance_id');
|
|
$table->integer('host_id');
|
|
$table->string('state');
|
|
$table->integer('size_bytes')->nullable();
|
|
});
|
|
$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('sync_state');
|
|
$table->integer('observed_revision');
|
|
$table->integer('desired_revision');
|
|
});
|
|
$schema->create('cloud_public_ip_pools',
|
|
static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->integer('host_id');
|
|
$table->integer('network_attachment_id');
|
|
$table->string('state');
|
|
$table->string('sync_state');
|
|
$table->integer('observed_revision');
|
|
$table->integer('desired_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('state');
|
|
$table->integer('reservation_id')->nullable();
|
|
$table->integer('instance_id')->nullable();
|
|
$table->string('operation_id')->nullable();
|
|
});
|
|
|
|
$assert(!$schema->hasColumn('cloud_hosts', 'resource_pool_id')
|
|
&& !$schema->hasColumn('cloud_hosts', 'scheduler_state'),
|
|
'Host projection regression schema accidentally restored retired columns.');
|
|
|
|
Capsule::table('cloud_hosts')->insert([
|
|
['id' => 2, 'name' => 'Host B', 'region' => 'Tokyo', 'country_code' => 'JP',
|
|
'default_network' => 'bridge:br0', 'enabled' => 0, 'created_at' => 20,
|
|
'updated_at' => 20],
|
|
['id' => 1, 'name' => 'Host A', 'region' => 'HongKong', 'country_code' => 'HK',
|
|
'default_network' => 'bridge:br0', 'enabled' => 1, 'created_at' => 10,
|
|
'updated_at' => 10],
|
|
]);
|
|
Capsule::table('cloud_host_resource_pool_memberships')->insert([
|
|
['id' => 1, 'host_id' => 1, 'resource_pool_id' => 10,
|
|
'scheduler_state' => 'disabled', 'priority' => 0, 'weight' => 100,
|
|
'created_at' => 1, 'updated_at' => 300],
|
|
['id' => 2, 'host_id' => 1, 'resource_pool_id' => 11,
|
|
'scheduler_state' => 'active', 'priority' => 0, 'weight' => 100,
|
|
'created_at' => 1, 'updated_at' => 100],
|
|
['id' => 3, 'host_id' => 1, 'resource_pool_id' => 12,
|
|
'scheduler_state' => 'draining', 'priority' => 0, 'weight' => 100,
|
|
'created_at' => 1, 'updated_at' => 400],
|
|
]);
|
|
Capsule::table('cloud_regions')->insert([
|
|
'id' => 1, 'code' => 'hongkong', 'name' => 'HongKong',
|
|
'country_code' => 'HK', 'status' => 'active', 'sort' => 10,
|
|
'created_at' => 1, 'updated_at' => 1,
|
|
]);
|
|
Capsule::table('cloud_network_lines')->insert([
|
|
'id' => 1, 'region_id' => 1, 'code' => 'default', 'name' => '默认线路',
|
|
'is_default' => 1, 'status' => 'active', 'sort' => 10,
|
|
'created_at' => 1, 'updated_at' => 1,
|
|
]);
|
|
Capsule::table('cloud_resource_pools')->insert([
|
|
'id' => 11, 'line_id' => 1, 'code' => 'hkg', 'name' => 'HKG',
|
|
'status' => 'active', 'scheduling_policy' => 'balanced', 'sort' => 10,
|
|
'created_at' => 1, 'updated_at' => 1,
|
|
]);
|
|
Capsule::table('cloud_host_capacity_policies')->insert([
|
|
'host_id' => 1, 'configured_cpu_cores' => 32,
|
|
'configured_memory_mib' => 65536, 'configured_disk_gib' => 1000,
|
|
'safety_cpu_cores' => 2, 'safety_memory_mib' => 2048,
|
|
'safety_disk_gib' => 50, 'max_instances' => 100,
|
|
'created_at' => 1, 'updated_at' => 1,
|
|
]);
|
|
Capsule::table('cloud_host_agents')->insert([
|
|
'host_id' => 1, 'sealed_api_secret' => 'sealed', 'enabled' => 1,
|
|
'revision' => 2, 'tested_revision' => 2,
|
|
'last_test_status' => 'healthy', 'last_error_code' => '',
|
|
'last_agent_version' => 'v3', 'last_tested_at' => 100,
|
|
'created_at' => 1, 'updated_at' => 100,
|
|
]);
|
|
Capsule::table('cloud_host_storage_configurations')->insert([
|
|
'host_id' => 1, 'revision' => 1,
|
|
'managed_directory' => '/managed', 'template_directory' => '/templates',
|
|
'seed_directory' => '/seeds', 'user_iso_directory' => '/user-isos',
|
|
'rescue_directory' => '/rescue', 'state' => 'applied',
|
|
'operation_id' => null, 'job_id' => null,
|
|
'created_at' => 1, 'updated_at' => 1,
|
|
]);
|
|
Capsule::table('cloud_resource_reservations')->insert([
|
|
'id' => 1, 'host_id' => 1, 'state' => 'committed', 'instance_id' => 1,
|
|
'cpu_cores' => 2, 'memory_mib' => 2048, 'disk_gib' => 20,
|
|
]);
|
|
Capsule::table('cloud_instances')->insert([
|
|
'id' => 1, 'host_id' => 1, 'status' => 'active',
|
|
'spec_json' => json_encode([
|
|
'vcpu' => 2, 'memory_mib' => 2048, 'disk_gib' => 20,
|
|
], JSON_THROW_ON_ERROR),
|
|
]);
|
|
Capsule::table('cloud_host_network_attachments')->insert([
|
|
'id' => 1, 'host_id' => 1, 'line_id' => 1,
|
|
'network_selector' => 'bridge:br0', 'state' => 'active',
|
|
'sync_state' => 'applied', 'observed_revision' => 1,
|
|
'desired_revision' => 1,
|
|
]);
|
|
Capsule::table('cloud_public_ip_pools')->insert([
|
|
'id' => 1, 'host_id' => 1, 'network_attachment_id' => 1,
|
|
'state' => 'active', 'sync_state' => 'applied',
|
|
'observed_revision' => 1, 'desired_revision' => 1,
|
|
]);
|
|
Capsule::table('cloud_public_ips')->insert([
|
|
['id' => 1, 'host_id' => 1, 'public_ip_pool_id' => 1,
|
|
'state' => 'assigned', 'reservation_id' => 1, 'instance_id' => 1,
|
|
'operation_id' => null],
|
|
['id' => 2, 'host_id' => 1, 'public_ip_pool_id' => 1,
|
|
'state' => 'available', 'reservation_id' => null, 'instance_id' => null,
|
|
'operation_id' => null],
|
|
['id' => 3, 'host_id' => 1, 'public_ip_pool_id' => 1,
|
|
'state' => 'disabled', 'reservation_id' => null, 'instance_id' => null,
|
|
'operation_id' => null],
|
|
]);
|
|
|
|
$request = Request::createFromEnvironment(Environment::mock([
|
|
'REQUEST_METHOD' => 'GET', 'REQUEST_URI' => '/api/v1/admin/hosts',
|
|
]))->withAttribute('cloud_actor', [
|
|
'user_id' => 7, 'is_admin' => true, 'is_super_admin' => false,
|
|
'can_manage' => false,
|
|
]);
|
|
Capsule::connection('default')->enableQueryLog();
|
|
$response = (new CloudAdminController())->hosts($request, new Response());
|
|
$queries = count(Capsule::connection('default')->getQueryLog());
|
|
$payload = json_decode((string) $response->getBody(), true, 512, JSON_THROW_ON_ERROR);
|
|
|
|
$assert($response->getStatusCode() === 200
|
|
&& $response->getHeaderLine('Cache-Control') === 'no-store'
|
|
&& isset($payload['hosts'])
|
|
&& count($payload['hosts']) === 2,
|
|
'Administrator host list did not return the expected JSON collection.');
|
|
[$first, $second] = $payload['hosts'];
|
|
$assert($first['id'] === 1 && $first['name'] === 'Host A'
|
|
&& $first['enabled'] === true && $first['resource_pool_id'] === 11
|
|
&& $first['scheduler_state'] === 'active'
|
|
&& $first['placement']['region_name'] === 'HongKong'
|
|
&& $first['placement']['line_name'] === '默认线路'
|
|
&& $first['placement']['pool_name'] === 'HKG'
|
|
&& $first['agent']['state'] === 'ready'
|
|
&& $first['readiness']['placement_ready'] === true
|
|
&& $first['capacity']['instances']['allocated'] === 1
|
|
&& $first['capacity']['ip']['available'] === 1,
|
|
'Host list did not project the selected active membership authority.');
|
|
$assert($second['id'] === 2 && $second['name'] === 'Host B'
|
|
&& $second['enabled'] === false && $second['resource_pool_id'] === null
|
|
&& $second['scheduler_state'] === 'disabled'
|
|
&& $second['placement']['region_id'] === null
|
|
&& $second['needs_attention'] === true,
|
|
'Host without a membership did not receive an explicit disabled projection.');
|
|
$assert($payload['summary']['regions_total'] === 1
|
|
&& $payload['summary']['hosts_total'] === 2
|
|
&& $payload['summary']['agent_ready_hosts'] === 1
|
|
&& count($payload['regions']) === 2
|
|
&& $payload['regions'][0]['name'] === 'HongKong'
|
|
&& $payload['regions'][0]['summary']['hosts_total'] === 1
|
|
&& $payload['regions'][1]['id'] === null,
|
|
'Host list did not provide stable region groups and page summary data.');
|
|
$assert($first['agent']['secret_configured'] === true
|
|
&& $first['agent']['revision'] === 2
|
|
&& $first['agent']['tested_revision'] === 2
|
|
&& $first['agent']['updated_at'] === 100
|
|
&& !array_key_exists('sealed_api_secret', $first['agent']),
|
|
'Host projection did not expose safe Agent metadata or leaked its secret.');
|
|
|
|
Capsule::connection('default')->flushQueryLog();
|
|
$agentListing = CloudAgentSettingsService::listing();
|
|
$agentQueries = count(Capsule::connection('default')->getQueryLog());
|
|
$assert($agentListing['summary']['regions_total'] === 1
|
|
&& $agentListing['summary']['agents_total'] === 2
|
|
&& $agentListing['summary']['ready_agents'] === 1
|
|
&& $agentListing['summary']['unconfigured_agents'] === 1
|
|
&& $agentListing['summary']['attention_agents'] === 1
|
|
&& count($agentListing['regions']) === 2
|
|
&& $agentListing['regions'][0]['name'] === 'HongKong'
|
|
&& $agentListing['regions'][0]['agents'][0]['status'] === 'ready'
|
|
&& $agentListing['regions'][1]['agents'][0]['status'] === 'unconfigured'
|
|
&& !str_contains(json_encode($agentListing, JSON_THROW_ON_ERROR),
|
|
'sealed_api_secret'),
|
|
'Agent administrator projection lost grouping, status totals or secrecy.');
|
|
|
|
Capsule::table('cloud_hosts')->insert([
|
|
'id' => 3, 'name' => 'Host C', 'region' => 'Singapore',
|
|
'country_code' => 'SG', 'default_network' => 'bridge:br0', 'enabled' => 0,
|
|
'created_at' => 30, 'updated_at' => 30,
|
|
]);
|
|
Capsule::connection('default')->flushQueryLog();
|
|
$secondListResponse = (new CloudAdminController())->hosts(
|
|
$request, new Response()
|
|
);
|
|
$expandedQueries = count(Capsule::connection('default')->getQueryLog());
|
|
$expandedPayload = json_decode(
|
|
(string) $secondListResponse->getBody(), true, 512, JSON_THROW_ON_ERROR
|
|
);
|
|
$assert(count($expandedPayload['hosts']) === 3 && $queries === $expandedQueries,
|
|
'Host list introduced an N+1 query pattern: '
|
|
. $queries . ' versus ' . $expandedQueries . ' queries.');
|
|
Capsule::connection('default')->flushQueryLog();
|
|
$expandedAgentListing = CloudAgentSettingsService::listing();
|
|
$expandedAgentQueries = count(Capsule::connection('default')->getQueryLog());
|
|
$assert($expandedAgentListing['summary']['agents_total'] === 3
|
|
&& $agentQueries === $expandedAgentQueries,
|
|
'Agent listing introduced an N+1 query pattern: '
|
|
. $agentQueries . ' versus ' . $expandedAgentQueries . ' queries.');
|
|
|
|
$oneRequest = Request::createFromEnvironment(Environment::mock([
|
|
'REQUEST_METHOD' => 'GET', 'REQUEST_URI' => '/api/v1/admin/hosts/1',
|
|
]))->withAttribute('cloud_actor', [
|
|
'user_id' => 7, 'is_admin' => true, 'is_super_admin' => false,
|
|
'can_manage' => false,
|
|
]);
|
|
$oneResponse = (new CloudAdminController())->hosts(
|
|
$oneRequest, new Response(), ['id' => '1']
|
|
);
|
|
$one = json_decode((string) $oneResponse->getBody(), true, 512, JSON_THROW_ON_ERROR);
|
|
$assert($oneResponse->getStatusCode() === 200
|
|
&& $oneResponse->getHeaderLine('Cache-Control') === 'no-store'
|
|
&& $one['id'] === 1 && $one['resource_pool_id'] === 11
|
|
&& $one['scheduler_state'] === 'active',
|
|
'Single-host read drifted from the authoritative membership projection.');
|
|
|
|
echo "Administrator host list membership projection passed.\n";
|