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'); $table->string('default_network'); }); $schema->create('cloud_regions', static function (Blueprint $table): void { $table->increments('id'); $table->string('name'); $table->string('country_code'); $table->string('status'); }); $schema->create('cloud_network_lines', static function (Blueprint $table): void { $table->increments('id'); $table->integer('region_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('name'); $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('revision')->default(1); }); $schema->create('cloud_host_capacity_policies', static function (Blueprint $table): void { $table->integer('host_id')->primary(); $table->integer('configured_cpu_cores'); $table->integer('configured_memory_mib'); $table->integer('configured_disk_gib'); $table->integer('safety_cpu_cores'); $table->integer('safety_memory_mib'); $table->integer('safety_disk_gib'); $table->integer('max_instances'); }); $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'); }); $schema->create('cloud_host_storage_configurations', static function (Blueprint $table): void { $table->integer('host_id')->primary(); $table->string('state'); }); $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->integer('priority'); $table->string('verification_state'); $table->integer('verified_agent_revision'); $table->integer('last_verified_at'); $table->string('sync_state'); $table->integer('observed_revision'); $table->integer('desired_revision'); }); $schema->create('cloud_public_ip_pools', static function (Blueprint $table): void { $table->increments('id'); $table->integer('host_id'); $table->integer('network_attachment_id'); $table->string('state'); $table->string('sync_state'); $table->integer('observed_revision'); $table->integer('desired_revision'); $table->integer('priority'); $table->integer('guest_prefix_length'); $table->string('pool_key'); $table->string('cidr'); $table->string('gateway'); $table->text('dns_servers_json'); $table->text('reserved_ips_json'); }); $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'); $table->integer('prefix_length'); $table->string('state'); $table->integer('reservation_id')->nullable(); $table->integer('instance_id')->nullable(); $table->string('operation_id')->nullable(); $table->integer('created_at'); $table->integer('updated_at'); }); $schema->create('cloud_resource_reservations', static function (Blueprint $table): void { $table->increments('id'); $table->string('reservation_key'); $table->string('operation_id'); $table->integer('order_id')->nullable(); $table->integer('backup_restore_id')->nullable(); $table->integer('offer_id')->nullable(); $table->integer('resource_pool_id'); $table->integer('host_id'); $table->integer('user_id'); $table->string('state'); $table->integer('instance_id')->nullable(); $table->integer('network_attachment_id')->nullable(); $table->integer('public_ip_pool_id')->nullable(); $table->integer('public_ip_id')->nullable(); $table->integer('cpu_cores'); $table->integer('memory_mib'); $table->integer('disk_gib'); $table->integer('expires_at')->nullable(); $table->integer('released_at')->nullable(); $table->string('release_reason'); $table->integer('created_at'); $table->integer('updated_at'); }); $schema->create('cloud_backup_restores', static function (Blueprint $table): void { $table->increments('id'); $table->integer('target_host_id'); $table->integer('reservation_id')->nullable(); $table->string('operation_id'); }); $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(); }); $capsule->table('cloud_regions')->insert([ 'id' => 1, 'name' => 'Hong Kong', 'country_code' => 'HK', 'status' => 'active', ]); $capsule->table('cloud_network_lines')->insert([ ['id' => 1, 'region_id' => 1, 'name' => 'International', 'status' => 'active'], ['id' => 2, 'region_id' => 1, 'name' => 'Premium', 'status' => 'active'], ]); $capsule->table('cloud_resource_pools')->insert([ ['id' => 1, 'line_id' => 1, 'name' => 'HKG International', 'status' => 'active'], ['id' => 2, 'line_id' => 2, 'name' => 'HKG Premium', 'status' => 'active'], ]); foreach ([1 => 'source-host', 2 => 'other-host'] as $hostId => $name) { $capsule->table('cloud_hosts')->insert([ 'id' => $hostId, 'name' => $name, 'enabled' => 1, 'default_network' => 'bridge:retired', ]); $capsule->table('cloud_host_resource_pool_memberships')->insert([ 'host_id' => $hostId, 'resource_pool_id' => 1, 'scheduler_state' => 'active', ]); $capsule->table('cloud_host_capacity_policies')->insert([ 'host_id' => $hostId, 'configured_cpu_cores' => 16, 'configured_memory_mib' => 32768, 'configured_disk_gib' => 1000, 'safety_cpu_cores' => 0, 'safety_memory_mib' => 0, 'safety_disk_gib' => 0, 'max_instances' => 20, ]); $capsule->table('cloud_host_agents')->insert([ 'host_id' => $hostId, 'enabled' => 1, 'revision' => 1, 'tested_revision' => 1, 'last_test_status' => 'healthy', 'network_selectors_json' => json_encode( $hostId === 1 ? ['bridge:restore-a', 'bridge:restore-c'] : ['bridge:restore-b'], JSON_THROW_ON_ERROR ), ]); $capsule->table('cloud_host_storage_configurations')->insert([ 'host_id' => $hostId, 'state' => 'applied', ]); $attachmentId = $hostId; $poolId = $hostId; $capsule->table('cloud_host_network_attachments')->insert([ 'id' => $attachmentId, 'host_id' => $hostId, 'line_id' => 1, 'network_selector' => $hostId === 1 ? 'bridge:restore-a' : 'bridge:restore-b', 'state' => 'active', 'priority' => 0, 'verification_state' => 'verified', 'verified_agent_revision' => 1, 'last_verified_at' => 1700000000, 'sync_state' => 'applied', 'observed_revision' => 1, 'desired_revision' => 1, ]); $capsule->table('cloud_public_ip_pools')->insert([ 'id' => $poolId, 'host_id' => $hostId, 'network_attachment_id' => $attachmentId, 'state' => 'active', 'sync_state' => 'applied', 'observed_revision' => 1, 'desired_revision' => 1, 'priority' => 0, 'guest_prefix_length' => 24, 'pool_key' => str_repeat((string) $hostId, 32), 'cidr' => '203.0.' . $hostId . '.0/24', 'gateway' => '203.0.' . $hostId . '.1', 'dns_servers_json' => '["1.1.1.1","8.8.8.8"]', 'reserved_ips_json' => '[]', ]); $capsule->table('cloud_public_ips')->insert([ 'host_id' => $hostId, 'public_ip_pool_id' => $poolId, 'address' => '203.0.' . $hostId . '.20', 'prefix_length' => 24, 'state' => 'available', 'reservation_id' => null, 'instance_id' => null, 'operation_id' => null, 'created_at' => 1700000000, 'updated_at' => 1700000000, ]); } // The same physical host is an active member of a second commercial line. // Restore inventory must expose both exact host/pool identities without // duplicating host-global capacity accounting or trusting default_network. $capsule->table('cloud_host_resource_pool_memberships')->insert([ 'host_id' => 1, 'resource_pool_id' => 2, 'scheduler_state' => 'active', ]); $capsule->table('cloud_host_network_attachments')->insert([ 'id' => 3, 'host_id' => 1, 'line_id' => 2, 'network_selector' => 'bridge:restore-c', 'state' => 'active', 'priority' => 0, 'verification_state' => 'verified', 'verified_agent_revision' => 1, 'last_verified_at' => 1700000000, 'sync_state' => 'never', 'observed_revision' => 0, 'desired_revision' => 7, ]); $capsule->table('cloud_public_ip_pools')->insert([ 'id' => 3, 'host_id' => 1, 'network_attachment_id' => 3, 'state' => 'active', 'sync_state' => 'applied', 'observed_revision' => 1, 'desired_revision' => 1, 'priority' => 0, 'guest_prefix_length' => 24, 'pool_key' => str_repeat('3', 32), 'cidr' => '198.51.100.0/24', 'gateway' => '198.51.100.1', 'dns_servers_json' => '["9.9.9.9"]', 'reserved_ips_json' => '[]', ]); $capsule->table('cloud_public_ips')->insert([ 'id' => 3, 'host_id' => 1, 'public_ip_pool_id' => 3, 'address' => '198.51.100.20', 'prefix_length' => 24, 'state' => 'available', 'reservation_id' => null, 'instance_id' => null, 'operation_id' => null, 'created_at' => 1700000000, 'updated_at' => 1700000000, ]); // The source VM consumes capacity on its current host. A same-host restore // must still reserve a second, independent resource and IP allocation. $capsule->table('cloud_instances')->insert([ 'id' => 1, 'host_id' => 1, 'status' => 'active', 'spec_json' => json_encode([ 'vcpu' => 2, 'memory_mib' => 2048, 'disk_gib' => 20, ], JSON_THROW_ON_ERROR), ]); $targets = CloudPlacementService::backupRestoreTargets(1, [ 'cpu_cores' => 2, 'memory_mib' => 2048, 'disk_gib' => 20, ]); $byIdentity = []; foreach ($targets as $target) { $byIdentity[(int) $target['id'] . ':' . (int) $target['resource_pool_id']] = $target; } $assert(isset($byIdentity['1:1']), 'The original host was excluded from eligible manual restore targets.'); $assert(($byIdentity['1:1']['is_source_host'] ?? null) === true, 'The original host target was not explicitly identified.'); $assert(isset($byIdentity['1:2']) && ($byIdentity['1:2']['network_selectors'] ?? null) === ['bridge:restore-c'], 'A second exact host/pool restore identity was overwritten or lost.'); $assert(isset($byIdentity['2:1']) && ($byIdentity['2:1']['is_source_host'] ?? null) === false, 'A different eligible host was lost or misidentified.'); $assert((int) $byIdentity['1:1']['available_ip_count'] === 1, 'The original host did not retain its independent IP capacity check.'); $capsule->table('cloud_host_network_attachments')->where('id', 3)->update([ 'last_verified_at' => 0, ]); $staleTargets = CloudPlacementService::backupRestoreTargets(1, [ 'cpu_cores' => 2, 'memory_mib' => 2048, 'disk_gib' => 20, ]); $assert(!in_array('2', array_map( static fn (array $target): string => (string) $target['resource_pool_id'], array_filter($staleTargets, static fn (array $target): bool => (int) $target['id'] === 1) ), true), 'Stale attachment evidence remained eligible for restore.'); $capsule->table('cloud_host_network_attachments')->where('id', 3)->update([ 'last_verified_at' => 1700000000, ]); $restore = new CloudBackupRestore(); $restore->target_host_id = 1; $restore->reservation_id = null; $restore->operation_id = str_repeat('a', 32); $restore->save(); $placement = CloudPlacementService::reserveBackupRestore($restore, 77, [ 'cpu_cores' => 2, 'memory_mib' => 2048, 'disk_gib' => 20, ], 1); $reservation = $placement['reservation']; $publicIp = $placement['public_ip']; $assert($placement['network']['dns_servers'] === ['1.1.1.1', '8.8.8.8'], 'A valid pool DNS list could not reach the restore placement payload.'); $assert($placement['network']['network'] === 'bridge:restore-a' && (int) $reservation->resource_pool_id === 1 && (int) $reservation->network_attachment_id === 1, 'Restore placement did not freeze the selected exact pool and attachment.'); $assert((int) $reservation->backup_restore_id === (int) $restore->id && (int) $reservation->public_ip_id === (int) $publicIp->id && (string) $reservation->state === 'held' && (string) $publicIp->state === 'reserved' && (int) $publicIp->reservation_id === (int) $reservation->id, 'Restore placement did not atomically reserve capacity and one public IP.'); // Maintenance migration consumes the exact pre-reserved network rather than // scheduling another address after the source VM has already stopped. $restore->reservation_id = (int) $reservation->id; $restore->save(); $reservation->instance_id = 99; $reservation->save(); $prepared = CloudPlacementService::preparedBackupRestorePlacement( $restore->fresh(), 99 ); $assert($prepared['network']['dns_servers'] === ['1.1.1.1', '8.8.8.8'] && $prepared['network']['network'] === 'bridge:restore-a' && (int) $prepared['reservation']->id === (int) $reservation->id, 'Maintenance migration did not preserve the pre-reserved pool DNS data.'); $capsule->table('cloud_public_ip_pools')->where('id', 1)->update([ 'dns_servers_json' => '["not-an-ip"]', ]); try { CloudPlacementService::preparedBackupRestorePlacement( $restore->fresh(), 99 ); throw new RuntimeException('A malformed prepared DNS list was accepted.'); } catch (CloudError $error) { $assert($error->httpStatus === 409 && $error->getMessage() === '公网地址池 DNS 配置损坏', 'Malformed prepared DNS did not return the bounded placement error.'); } $capsule->table('cloud_public_ip_pools')->where('id', 2)->update([ 'dns_servers_json' => '["not-an-ip"]', ]); $invalidRestore = new CloudBackupRestore(); $invalidRestore->target_host_id = 2; $invalidRestore->reservation_id = null; $invalidRestore->operation_id = str_repeat('b', 32); $invalidRestore->save(); try { CloudPlacementService::reserveBackupRestore($invalidRestore, 77, [ 'cpu_cores' => 2, 'memory_mib' => 2048, 'disk_gib' => 20, ], 1); throw new RuntimeException('A malformed pool DNS list was accepted.'); } catch (CloudError $error) { $assert($error->httpStatus === 409 && $error->getMessage() === '目标宿主机没有可分配的公网 IP', 'Malformed pool DNS was not excluded from target allocation.'); } $assert($capsule->table('cloud_resource_reservations')->count() === 1 && $capsule->table('cloud_public_ips')->where('host_id', 2) ->value('state') === 'available', 'Malformed pool DNS consumed restore capacity or a public IP.'); echo "Portable backup same-host restore target behavior passed.\n";