httpStatus !== $status) { throw new RuntimeException( $message . ': expected HTTP ' . $status . ', got ' . $error->httpStatus ); } return; } throw new RuntimeException($message . ': expected CloudError'); }; $membership = static function (array $payload, int $poolId): array { foreach ($payload['memberships'] as $row) { if ((int) $row['resource_pool_id'] === $poolId) { return $row; } } throw new RuntimeException('Missing membership for pool ' . $poolId); }; $capsule = new Capsule(); $capsule->addConnection([ 'driver' => 'sqlite', 'database' => ':memory:', ], 'default'); $capsule->setAsGlobal(); $capsule->bootEloquent(); $schema = $capsule->schema('default'); $schema->create('configurations', static function (Blueprint $table): void { $table->increments('id'); $table->string('name')->unique(); $table->text('value'); }); $schema->create('cloud_hosts', static function (Blueprint $table): void { $table->increments('id'); $table->string('name'); $table->boolean('enabled'); $table->integer('created_at'); $table->integer('updated_at'); }); $schema->create('cloud_host_agents', static function (Blueprint $table): void { $table->integer('host_id')->primary(); $table->boolean('enabled'); $table->integer('revision'); $table->integer('tested_revision'); $table->string('last_test_status'); $table->text('network_selectors_json'); $table->text('public_network_capabilities_json'); }); $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('code'); $table->string('name'); $table->boolean('is_default'); $table->string('status'); $table->integer('sort'); $table->integer('created_at'); $table->integer('updated_at'); }); $schema->create('cloud_resource_pools', static 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'); $table->integer('created_at'); $table->integer('updated_at'); }); $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'); $table->integer('weight'); $table->integer('revision')->default(1); $table->integer('created_at'); $table->integer('updated_at'); $table->unique(['host_id', 'resource_pool_id']); }); $schema->create('cloud_host_capacity_policies', static function (Blueprint $table): void { $table->integer('host_id')->primary(); foreach (['configured_cpu_cores', 'configured_memory_mib', 'configured_disk_gib', 'safety_cpu_cores', 'safety_memory_mib', 'safety_disk_gib', 'max_instances', 'created_at', 'updated_at'] as $field) { $table->integer($field); } }); $schema->create('cloud_resource_reservations', static function (Blueprint $table): void { $table->increments('id'); $table->integer('host_id'); $table->integer('resource_pool_id'); $table->string('state'); $table->integer('instance_id')->nullable(); $table->integer('cpu_cores'); $table->integer('memory_mib'); $table->integer('disk_gib'); }); $schema->create('cloud_instances', static function (Blueprint $table): void { $table->increments('id'); $table->integer('host_id'); $table->string('status'); $table->text('spec_json'); }); $schema->create('cloud_instance_snapshots', static function (Blueprint $table): void { $table->increments('id'); $table->integer('instance_id'); $table->integer('host_id'); $table->string('state'); $table->integer('size_bytes')->nullable(); }); $schema->create('cloud_host_network_attachments', static function (Blueprint $table): void { $table->increments('id'); $table->integer('host_id'); $table->integer('line_id'); $table->string('network_selector'); $table->string('state'); $table->string('sync_state'); $table->integer('observed_revision'); $table->integer('desired_revision'); $table->string('verification_state'); $table->integer('verified_agent_revision'); $table->integer('last_verified_at'); }); $schema->create('cloud_public_ip_pools', static function (Blueprint $table): void { $table->increments('id'); $table->integer('host_id'); $table->integer('network_attachment_id'); $table->integer('address_family'); $table->integer('max_allocations')->default(0); $table->string('state'); $table->string('sync_state'); $table->integer('observed_revision'); $table->integer('desired_revision'); }); $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('state'); $table->integer('reservation_id')->nullable(); $table->integer('instance_id')->nullable(); $table->string('operation_id')->nullable(); }); $assert(!$schema->hasColumn('cloud_hosts', 'default_network'), 'Test schema must prove scheduling no longer reads default_network.'); Capsule::table('configurations')->insert([ 'name' => 'cloud_network_lines_enabled', 'value' => 'true', ]); Capsule::table('cloud_hosts')->insert([ 'id' => 1, 'name' => 'HKG-A', 'enabled' => 1, 'created_at' => 1, 'updated_at' => 1, ]); Capsule::table('cloud_host_agents')->insert([ 'host_id' => 1, 'enabled' => 1, 'revision' => 1, 'tested_revision' => 1, 'last_test_status' => 'healthy', 'network_selectors_json' => '["bridge:line-a","bridge:line-b"]', 'public_network_capabilities_json' => '{"contract":1,"dual_stack_required":true,"project_owned_filter":true,"ndp_guard":true}', ]); Capsule::table('cloud_regions')->insert([ ['id' => 1, 'code' => 'hkg', 'name' => 'Hong Kong', 'country_code' => 'HK', 'status' => 'active', 'sort' => 10, 'created_at' => 1, 'updated_at' => 1], ['id' => 2, 'code' => 'nrt', 'name' => 'Tokyo', 'country_code' => 'JP', 'status' => 'active', 'sort' => 20, 'created_at' => 1, 'updated_at' => 1], ]); Capsule::table('cloud_network_lines')->insert([ ['id' => 10, 'region_id' => 1, 'code' => 'hkg-a', 'name' => 'HKG A', 'is_default' => 1, 'status' => 'active', 'sort' => 10, 'created_at' => 1, 'updated_at' => 1], ['id' => 11, 'region_id' => 1, 'code' => 'hkg-b', 'name' => 'HKG B', 'is_default' => 0, 'status' => 'active', 'sort' => 20, 'created_at' => 1, 'updated_at' => 1], ['id' => 20, 'region_id' => 2, 'code' => 'nrt-a', 'name' => 'NRT A', 'is_default' => 1, 'status' => 'active', 'sort' => 30, 'created_at' => 1, 'updated_at' => 1], ]); Capsule::table('cloud_resource_pools')->insert([ ['id' => 100, 'line_id' => 10, 'code' => 'hkg-a', 'name' => 'HKG A', 'status' => 'active', 'scheduling_policy' => 'balanced', 'sort' => 10, 'created_at' => 1, 'updated_at' => 1], ['id' => 101, 'line_id' => 11, 'code' => 'hkg-b', 'name' => 'HKG B', 'status' => 'active', 'scheduling_policy' => 'balanced', 'sort' => 20, 'created_at' => 1, 'updated_at' => 1], ['id' => 200, 'line_id' => 20, 'code' => 'nrt-a', 'name' => 'NRT A', 'status' => 'active', 'scheduling_policy' => 'balanced', 'sort' => 30, 'created_at' => 1, 'updated_at' => 1], ]); Capsule::table('cloud_host_network_attachments')->insert([ ['id' => 1, 'host_id' => 1, 'line_id' => 10, 'state' => 'active', 'network_selector' => 'bridge:line-a', 'sync_state' => 'never', 'observed_revision' => 0, 'desired_revision' => 9, 'verification_state' => 'verified', 'verified_agent_revision' => 1, 'last_verified_at' => time()], ['id' => 2, 'host_id' => 1, 'line_id' => 11, 'state' => 'active', 'network_selector' => 'bridge:line-b', 'sync_state' => 'applied', 'observed_revision' => 1, 'desired_revision' => 1, 'verification_state' => 'verified', 'verified_agent_revision' => 1, 'last_verified_at' => time()], ]); Capsule::table('cloud_public_ip_pools')->insert([ ['id' => 1, 'host_id' => 1, 'network_attachment_id' => 1, 'address_family' => 4, 'max_allocations' => 0, 'state' => 'active', 'sync_state' => 'applied', 'observed_revision' => 1, 'desired_revision' => 1], ['id' => 2, 'host_id' => 1, 'network_attachment_id' => 2, 'address_family' => 4, 'max_allocations' => 0, 'state' => 'active', 'sync_state' => 'applied', 'observed_revision' => 1, 'desired_revision' => 1], ['id' => 3, 'host_id' => 1, 'network_attachment_id' => 1, 'address_family' => 6, 'max_allocations' => 100, 'state' => 'active', 'sync_state' => 'applied', 'observed_revision' => 1, 'desired_revision' => 1], ['id' => 4, 'host_id' => 1, 'network_attachment_id' => 2, 'address_family' => 6, 'max_allocations' => 100, 'state' => 'active', 'sync_state' => 'applied', 'observed_revision' => 1, 'desired_revision' => 1], ]); Capsule::table('cloud_public_ips')->insert([ ['id' => 1, 'host_id' => 1, 'public_ip_pool_id' => 1, 'address_family' => 4, 'state' => 'available', 'reservation_id' => null, 'instance_id' => null, 'operation_id' => null], ['id' => 2, 'host_id' => 1, 'public_ip_pool_id' => 2, 'address_family' => 4, 'state' => 'available', 'reservation_id' => null, 'instance_id' => null, 'operation_id' => null], ['id' => 3, 'host_id' => 1, 'public_ip_pool_id' => 2, 'address_family' => 4, 'state' => 'available', 'reservation_id' => null, 'instance_id' => null, 'operation_id' => null], ]); $capacityBody = [ 'configured_cpu_cores' => 32, 'configured_memory_mib' => 65536, 'configured_disk_gib' => 1000, 'safety_cpu_cores' => 2, 'safety_memory_mib' => 2048, 'safety_disk_gib' => 50, 'max_instances' => 100, ]; $initial = CloudAdminHostSchedulingService::saveCapacity(1, $capacityBody); $assert($initial['memberships'] === [] && Capsule::table('cloud_host_capacity_policies')->count() === 1, 'Capacity save must not create or replace a membership.'); $first = CloudAdminHostSchedulingService::saveMembership(1, 100, [ 'scheduler_state' => 'active', 'priority' => 10, 'weight' => 100, 'expected_revision' => 0, ]); $second = CloudAdminHostSchedulingService::saveMembership(1, 101, [ 'scheduler_state' => 'active', 'priority' => 20, 'weight' => 200, 'expected_revision' => 0, ]); $poolA = $membership($second, 100); $poolB = $membership($second, 101); $assert(count($second['memberships']) === 2 && $second['host']['active_memberships'] === 2 && $poolA['revision'] === 1 && $poolB['revision'] === 1 && $poolA['region_id'] === 1 && $poolB['region_id'] === 1 && $poolA['line_id'] === 10 && $poolB['line_id'] === 11 && $poolA['ip_capacity']['available'] === 1 && $poolB['ip_capacity']['available'] === 2 && $second['capacity']['ip']['available'] === 3, 'Verified current Agent evidence did not make an otherwise unsynchronized attachment schedulable.'); Capsule::table('cloud_host_agents')->where('host_id', 1)->update([ 'public_network_capabilities_json' => '{"contract":1,"dual_stack_required":true,"project_owned_filter":true}', ]); $missingNdpGuard = CloudAdminHostSchedulingService::read(1); $assert($membership($missingNdpGuard, 100)['ip_capacity']['available'] === 0 && $membership($missingNdpGuard, 101)['ip_capacity']['available'] === 0, 'Scheduling accepted public-network evidence without ndp_guard.'); Capsule::table('cloud_host_agents')->where('host_id', 1)->update([ 'public_network_capabilities_json' => '{"contract":1,"dual_stack_required":true,"project_owned_filter":true,"ndp_guard":true}', ]); Capsule::table('cloud_public_ip_pools')->where('id', 3)->update([ 'state' => 'disabled', ]); $missingIpv6Pair = CloudAdminHostSchedulingService::read(1); $assert($membership($missingIpv6Pair, 100)['ip_capacity']['available'] === 0 && $membership($missingIpv6Pair, 100)['placement_ready'] === false && $membership($missingIpv6Pair, 101)['ip_capacity']['available'] === 2, 'Scheduling accepted IPv4 capacity without its same-attachment IPv6 pool.'); Capsule::table('cloud_public_ip_pools')->where('id', 3)->update([ 'state' => 'active', ]); Capsule::table('cloud_host_agents')->where('host_id', 1)->update([ 'revision' => 2, 'tested_revision' => 2, ]); $staleEvidence = CloudAdminHostSchedulingService::read(1); $assert($membership($staleEvidence, 100)['ip_capacity']['available'] === 0 && $membership($staleEvidence, 101)['ip_capacity']['available'] === 0 && $membership($staleEvidence, 100)['placement_ready'] === false && $membership($staleEvidence, 101)['placement_ready'] === false, 'Scheduling projected stale attachment verification as placement-ready.'); Capsule::table('cloud_host_network_attachments')->update([ 'verified_agent_revision' => 2, 'last_verified_at' => time(), ]); $refreshedEvidence = CloudAdminHostSchedulingService::read(1); $assert($membership($refreshedEvidence, 100)['ip_capacity']['available'] === 1 && $membership($refreshedEvidence, 101)['ip_capacity']['available'] === 2, 'Current Agent verification did not restore scheduling IP capacity.'); Capsule::table('cloud_host_agents')->where('host_id', 1)->update([ 'network_selectors_json' => '["bridge:line-a"]', ]); $missingSelector = CloudAdminHostSchedulingService::read(1); $assert($membership($missingSelector, 100)['ip_capacity']['available'] === 1 && $membership($missingSelector, 101)['ip_capacity']['available'] === 0 && $membership($missingSelector, 101)['placement_ready'] === false, 'Selector absent from current Agent evidence remained placement-ready.'); Capsule::table('cloud_host_agents')->where('host_id', 1)->update([ 'network_selectors_json' => '["bridge:line-a","bridge:line-b"]', ]); $afterCapacity = CloudAdminHostSchedulingService::saveCapacity(1, array_replace($capacityBody, ['configured_cpu_cores' => 64])); $assert(count($afterCapacity['memberships']) === 2 && $afterCapacity['policy']['configured_cpu_cores'] === 64 && $membership($afterCapacity, 100)['revision'] === 1 && $membership($afterCapacity, 101)['revision'] === 1 && Capsule::table('cloud_host_capacity_policies')->count() === 1, 'Host-global capacity save changed independently revisioned memberships.'); $draining = CloudAdminHostSchedulingService::saveMembership(1, 100, [ 'scheduler_state' => 'draining', 'priority' => 10, 'weight' => 100, 'expected_revision' => 1, ]); $assert($membership($draining, 100)['scheduler_state'] === 'draining' && $membership($draining, 100)['revision'] === 2 && $membership($draining, 101)['scheduler_state'] === 'active' && $membership($draining, 101)['revision'] === 1, 'Updating one membership changed its sibling.'); $expectError(static function (): void { CloudAdminHostSchedulingService::saveMembership(1, 100, [ 'scheduler_state' => 'active', 'priority' => 10, 'weight' => 100, 'expected_revision' => 1, ]); }, 409, 'Stale membership revision was accepted'); $expectError(static function (): void { CloudAdminHostSchedulingService::saveMembership(1, 200, [ 'scheduler_state' => 'active', 'priority' => 30, 'weight' => 100, 'expected_revision' => 0, ]); }, 409, 'Cross-region active membership was accepted'); Capsule::table('cloud_resource_reservations')->insert([ 'id' => 1, 'host_id' => 1, 'resource_pool_id' => 100, 'state' => 'committed', 'instance_id' => null, 'cpu_cores' => 1, 'memory_mib' => 512, 'disk_gib' => 5, ]); $expectError(static function (): void { CloudAdminHostSchedulingService::saveMembership(1, 100, [ 'scheduler_state' => 'disabled', 'priority' => 10, 'weight' => 100, 'expected_revision' => 2, ]); }, 409, 'Exact-pool reservation did not block membership disable'); $disabledB = CloudAdminHostSchedulingService::saveMembership(1, 101, [ 'scheduler_state' => 'disabled', 'priority' => 20, 'weight' => 200, 'expected_revision' => 1, ]); $assert($membership($disabledB, 101)['revision'] === 2 && $membership($disabledB, 101)['scheduler_state'] === 'disabled', 'Reservation in another pool incorrectly blocked disable.'); $afterDelete = CloudAdminHostSchedulingService::deleteMembership(1, 101, 2); $assert(count($afterDelete['memberships']) === 1 && $membership($afterDelete, 100)['revision'] === 2, 'Deleting one empty membership disturbed its sibling.'); $expectError(static function (): void { CloudAdminHostSchedulingService::deleteMembership(1, 100, 2); }, 409, 'Exact-pool reservation did not block membership deletion'); $expectError(static function (): void { CloudAdminHostSchedulingService::deleteMembership(1, 101, 2); }, 404, 'Deleting an absent membership did not return not found'); echo "Administrator host multi-membership behavior passed.\n";