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.
172 lines
6.3 KiB
PHTML
172 lines
6.3 KiB
PHTML
|
4 weeks ago
|
<?php
|
||
|
|
|
||
|
|
use App\Services\Cloud\CloudAdminResourceService;
|
||
|
|
use App\Services\Cloud\CloudError;
|
||
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
||
|
|
use Illuminate\Database\Schema\Blueprint;
|
||
|
|
|
||
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
||
|
|
|
||
|
|
$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('configurations', function (Blueprint $table): void {
|
||
|
|
$table->increments('id');
|
||
|
|
$table->string('name')->unique();
|
||
|
|
$table->text('value');
|
||
|
|
});
|
||
|
|
$schema->create('cloud_regions', function (Blueprint $table): void {
|
||
|
|
$table->increments('id');
|
||
|
|
$table->string('code')->unique();
|
||
|
|
$table->string('name');
|
||
|
|
$table->string('country_code');
|
||
|
|
$table->string('status');
|
||
|
|
$table->integer('sort')->default(0);
|
||
|
|
$table->integer('created_at');
|
||
|
|
$table->integer('updated_at');
|
||
|
|
});
|
||
|
|
$schema->create('cloud_network_lines', function (Blueprint $table): void {
|
||
|
|
$table->increments('id');
|
||
|
|
$table->integer('region_id');
|
||
|
|
$table->string('code');
|
||
|
|
$table->string('name');
|
||
|
|
$table->boolean('is_default')->default(false);
|
||
|
|
$table->string('status');
|
||
|
|
$table->integer('sort')->default(0);
|
||
|
|
$table->integer('created_at');
|
||
|
|
$table->integer('updated_at');
|
||
|
|
$table->unique(['region_id', 'code']);
|
||
|
|
});
|
||
|
|
$schema->create('cloud_resource_pools', 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')->default(0);
|
||
|
|
$table->integer('created_at');
|
||
|
|
$table->integer('updated_at');
|
||
|
|
});
|
||
|
|
$schema->create('cloud_offers', function (Blueprint $table): void {
|
||
|
|
$table->increments('id');
|
||
|
|
$table->integer('resource_pool_id');
|
||
|
|
});
|
||
|
|
$schema->create('cloud_host_resource_pool_memberships',
|
||
|
|
function (Blueprint $table): void {
|
||
|
|
$table->increments('id');
|
||
|
|
$table->integer('host_id');
|
||
|
|
$table->integer('resource_pool_id');
|
||
|
|
});
|
||
|
|
$schema->create('cloud_resource_reservations',
|
||
|
|
function (Blueprint $table): void {
|
||
|
|
$table->increments('id');
|
||
|
|
$table->integer('resource_pool_id');
|
||
|
|
$table->integer('host_id');
|
||
|
|
$table->string('state');
|
||
|
|
$table->integer('cpu_cores');
|
||
|
|
$table->integer('memory_mib');
|
||
|
|
$table->integer('disk_gib');
|
||
|
|
});
|
||
|
|
|
||
|
|
$capsule->connection('default')->table('configurations')->insert([
|
||
|
|
'name' => 'cloud_network_lines_enabled',
|
||
|
|
'value' => 'false',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$created = CloudAdminResourceService::saveRegion(null, [
|
||
|
|
'name' => '东京',
|
||
|
|
'country_code' => 'JP',
|
||
|
|
'status' => 'active',
|
||
|
|
'sort' => 0,
|
||
|
|
]);
|
||
|
|
$region = $created['item'];
|
||
|
|
$assert(preg_match('/^region-[a-f0-9]{8}$/D', $region['code']) === 1,
|
||
|
|
'Region create did not generate its immutable system identifier.');
|
||
|
|
$assert($region['status'] === 'active' && is_int($region['default_line_id']),
|
||
|
|
'A new active region did not expose its automatic default line.');
|
||
|
|
|
||
|
|
$line = $capsule->connection('default')->table('cloud_network_lines')
|
||
|
|
->where('region_id', $region['id'])->first();
|
||
|
|
$assert($line !== null && $line->code === 'default'
|
||
|
|
&& $line->name === '默认线路' && (int) $line->is_default === 1
|
||
|
|
&& $line->status === 'active',
|
||
|
|
'Hidden-line mode did not atomically create the unique active default line.');
|
||
|
|
|
||
|
|
$page = CloudAdminResourceService::page();
|
||
|
|
$assert($page['view'] === 'topology'
|
||
|
|
&& count($page['topology']) === 1
|
||
|
|
&& count($page['topology'][0]['lines']) === 1
|
||
|
|
&& $page['topology'][0]['default_line_id'] === (int) $line->id
|
||
|
|
&& $page['topology'][0]['effective_state'] === 'blocked'
|
||
|
|
&& $page['topology'][0]['effective_state_label'] === '暂不可调度',
|
||
|
|
'Topology did not expose the default line and honest no-pool health state.');
|
||
|
|
$assert(isset($page['summary']['regions_total'],
|
||
|
|
$page['summary']['placement_ready_pools'],
|
||
|
|
$page['summary']['placement_capacity'])
|
||
|
|
&& !array_key_exists('active_pools', $page['summary'])
|
||
|
|
&& !array_key_exists('schedulable_hosts', $page['summary']),
|
||
|
|
'Summary retained a retired or misleading clean-install field.');
|
||
|
|
|
||
|
|
$updated = CloudAdminResourceService::saveRegion((string) $region['id'], [
|
||
|
|
'name' => '东京一区',
|
||
|
|
'country_code' => 'JP',
|
||
|
|
'status' => 'active',
|
||
|
|
'sort' => 10,
|
||
|
|
]);
|
||
|
|
$assert($updated['item']['code'] === $region['code']
|
||
|
|
&& $updated['item']['name'] === '东京一区',
|
||
|
|
'Region edit did not preserve the immutable system identifier.');
|
||
|
|
|
||
|
|
$rejected = false;
|
||
|
|
try {
|
||
|
|
CloudAdminResourceService::saveRegion((string) $region['id'], [
|
||
|
|
'code' => 'replacement',
|
||
|
|
'name' => '东京一区',
|
||
|
|
'country_code' => 'JP',
|
||
|
|
'status' => 'active',
|
||
|
|
'sort' => 10,
|
||
|
|
]);
|
||
|
|
} catch (CloudError $error) {
|
||
|
|
$rejected = $error->httpStatus === 409;
|
||
|
|
}
|
||
|
|
$assert($rejected, 'Region PATCH accepted a system identifier field.');
|
||
|
|
|
||
|
|
CloudAdminResourceService::saveSettings(['network_lines_enabled' => true]);
|
||
|
|
$hiddenRegion = CloudAdminResourceService::saveRegion(null, [
|
||
|
|
'name' => '大阪',
|
||
|
|
'country_code' => 'JP',
|
||
|
|
'status' => 'disabled',
|
||
|
|
'sort' => 0,
|
||
|
|
]);
|
||
|
|
$assert($hiddenRegion['item']['default_line_id'] === null
|
||
|
|
&& $capsule->connection('default')->table('cloud_network_lines')
|
||
|
|
->where('region_id', $hiddenRegion['item']['id'])->count() === 0,
|
||
|
|
'Enabled-line mode unexpectedly created a hidden default line.');
|
||
|
|
CloudAdminResourceService::saveSettings(['network_lines_enabled' => false]);
|
||
|
|
$backfilledRegion = CloudAdminResourceService::region(
|
||
|
|
(string) $hiddenRegion['item']['id']
|
||
|
|
);
|
||
|
|
$backfilledLine = $capsule->connection('default')->table('cloud_network_lines')
|
||
|
|
->where('region_id', $hiddenRegion['item']['id'])->first();
|
||
|
|
$assert($backfilledLine !== null
|
||
|
|
&& $backfilledLine->code === 'default'
|
||
|
|
&& (int) $backfilledLine->is_default === 1
|
||
|
|
&& $backfilledLine->status === 'active'
|
||
|
|
&& $backfilledRegion['item']['default_line_id'] === (int) $backfilledLine->id,
|
||
|
|
'Disabling line selection did not atomically provision a usable default line for every region.');
|
||
|
|
|
||
|
|
echo "Administrator resource projection workflow passed.\n";
|