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->string('default_network'); $table->integer('created_at'); $table->integer('updated_at'); }); $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'); }); $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('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('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->string('name'); $table->string('cidr'); $table->string('gateway'); $table->integer('guest_prefix_length'); $table->text('dns_servers_json'); $table->text('reserved_ips_json'); $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->string('address')->unique(); $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'); }); $now = time(); $hostId = Capsule::table('cloud_hosts')->insertGetId([ 'name' => 'Host 1', 'enabled' => 1, 'default_network' => 'bridge:br0', 'created_at' => $now, 'updated_at' => $now, ]); $lineId = Capsule::table('cloud_network_lines')->insertGetId([ 'name' => 'China optimized', 'status' => 'active', ]); $resourcePoolId = Capsule::table('cloud_resource_pools')->insertGetId([ 'line_id' => $lineId, ]); 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, ]); $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, '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, 'name' => 'Public pool', 'cidr' => '8.8.8.0/29', 'gateway' => '8.8.8.1', 'guest_prefix_length' => 29, 'dns_servers_json' => '["1.1.1.1"]', 'reserved_ips_json' => '[]', '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' => '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' => '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') => [ 'name' => 'Public pool', 'cidr' => '8.8.8.0/29', 'gateway' => '8.8.8.1', 'dns_servers' => [$dns], 'reserved_ips' => $reserved, 'guest_prefix_length' => 29, 'state' => 'active', '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 && (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.'); $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.'); echo "Occupied pools allow only unoccupied disabled-IP edits.\n";