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'); $table->string('value'); }); $schema->create('user', static function (Blueprint $table): void { $table->increments('id'); $table->string('user_name'); $table->string('email'); }); $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'); }); $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->string('name'); $table->string('region'); $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'); $table->string('last_error_code')->default(''); }); $schema->create('cloud_vpc_cidr_pools', static function (Blueprint $table): void { $table->increments('id'); $table->string('pool_key'); $table->integer('region_id'); $table->string('name'); $table->string('cidr'); $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'); $table->integer('user_id'); $table->integer('region_id'); $table->integer('cidr_pool_id'); $table->string('name'); $table->string('cidr'); $table->integer('prefix_length'); $table->string('gateway'); $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'); }); $schema->create('cloud_instances', static function (Blueprint $table): void { $table->increments('id'); $table->string('uuid'); $table->integer('host_id'); $table->integer('user_id'); $table->string('name'); $table->text('spec_json'); }); $schema->create('cloud_vpc_ports', static function (Blueprint $table): void { $table->increments('id'); $table->string('port_key'); $table->integer('vpc_id'); $table->integer('instance_id'); $table->integer('migration_host_id')->nullable(); $table->integer('user_id'); $table->string('private_ip'); $table->string('mac_address'); $table->integer('active_claim')->nullable(); $table->string('state'); $table->integer('desired_revision'); $table->integer('applied_revision'); $table->string('last_operation_id')->nullable(); $table->string('last_error_code')->default(''); $table->integer('attached_at')->nullable(); $table->integer('released_at')->nullable(); $table->integer('created_at'); $table->integer('updated_at'); }); $schema->create('cloud_vpc_host_states', static function (Blueprint $table): void { $table->increments('id'); $table->integer('vpc_id'); $table->integer('host_id'); $table->integer('desired_revision'); $table->integer('applied_revision'); $table->string('desired_digest'); $table->string('applied_digest')->nullable(); $table->string('sync_state'); $table->string('last_operation_id')->nullable(); $table->string('last_error_code')->default(''); $table->integer('last_success_at'); $table->integer('last_failure_at'); $table->integer('created_at'); $table->integer('updated_at'); }); $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_jobs', static function (Blueprint $table): void { $table->increments('id'); $table->string('operation_id'); $table->string('kind'); $table->string('phase'); $table->string('outcome')->nullable(); $table->string('status'); $table->string('error_code')->nullable(); $table->integer('vpc_id')->nullable(); $table->integer('vpc_batch_id')->nullable(); $table->integer('created_at'); $table->integer('updated_at'); }); Capsule::table('configurations')->insert([ 'name' => 'cloud_vpc_enabled', 'value' => 'true', ]); Capsule::table('user')->insert([ ['id' => 7, 'user_name' => 'alice', 'email' => 'alice@example.test'], ['id' => 8, 'user_name' => 'bob', 'email' => 'bob@example.test'], ]); Capsule::table('cloud_regions')->insert([ ['id' => 1, 'code' => 'hkg', 'name' => '香港', 'country_code' => 'HK', 'status' => 'active', 'sort' => 0], ['id' => 2, 'code' => 'nrt', 'name' => '东京', 'country_code' => 'JP', 'status' => 'active', 'sort' => 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, 'name' => 'HKG-A', 'region' => 'HongKong', 'enabled' => 1, ]); Capsule::table('cloud_hosts')->insert([ 'id' => 2, 'name' => 'HKG-B', 'region' => 'HongKong', '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' => 3, 'observed_revision' => 3, 'last_error_code' => '', ]); Capsule::table('cloud_vpc_cidr_pools')->insert([ 'id' => 1, 'pool_key' => str_repeat('a', 32), 'region_id' => 1, 'name' => 'HKG Private', 'cidr' => '10.0.0.0/24', 'prefix_length' => 24, 'allocation_prefix' => 28, 'state' => 'active', 'revision' => 1, 'created_at' => 1, 'updated_at' => 1, ]); Capsule::table('cloud_vpcs')->insert([ ['id' => 1, 'vpc_key' => str_repeat('1', 32), 'user_id' => 7, 'region_id' => 1, 'cidr_pool_id' => 1, 'name' => 'production', 'cidr' => '10.0.0.0/28', 'prefix_length' => 28, 'gateway' => '10.0.0.1', 'active_claim' => 1, 'state' => 'active', 'desired_revision' => 2, 'deleted_at' => null, 'created_at' => 10, 'updated_at' => 20], ['id' => 2, 'vpc_key' => str_repeat('2', 32), 'user_id' => 8, 'region_id' => 1, 'cidr_pool_id' => 1, 'name' => 'staging', 'cidr' => '10.0.0.16/28', 'prefix_length' => 28, 'gateway' => '10.0.0.17', 'active_claim' => 1, 'state' => 'degraded', 'desired_revision' => 4, 'deleted_at' => null, 'created_at' => 11, 'updated_at' => 30], ]); $publicNetwork = static fn (int $suffix): array => ['public_network' => [ 'contract' => 1, 'network_selector' => 'bridge:br0', 'ipv4' => [ 'pool_key' => str_repeat('1', 32), 'pool_cidr' => '8.8.8.0/24', 'address_cidr' => '8.8.8.' . $suffix . '/24', 'gateway' => '8.8.8.1', 'gateway_on_link' => false, ], 'ipv6' => [ 'pool_key' => str_repeat('2', 32), 'pool_cidr' => '2404:c140:156:3a::/64', 'address_cidr' => '2404:c140:156:3a::' . $suffix . '/64', 'gateway' => '2404:c140:156::1', 'gateway_on_link' => true, ], 'dns_servers' => ['1.1.1.1', '2606:4700:4700::1111'], ]]; Capsule::table('cloud_instances')->insert([ ['id' => 11, 'uuid' => '11111111-1111-4111-8111-111111111111', 'host_id' => 1, 'user_id' => 7, 'name' => 'vm-prod', 'spec_json' => json_encode($publicNetwork(11), JSON_THROW_ON_ERROR)], ['id' => 12, 'uuid' => '22222222-2222-4222-8222-222222222222', 'host_id' => 1, 'user_id' => 8, 'name' => 'vm-stage', 'spec_json' => json_encode($publicNetwork(12), JSON_THROW_ON_ERROR)], ]); Capsule::table('cloud_vpc_ports')->insert([ ['id' => 1, 'port_key' => str_repeat('3', 32), 'vpc_id' => 1, 'instance_id' => 11, 'migration_host_id' => null, 'user_id' => 7, 'private_ip' => '10.0.0.2', 'mac_address' => '02:00:00:00:00:01', 'active_claim' => 1, 'state' => 'attached', 'desired_revision' => 2, 'applied_revision' => 2, 'last_operation_id' => str_repeat('a', 32), 'last_error_code' => '', 'attached_at' => 20, 'released_at' => null, 'created_at' => 10, 'updated_at' => 20], ['id' => 2, 'port_key' => str_repeat('4', 32), 'vpc_id' => 2, 'instance_id' => 12, 'migration_host_id' => 2, 'user_id' => 8, 'private_ip' => '10.0.0.18', 'mac_address' => '02:00:00:00:00:02', 'active_claim' => 1, 'state' => 'attached', 'desired_revision' => 4, 'applied_revision' => 3, 'last_operation_id' => str_repeat('b', 32), 'last_error_code' => 'vpc_result_uncertain', 'attached_at' => 21, 'released_at' => null, 'created_at' => 11, 'updated_at' => 30], ]); $digest = str_repeat('d', 64); Capsule::table('cloud_vpc_host_states')->insert([ ['id' => 1, 'vpc_id' => 1, 'host_id' => 1, 'desired_revision' => 2, 'applied_revision' => 2, 'desired_digest' => $digest, 'applied_digest' => $digest, 'sync_state' => 'ready', 'last_operation_id' => str_repeat('c', 32), 'last_error_code' => '', 'last_success_at' => 20, 'last_failure_at' => 0, 'created_at' => 10, 'updated_at' => 20], ['id' => 2, 'vpc_id' => 2, 'host_id' => 1, 'desired_revision' => 4, 'applied_revision' => 3, 'desired_digest' => str_repeat('e', 64), 'applied_digest' => null, 'sync_state' => 'unknown', 'last_operation_id' => str_repeat('f', 32), 'last_error_code' => 'vpc_result_uncertain', 'last_success_at' => 0, 'last_failure_at' => 30, 'created_at' => 11, 'updated_at' => 30], ]); Capsule::table('cloud_vpc_membership_batches')->insert([ 'id' => 1, 'batch_key' => str_repeat('5', 32), 'vpc_id' => 2, 'user_id' => 8, 'parent_job_id' => 91, 'kind' => 'attach', 'state' => 'blocked', 'desired_revision' => 4, 'active_claim' => 1, 'last_error_code' => 'vpc_result_uncertain', 'terminal_at' => null, 'created_at' => 25, 'updated_at' => 30, ]); Capsule::table('cloud_vpc_membership_batch_items')->insert([ 'id' => 1, 'batch_id' => 1, 'user_id' => 8, 'instance_id' => 12, 'vpc_port_id' => 2, 'host_id' => 1, 'child_job_id' => null, 'state' => 'unknown', 'last_error_code' => 'vpc_result_uncertain', 'created_at' => 25, 'updated_at' => 30, ]); Capsule::table('cloud_jobs')->insert([ 'id' => 91, 'operation_id' => str_repeat('6', 32), 'kind' => 'vpc_membership_batch', 'phase' => 'awaiting_receipt', 'outcome' => null, 'status' => 'running', 'error_code' => 'vpc_result_uncertain', 'vpc_id' => 2, 'vpc_batch_id' => 1, 'created_at' => 25, 'updated_at' => 30, ]); $actor = ['is_admin' => true, 'is_super_admin' => true, 'can_manage' => true]; $overview = CloudAdminVpcControlService::overview($actor); $assert($overview['feature']['enabled'] === true && $overview['summary'] === [ 'total' => 2, 'active' => 1, 'changing' => 0, 'degraded' => 1, 'ports' => 2, 'active_batches' => 1, ], 'Administrator VPC overview summary is inconsistent.'); $assert(count($overview['regions']) === 2 && $overview['regions'][0]['ready'] === true && $overview['regions'][0]['available'] === 14 && $overview['regions'][0]['hosts_ready'] === 1, 'Ready VPC region projection is incomplete.'); $assert($overview['regions'][1]['ready'] === false && count($overview['regions'][1]['blockers']) === 2, 'Unavailable VPC region did not expose its blockers.'); $assert(count($overview['attention']) === 1 && $overview['attention'][0]['id'] === 2, 'Degraded VPC was not projected into the attention list.'); $degraded = CloudAdminVpcControlService::listing($actor, [ 'state' => 'degraded', 'page' => '1', 'per_page' => '20', ]); $assert($degraded['pagination']['total'] === 1 && $degraded['items'][0]['state'] === 'degraded' && $degraded['items'][0]['active_batch']['job_id'] === 91, 'Degraded VPC listing or active batch projection is incorrect.'); $search = CloudAdminVpcControlService::listing($actor, [ 'q' => 'production', 'region_id' => '1', 'per_page' => '20', ]); $assert($search['pagination']['total'] === 1 && $search['items'][0]['id'] === 1 && $search['items'][0]['host_summary'] === ['total' => 1, 'ready' => 1], 'VPC search, region filter or host summary is incorrect.'); $detail = CloudAdminVpcControlService::detail($actor, 1); $assert($detail['vpc']['name'] === 'production' && count($detail['ports']) === 1 && $detail['ports'][0]['public_ipv4'] === '8.8.8.11' && $detail['ports'][0]['public_ipv6'] === '2404:c140:156:3a::11' && $detail['host_states'][0]['ready'] === true && $detail['batches'] === [] && $detail['jobs'] === [], 'Healthy VPC detail projection is incomplete.'); $degradedDetail = CloudAdminVpcControlService::detail($actor, 2); $assert($degradedDetail['host_states'][0]['ready'] === false && $degradedDetail['ports'][0]['host_id'] === 2 && $degradedDetail['ports'][0]['host_name'] === 'HKG-B' && $degradedDetail['batches'][0]['items']['unknown'] === 1 && $degradedDetail['jobs'][0]['kind_label'] !== '' && $degradedDetail['jobs'][0]['url'] === '/admin/operations?q=91', 'VPC detail did not retain the exact task recovery navigation.'); try { CloudAdminVpcControlService::detail($actor, 999); throw new RuntimeException('Missing VPC detail was returned.'); } catch (CloudError $error) { $assert($error->httpStatus === 404, 'Missing VPC detail used the wrong response status.'); } echo "Administrator VPC control-plane read model passed.\n";