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.
147 lines
6.0 KiB
PHP
147 lines
6.0 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Services\Cloud\CloudAdminVpcCidrPoolService;
|
|
use App\Services\Cloud\CloudError;
|
|
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 $callback, string $message) use (
|
|
$assert
|
|
): void {
|
|
try {
|
|
$callback();
|
|
throw new RuntimeException($message);
|
|
} catch (CloudError $error) {
|
|
$assert($error->httpStatus === 409,
|
|
$message . ' (status ' . $error->httpStatus . ')');
|
|
}
|
|
};
|
|
|
|
$capsule = new Capsule();
|
|
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default');
|
|
$capsule->setAsGlobal();
|
|
$capsule->bootEloquent();
|
|
$schema = $capsule->schema('default');
|
|
$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_vpc_cidr_pools',
|
|
static function (Blueprint $table): void {
|
|
$table->increments('id'); $table->string('pool_key', 32)->unique();
|
|
$table->integer('region_id'); $table->string('name');
|
|
$table->string('cidr', 18); $table->integer('prefix_length');
|
|
$table->integer('allocation_prefix'); $table->string('state');
|
|
$table->integer('revision'); $table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
});
|
|
$schema->create('cloud_vpcs', static function (Blueprint $table): void {
|
|
$table->increments('id'); $table->string('vpc_key', 32);
|
|
$table->integer('user_id'); $table->integer('region_id');
|
|
$table->integer('cidr_pool_id'); $table->string('name');
|
|
$table->string('cidr', 18); $table->integer('prefix_length');
|
|
$table->string('gateway', 15); $table->integer('active_claim')->nullable();
|
|
$table->string('state'); $table->integer('desired_revision');
|
|
$table->integer('deleted_at')->nullable(); $table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
});
|
|
Capsule::table('cloud_regions')->insert([
|
|
['id' => 1, 'code' => 'hkg', 'name' => 'Hong Kong',
|
|
'country_code' => 'HK', 'status' => 'active', 'sort' => 0,
|
|
'created_at' => 1, 'updated_at' => 1],
|
|
['id' => 2, 'code' => 'nrt', 'name' => 'Tokyo',
|
|
'country_code' => 'JP', 'status' => 'active', 'sort' => 1,
|
|
'created_at' => 1, 'updated_at' => 1],
|
|
]);
|
|
|
|
$created = CloudAdminVpcCidrPoolService::create([
|
|
'request_key' => str_repeat('a', 32), 'region_id' => 1,
|
|
'name' => 'HKG Private', 'cidr' => '10.64.0.0/12',
|
|
'allocation_prefix' => 24, 'state' => 'active',
|
|
]);
|
|
$pool = $created['item'];
|
|
$assert($pool['revision'] === 1 && $pool['capacity'] === 4096
|
|
&& $pool['available'] === 4096 && $pool['address_locked'] === false,
|
|
'VPC pool capacity or initial revision is incorrect.');
|
|
|
|
$replay = CloudAdminVpcCidrPoolService::create([
|
|
'request_key' => str_repeat('a', 32), 'region_id' => 1,
|
|
'name' => 'HKG Private', 'cidr' => '10.64.0.0/12',
|
|
'allocation_prefix' => 24, 'state' => 'active',
|
|
]);
|
|
$assert($replay['item']['id'] === $pool['id']
|
|
&& Capsule::table('cloud_vpc_cidr_pools')->count() === 1,
|
|
'Idempotent VPC pool creation produced a duplicate.');
|
|
|
|
$expectConflict(static fn () => CloudAdminVpcCidrPoolService::create([
|
|
'request_key' => str_repeat('b', 32), 'region_id' => 1,
|
|
'name' => 'Overlap', 'cidr' => '10.64.16.0/20',
|
|
'allocation_prefix' => 24, 'state' => 'active',
|
|
]), 'Overlapping pools in one region were accepted.');
|
|
|
|
$otherRegion = CloudAdminVpcCidrPoolService::create([
|
|
'request_key' => str_repeat('c', 32), 'region_id' => 2,
|
|
'name' => 'NRT Private', 'cidr' => '10.64.0.0/12',
|
|
'allocation_prefix' => 24, 'state' => 'active',
|
|
]);
|
|
$assert($otherRegion['item']['region_id'] === 2,
|
|
'Identical private ranges in isolated regions were incorrectly rejected.');
|
|
|
|
try {
|
|
CloudAdminVpcCidrPoolService::create([
|
|
'request_key' => str_repeat('d', 32), 'region_id' => 1,
|
|
'name' => 'Noncanonical', 'cidr' => '10.80.1.1/16',
|
|
'allocation_prefix' => 24, 'state' => 'active',
|
|
]);
|
|
throw new RuntimeException('Noncanonical private CIDR was accepted.');
|
|
} catch (CloudError $error) {
|
|
$assert($error->httpStatus === 422,
|
|
'Noncanonical CIDR rejection used the wrong status.');
|
|
}
|
|
|
|
$updated = CloudAdminVpcCidrPoolService::update($pool['id'], [
|
|
'region_id' => 1, 'name' => 'HKG Primary', 'cidr' => '10.64.0.0/12',
|
|
'allocation_prefix' => 24, 'state' => 'active',
|
|
'expected_revision' => 1,
|
|
]);
|
|
$assert($updated['item']['revision'] === 2
|
|
&& $updated['item']['name'] === 'HKG Primary',
|
|
'VPC pool update did not advance the revision.');
|
|
|
|
Capsule::table('cloud_vpcs')->insert([
|
|
'id' => 1, 'vpc_key' => str_repeat('e', 32), 'user_id' => 7,
|
|
'region_id' => 1, 'cidr_pool_id' => $pool['id'], 'name' => 'prod',
|
|
'cidr' => '10.64.0.0/24', 'prefix_length' => 24,
|
|
'gateway' => '10.64.0.1', 'active_claim' => 1, 'state' => 'active',
|
|
'desired_revision' => 1, 'deleted_at' => null,
|
|
'created_at' => 2, 'updated_at' => 2,
|
|
]);
|
|
$expectConflict(static fn () => CloudAdminVpcCidrPoolService::update(
|
|
$pool['id'], [
|
|
'region_id' => 1, 'name' => 'HKG Primary',
|
|
'cidr' => '10.96.0.0/12', 'allocation_prefix' => 24,
|
|
'state' => 'active', 'expected_revision' => 2,
|
|
]
|
|
), 'Occupied VPC pool boundary was editable.');
|
|
|
|
$disabled = CloudAdminVpcCidrPoolService::update($pool['id'], [
|
|
'region_id' => 1, 'name' => 'HKG Primary', 'cidr' => '10.64.0.0/12',
|
|
'allocation_prefix' => 24, 'state' => 'disabled',
|
|
'expected_revision' => 2,
|
|
]);
|
|
$assert($disabled['item']['revision'] === 3
|
|
&& $disabled['item']['used'] === 1
|
|
&& $disabled['item']['address_locked'] === true,
|
|
'Occupied pool could not be safely disabled or usage was lost.');
|
|
|
|
echo "Administrator VPC CIDR pool service passed.\n";
|