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/CloudAdminHostNetworkServic...

887 lines
33 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
declare(strict_types=1);
require dirname(__DIR__) . '/vendor/autoload.php';
$System_Config['cloud_data_key'] = base64_encode(str_repeat('n', 32));
use App\Models\CloudPublicIp;
use App\Models\CloudPublicIpPool;
use App\Services\Cloud\CloudAdminHostNetworkService;
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);
}
};
$expectCloudError = static function (callable $callable, int $status,
string $messagePart) use ($assert): CloudError {
try {
$callable();
} catch (CloudError $error) {
$assert($error->httpStatus === $status,
'Unexpected HTTP status: ' . $error->httpStatus);
$assert(str_contains($error->getMessage(), $messagePart),
'Unexpected Cloud error: ' . $error->getMessage());
return $error;
}
throw new RuntimeException('Expected CloudError was not thrown.');
};
$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->boolean('enabled')->default(true);
$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->text('network_selectors_json');
$table->text('public_network_capabilities_json');
$table->integer('last_tested_at');
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_public_ip_pool_catalog_mutex',
static function (Blueprint $table): void {
$table->integer('id')->primary();
$table->integer('updated_at')->default(0);
});
$schema->create('cloud_network_lines', static function (Blueprint $table): void {
$table->increments('id');
$table->string('name');
$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_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')->default(0);
$table->integer('weight')->default(100);
$table->integer('revision')->default(1);
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_host_network_attachments',
static function (Blueprint $table): void {
$table->increments('id');
$table->string('attachment_key');
$table->integer('host_id');
$table->integer('line_id');
$table->string('name');
$table->string('network_mode');
$table->string('network_selector');
$table->integer('vlan_id')->nullable();
$table->integer('routing_table')->nullable();
$table->string('state');
$table->integer('priority');
$table->integer('config_revision');
$table->integer('verified_agent_revision');
$table->string('verification_state');
$table->integer('last_verified_at');
$table->string('verification_error_code');
$table->integer('desired_revision');
$table->integer('observed_revision');
$table->string('sync_state');
$table->string('last_error_code');
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_public_ip_pools',
static function (Blueprint $table): void {
$table->increments('id');
$table->string('pool_key');
$table->integer('host_id');
$table->integer('network_attachment_id');
$table->integer('address_family');
$table->string('name');
$table->string('cidr');
$table->string('gateway');
$table->boolean('gateway_on_link')->default(false);
$table->integer('guest_prefix_length');
$table->text('dns_servers_json');
$table->text('reserved_ips_json');
$table->string('allocation_start')->nullable();
$table->string('allocation_end')->nullable();
$table->string('allocation_cursor')->nullable();
$table->integer('max_allocations')->default(0);
$table->string('state');
$table->integer('priority');
$table->integer('desired_revision');
$table->integer('observed_revision');
$table->string('sync_state');
$table->string('last_error_code');
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_public_ips', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('public_ip_pool_id');
$table->integer('address_family');
$table->string('address');
$table->binary('address_binary');
$table->integer('prefix_length');
$table->string('state');
$table->integer('reservation_id')->nullable()->unique();
$table->integer('instance_id')->nullable();
$table->string('operation_id')->nullable()->unique();
$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->integer('public_ip_pool_id')->nullable();
$table->integer('instance_id')->nullable();
$table->string('state');
});
$schema->create('cloud_public_ip_pool_replacement_batches',
static function (Blueprint $table): void {
$table->increments('id');
$table->string('batch_key');
$table->integer('host_id');
$table->integer('source_public_ip_pool_id');
$table->integer('target_public_ip_pool_id');
$table->string('source_initial_state');
$table->integer('parent_job_id');
$table->integer('requested_by_user_id');
$table->string('state');
$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_public_ip_pool_replacement_batch_items',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('batch_id');
$table->integer('instance_id');
$table->integer('child_job_id');
$table->string('state');
$table->string('last_error_code')->default('');
});
$now = time();
Capsule::table('cloud_public_ip_pool_catalog_mutex')->insert([
'id' => 1, 'updated_at' => 0,
]);
$hostId = Capsule::table('cloud_hosts')->insertGetId([
'name' => 'Host 1', 'enabled' => 1,
'created_at' => $now, 'updated_at' => $now,
]);
$foreignHostId = Capsule::table('cloud_hosts')->insertGetId([
'name' => 'Host 2', 'enabled' => 1,
'created_at' => $now, 'updated_at' => $now,
]);
$lineId = Capsule::table('cloud_network_lines')->insertGetId([
'name' => 'China optimized', 'status' => 'active',
]);
$otherLineId = Capsule::table('cloud_network_lines')->insertGetId([
'name' => 'No membership', 'status' => 'active',
]);
$resourcePoolId = Capsule::table('cloud_resource_pools')->insertGetId([
'line_id' => $lineId, 'status' => 'active',
]);
Capsule::table('cloud_host_resource_pool_memberships')->insert([
'host_id' => $hostId, 'resource_pool_id' => $resourcePoolId,
'scheduler_state' => 'active', 'priority' => 0, 'weight' => 100,
'created_at' => $now, 'updated_at' => $now,
]);
$selectors = ['bridge:br0', 'bridge:br1', 'bridge:br2'];
Capsule::table('cloud_host_agents')->insert([
'host_id' => $hostId, 'sealed_api_secret' => 'sealed', 'enabled' => 1,
'revision' => 1, 'tested_revision' => 1,
'last_test_status' => 'healthy', 'last_error_code' => '',
'last_agent_version' => 'test',
'network_selectors_json' => json_encode($selectors, JSON_THROW_ON_ERROR),
'public_network_capabilities_json' =>
'{"contract":1,"dual_stack_required":true,"project_owned_filter":true,"ndp_guard":true}',
'last_tested_at' => $now, 'created_at' => $now, 'updated_at' => $now,
]);
$selector = 'bridge:br0';
$attachmentId = Capsule::table('cloud_host_network_attachments')->insertGetId([
'attachment_key' => substr(hash('sha256', $selector), 0, 32),
'host_id' => $hostId, 'line_id' => $lineId,
'name' => $selector, 'network_mode' => 'selector',
'network_selector' => $selector, 'vlan_id' => null,
'routing_table' => null, 'state' => 'active', 'priority' => 0,
'config_revision' => 1, 'verified_agent_revision' => 1,
'verification_state' => 'verified', 'last_verified_at' => $now,
'verification_error_code' => '',
'desired_revision' => 1, 'observed_revision' => 1,
'sync_state' => 'applied', 'last_error_code' => '',
'created_at' => $now, 'updated_at' => $now,
]);
$attachmentBId = Capsule::table(
'cloud_host_network_attachments'
)->insertGetId([
'attachment_key' => str_repeat('c', 32),
'host_id' => $hostId, 'line_id' => $lineId,
'name' => 'Secondary', 'network_mode' => 'selector',
'network_selector' => 'bridge:br1', 'vlan_id' => null,
'routing_table' => null, 'state' => 'active', 'priority' => 10,
'config_revision' => 1, 'verified_agent_revision' => 1,
'verification_state' => 'verified', 'last_verified_at' => $now,
'verification_error_code' => '',
'desired_revision' => 1, 'observed_revision' => 1,
'sync_state' => 'applied', 'last_error_code' => '',
'created_at' => $now, 'updated_at' => $now,
]);
$noMembershipAttachmentId = Capsule::table(
'cloud_host_network_attachments'
)->insertGetId([
'attachment_key' => str_repeat('d', 32),
'host_id' => $hostId, 'line_id' => $otherLineId,
'name' => 'No membership', 'network_mode' => 'selector',
'network_selector' => 'bridge:br2', 'vlan_id' => null,
'routing_table' => null, 'state' => 'active', 'priority' => 20,
'config_revision' => 1, 'verified_agent_revision' => 1,
'verification_state' => 'verified', 'last_verified_at' => $now,
'verification_error_code' => '',
'desired_revision' => 1, 'observed_revision' => 1,
'sync_state' => 'applied', 'last_error_code' => '',
'created_at' => $now, 'updated_at' => $now,
]);
$foreignAttachmentId = Capsule::table(
'cloud_host_network_attachments'
)->insertGetId([
'attachment_key' => str_repeat('e', 32),
'host_id' => $foreignHostId, 'line_id' => $lineId,
'name' => 'Foreign', 'network_mode' => 'selector',
'network_selector' => 'bridge:foreign', 'vlan_id' => null,
'routing_table' => null, 'state' => 'active', 'priority' => 0,
'config_revision' => 1, 'verified_agent_revision' => 1,
'verification_state' => 'verified', 'last_verified_at' => $now,
'verification_error_code' => '',
'desired_revision' => 1, 'observed_revision' => 1,
'sync_state' => 'applied', 'last_error_code' => '',
'created_at' => $now, 'updated_at' => $now,
]);
$poolKey = str_repeat('a', 32);
$poolId = Capsule::table('cloud_public_ip_pools')->insertGetId([
'pool_key' => $poolKey, 'host_id' => $hostId,
'network_attachment_id' => $attachmentId, 'address_family' => 4,
'name' => 'Public pool',
'cidr' => '8.8.8.0/29', 'gateway' => '8.8.8.1',
'gateway_on_link' => 0,
'guest_prefix_length' => 29, 'dns_servers_json' => '["1.1.1.1"]',
'reserved_ips_json' => '[]', 'allocation_start' => null,
'allocation_end' => null, 'allocation_cursor' => null,
'max_allocations' => 0, 'state' => 'active', 'priority' => 0,
'desired_revision' => 1, 'observed_revision' => 1,
'sync_state' => 'applied', 'last_error_code' => '',
'created_at' => $now, 'updated_at' => $now,
]);
Capsule::table('cloud_public_ips')->insert([
[
'host_id' => $hostId, 'public_ip_pool_id' => $poolId,
'address_family' => 4, 'address' => '8.8.8.2',
'address_binary' => inet_pton('8.8.8.2'), 'prefix_length' => 29,
'state' => 'assigned', 'reservation_id' => 41,
'instance_id' => 51, 'operation_id' => str_repeat('b', 32),
'created_at' => $now, 'updated_at' => $now,
],
[
'host_id' => $hostId, 'public_ip_pool_id' => $poolId,
'address_family' => 4, 'address' => '8.8.8.3',
'address_binary' => inet_pton('8.8.8.3'), 'prefix_length' => 29,
'state' => 'available', 'reservation_id' => null,
'instance_id' => null, 'operation_id' => null,
'created_at' => $now, 'updated_at' => $now,
],
]);
$actor = [
'user_id' => 1, 'is_admin' => true, 'is_super_admin' => true,
'can_manage' => true, 'session_id' => 'network-pool-test',
];
$body = static fn (
int $revision,
array $reserved,
string $dns = '1.1.1.1',
?int $targetAttachmentId = null,
string $state = 'active'
) => [
'network_attachment_id' => $targetAttachmentId ?? $attachmentId,
'address_family' => 4, 'name' => 'Public pool', 'cidr' => '8.8.8.0/29',
'gateway' => '8.8.8.1', 'dns_servers' => [$dns],
'reserved_ips' => $reserved, 'guest_prefix_length' => 29,
'gateway_on_link' => false, 'allocation_start' => null,
'allocation_end' => null, 'max_allocations' => 0,
'state' => $state, 'expected_revision' => $revision,
];
$service = new CloudAdminHostNetworkService();
$saved = $service->update(
$hostId, $poolKey, $actor, $body(1, ['8.8.8.3'])
);
$pool = CloudPublicIpPool::find($poolId);
$assigned = CloudPublicIp::where('address', '8.8.8.2')->first();
$disabled = CloudPublicIp::where('address', '8.8.8.3')->first();
$assert(($saved['saved_pool_key'] ?? null) === $poolKey
&& !array_key_exists('default_network', $saved['host'])
&& count($saved['attachments']) === 3
&& (int) $saved['items'][0]['network_attachment_id'] === $attachmentId
&& (int) $saved['items'][0]['attachment']['id'] === $attachmentId
&& (int) $saved['items'][0]['line']['id'] === $lineId
&& (int) $pool->desired_revision === 2
&& json_decode((string) $pool->reserved_ips_json, true) === ['8.8.8.3']
&& (string) $assigned->state === 'assigned'
&& (int) $assigned->instance_id === 51
&& (string) $disabled->state === 'disabled',
'An unoccupied IP was not disabled without disturbing the assigned IP.');
Capsule::table('cloud_host_resource_pool_memberships')
->where('host_id', $hostId)->update(['scheduler_state' => 'disabled']);
$withoutMembership = $service->read($hostId, $actor);
$assert($withoutMembership['attachments'][0]['ready'] === false,
'Attachment remained ready without an active line membership.');
Capsule::table('cloud_host_resource_pool_memberships')
->where('host_id', $hostId)->update(['scheduler_state' => 'active']);
$expectCloudError(
static fn () => $service->update(
$hostId, $poolKey, $actor, $body(2, ['8.8.8.2', '8.8.8.3'])
),
409,
'已分配或任务占用的公网 IP 不能加入保留 / 禁用列表:8.8.8.2'
);
$pool->refresh();
$assigned->refresh();
$assert((int) $pool->desired_revision === 2
&& json_decode((string) $pool->reserved_ips_json, true) === ['8.8.8.3']
&& (string) $assigned->state === 'assigned',
'Rejecting an occupied disabled IP changed the pool or assigned ledger row.');
$expectCloudError(
static fn () => $service->update(
$hostId, $poolKey, $actor, $body(2, ['8.8.8.3'], '9.9.9.9')
),
409,
'只能修改名称、运行状态和未占用的保留 / 禁用 IP'
);
$pool->refresh();
$assert((int) $pool->desired_revision === 2
&& (string) $pool->dns_servers_json === '["1.1.1.1"]',
'An occupied pool accepted a DNS identity change.');
$service->update($hostId, $poolKey, $actor, $body(2, []));
$pool->refresh();
$disabled->refresh();
$assert((int) $pool->desired_revision === 3
&& json_decode((string) $pool->reserved_ips_json, true) === []
&& (string) $disabled->state === 'available',
'Removing an unoccupied disabled IP did not restore its availability.');
$expectCloudError(
static fn () => $service->update(
$hostId,
$poolKey,
$actor,
$body(3, [], '1.1.1.1', $attachmentBId)
),
409,
'只能修改名称、运行状态和未占用的保留 / 禁用 IP'
);
$pool->refresh();
$assert((int) $pool->network_attachment_id === $attachmentId
&& (int) $pool->desired_revision === 3,
'An occupied pool switched to a different attachment.');
$expectCloudError(
static fn () => $service->update(
$hostId,
$poolKey,
$actor,
$body(2, [], '1.1.1.1', $foreignAttachmentId)
),
409,
'版本已变化'
);
$expectCloudError(
static fn () => $service->update(
$hostId,
$poolKey,
$actor,
$body(3, [], '1.1.1.1', $foreignAttachmentId)
),
409,
'不存在或不属于该宿主机'
);
$expectCloudError(
static fn () => $service->update(
$hostId,
$poolKey,
$actor,
$body(3, [], '1.1.1.1', $noMembershipAttachmentId)
),
409,
'没有启用中的资源池成员关系'
);
$createBody = [
'network_attachment_id' => $attachmentBId,
'address_family' => 4,
'name' => 'Second public pool',
'cidr' => '8.8.4.0/29',
'gateway' => '8.8.4.1',
'dns_servers' => ['1.1.1.1'],
'reserved_ips' => [],
'guest_prefix_length' => 29,
'gateway_on_link' => false,
'allocation_start' => null,
'allocation_end' => null,
'max_allocations' => 0,
'state' => 'active',
'request_key' => str_repeat('f', 32),
];
$created = $service->create($hostId, $actor, $createBody);
$createdPoolKey = (string) $created['saved_pool_key'];
$createdPool = CloudPublicIpPool::where(
'pool_key', $createdPoolKey
)->first();
$replayed = $service->create($hostId, $actor, $createBody);
$assert($createdPool !== null
&& (int) $createdPool->network_attachment_id === $attachmentBId
&& (int) $createdPool->desired_revision === 1
&& (string) $createdPool->sync_state === 'applied'
&& (int) $createdPool->observed_revision === 1
&& (string) $replayed['saved_pool_key'] === $createdPoolKey
&& CloudPublicIpPool::where('pool_key', $createdPoolKey)->count() === 1,
'Create/replay did not preserve one applied attachment-bound pool.');
Capsule::table('cloud_public_ip_pools')->insert([
'pool_key' => str_repeat('e', 32), 'host_id' => $foreignHostId,
'network_attachment_id' => $foreignAttachmentId, 'address_family' => 4,
'name' => 'Foreign public pool', 'cidr' => '9.9.8.0/29',
'gateway' => '9.9.8.1', 'gateway_on_link' => 0,
'guest_prefix_length' => 29, 'dns_servers_json' => '["1.1.1.1"]',
'reserved_ips_json' => '[]', 'allocation_start' => null,
'allocation_end' => null, 'allocation_cursor' => null,
'max_allocations' => 0, 'state' => 'active', 'priority' => 0,
'desired_revision' => 1, 'observed_revision' => 1,
'sync_state' => 'applied', 'last_error_code' => '',
'created_at' => $now, 'updated_at' => $now,
]);
$expectCloudError(
static fn () => $service->create(
$hostId,
$actor,
array_replace($createBody, [
'request_key' => str_repeat('d', 32),
'cidr' => '9.9.8.0/29',
'gateway' => '9.9.8.1',
])
),
409,
'地址段已被其他宿主机占用'
);
$ipv6Body = [
'network_attachment_id' => $attachmentId,
'address_family' => 6,
'name' => 'Public IPv6 pool',
'cidr' => '2404:c140:156:3a::/64',
'gateway' => '2404:c140:156::1',
'gateway_on_link' => true,
'dns_servers' => ['2606:4700:4700::1111'],
'reserved_ips' => ['2404:c140:156:3a::2'],
'allocation_start' => '2404:c140:156:3a::1000',
'allocation_end' => '2404:c140:156:3a::1fff',
'max_allocations' => 100,
'guest_prefix_length' => 64,
'state' => 'active',
'request_key' => str_repeat('6', 32),
];
$expectCloudError(
static fn () => $service->create($hostId, $actor, $ipv6Body),
409,
'启用地址池必须使用完全相同且顺序一致的 DNS'
);
$assert(CloudPublicIpPool::where('host_id', $hostId)
->where('address_family', 6)->count() === 0,
'A mismatched active IPv6 DNS contract created a partial pool.');
$ipv6Body['dns_servers'] = ['1.1.1.1'];
$ipv6Created = $service->create($hostId, $actor, $ipv6Body);
$ipv6Pool = CloudPublicIpPool::where(
'pool_key', (string) $ipv6Created['saved_pool_key']
)->first();
$ipv6Item = array_values(array_filter(
$ipv6Created['items'],
static fn (array $item): bool =>
(string) $item['pool_key'] === (string) $ipv6Created['saved_pool_key']
))[0] ?? null;
$assert($ipv6Pool !== null
&& (int) $ipv6Pool->address_family === 6
&& (bool) $ipv6Pool->gateway_on_link === true
&& (string) $ipv6Pool->allocation_start === '2404:c140:156:3a::1000'
&& (string) $ipv6Pool->allocation_end === '2404:c140:156:3a::1fff'
&& (int) $ipv6Pool->max_allocations === 100
&& CloudPublicIp::where('public_ip_pool_id', (int) $ipv6Pool->id)->count() === 0
&& is_array($ipv6Item)
&& (int) $ipv6Item['available_count'] === 100
&& $ipv6Item['dual_stack_ready'] === true
&& $ipv6Item['attachment']['dual_stack_ready'] === true,
'IPv6 /64 was materialized or its bounded sparse capacity was not exposed.');
$publicCapacity = $ipv6Item['attachment']['public_network_capacity'];
$assert((int) $publicCapacity['bundles']['available'] === 4
&& (string) $publicCapacity['bundles']['unit'] === 'dual_stack_bundle',
'Host network did not expose the canonical dual-stack bundle capacity.');
$assert((int) $publicCapacity['incomplete_claims']['allocated'] === 1,
'A single-family allocation was not exposed as an incomplete identity.');
$expectCloudError(
static fn () => $service->update(
$hostId,
(string) $ipv6Created['saved_pool_key'],
$actor,
array_replace($ipv6Body, [
'dns_servers' => ['9.9.9.9'],
'expected_revision' => 1,
])
),
409,
'启用地址池必须使用完全相同且顺序一致的 DNS'
);
$ipv6Pool->refresh();
$assert((int) $ipv6Pool->desired_revision === 1
&& (string) $ipv6Pool->dns_servers_json === '["1.1.1.1"]',
'Rejecting a mismatched paired DNS update changed the IPv6 pool.');
$blockedRangeBody = array_replace($ipv6Body, [
'name' => 'Blocked sparse range',
'cidr' => '2404:c140:156:3b::/120',
'gateway' => '2404:c140:156:3b::10',
'dns_servers' => ['1.1.1.1'],
'reserved_ips' => ['2404:c140:156:3b::10'],
'allocation_start' => '2404:c140:156:3b::10',
'allocation_end' => '2404:c140:156:3b::13',
'max_allocations' => 4,
'guest_prefix_length' => 120,
'request_key' => str_repeat('b', 32),
]);
$expectCloudError(
static fn () => $service->create($hostId, $actor, $blockedRangeBody),
422,
'扣除网关、DNS 和保留地址后小于最大分配数量'
);
$deduplicatedBlockers = $service->create(
$hostId,
$actor,
array_replace($blockedRangeBody, ['max_allocations' => 3])
);
$deduplicatedPool = CloudPublicIpPool::where(
'pool_key', (string) $deduplicatedBlockers['saved_pool_key']
)->first();
$assert($deduplicatedPool !== null
&& (int) $deduplicatedPool->max_allocations === 3,
'The same blocked IPv6 address was deducted more than once across lists.');
$outsideRange = $service->create($hostId, $actor, array_replace(
$blockedRangeBody,
[
'name' => 'External gateway and DNS',
'cidr' => '2404:c140:156:3c::/120',
'gateway' => '2404:c140:156::1',
'gateway_on_link' => true,
'dns_servers' => ['1.1.1.1'],
'reserved_ips' => ['2404:c140:156:3c::20'],
'allocation_start' => '2404:c140:156:3c::10',
'allocation_end' => '2404:c140:156:3c::13',
'max_allocations' => 4,
'request_key' => str_repeat('c', 32),
]
));
$outsideRangePool = CloudPublicIpPool::where(
'pool_key', (string) $outsideRange['saved_pool_key']
)->first();
$assert($outsideRangePool !== null
&& (int) $outsideRangePool->max_allocations === 4,
'An off-prefix gateway, other-family DNS or out-of-range reservation reduced sparse capacity.');
Capsule::table('cloud_public_ips')->insert([
[
'host_id' => $hostId, 'public_ip_pool_id' => (int) $ipv6Pool->id,
'address_family' => 6, 'address' => '2404:c140:156:3a::1000',
'address_binary' => inet_pton('2404:c140:156:3a::1000'),
'prefix_length' => 64, 'state' => 'reserved',
'reservation_id' => 61, 'instance_id' => null,
'operation_id' => str_repeat('7', 32),
'created_at' => $now, 'updated_at' => $now,
],
[
'host_id' => $hostId, 'public_ip_pool_id' => (int) $ipv6Pool->id,
'address_family' => 6, 'address' => '2404:c140:156:3a::1001',
'address_binary' => inet_pton('2404:c140:156:3a::1001'),
'prefix_length' => 64, 'state' => 'changing',
'reservation_id' => 62, 'instance_id' => 62,
'operation_id' => str_repeat('8', 32),
'created_at' => $now, 'updated_at' => $now,
],
[
'host_id' => $hostId, 'public_ip_pool_id' => (int) $ipv6Pool->id,
'address_family' => 6, 'address' => '2404:c140:156:3a::1002',
'address_binary' => inet_pton('2404:c140:156:3a::1002'),
'prefix_length' => 64, 'state' => 'uncertain',
'reservation_id' => 63, 'instance_id' => null,
'operation_id' => str_repeat('9', 32),
'created_at' => $now, 'updated_at' => $now,
],
[
'host_id' => $hostId, 'public_ip_pool_id' => (int) $ipv6Pool->id,
'address_family' => 6, 'address' => '2404:c140:156:3a::1003',
'address_binary' => inet_pton('2404:c140:156:3a::1003'),
'prefix_length' => 64, 'state' => 'available',
'reservation_id' => null, 'instance_id' => null,
'operation_id' => str_repeat('a', 32),
'created_at' => $now, 'updated_at' => $now,
],
]);
$occupiedIpv6Read = $service->read($hostId, $actor);
$occupiedIpv6Item = array_values(array_filter(
$occupiedIpv6Read['items'],
static fn (array $item): bool =>
(string) $item['pool_key'] === (string) $ipv6Created['saved_pool_key']
))[0] ?? null;
$assert(is_array($occupiedIpv6Item)
&& (int) $occupiedIpv6Item['occupied_count'] === 4
&& (int) $occupiedIpv6Item['available_count'] === 96,
'IPv6 sparse capacity did not deduct every occupied state or pointer.');
$expectCloudError(
static fn () => $service->update(
$hostId,
(string) $ipv6Created['saved_pool_key'],
$actor,
array_replace($ipv6Body, [
'reserved_ips' => [
'2404:c140:156:3a::2',
'2404:c140:156:3a::1000',
'2404:c140:156:3a::1001',
'2404:c140:156:3a::1002',
'2404:c140:156:3a::1003',
],
'expected_revision' => 1,
])
),
409,
'已分配或任务占用的公网 IP 不能加入保留 / 禁用列表'
);
$expectCloudError(
static fn () => $service->create(
$hostId,
$actor,
array_replace($createBody, [
'network_attachment_id' => $attachmentId,
])
),
409,
'同一创建请求标识不能用于不同的公网地址池配置'
);
$switched = $service->update(
$hostId,
$createdPoolKey,
$actor,
[
...$createBody,
'network_attachment_id' => $attachmentId,
'expected_revision' => 1,
]
);
$createdPool->refresh();
$assert((int) $createdPool->network_attachment_id === $attachmentId
&& (int) $createdPool->desired_revision === 2
&& (int) $switched['items'][1]['attachment']['id'] === $attachmentId,
'An unoccupied pool could not switch its explicit attachment.');
$expectCloudError(
static fn () => $service->update(
$hostId,
$createdPoolKey,
$actor,
array_replace($ipv6Body, [
'name' => 'Converted IPv6 pool',
'cidr' => '2404:c140:156:3b::/64',
'reserved_ips' => ['2404:c140:156:3b::2'],
'allocation_start' => '2404:c140:156:3b::1000',
'allocation_end' => '2404:c140:156:3b::1fff',
'expected_revision' => 2,
])
),
409,
'地址族创建后不能修改'
);
$createdPool->refresh();
$assert((int) $createdPool->address_family === 4
&& CloudPublicIp::where('public_ip_pool_id', (int) $createdPool->id)
->where('address_family', 4)->count() > 0,
'Rejecting an address-family change damaged the concrete ledger.');
Capsule::table('cloud_public_ip_pools')->where('id', $createdPool->id)
->update(['network_attachment_id' => 999999]);
$missingRead = $service->read($hostId, $actor);
$missingItem = array_values(array_filter(
$missingRead['items'],
static fn (array $item): bool =>
(string) $item['pool_key'] === $createdPoolKey
))[0] ?? null;
$assert(is_array($missingItem)
&& $missingItem['attachment']['state'] === 'missing'
&& $missingItem['line'] === null,
'A missing attachment made the read model unavailable.');
Capsule::table('cloud_public_ip_pools')->where('id', $createdPool->id)
->update(['network_attachment_id' => $attachmentId]);
Capsule::table('cloud_host_network_attachments')
->where('id', $attachmentBId)->update(['state' => 'disabled']);
$expectCloudError(
static fn () => $service->create(
$hostId,
$actor,
array_replace($createBody, [
'request_key' => str_repeat('1', 32),
'cidr' => '8.8.5.0/29',
'gateway' => '8.8.5.1',
])
),
409,
'网络接入未启用'
);
Capsule::table('cloud_host_network_attachments')
->where('id', $attachmentBId)->update([
'state' => 'active', 'verification_state' => 'stale',
]);
$expectCloudError(
static fn () => $service->create(
$hostId,
$actor,
array_replace($createBody, [
'request_key' => str_repeat('2', 32),
'cidr' => '8.8.6.0/29',
'gateway' => '8.8.6.1',
])
),
409,
'没有当前 Agent 版本的有效验证证据'
);
Capsule::table('cloud_host_network_attachments')
->where('id', $attachmentBId)->update([
'verification_state' => 'verified',
]);
Capsule::table('cloud_host_agents')->where('host_id', $hostId)->update([
'revision' => 2, 'tested_revision' => 2,
]);
$expectCloudError(
static fn () => $service->create(
$hostId,
$actor,
array_replace($createBody, [
'request_key' => str_repeat('3', 32),
'cidr' => '8.8.7.0/29',
'gateway' => '8.8.7.1',
])
),
409,
'没有当前 Agent 版本的有效验证证据'
);
Capsule::table('cloud_host_network_attachments')
->where('id', $attachmentBId)->update([
'verified_agent_revision' => 2,
]);
Capsule::table('cloud_host_agents')->where('host_id', $hostId)->update([
'network_selectors_json' => json_encode(
['bridge:br0'], JSON_THROW_ON_ERROR
),
]);
$expectCloudError(
static fn () => $service->create(
$hostId,
$actor,
array_replace($createBody, [
'request_key' => str_repeat('4', 32),
'cidr' => '8.8.9.0/29',
'gateway' => '8.8.9.1',
])
),
409,
'没有当前 Agent 版本的有效验证证据'
);
Capsule::table('cloud_public_ip_pool_replacement_batches')->insert([
'batch_key' => str_repeat('9', 32),
'host_id' => $hostId,
'source_public_ip_pool_id' => $poolId,
'target_public_ip_pool_id' => (int) $createdPool->id,
'source_initial_state' => 'active',
'parent_job_id' => 9001,
'requested_by_user_id' => 1,
'state' => 'succeeded',
'active_claim' => null,
'last_error_code' => '',
'terminal_at' => $now,
'created_at' => $now,
'updated_at' => $now,
]);
$historyRead = $service->read($hostId, $actor);
$historyItem = array_values(array_filter(
$historyRead['items'],
static fn (array $item): bool =>
(string) $item['pool_key'] === $poolKey
))[0] ?? null;
$assert(is_array($historyItem)
&& $historyItem['replacement_history_count'] === 1
&& $historyItem['has_replacement_history'] === true,
'Terminal replacement history was not exposed on the pool DTO.');
Capsule::table('cloud_host_agents')->where('host_id', $hostId)->update([
'public_network_capabilities_json' =>
'{"contract":1,"dual_stack_required":true,"project_owned_filter":true}',
]);
$missingNdpCapability = $service->read($hostId, $actor);
$assert(count(array_filter(
$missingNdpCapability['attachments'],
static fn (array $attachment): bool => $attachment['ready'] === true
)) === 0,
'Administrator pool attachments remained ready without ndp_guard.');
Capsule::table('cloud_public_ip_pool_catalog_mutex')->delete();
$expectCloudError(
static fn () => $service->create(
$hostId,
$actor,
array_replace($createBody, [
'request_key' => str_repeat('5', 32),
'cidr' => '9.9.10.0/29',
'gateway' => '9.9.10.1',
])
),
503,
'公网地址池全局目录锁未初始化'
);
echo "Public pools bind explicit, verified host attachments safely.\n";