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/CloudVpcServiceTest.php

238 lines
10 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudVpcService;
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);
}
};
$actor = static fn (string $key): array => [
'user_id' => 7, 'is_admin' => false, 'can_manage' => false,
'request_idempotency_key' => $key,
];
$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_network_lines', static function (Blueprint $table): void {
$table->increments('id'); $table->integer('region_id');
$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->boolean('enabled');
});
$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_vpc_transports',
static function (Blueprint $table): void {
$table->integer('host_id')->primary(); $table->string('sync_state');
$table->integer('desired_revision'); $table->integer('observed_revision');
});
$schema->create('cloud_vpc_cidr_pools',
static function (Blueprint $table): void {
$table->increments('id'); $table->string('pool_key', 32);
$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)->unique();
$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');
$table->unique(['region_id', 'cidr', 'active_claim']);
});
$schema->create('cloud_vpc_ports', static function (Blueprint $table): void {
$table->increments('id'); $table->integer('vpc_id');
$table->integer('active_claim')->nullable();
});
$schema->create('cloud_vpc_host_states',
static function (Blueprint $table): void {
$table->increments('id'); $table->integer('vpc_id');
});
$schema->create('cloud_vpc_membership_batches',
static function (Blueprint $table): void {
$table->increments('id'); $table->string('batch_key');
$table->integer('vpc_id'); $table->integer('user_id');
$table->integer('parent_job_id'); $table->string('kind');
$table->string('state'); $table->integer('desired_revision');
$table->integer('active_claim')->nullable();
$table->string('last_error_code')->default('');
$table->integer('terminal_at')->nullable();
$table->integer('created_at'); $table->integer('updated_at');
});
$schema->create('cloud_vpc_membership_batch_items',
static function (Blueprint $table): void {
$table->increments('id'); $table->integer('batch_id');
$table->integer('user_id'); $table->integer('instance_id');
$table->integer('vpc_port_id'); $table->integer('host_id');
$table->integer('child_job_id')->nullable(); $table->string('state');
$table->string('last_error_code')->default('');
$table->integer('created_at'); $table->integer('updated_at');
});
$schema->create('cloud_instances', static function (Blueprint $table): void {
$table->increments('id'); $table->string('uuid');
$table->integer('host_id'); $table->integer('user_id');
$table->integer('resource_pool_id')->nullable(); $table->string('name');
$table->string('status'); $table->string('service_status');
$table->string('network_status'); $table->integer('expires_at')->nullable();
$table->text('spec_json');
});
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,
]);
Capsule::table('cloud_network_lines')->insert([
'id' => 1, 'region_id' => 1, 'status' => 'active',
]);
Capsule::table('cloud_resource_pools')->insert([
'id' => 1, 'line_id' => 1, 'status' => 'active',
]);
Capsule::table('cloud_hosts')->insert(['id' => 1, 'enabled' => 1]);
Capsule::table('cloud_host_resource_pool_memberships')->insert([
'id' => 1, 'host_id' => 1, 'resource_pool_id' => 1,
'scheduler_state' => 'active',
]);
Capsule::table('cloud_host_vpc_transports')->insert([
'host_id' => 1, 'sync_state' => 'ready', 'desired_revision' => 1,
'observed_revision' => 1,
]);
Capsule::table('cloud_vpc_cidr_pools')->insert([
'id' => 1, 'pool_key' => str_repeat('a', 32), 'region_id' => 1,
'name' => 'HKG', 'cidr' => '10.64.0.0/16', 'prefix_length' => 16,
'allocation_prefix' => 24, 'state' => 'active', 'revision' => 1,
'created_at' => 1, 'updated_at' => 1,
]);
$first = CloudVpcService::create($actor('vpc-create-request-0001'), [
'name' => 'production', 'region_id' => 1,
]);
$assert($first['vpc']['cidr'] === '10.64.0.0/24'
&& $first['vpc']['gateway'] === '10.64.0.1'
&& $first['vpc']['state'] === 'active',
'First VPC did not receive the first canonical subnet and gateway.');
$replay = CloudVpcService::create($actor('vpc-create-request-0001'), [
'name' => 'production', 'region_id' => 1,
]);
$assert($replay['vpc']['id'] === $first['vpc']['id']
&& Capsule::table('cloud_vpcs')->count() === 1,
'VPC create idempotency replay allocated a second subnet.');
try {
CloudVpcService::create($actor('vpc-create-request-0001'), [
'name' => 'different', 'region_id' => 1,
]);
throw new RuntimeException('Reused VPC idempotency key changed intent.');
} catch (CloudError $error) {
$assert($error->httpStatus === 409,
'Changed VPC idempotency intent did not conflict.');
}
$second = CloudVpcService::create($actor('vpc-create-request-0002'), [
'name' => 'staging', 'region_id' => 1,
]);
$assert($second['vpc']['cidr'] === '10.64.1.0/24',
'VPC allocator did not select the first free gap.');
Capsule::table('cloud_vpc_ports')->insert([
'id' => 1, 'vpc_id' => $first['vpc']['id'], 'active_claim' => 1,
]);
try {
CloudVpcService::delete(['user_id' => 7], $first['vpc']['vpc_key']);
throw new RuntimeException('VPC with an active port was deleted.');
} catch (CloudError $error) {
$assert($error->httpStatus === 409,
'Active VPC port did not protect deletion.');
}
Capsule::table('cloud_vpc_ports')->where('id', 1)->delete();
$deleted = CloudVpcService::delete(
['user_id' => 7], $first['vpc']['vpc_key']
);
$assert($deleted['vpc']['state'] === 'deleted'
&& Capsule::table('cloud_vpcs')->where('id', $first['vpc']['id'])
->value('active_claim') === null,
'VPC deletion did not release its active CIDR claim.');
$third = CloudVpcService::create($actor('vpc-create-request-0003'), [
'name' => 'replacement', 'region_id' => 1,
]);
$assert($third['vpc']['cidr'] === '10.64.0.0/24',
'Released VPC CIDR was not safely reusable.');
$library = CloudVpcService::library(['user_id' => 7]);
$assert($library['used'] === 2 && count($library['items']) === 2
&& $library['instances'] === []
&& count($library['regions']) === 1
&& $library['regions'][0]['available'] === 254,
'VPC library or regional availability projection is incorrect.');
Capsule::table('cloud_host_vpc_transports')->where('host_id', 1)->update([
'observed_revision' => 0,
]);
$unverified = CloudVpcService::library(['user_id' => 7]);
$assert($unverified['regions'] === [],
'An unverified host transport still exposed its region for VPC creation.');
try {
CloudVpcService::create($actor('vpc-create-request-0004'), [
'name' => 'blocked-transport', 'region_id' => 1,
]);
throw new RuntimeException(
'VPC creation used a host transport whose revision was not verified.'
);
} catch (CloudError $error) {
$assert($error->httpStatus === 409,
'Unverified VPC transport did not use the expected conflict status.');
}
Capsule::table('cloud_host_vpc_transports')->where('host_id', 1)->update([
'observed_revision' => 1,
]);
Capsule::table('cloud_vpc_cidr_pools')->where('id', 1)->update([
'state' => 'disabled',
]);
$disabledPool = CloudVpcService::library(['user_id' => 7]);
$assert($disabledPool['regions'] === [],
'A disabled VPC address pool still exposed allocatable capacity.');
try {
CloudVpcService::create($actor('vpc-create-request-0005'), [
'name' => 'blocked-pool', 'region_id' => 1,
]);
throw new RuntimeException('VPC creation used a disabled address pool.');
} catch (CloudError $error) {
$assert($error->httpStatus === 409,
'Disabled VPC pool did not use the expected conflict status.');
}
echo "User VPC lifecycle and IPAM service passed.\n";