'99']; public static function get($name, $default = null) { return self::$values[$name] ?? $default; } } final class Auth { public static $user; public static function getUser() { return self::$user; } } } namespace App\Services\Cloud { final class CloudSecrets { private static array $memory = []; public static function key(string $purpose): string { return str_repeat('k', 32); } public static function encrypt(array $payload, string $purpose): string { $id = bin2hex(random_bytes(16)); self::$memory[$id] = $payload; return 'test-only:' . $id; } public static function decrypt(string $cipher, string $purpose): array { return self::$memory[substr($cipher, 10)]; } } } namespace { require dirname(__DIR__) . '/vendor/autoload.php'; use Illuminate\Database\Capsule\Manager as Capsule; use Illuminate\Database\Schema\Blueprint; use App\Models\User; use App\Models\Shop; use App\Models\CloudHost; use App\Models\CloudHostCapacityPolicy; use App\Models\CloudHostNetworkAttachment; use App\Models\CloudHostResourcePoolMembership; use App\Models\CloudInstance; use App\Models\CloudInvoice; use App\Models\CloudInvoicePayment; use App\Models\CloudMailOutbox; use App\Models\CloudNetworkLine; use App\Models\CloudOffer; use App\Models\CloudOrder; use App\Models\CloudJob; use App\Models\CloudPublicIp; use App\Models\CloudPublicIpPool; use App\Models\CloudRegion; use App\Models\CloudResourcePool; use App\Models\CloudResourceReservation; use App\Models\CloudTemplate; use App\Services\Cloud\CloudInvoiceService; use App\Services\Cloud\CloudInvoicePaymentService; use App\Services\Cloud\CloudOrderService; use App\Services\Cloud\CloudWorkerService; use App\Services\Cloud\CloudAgentClient; use App\Services\Cloud\CloudAccess; use App\Services\Cloud\CloudJobService; use App\Services\Cloud\CloudError; use App\Services\Cloud\CloudMoney; use App\Services\Cloud\CloudPlatformTestNetworkPolicy; use App\Services\Cloud\CloudDeploymentPageService; use App\Services\Cloud\CloudTemplateCatalogService; use GuzzleHttp\Client; use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\HandlerStack; use GuzzleHttp\Middleware; use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Exception\ConnectException; $assert = function ($condition, $message) { if (!$condition) { throw new RuntimeException($message); } }; /** Strictly unwrap one local PHP-to-Gateway request into its Agent request. */ $agentRequest = static function (array $historyEntry) use ($assert): array { $outer = $historyEntry['request'] ?? null; $assert($outer instanceof \Psr\Http\Message\RequestInterface, 'Gateway history entry is missing its outer request.'); $uri = $outer->getUri(); $assert($outer->getMethod() === 'POST' && $uri->getScheme() === 'http' && $uri->getHost() === '127.0.0.1' && $uri->getPort() === 8001 && $uri->getPath() === '/internal/agent/request' && $uri->getQuery() === '' && $uri->getFragment() === '', 'Agent request did not use the fixed local Gateway endpoint.'); $envelope = json_decode((string) $outer->getBody(), true, 32, JSON_THROW_ON_ERROR); $assert(is_array($envelope) && !array_is_list($envelope), 'Gateway request envelope is not a JSON object.'); $actualKeys = array_keys($envelope); $expectedKeys = [ 'agent_id', 'type', 'id', 'method', 'request_target', 'body', 'headers', ]; sort($actualKeys); sort($expectedKeys); $assert($actualKeys === $expectedKeys && ($envelope['agent_id'] ?? null) === '1' && ($envelope['type'] ?? null) === 'request' && is_string($envelope['id'] ?? null) && preg_match('/^[a-f0-9]{32}$/D', $envelope['id']) === 1 && is_string($envelope['method'] ?? null) && in_array($envelope['method'], ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'], true) && is_string($envelope['request_target'] ?? null) && preg_match('#^/v1(?:/|$)#D', $envelope['request_target']) === 1 && is_string($envelope['body'] ?? null) && is_array($envelope['headers'] ?? null) && !array_is_list($envelope['headers']), 'Gateway request envelope has an invalid shape.'); $headers = $envelope['headers']; $actualHeaderKeys = array_keys($headers); $expectedHeaderKeys = [ 'X-Virt-Timestamp', 'X-Virt-Nonce', 'X-Virt-Content-SHA256', 'X-Virt-Actor', 'X-Virt-Signature', ]; sort($actualHeaderKeys); sort($expectedHeaderKeys); $assert($actualHeaderKeys === $expectedHeaderKeys && preg_match('/^[0-9]{1,20}$/D', (string) ($headers['X-Virt-Timestamp'] ?? '')) === 1 && preg_match('/^[a-f0-9]{32}$/D', (string) ($headers['X-Virt-Nonce'] ?? '')) === 1 && ($headers['X-Virt-Content-SHA256'] ?? null) === hash('sha256', $envelope['body']) && preg_match('/^[A-Za-z0-9_-]+$/D', (string) ($headers['X-Virt-Actor'] ?? '')) === 1 && preg_match('/^[a-f0-9]{64}$/D', (string) ($headers['X-Virt-Signature'] ?? '')) === 1, 'Gateway envelope lost the signed Agent request headers.'); if ($envelope['body'] !== '') { $innerBody = json_decode($envelope['body'], false, 64, JSON_THROW_ON_ERROR); $assert($innerBody instanceof \stdClass, 'Agent request body is not a JSON object.'); } $encodedActor = $headers['X-Virt-Actor']; $padding = str_repeat('=', (4 - strlen($encodedActor) % 4) % 4); $decodedActor = base64_decode( strtr($encodedActor, '-_', '+/') . $padding, true ); $actorPayload = is_string($decodedActor) ? json_decode($decodedActor, true, 16, JSON_THROW_ON_ERROR) : null; $assert(is_array($actorPayload) && !array_is_list($actorPayload) && is_int($actorPayload['user_id'] ?? null) && is_bool($actorPayload['is_admin'] ?? null) && is_bool($actorPayload['is_super_admin'] ?? null) && is_bool($actorPayload['can_manage'] ?? null) && (!isset($actorPayload['operation_id']) || (is_string($actorPayload['operation_id']) && preg_match('/^[A-Za-z0-9_-]{8,80}$/D', $actorPayload['operation_id']) === 1)), 'Gateway envelope contains an invalid signed Agent actor.'); return $envelope; }; $capsule = new Capsule(); $capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default'); $capsule->setAsGlobal(); $capsule->bootEloquent(); $schema = $capsule->schema('default'); $schema->create('user', function (Blueprint $t) { $t->increments('id'); $t->string('money'); $t->integer('enable')->default(1); $t->integer('is_admin')->default(0); $t->integer('restrict_balance_purchase')->default(0); }); $schema->create('shop', function (Blueprint $t) { $t->increments('id'); $t->string('name'); foreach (['cpu_cores', 'memory_mib', 'bandwidth_mbps', 'disk_gib', 'validity_days', 'traffic_quota_bytes', 'status', 'sort'] as $v) { $t->integer($v)->default(0); } }); $schema->create('cloud_regions', function (Blueprint $t) { $t->increments('id'); $t->string('code'); $t->string('name'); $t->string('country_code'); $t->string('status'); $t->integer('sort')->default(0); $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_network_lines', function (Blueprint $t) { $t->increments('id'); $t->integer('region_id'); $t->string('code'); $t->string('name'); $t->integer('is_default')->default(0); $t->string('status'); $t->integer('sort')->default(0); $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_resource_pools', function (Blueprint $t) { $t->increments('id'); $t->integer('line_id'); $t->string('code'); $t->string('name'); $t->string('status'); $t->string('scheduling_policy')->default('balanced'); $t->integer('sort')->default(0); $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_hosts', function (Blueprint $t) { $t->increments('id'); $t->string('name'); $t->string('region')->default(''); $t->string('country_code')->default(''); $t->integer('enabled'); $t->integer('resource_pool_id')->nullable(); $t->string('scheduler_state')->default('disabled'); $t->string('default_network'); $t->integer('created_at')->default(0); $t->integer('updated_at')->default(0); }); $schema->create('cloud_host_agents', function (Blueprint $t) { $t->integer('host_id')->primary(); $t->text('sealed_api_secret'); $t->integer('enabled')->default(0); $t->integer('revision')->default(1); $t->integer('tested_revision')->default(0); $t->string('last_test_status')->default('never'); $t->string('last_error_code')->default(''); $t->string('last_agent_version')->default(''); $t->integer('last_tested_at')->default(0); $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_host_resource_pool_memberships', function (Blueprint $t) { $t->increments('id'); $t->integer('host_id'); $t->integer('resource_pool_id'); $t->string('scheduler_state')->default('disabled'); $t->integer('priority')->default(0); $t->integer('weight')->default(100); $t->integer('created_at'); $t->integer('updated_at'); $t->unique(['host_id', 'resource_pool_id']); }); $schema->create('cloud_host_network_attachments', function (Blueprint $t) { $t->increments('id'); $t->string('attachment_key'); $t->integer('host_id'); $t->integer('line_id'); $t->string('name'); $t->string('network_mode')->default('selector'); $t->string('network_selector'); $t->integer('vlan_id')->nullable(); $t->integer('routing_table')->nullable(); $t->string('state')->default('active'); $t->integer('priority')->default(0); $t->integer('desired_revision')->default(1); $t->integer('observed_revision')->default(0); $t->string('sync_state')->default('never'); $t->string('last_error_code')->default(''); $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_public_ip_pools', function (Blueprint $t) { $t->increments('id'); $t->string('pool_key'); $t->integer('host_id'); $t->integer('network_attachment_id'); $t->string('name'); $t->string('cidr'); $t->string('gateway'); $t->integer('guest_prefix_length'); $t->text('dns_servers_json'); $t->text('reserved_ips_json'); $t->string('state')->default('active'); $t->integer('priority')->default(0); $t->integer('desired_revision')->default(1); $t->integer('observed_revision')->default(0); $t->string('sync_state')->default('never'); $t->string('last_error_code')->default(''); $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_host_capacity_policies', function (Blueprint $t) { $t->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 $v) { $t->integer($v)->default(0); } }); $schema->create('cloud_public_ips', function (Blueprint $t) { $t->increments('id'); $t->integer('host_id'); $t->integer('public_ip_pool_id')->nullable(); $t->string('address')->unique(); $t->integer('prefix_length'); $t->string('state'); $t->integer('reservation_id')->nullable()->unique(); $t->integer('instance_id')->nullable(); $t->string('operation_id')->nullable()->unique(); $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_offers', function (Blueprint $t) { $t->increments('id'); $t->integer('plan_id'); $t->integer('resource_pool_id'); $t->string('code')->unique(); $t->string('name'); $t->string('price'); $t->string('currency')->default('USD'); $t->integer('price_version')->default(1); $t->integer('revision')->default(1); $t->integer('status')->default(1); $t->integer('stock')->default(-1); $t->integer('sort')->default(0); $t->integer('created_at'); $t->integer('updated_at'); $t->unique(['plan_id', 'resource_pool_id']); }); $schema->create('cloud_templates', function (Blueprint $t) { $t->string('id')->primary(); $t->string('name'); $t->string('os_family'); $t->string('os_version'); $t->string('source_type'); $t->text('sealed_source_url'); $t->integer('image_bytes'); $t->string('sha256'); $t->text('startup_script'); $t->integer('revision'); $t->string('generation_token'); $t->string('state'); $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_template_replicas', function (Blueprint $t) { $t->integer('host_id'); $t->string('template_id'); $t->integer('revision'); $t->string('generation_token'); $t->string('state')->default('pending'); $t->integer('image_bytes')->default(0); $t->string('sha256')->default(''); $t->string('operation_id')->default(''); $t->string('error_code')->default(''); $t->integer('created_at'); $t->integer('updated_at'); $t->primary(['host_id', 'template_id']); }); $schema->create('cloud_platform_test_policy_state', function (Blueprint $t) { $t->integer('id')->primary(); $t->integer('revision')->default(0); $t->integer('updated_by')->nullable(); $t->integer('updated_at')->default(0); }); $schema->create('cloud_platform_test_policy_rules', function (Blueprint $t) { $t->increments('id'); $t->string('rule_key', 32)->unique(); $t->string('service', 120); $t->string('protocol', 8); $t->integer('port_from'); $t->integer('port_to'); $t->string('direction', 8); $t->string('reason', 500); $t->integer('created_by'); $t->integer('updated_by'); $t->integer('deleted_by')->nullable(); $t->integer('deleted_at')->nullable(); $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_orders', function (Blueprint $t) { $t->increments('id'); foreach (['user_id', 'plan_id', 'offer_id', 'resource_pool_id'] as $v) { $t->integer($v); } $t->integer('host_id')->nullable(); $t->integer('reservation_id')->nullable()->unique(); $t->string('idempotency_key'); $t->string('request_hash'); $t->string('amount'); $t->string('currency')->default('USD'); $t->string('status'); $t->text('spec_json'); $t->text('sealed_payload')->nullable(); $t->integer('instance_id')->nullable(); $t->integer('refunded_at')->nullable(); $t->integer('stock_reserved_at')->nullable(); $t->integer('stock_released_at')->nullable(); $t->integer('created_at'); $t->integer('updated_at'); $t->unique(['user_id', 'idempotency_key']); }); $schema->create('cloud_resource_reservations', function (Blueprint $t) { $t->increments('id'); $t->string('reservation_key')->unique(); $t->string('operation_id')->unique(); foreach (['order_id', 'offer_id', 'resource_pool_id', 'host_id', 'user_id'] as $v) { $t->integer($v); } $t->integer('instance_id')->nullable(); $t->integer('network_attachment_id')->nullable(); $t->integer('public_ip_pool_id')->nullable(); $t->integer('public_ip_id')->nullable(); foreach (['cpu_cores', 'memory_mib', 'disk_gib'] as $v) { $t->integer($v); } $t->string('state'); $t->integer('expires_at')->nullable(); $t->integer('released_at')->nullable(); $t->string('release_reason')->default(''); $t->integer('created_at'); $t->integer('updated_at'); $t->unique('order_id'); }); $schema->create('cloud_jobs', function (Blueprint $t) { $t->increments('id'); $t->string('operation_id')->unique(); foreach (['order_id', 'instance_id', 'snapshot_id'] as $v) { $t->integer($v)->nullable(); } foreach (['user_id', 'host_id'] as $v) { $t->integer($v); } $t->string('kind'); $t->string('status'); $t->text('sealed_payload'); $t->integer('attempts')->default(0); $t->integer('dispatched_at')->nullable(); $t->string('lease_token')->nullable(); $t->integer('lease_until')->default(0); $t->integer('available_at'); $t->string('error_code')->nullable(); $t->text('result_json')->nullable(); $t->integer('created_at'); $t->integer('updated_at'); $t->unique('order_id'); }); $schema->create('cloud_instances', function (Blueprint $t) { $t->increments('id'); foreach (['host_id', 'user_id'] as $v) { $t->integer($v); } foreach (['order_id', 'plan_id', 'offer_id', 'resource_pool_id', 'reservation_id', 'current_period_id', 'expires_at', 'suspended_at', 'network_suspended_at'] as $v) { $t->integer($v)->nullable(); } foreach (['renewal_days', 'renewal_traffic_bytes', 'traffic_quota_bytes', 'traffic_download_bytes', 'traffic_upload_bytes', 'lifetime_download_bytes', 'lifetime_upload_bytes', 'traffic_synced_at'] as $v) { $t->integer($v)->default(0); } $t->string('renewal_price')->nullable(); $t->string('uuid'); $t->string('name'); $t->string('status'); $t->string('service_status')->default('exempt'); $t->string('network_status')->default('active'); $t->text('spec_json'); $t->text('sealed_credential')->nullable(); $t->integer('created_at'); $t->integer('updated_at'); $t->unique('order_id'); $t->unique(['host_id', 'uuid']); }); $schema->create('cloud_instance_snapshots', function (Blueprint $t) { $t->increments('id'); $t->string('snapshot_key')->unique(); foreach (['instance_id', 'user_id', 'host_id'] as $v) { $t->integer($v); } $t->string('name'); $t->string('state'); $t->string('consistency'); $t->integer('size_bytes')->nullable(); $t->string('last_operation_id')->nullable(); foreach (['ready_at', 'last_restored_at', 'deleted_at'] as $v) { $t->integer($v)->nullable(); } $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_service_periods', function (Blueprint $t) { $t->increments('id'); foreach (['instance_id', 'user_id'] as $v) { $t->integer($v); } foreach (['plan_id', 'order_id', 'renewal_id', 'download_base_bytes', 'upload_base_bytes', 'activated_at'] as $v) { $t->integer($v)->nullable(); } foreach (['starts_at', 'ends_at', 'traffic_quota_bytes', 'download_used_bytes', 'upload_used_bytes', 'created_at', 'updated_at'] as $v) { $t->integer($v)->default(0); } $t->string('status'); $t->unique('order_id'); $t->unique('renewal_id'); }); $schema->create('cloud_invoices', function (Blueprint $t) { $t->increments('id'); $t->string('invoice_no')->unique(); $t->string('purpose')->default('renewal'); $t->integer('instance_id')->nullable(); $t->integer('order_id')->nullable()->unique(); $t->integer('user_id'); $t->integer('plan_id')->nullable(); $t->integer('offer_id')->nullable(); $t->integer('renewal_id')->nullable(); $t->integer('renews_expires_at')->nullable(); $t->integer('due_at'); $t->string('amount'); $t->string('currency')->default('USD'); $t->integer('validity_days'); $t->integer('traffic_quota_bytes'); $t->string('status'); $t->string('paid_via')->nullable(); $t->string('payment_reference')->nullable(); $t->integer('paid_at')->nullable(); $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_invoice_payments', function (Blueprint $t) { $t->increments('id'); $t->integer('invoice_id'); $t->integer('user_id'); $t->string('provider'); $t->string('provider_order_no'); $t->string('idempotency_key'); $t->string('amount'); $t->string('status'); $t->integer('created_at'); $t->integer('updated_at'); $t->unique(['provider', 'provider_order_no']); $t->unique(['user_id', 'idempotency_key']); }); $schema->create('cloud_mail_outbox', function (Blueprint $t) { $t->increments('id'); $t->integer('invoice_id'); $t->string('kind'); $t->string('status'); $t->integer('attempts'); $t->integer('available_at'); $t->string('lease_token')->nullable(); $t->integer('lease_until'); $t->integer('sent_at')->nullable(); $t->string('last_error'); $t->integer('created_at'); $t->integer('updated_at'); $t->unique(['invoice_id', 'kind']); }); $schema->create('cloud_ssh_keys', function (Blueprint $t) { $t->increments('id'); $t->integer('user_id'); $t->string('name'); $t->text('public_key'); $t->string('fingerprint'); $t->integer('created_at'); }); $capsule->connection('default')->table( 'cloud_platform_test_policy_state' )->insert(['id' => 1, 'revision' => 0, 'updated_by' => null, 'updated_at' => 0]); $user = new User(); $user->id = 1; $user->money = '0.00'; $user->enable = 1; $user->is_admin = false; $user->save(); $user->isLogin = true; \App\Services\Auth::$user = $user; $now = time(); $region = new CloudRegion(); $region->code = 'test-region'; $region->name = 'Test Region'; $region->country_code = 'US'; $region->status = 'active'; $region->sort = 0; $region->created_at = $region->updated_at = $now; $region->save(); $line = new CloudNetworkLine(); $line->region_id = $region->id; $line->code = 'default'; $line->name = 'Default'; $line->is_default = true; $line->status = 'active'; $line->sort = 0; $line->created_at = $line->updated_at = $now; $line->save(); $pool = new CloudResourcePool(); $pool->line_id = $line->id; $pool->code = 'general'; $pool->name = 'General'; $pool->status = 'active'; $pool->scheduling_policy = 'balanced'; $pool->sort = 0; $pool->created_at = $pool->updated_at = $now; $pool->save(); $host = new CloudHost(); $host->name = 'host'; $host->enabled = true; $host->resource_pool_id = $pool->id; $host->scheduler_state = 'active'; $host->default_network = 'public'; $host->created_at = $host->updated_at = $now; $host->save(); $capsule->connection('default')->table('cloud_host_agents')->insert([ 'host_id' => $host->id, 'sealed_api_secret' => 'test-only-agent-secret', 'enabled' => 1, 'revision' => 1, 'tested_revision' => 1, 'last_test_status' => 'healthy', 'last_error_code' => '', 'last_agent_version' => 'test', 'last_tested_at' => $now, 'created_at' => $now, 'updated_at' => $now, ]); $membership = new CloudHostResourcePoolMembership(); $membership->host_id = $host->id; $membership->resource_pool_id = $pool->id; $membership->scheduler_state = 'active'; $membership->priority = 0; $membership->weight = 100; $membership->created_at = $membership->updated_at = $now; $membership->save(); $attachment = new CloudHostNetworkAttachment(); $attachment->attachment_key = str_repeat('a', 32); $attachment->host_id = $host->id; $attachment->line_id = $line->id; $attachment->name = 'Primary selector'; $attachment->network_mode = 'selector'; $attachment->network_selector = 'public'; $attachment->state = 'active'; $attachment->priority = 0; $attachment->desired_revision = 1; $attachment->observed_revision = 1; $attachment->sync_state = 'applied'; $attachment->last_error_code = ''; $attachment->created_at = $attachment->updated_at = $now; $attachment->save(); $sourcePoolKey = str_repeat('1', 32); $targetPoolKey = str_repeat('2', 32); $sourcePublicPool = new CloudPublicIpPool(); $sourcePublicPool->pool_key = $sourcePoolKey; $sourcePublicPool->host_id = $host->id; $sourcePublicPool->network_attachment_id = $attachment->id; $sourcePublicPool->name = 'Primary public pool'; $sourcePublicPool->cidr = '203.0.113.16/29'; $sourcePublicPool->gateway = '203.0.113.17'; $sourcePublicPool->guest_prefix_length = 28; $sourcePublicPool->dns_servers_json = '["1.1.1.1"]'; $sourcePublicPool->reserved_ips_json = '[]'; $sourcePublicPool->state = 'active'; $sourcePublicPool->priority = 0; $sourcePublicPool->desired_revision = 1; $sourcePublicPool->observed_revision = 1; $sourcePublicPool->sync_state = 'applied'; $sourcePublicPool->last_error_code = ''; $sourcePublicPool->created_at = $sourcePublicPool->updated_at = $now; $sourcePublicPool->save(); $targetPublicPool = new CloudPublicIpPool(); $targetPublicPool->pool_key = $targetPoolKey; $targetPublicPool->host_id = $host->id; $targetPublicPool->network_attachment_id = $attachment->id; $targetPublicPool->name = 'Secondary public pool'; $targetPublicPool->cidr = '203.0.113.32/29'; $targetPublicPool->gateway = '203.0.113.33'; $targetPublicPool->guest_prefix_length = 28; $targetPublicPool->dns_servers_json = '["1.0.0.1"]'; $targetPublicPool->reserved_ips_json = '[]'; $targetPublicPool->state = 'active'; $targetPublicPool->priority = 10; $targetPublicPool->desired_revision = 1; $targetPublicPool->observed_revision = 1; $targetPublicPool->sync_state = 'applied'; $targetPublicPool->last_error_code = ''; $targetPublicPool->created_at = $targetPublicPool->updated_at = $now; $targetPublicPool->save(); $policy = new CloudHostCapacityPolicy(); $policy->host_id = $host->id; $policy->configured_cpu_cores = 64; $policy->configured_memory_mib = 131072; $policy->configured_disk_gib = 4096; $policy->safety_cpu_cores = 2; $policy->safety_memory_mib = 2048; $policy->safety_disk_gib = 64; $policy->max_instances = 100; $policy->created_at = $policy->updated_at = $now; $policy->save(); foreach (['203.0.113.18', '203.0.113.19'] as $address) { $ip = new CloudPublicIp(); $ip->host_id = $host->id; $ip->public_ip_pool_id = $sourcePublicPool->id; $ip->address = $address; $ip->prefix_length = 28; $ip->state = 'available'; $ip->created_at = $ip->updated_at = $now; $ip->save(); } foreach (['203.0.113.34', '203.0.113.35'] as $address) { $ip = new CloudPublicIp(); $ip->host_id = $host->id; $ip->public_ip_pool_id = $targetPublicPool->id; $ip->address = $address; $ip->prefix_length = 28; $ip->state = 'available'; $ip->created_at = $ip->updated_at = $now; $ip->save(); } $plan = new Shop(); $plan->name = 'plan'; $plan->cpu_cores = 2; $plan->memory_mib = 2048; $plan->bandwidth_mbps = 100; $plan->disk_gib = 20; $plan->validity_days = 30; $plan->traffic_quota_bytes = 1024 * 1024 * 1024; $plan->status = 1; $plan->sort = 0; $plan->save(); $soldOutPlan = new Shop(); $soldOutPlan->name = 'sold-out-plan'; $soldOutPlan->cpu_cores = 2; $soldOutPlan->memory_mib = 2048; $soldOutPlan->bandwidth_mbps = 100; $soldOutPlan->disk_gib = 20; $soldOutPlan->validity_days = 30; $soldOutPlan->traffic_quota_bytes = 1024 * 1024 * 1024; $soldOutPlan->status = 1; $soldOutPlan->sort = 0; $soldOutPlan->save(); $offer = new CloudOffer(); $offer->plan_id = $plan->id; $offer->resource_pool_id = $pool->id; $offer->code = 'test-offer'; $offer->name = 'Test Offer'; $offer->price = '9.99'; $offer->currency = 'USD'; $offer->price_version = 1; $offer->status = 1; $offer->stock = 10; $offer->sort = 0; $offer->created_at = $offer->updated_at = $now; $offer->save(); $soldOutOffer = new CloudOffer(); $soldOutOffer->plan_id = $soldOutPlan->id; $soldOutOffer->resource_pool_id = $pool->id; $soldOutOffer->code = 'sold-out-offer'; $soldOutOffer->name = 'Sold Out Offer'; $soldOutOffer->price = '8.99'; $soldOutOffer->currency = 'USD'; $soldOutOffer->price_version = 1; $soldOutOffer->status = 1; $soldOutOffer->stock = 0; $soldOutOffer->sort = 0; $soldOutOffer->created_at = $soldOutOffer->updated_at = $now; $soldOutOffer->save(); $template = new CloudTemplate(); $template->id = 'alma10-1234abcd-0123456789abcdef'; $template->name = 'AlmaLinux 10'; $template->os_family = 'AlmaLinux'; $template->os_version = '10'; $template->source_type = 'remote'; $template->sealed_source_url = \App\Services\Cloud\CloudSecrets::encrypt([ 'url' => 'https://example.invalid/alma10.qcow2', ], 'template-source'); $template->image_bytes = 1024; $template->sha256 = str_repeat('a', 64); $template->startup_script = ''; $template->revision = 1; $template->generation_token = str_repeat('b', 32); $template->state = 'active'; $template->created_at = $template->updated_at = $now; $template->save(); $reinstallTemplate = new CloudTemplate(); $reinstallTemplate->id = 'debian13-1234abcd-0123456789abcdef'; $reinstallTemplate->name = 'Debian 13'; $reinstallTemplate->os_family = 'Debian'; $reinstallTemplate->os_version = '13'; $reinstallTemplate->source_type = 'remote'; $reinstallTemplate->sealed_source_url = \App\Services\Cloud\CloudSecrets::encrypt([ 'url' => 'https://example.invalid/debian13.qcow2', ], 'template-source'); $reinstallTemplate->image_bytes = 2048; $reinstallTemplate->sha256 = str_repeat('c', 64); $reinstallTemplate->startup_script = ''; $reinstallTemplate->revision = 1; $reinstallTemplate->generation_token = str_repeat('d', 32); $reinstallTemplate->state = 'active'; $reinstallTemplate->created_at = $reinstallTemplate->updated_at = $now; $reinstallTemplate->save(); $capsule->connection('default')->table('cloud_template_replicas')->insert([ 'host_id' => $host->id, 'template_id' => $template->id, 'revision' => $template->revision, 'generation_token' => $template->generation_token, 'state' => 'active', 'image_bytes' => $template->image_bytes, 'sha256' => $template->sha256, 'operation_id' => 'fixture-template-import', 'error_code' => '', 'created_at' => $now, 'updated_at' => $now, ]); $actor = ['user_id' => 1, 'is_admin' => false, 'is_super_admin' => false, 'can_manage' => false, 'session_id' => '']; $input = ['offer_id' => 1, 'source' => 'cloud', 'name' => 'vm-one', 'template_id' => 'alma10-1234abcd-0123456789abcdef', 'idempotency_key' => 'test-request-key-0001']; $catalogConnection = Capsule::connection('default'); $catalogConnection->flushQueryLog(); $catalogConnection->enableQueryLog(); $catalog = CloudDeploymentPageService::saleCatalog(); $catalogQueryCount = count($catalogConnection->getQueryLog()); $catalogConnection->disableQueryLog(); $assert(count($catalog['offers'] ?? []) === 2 && ($catalog['offers'][0]['sellable'] ?? null) === true && ($catalog['offers'][1]['sellable'] ?? null) === false && $catalogQueryCount <= 5, 'Catalog availability hid a sold-out offer, misclassified capacity, or queried per offer.'); $membership->scheduler_state = 'disabled'; $membership->updated_at = time(); $membership->save(); $membershipUnavailable = CloudDeploymentPageService::saleCatalog(); $assert(($membershipUnavailable['offers'][0]['sellable'] ?? null) === false, 'Legacy host scheduling fields bypassed the canonical pool membership.'); $membership->scheduler_state = 'active'; $membership->updated_at = time(); $membership->save(); // Network application state is informational; panel capacity and the // concrete IP ledger decide sellability. A failed engine apply leaves no // ledger rows for new addresses, so placement still fails closed. CloudPublicIp::query()->update(['state' => 'reserved', 'updated_at' => time()]); $noAddressCatalog = CloudDeploymentPageService::saleCatalog(); $assert(($noAddressCatalog['offers'][0]['sellable'] ?? null) === false, 'A pool without an available public IP remained sellable.'); try { CloudOrderService::purchase(array_replace($input, [ 'name' => 'vm-without-ip', 'idempotency_key' => 'test-unavailable-address-0001', ]), $actor); throw new RuntimeException('Missing public IP created an order.'); } catch (CloudError $e) { $assert($e->httpStatus === 409, 'Missing public IP returned the wrong status.'); } $assert(CloudOrder::count() === 0 && CloudInvoice::count() === 0 && CloudOffer::find(1)->stock === 10, 'Address admission failure created finance state or consumed stock.'); CloudPublicIp::query()->update(['state' => 'available', 'updated_at' => time()]); $first = CloudOrderService::purchase($input, $actor); $same = CloudOrderService::purchase($input, $actor); $openingInvoice = CloudInvoice::where('order_id', $first->id)->first(); $openingDisplay = $openingInvoice === null ? [] : CloudInvoiceService::display($openingInvoice); $connection = Capsule::connection('default'); $connection->flushQueryLog(); $connection->enableQueryLog(); $openingSummaries = $openingInvoice === null ? [] : CloudInvoiceService::listSummaries(array_fill(0, 25, $openingInvoice)); $summaryQueryCount = count($connection->getQueryLog()); $connection->disableQueryLog(); $assert($first->id === $same->id && (string) $first->status === 'awaiting_payment' && $openingInvoice !== null && (string) $openingInvoice->purpose === 'provision' && $openingInvoice->instance_id === null && ($openingDisplay['purpose'] ?? null) === 'provision' && ($openingDisplay['title'] ?? null) === '实例开通' && ($openingDisplay['type_label'] ?? null) === '实例开通' && ($openingDisplay['cpu_cores'] ?? null) === 2 && ($openingDisplay['memory_mib'] ?? null) === 2048 && !array_key_exists('invoice_no', $openingDisplay) && ($openingDisplay['order_status'] ?? null) === 'awaiting_payment' && User::find(1)->money === '0.00' && CloudJob::count() === 0 && CloudOffer::find(1)->stock === 9, 'Idempotent deployment request charged money, queued work, lost its opening invoice, or reserved stock twice.'); $assert(count($openingSummaries) === 25 && $summaryQueryCount <= 3 && array_keys($openingSummaries[0] ?? []) === [ 'id', 'title', 'type_label', 'purpose', 'amount', 'status', 'created_at', 'order_status', 'fulfillment_status', 'job_status', ] && ($openingSummaries[0]['order_status'] ?? null) === 'awaiting_payment', 'Invoice list summaries are not compact or use per-invoice association queries.'); try { CloudOrderService::purchase($input + ['vcpu' => 64], $actor); throw new RuntimeException('Resource bypass accepted.'); } catch (CloudError $e) {} try { CloudOrderService::purchase(array_replace($input, ['name' => 'changed']), $actor); throw new RuntimeException('Idempotency conflict accepted.'); } catch (CloudError $e) { $assert($e->httpStatus === 409, 'Wrong idempotency response.'); } try { CloudInvoiceService::payBalance($openingInvoice, array_replace($actor, ['user_id' => 2]), 'provision-cross-tenant-0001'); throw new RuntimeException('Another tenant paid an opening invoice.'); } catch (CloudError $e) { $assert($e->httpStatus === 403, 'Opening invoice ownership failure leaked another status.'); } $assert(User::find(1)->money === '0.00' && CloudOrder::find($first->id)->status === 'awaiting_payment' && CloudJob::count() === 0, 'Cross-tenant opening-invoice access changed financial or queue state.'); try { CloudInvoiceService::payBalance( $openingInvoice, $actor, 'provision-insufficient-balance-0001'); throw new RuntimeException('Opening invoice was paid without sufficient balance.'); } catch (CloudError $e) { $assert($e->httpStatus === 409, 'Insufficient opening-invoice balance returned the wrong status.'); } $assert(CloudOrder::find($first->id)->status === 'awaiting_payment' && CloudInvoice::find($openingInvoice->id)->status === 'open' && CloudJob::count() === 0, 'Failed opening-invoice payment changed invoice, order, or queue state.'); $user = User::find(1); $user->money = '100.00'; $user->save(); // Capacity must remain tied to the immutable order snapshot even if an // administrator changes the base plan while the invoice is still open. $changedPlan = Shop::find($plan->id); $changedPlan->cpu_cores = 8; $changedPlan->memory_mib = 8192; $changedPlan->disk_gib = 80; $changedPlan->save(); $policyManager = [ 'user_id' => 99, 'is_admin' => true, 'is_super_admin' => true, 'can_manage' => true, ]; $testCatalog = CloudPlatformTestNetworkPolicy::create([ 'expected_revision' => 0, 'service' => 'Provision snapshot test', 'protocol' => 'tcp', 'port' => '8080', 'direction' => 'ingress', 'reason' => 'Verify queued creates retain deleted catalog rules.', ], $policyManager); $testRuleKey = (string) $testCatalog['rules'][0]['id']; $paidOpening = CloudInvoiceService::payBalance( $openingInvoice, $actor, 'provision-balance-test-0001'); $paidOpeningAgain = CloudInvoiceService::payBalance( $paidOpening->fresh(), $actor, 'provision-balance-test-0001'); $openingOrder = CloudOrder::find($first->id); $openingReservation = CloudResourceReservation::where('order_id', $first->id)->first(); $openingJob = CloudJob::where('order_id', $first->id)->first(); $queuedCreate = \App\Services\Cloud\CloudSecrets::decrypt( (string) $openingJob->sealed_payload, 'job-payload' ); $queuedPolicy = $queuedCreate['body']['platform_network_policy_snapshot'] ?? null; $orderPolicy = json_decode( (string) $openingOrder->spec_json, true, flags: JSON_THROW_ON_ERROR )['platform_network_policy_snapshot'] ?? null; $openingSpec = json_decode( (string) $openingOrder->spec_json, true, flags: JSON_THROW_ON_ERROR ); CloudPlatformTestNetworkPolicy::delete( $testRuleKey, ['expected_revision' => 1], $policyManager ); $currentPolicy = CloudPlatformTestNetworkPolicy::snapshotForProvisioning(); $sealedJobAfterDelete = CloudJob::where('order_id', $first->id)->first(); $sealedCreateAfterDelete = \App\Services\Cloud\CloudSecrets::decrypt( (string) $sealedJobAfterDelete->sealed_payload, 'job-payload' ); $queuedPolicyAfterDelete = $sealedCreateAfterDelete['body'] ['platform_network_policy_snapshot'] ?? null; $orderPolicyAfterDelete = json_decode( (string) CloudOrder::find($first->id)->spec_json, true, flags: JSON_THROW_ON_ERROR )['platform_network_policy_snapshot'] ?? null; $assert((string) $paidOpeningAgain->status === 'paid' && User::find(1)->money === '90.01' && $openingOrder->status === 'queued' && (int) $openingOrder->host_id === (int) $host->id && $openingReservation !== null && $openingReservation->state === 'held' && (int) $openingReservation->cpu_cores === 2 && (int) $openingReservation->memory_mib === 2048 && (int) $openingReservation->disk_gib === 20 && (int) $openingReservation->network_attachment_id === (int) $attachment->id && (int) $openingReservation->public_ip_pool_id === (int) $sourcePublicPool->id && (string) $openingJob->operation_id === (string) $openingReservation->operation_id && CloudPublicIp::find($openingReservation->public_ip_id)->state === 'reserved' && CloudJob::where('order_id', $first->id)->count() === 1 && is_array($queuedPolicy) && $queuedPolicy === $orderPolicy && ($queuedPolicy['catalog_revision'] ?? null) === 1 && ($queuedPolicy['rules'][9]['id'] ?? null) === 'test-' . $testRuleKey && $queuedPolicyAfterDelete === $queuedPolicy && $orderPolicyAfterDelete === $queuedPolicy && ($currentPolicy['catalog_revision'] ?? null) === 2 && count($currentPolicy['rules']) === 9 && ($openingSpec['template_id'] ?? null) === $template->id && ($openingSpec['os_source'] ?? null) === 'cloud_template' && ($openingSpec['os_name'] ?? null) === 'AlmaLinux 10' && ($openingSpec['login_name'] ?? null) === 'root' && !array_key_exists('managed', $openingSpec) && CloudPlatformTestNetworkPolicy::assertSnapshot($queuedPolicy) === $queuedPolicy && $openingOrder->sealed_payload === null, 'Opening payment did not atomically freeze an immutable per-instance platform policy.'); $changedPlan->cpu_cores = 2; $changedPlan->memory_mib = 2048; $changedPlan->disk_gib = 20; $changedPlan->save(); $history = []; $createdVm = [ 'uuid' => '11111111-1111-4111-8111-111111111111', 'name' => '1:vm-111111111111', 'vcpu' => 2, 'memory_mib' => 2048, 'disk_gib' => 20, 'disks' => [[ 'device' => 'disk', 'format' => 'qcow2', ]], 'autostart' => true, 'public_pool_key' => $sourcePoolKey, 'public_ip_cidr' => '203.0.113.18/28', 'console_port' => 5908, 'generated_password' => 'must-not-persist', ]; $mismatchedVm = array_replace($createdVm, [ 'public_ip_cidr' => '203.0.113.19/28', ]); $mock = new MockHandler([ new Response(200, [], json_encode([ 'operation_id' => $openingJob->operation_id, 'status' => 'succeeded', 'result' => $mismatchedVm, ])), ]); $stack = HandlerStack::create($mock); $stack->push(Middleware::history($history)); $worker = new CloudWorkerService(new CloudAgentClient( '1', 'test-secret-which-is-at-least-32-bytes', new Client(['handler' => $stack]), 20.0, 'http://127.0.0.1:8001/internal/agent/request', 'test-gateway-secret-at-least-32-bytes' )); $worker->tick(); $openingJob->refresh(); $openingReservation->refresh(); $assert($openingJob->status === 'uncertain' && CloudOrder::find($first->id)->status === 'uncertain' && $openingReservation->state === 'uncertain' && CloudPublicIp::find($openingReservation->public_ip_id)->state === 'uncertain' && CloudInstance::count() === 0 && User::find(1)->money === '90.01', 'A create result with a mismatched public IP was committed or refunded.'); $openingJob->available_at = 0; $openingJob->save(); $poolMismatchedVm = array_replace($createdVm, [ 'public_pool_key' => str_repeat('f', 32), ]); $mock->append(new Response(200, [], json_encode([ 'operation_id' => $openingJob->operation_id, 'status' => 'succeeded', 'result' => $poolMismatchedVm, ]))); $worker->tick(); $openingJob->refresh(); $openingReservation->refresh(); $assert($openingJob->status === 'uncertain' && $openingReservation->state === 'uncertain' && CloudPublicIp::find($openingReservation->public_ip_id)->state === 'uncertain' && CloudInstance::count() === 0, 'A create result with a mismatched public pool was committed.'); $openingJob->available_at = 0; $openingJob->save(); $mock->append(new Response(200, [], json_encode([ 'operation_id' => $openingJob->operation_id, 'status' => 'succeeded', 'result' => $createdVm, ]))); $worker->tick(); $openingDisplay = CloudInvoiceService::display($openingInvoice->fresh()); $openingJobDisplay = CloudJobService::display(CloudJob::where('order_id', $first->id)->first()); $assert(CloudOrder::find($first->id)->status === 'succeeded' && CloudInstance::count() === 1 && \App\Models\CloudServicePeriod::count() === 1 && CloudResourceReservation::where('order_id', $first->id)->first()->state === 'committed' && CloudPublicIp::find($openingReservation->public_ip_id)->state === 'assigned' && CloudInvoice::find($openingInvoice->id)->instance_id !== null && CloudMailOutbox::where('invoice_id', $openingInvoice->id) ->where('kind', 'provision_succeeded')->count() === 1 && ($openingDisplay['order_status'] ?? null) === 'succeeded' && ($openingDisplay['instance_id'] ?? null) !== null && ($openingDisplay['job_status'] ?? null) === 'succeeded', 'Successful provision did not create its first service period or update the opening bill.'); $assert(!CloudInvoiceService::queueProvisionSucceeded( CloudInvoice::find($openingInvoice->id), time()) && CloudMailOutbox::where('invoice_id', $openingInvoice->id) ->where('kind', 'provision_succeeded')->count() === 1, 'Successful provisioning notification was not idempotent.'); $assert(($openingJobDisplay['attempts'] ?? 0) >= 1 && ($openingJobDisplay['created_at'] ?? 0) > 0 && ($openingJobDisplay['updated_at'] ?? 0) > 0, 'User-visible creation progress lacks safe timing or attempt metadata.'); $openingAgentRequest = $agentRequest($history[0]); $posted = json_decode($openingAgentRequest['body'], true); $signedActor = json_decode(base64_decode(strtr( $openingAgentRequest['headers']['X-Virt-Actor'], '-_', '+/' )), true); $assert($posted['vcpu'] === 2 && $posted['bandwidth_mbps'] === 100 && $posted['network'] === 'public' && $posted['public_pool_key'] === $sourcePoolKey && ($posted['platform_network_policy_snapshot'] ?? null) === $queuedPolicy && $signedActor['can_manage'] === true && ($signedActor['operation_id'] ?? null) === $openingJob->operation_id && !isset($signedActor['can_provision']), 'Plan snapshot or typed Agent provisioning authority was lost.'); $assert(!str_contains(CloudJob::first()->result_json, 'must-not-persist'), 'Plain password saved in result.'); $terminalPayload = \App\Services\Cloud\CloudSecrets::decrypt( CloudJob::first()->sealed_payload, 'job-payload' ); $assert(!array_key_exists('credential', $terminalPayload) && !array_key_exists('root_password', $terminalPayload['body'] ?? []), 'Terminal provisioning job retained a recoverable root password.'); $instance = CloudInstance::first(); $instanceSpec = json_decode((string) $instance->spec_json, true, flags: JSON_THROW_ON_ERROR); $assert($instance->name === 'vm-one' && ($instanceSpec['public_ip_cidr'] ?? null) === '203.0.113.18/28' && ($instanceSpec['os_source'] ?? null) === 'cloud_template' && ($instanceSpec['os_name'] ?? null) === 'AlmaLinux 10' && ($instanceSpec['login_name'] ?? null) === 'root' && ($instanceSpec['managed'] ?? null) === true && ($instanceSpec['agent_metadata_version'] ?? null) === 1 && ($instanceSpec['disk_gib'] ?? null) === 20 && !isset($instanceSpec['disks']) && !isset($instanceSpec['console_port']) && !isset($instanceSpec['disks'][0]['source']), 'Successful provisioning did not persist the safe Agent metadata snapshot.'); \App\Services\Cloud\CloudPlacementService::markUncertain($openingOrder->fresh()); $assert(CloudResourceReservation::find($openingReservation->id)->state === 'committed' && CloudPublicIp::find($openingReservation->public_ip_id)->state === 'assigned', 'A late uncertainty marker rolled a committed reservation or assigned IP backward.'); // An unpaid provisioning reservation expires without entering the worker // queue, changing the wallet, or retaining its encrypted credential. $abandoned = CloudOrderService::purchase(array_replace($input, [ 'name' => 'vm-abandoned', 'idempotency_key' => 'test-request-key-abandoned', ]), $actor); $abandonedInvoice = CloudInvoice::where('order_id', $abandoned->id)->first(); $abandonedInvoice->due_at = time() - 1; $abandonedInvoice->save(); $assert(CloudInvoiceService::expireProvision() === 1 && CloudInvoiceService::expireProvision() === 0 && CloudOrder::find($abandoned->id)->status === 'expired' && CloudOrder::find($abandoned->id)->sealed_payload === null && CloudOrder::find($abandoned->id)->stock_released_at !== null && CloudJob::where('order_id', $abandoned->id)->count() === 0 && User::find(1)->money === '90.01' && CloudOffer::find(1)->stock === 9, 'Expired opening invoice queued work, changed money, retained credentials, or released stock more than once.'); $assert(CloudAccess::instance((string) $instance->id, $actor)->id === $instance->id, 'Owner denied own VM.'); $other = array_replace($actor, ['user_id' => 2]); try { CloudAccess::instance((string) $instance->id, $other); throw new RuntimeException('Tenant isolation failed.'); } catch (CloudError $e) { $assert($e->httpStatus === 404, 'Nonowner information leaked.'); } $admin = array_replace($other, ['is_admin' => true]); $assert(CloudAccess::instance((string) $instance->id, $admin)->id === $instance->id, 'Admin readonly denied.'); try { CloudAccess::instance((string) $instance->id, $admin, true); throw new RuntimeException('Regular admin wrote other owner.'); } catch (CloudError $e) {} try { CloudJobService::enqueue($instance, array_replace($admin, ['can_manage' => true]), 'action', 'POST', '/actions', ['action' => 'reboot']); throw new RuntimeException('Scoped can_manage bypassed cross-owner queue authorization.'); } catch (CloudError $e) {} try { CloudAccess::manage($actor + ['can_provision' => true]); throw new RuntimeException('Provision actor globally privileged.'); } catch (CloudError $e) {} $failed = CloudOrderService::purchase(array_replace($input, ['name' => 'vm-two', 'idempotency_key' => 'test-request-key-0002']), $actor); $assert(User::find(1)->money === '90.01' && CloudJob::where('order_id', $failed->id)->count() === 0, 'A second unpaid deployment charged or entered the worker queue.'); CloudInvoiceService::payBalance(CloudInvoice::where('order_id', $failed->id)->first(), $actor, 'provision-balance-test-0002'); $failedJob = CloudJob::where('order_id', $failed->id)->first(); $mock->append(new Response(422, [], json_encode(['error' => [ 'code' => 'invalid_request', 'message' => '虚拟机参数校验失败', 'retryable' => false, 'dispatch_state' => 'not_dispatched', ]]))); $worker->tick(); $worker->tick(); $failedDisplay = CloudOrderService::display(CloudOrder::find($failed->id)); $failedInvoiceDisplay = CloudInvoiceService::display( CloudInvoice::where('order_id', $failed->id)->first()); $assert(User::find(1)->money === '90.01' && CloudOrder::find($failed->id)->refunded_at !== null && CloudOffer::find(1)->stock === 9, 'A definitive pre-operation Agent rejection was not refunded exactly once.'); $failedReservation = CloudResourceReservation::where('order_id', $failed->id)->first(); $assert($failedReservation !== null && $failedReservation->state === 'released' && CloudPublicIp::find($failedReservation->public_ip_id)->state === 'available', 'A safely failed create retained host capacity or its public IP.'); $assert($failedDisplay['job_status'] === 'failed' && $failedDisplay['attempts'] >= 1 && !array_key_exists('host_id', $failedDisplay) && !array_key_exists('resource_pool_id', $failedDisplay) && !array_key_exists('plan_id', $failedDisplay) && str_contains($failedDisplay['error'], '开通失败,订单已退款') && ($failedInvoiceDisplay['status'] ?? null) === 'refunded' && ($failedInvoiceDisplay['order_status'] ?? null) === 'failed' && str_contains((string) ($failedInvoiceDisplay['error'] ?? ''), '开通失败,订单已退款') && CloudMailOutbox::where('kind', 'provision_succeeded')->count() === 1, 'Safe local failure diagnostics are missing from the unified invoice display.'); $uncertain = CloudOrderService::purchase(array_replace($input, ['name' => 'vm-three', 'idempotency_key' => 'test-request-key-0003']), $actor); $assert(User::find(1)->money === '90.01' && CloudJob::where('order_id', $uncertain->id)->count() === 0, 'An unpaid ambiguous-case deployment charged or entered the worker queue.'); CloudInvoiceService::payBalance(CloudInvoice::where('order_id', $uncertain->id)->first(), $actor, 'provision-balance-test-0003'); $mock->append(new ConnectException('simulated timeout', new Request('POST', 'http://127.0.0.1:8000'))); $worker->tick(); $uncertainJob = CloudJob::where('order_id', $uncertain->id)->first(); $uncertainInvoiceDisplay = CloudInvoiceService::display( CloudInvoice::where('order_id', $uncertain->id)->first()); $uncertainReservation = CloudResourceReservation::where( 'order_id', $uncertain->id )->first(); $uncertainIp = CloudPublicIp::find($uncertainReservation->public_ip_id); $assert($uncertainJob->status === 'uncertain' && CloudOrder::find($uncertain->id)->refunded_at === null && $uncertainReservation->state === 'uncertain' && User::find(1)->money === '80.02' && ($uncertainInvoiceDisplay['order_status'] ?? null) === 'uncertain' && CloudMailOutbox::where('kind', 'provision_succeeded')->count() === 1, 'Timeout incorrectly refunded or disappeared from unified billing.'); $uncertainJob->available_at = 0; $uncertainJob->save(); $beforeMissingCreate = count($history); $mock->append(new Response(404, [], '{}')); $worker->tick(); $missingCreateRequests = array_slice($history, $beforeMissingCreate); $last = $agentRequest(end($history)); $assert(count($missingCreateRequests) === 1 && $last['method'] === 'GET' && str_contains($last['request_target'], '/operations/') && CloudJob::find($uncertainJob->id)->status === 'uncertain' && User::find(1)->money === '80.02', 'Ambiguous create was replayed, closed or refunded after Agent 404.'); $uncertainJob->refresh(); $uncertainJob->available_at = 0; $uncertainJob->save(); $reconciledVm = array_replace($createdVm, [ 'uuid' => '33333333-3333-4333-8333-333333333333', 'name' => 'vm-three', 'public_ip_cidr' => (string) $uncertainIp->address . '/' . (int) $uncertainIp->prefix_length, ]); $beforeFailedCreateReconcile = count($history); $mock->append( new Response(200, [], json_encode([ 'operation_id' => $uncertainJob->operation_id, 'status' => 'failed', 'error' => [ 'code' => 'network_not_found', 'message' => 'host network selector is not configured', ], ])), new Response(200, [], json_encode([ 'operation_id' => $uncertainJob->operation_id, 'status' => 'succeeded', 'result' => $reconciledVm, ])) ); $worker->tick(); $failedCreateReconcileRequests = array_slice( $history, $beforeFailedCreateReconcile ); $failedCreateLookup = count($failedCreateReconcileRequests) === 2 ? $agentRequest($failedCreateReconcileRequests[0]) : []; $failedCreateReconcile = count($failedCreateReconcileRequests) === 2 ? $agentRequest($failedCreateReconcileRequests[1]) : []; $assert(CloudInstance::count() === 2 && CloudOrder::find($uncertain->id)->status === 'succeeded' && CloudResourceReservation::where('order_id', $uncertain->id)->first()->state === 'committed' && User::find(1)->money === '80.02' && CloudMailOutbox::where('kind', 'provision_succeeded')->count() === 2, 'Reconciliation did not recover exactly once or queue its success email.'); $assert(count($failedCreateReconcileRequests) === 2 && ($failedCreateLookup['method'] ?? null) === 'GET' && ($failedCreateLookup['request_target'] ?? null) === '/v1/operations/' . $uncertainJob->operation_id && ($failedCreateReconcile['method'] ?? null) === 'POST' && ($failedCreateReconcile['request_target'] ?? null) === '/v1/operations/' . $uncertainJob->operation_id . '/reconcile', 'A failed create was replayed or reconciled with a different operation ID.'); $actionJob = CloudJobService::enqueue($instance, $actor, 'action', 'POST', '/actions', ['action' => 'reboot']); $mock->append(new Response(200, [], json_encode([ 'operation_id' => $actionJob->operation_id, 'status' => 'succeeded', 'result' => [ 'uuid' => '11111111-1111-4111-8111-111111111111', 'state' => 'running', ], ]))); $worker->tick(); $last = $agentRequest(end($history)); $signedActor = json_decode(base64_decode(strtr( $last['headers']['X-Virt-Actor'], '-_', '+/' )), true); $assert(CloudJob::find($actionJob->id)->status === 'succeeded' && $signedActor['can_manage'] === true && ($signedActor['operation_id'] ?? null) === $actionJob->operation_id, 'Power task lost its typed Agent authority or did not complete.'); // Keep the panel/audit vocabulary stable while translating the force-off // command to the Go Agent's strict PowerAction wire value. $poweroffJob = CloudJobService::enqueue( $instance, $actor, 'action', 'POST', '/actions', ['action' => 'poweroff'] ); $storedPoweroff = \App\Services\Cloud\CloudSecrets::decrypt( (string) $poweroffJob->sealed_payload, 'job-payload' ); $mock->append(new Response(200, [], json_encode([ 'operation_id' => $poweroffJob->operation_id, 'status' => 'succeeded', 'result' => [ 'uuid' => (string) $instance->uuid, 'state' => 'stopped', ], ]))); $worker->tick(); $poweroffRequest = $agentRequest(end($history)); $poweroffBody = json_decode( (string) ($poweroffRequest['body'] ?? ''), true, 8, JSON_THROW_ON_ERROR ); $assert(CloudJob::find($poweroffJob->id)->status === 'succeeded' && ($storedPoweroff['body']['action'] ?? null) === 'poweroff' && $poweroffBody === ['action' => 'destroy'], 'Panel poweroff was not translated exactly once at the Agent wire boundary.'); // Keep one bounded Agent cause for administrator diagnosis while the // public job contract continues to expose only the panel-owned error. $diagnosticJob = CloudJobService::enqueue( $instance, $actor, 'action', 'POST', '/actions', ['action' => 'reboot'] ); $mock->append(new Response(200, [], json_encode([ 'operation_id' => $diagnosticJob->operation_id, 'status' => 'failed', 'cause_code' => 'domain_start_failed', 'error' => [ 'code' => 'libvirt_error', 'message' => 'private path /srv/libvirt/domain.xml', ], 'result' => ['state' => 'stopped'], ]))); $worker->tick(); $diagnosticRow = CloudJob::find($diagnosticJob->id); $diagnosticResult = json_decode((string) $diagnosticRow->result_json, true, flags: JSON_THROW_ON_ERROR); $publicDiagnostic = CloudJobService::display($diagnosticRow); $assert($diagnosticRow->status === 'failed' && $diagnosticRow->error_code === 'operation_failed' && ($diagnosticResult['cause_code'] ?? null) === 'domain_start_failed' && !isset($publicDiagnostic['result']['cause_code']) && !str_contains(json_encode($publicDiagnostic, JSON_UNESCAPED_UNICODE), '/srv/libvirt'), 'Stable Agent cause was not persisted privately or leaked into the public job DTO.'); // A signed two-hop admission rejection proves that the mutation never // reached the Agent journal. Only that exact case may clear dispatched_at // and retry the same durable operation from the PHP queue. $requeueJob = CloudJobService::enqueue( $instance, $actor, 'action', 'POST', '/actions', ['action' => 'reboot'] ); $beforeRequeue = count($history); $mock->append(new Response(503, [], json_encode(['error' => [ 'code' => 'agent_busy', 'message' => 'Agent control request was not dispatched', 'retryable' => true, 'dispatch_state' => 'not_dispatched', ]]))); $worker->tick(); $requeueJob->refresh(); $assert($requeueJob->status === 'queued' && $requeueJob->dispatched_at === null && $requeueJob->lease_token === null && (string) $requeueJob->error_code === 'agent_busy', 'Definitive pre-dispatch backpressure was made permanently uncertain.'); $requeueJob->available_at = 0; $requeueJob->save(); $mock->append(new Response(200, [], json_encode([ 'operation_id' => $requeueJob->operation_id, 'status' => 'succeeded', 'result' => [ 'uuid' => (string) $instance->uuid, 'state' => 'running', ], ]))); $worker->tick(); $requeueRequests = array_slice($history, $beforeRequeue); $assert(CloudJob::find($requeueJob->id)->status === 'succeeded' && count($requeueRequests) === 2 && $agentRequest($requeueRequests[0])['method'] === 'POST' && $agentRequest($requeueRequests[1])['method'] === 'POST', 'Safe pre-dispatch retry did not reuse and complete the durable job.'); // A strict DTO rejection happens before the Agent operation journal for // every mutation kind, not only provisioning. It is terminal validation // failure and must not strand a non-create task in uncertain/404 polling. $invalidActionJob = CloudJobService::enqueue( $instance, $actor, 'action', 'POST', '/actions', ['action' => 'reboot'] ); $beforeInvalidAction = count($history); $mock->append(new Response(422, [], json_encode(['error' => [ 'code' => 'invalid_json', 'message' => 'request contains an unknown field', 'retryable' => false, 'dispatch_state' => 'not_dispatched', ]]))); $worker->tick(); $invalidActionJob->refresh(); $assert($invalidActionJob->status === 'failed' && $invalidActionJob->dispatched_at !== null && count($history) === $beforeInvalidAction + 1, 'Definitive non-create validation failure became permanently uncertain.'); // A gateway secret mismatch is a terminal configuration/authentication // rejection before WSS dispatch. It must not strand the task in uncertain // polling or loop forever in the queue. $gatewayAuthJob = CloudJobService::enqueue( $instance, $actor, 'action', 'POST', '/actions', ['action' => 'reboot'] ); $beforeGatewayAuth = count($history); $mock->append(new Response(403, [], json_encode(['error' => [ 'code' => 'gateway_forbidden', 'message' => 'Agent gateway request is forbidden', 'retryable' => false, 'dispatch_state' => 'not_dispatched', ]]))); $worker->tick(); $gatewayAuthJob->refresh(); $assert($gatewayAuthJob->status === 'failed' && $gatewayAuthJob->dispatched_at !== null && $gatewayAuthJob->lease_token === null, 'Gateway authentication failure did not terminate before journal dispatch.'); $gatewayAuthRequests = array_slice($history, $beforeGatewayAuth); $assert(count($gatewayAuthRequests) === 1 && $agentRequest($gatewayAuthRequests[0])['method'] === 'POST', 'Gateway authentication failure was replayed after a terminal response.'); // A journal-backed error envelope for another operation must never finish // this job, even when the foreign record itself is terminal. $crossBoundJob = CloudJobService::enqueue( $instance, $actor, 'action', 'POST', '/actions', ['action' => 'reset'] ); $mock->append(new Response(409, [], json_encode([ 'error' => [ 'code' => 'operation_payload_mismatch', 'retryable' => false, 'operation' => [ 'operation_id' => $actionJob->operation_id, 'status' => 'failed', 'error' => ['code' => 'operation_payload_mismatch'], ], ], ]))); $worker->tick(); $crossBoundJob->refresh(); $assert($crossBoundJob->status === 'uncertain' && $instance->fresh()->status === 'active', 'A foreign durable error envelope advanced the current job.'); $crossBoundJob->status = 'failed'; $crossBoundJob->available_at = time() + 3600; $crossBoundJob->save(); // Clean-break protocol: a legacy success shortcut without operation_id is // ambiguous and cannot be used to advance any mutation. $legacyShortcutJob = CloudJobService::enqueue( $instance, $actor, 'action', 'POST', '/actions', ['action' => 'reboot'] ); $mock->append(new Response(200, [], json_encode([ 'status' => 'succeeded', 'uuid' => (string) $instance->uuid, 'state' => 'running', ]))); $worker->tick(); $legacyShortcutJob->refresh(); $assert($legacyShortcutJob->status === 'uncertain', 'A mutation response without operation_id was accepted as success.'); $legacyShortcutJob->status = 'failed'; $legacyShortcutJob->available_at = time() + 3600; $legacyShortcutJob->save(); // Both GET and reconcile responses are bound to the current operation. // A vanished durable record is ambiguous and must never cause replay. $instance->network_status = 'blocking'; $instance->save(); $policy = new CloudJob(); $policy->operation_id = bin2hex(random_bytes(16)); $policy->instance_id = $instance->id; $policy->user_id = $instance->user_id; $policy->host_id = $instance->host_id; $policy->kind = 'network_suspend'; $policy->status = 'uncertain'; $policy->sealed_payload = \App\Services\Cloud\CloudSecrets::encrypt([ 'method' => 'POST', 'request_target' => '/v1/vms/' . $instance->uuid . '/network/suspend', 'body' => ['reason' => 'traffic'], 'owner_id' => (int) $instance->user_id, 'system_policy' => true, 'suspend_reason' => 'traffic', ], 'job-payload'); $policy->attempts = 1; $policy->dispatched_at = time() - 30; $policy->lease_until = 0; $policy->available_at = 0; $policy->created_at = $policy->updated_at = time() - 30; $policy->save(); $beforePolicyRequests = count($history); $mock->append(new Response(200, [], json_encode([ 'operation_id' => $policy->operation_id, 'status' => 'uncertain', ]))); $mock->append(new Response(200, [], json_encode([ 'operation_id' => $actionJob->operation_id, 'status' => 'succeeded', 'result' => ['network_suspended' => true], ]))); $worker->tick(); $policyRequests = array_slice($history, $beforePolicyRequests); $policyLookupRequest = count($policyRequests) === 2 ? $agentRequest($policyRequests[0]) : []; $policyReconcileRequest = count($policyRequests) === 2 ? $agentRequest($policyRequests[1]) : []; $assert(count($policyRequests) === 2 && $policyLookupRequest['method'] === 'GET' && $policyReconcileRequest['method'] === 'POST' && str_ends_with($policyReconcileRequest['request_target'], '/operations/' . $policy->operation_id . '/reconcile') && CloudJob::find($policy->id)->status === 'uncertain' && $instance->fresh()->network_status === 'blocking', 'A reconcile response for another operation advanced the policy job.'); $policy->refresh(); $policy->available_at = 0; $policy->save(); $beforePolicyRequests = count($history); $mock->append(new Response(404, [], '{}')); $worker->tick(); $policyRequests = array_slice($history, $beforePolicyRequests); $policyLookupRequest = count($policyRequests) === 1 ? $agentRequest($policyRequests[0]) : []; $assert(count($policyRequests) === 1 && $policyLookupRequest['method'] === 'GET' && !str_contains($policyLookupRequest['request_target'], '?') && CloudJob::find($policy->id)->status === 'uncertain' && $instance->fresh()->network_status === 'blocking', 'Missing target-state operation was replayed or closed automatically.'); $policy->refresh(); $policy->status = 'failed'; $policy->available_at = time() + 3600; $policy->save(); $instance->network_status = 'active'; $instance->save(); $stalePolicy = $policy->replicate(); $stalePolicy->operation_id = bin2hex(random_bytes(16)); $stalePolicy->status = 'uncertain'; $stalePolicy->result_json = null; $stalePolicy->error_code = null; $stalePolicy->lease_token = null; $stalePolicy->lease_until = 0; $stalePolicy->available_at = 0; $stalePolicy->created_at = $stalePolicy->updated_at = time() - 30; $stalePolicy->save(); $beforeStaleRequests = count($history); $mock->append(new Response(404, [], '{}')); $worker->tick(); $stalePolicyRequest = $agentRequest(end($history)); $assert(count($history) === $beforeStaleRequests + 1 && !str_contains($stalePolicyRequest['request_target'], '?') && CloudJob::find($stalePolicy->id)->status === 'uncertain' && $instance->fresh()->network_status === 'active', 'A missing stale policy operation was replayed or closed automatically.'); $stalePolicy->refresh(); $stalePolicy->status = 'failed'; $stalePolicy->available_at = time() + 3600; $stalePolicy->save(); // Desired state may reverse while the old Agent request is still inside // libvirt. The PHP job must remain nonterminal until that executor exits; // otherwise lifecycle could dispatch the opposite request concurrently. $instance->network_status = 'restore_pending'; $instance->save(); $activePolicy = $policy->replicate(); $activePolicy->operation_id = bin2hex(random_bytes(16)); $activePolicy->status = 'uncertain'; $activePolicy->result_json = null; $activePolicy->error_code = null; $activePolicy->lease_token = null; $activePolicy->lease_until = 0; $activePolicy->available_at = 0; $activePolicy->created_at = $activePolicy->updated_at = time() - 30; $activePolicy->save(); $beforeActiveRequests = count($history); $mock->append(new Response(200, [], json_encode([ 'operation_id' => $activePolicy->operation_id, 'status' => 'uncertain', ]))); $worker->tick(); $activePolicy->refresh(); $activePolicyRequest = $agentRequest(end($history)); $assert(count($history) === $beforeActiveRequests + 1 && !str_contains($activePolicyRequest['request_target'], '?') && $activePolicy->status === 'uncertain' && $activePolicy->available_at > time() && $instance->fresh()->network_status === 'restore_pending', 'An active stale policy executor was closed before libvirt finished.'); $activePolicy->available_at = 0; $activePolicy->save(); $mock->append(new Response(200, [], json_encode([ 'operation_id' => $activePolicy->operation_id, 'status' => 'succeeded', 'result' => ['network_suspended' => true], ]))); $worker->tick(); $assert(CloudJob::find($activePolicy->id)->status === 'succeeded' && $instance->fresh()->network_status === 'restore_pending', 'A completed stale policy did not preserve the newer desired state.'); // A nonterminal Agent record for an obsolete target must not be replayed // or force the newer reverse target to run concurrently. $crashedPolicy = $policy->replicate(); $crashedPolicy->operation_id = bin2hex(random_bytes(16)); $crashedPolicy->status = 'uncertain'; $crashedPolicy->result_json = null; $crashedPolicy->error_code = null; $crashedPolicy->lease_token = null; $crashedPolicy->lease_until = 0; $crashedPolicy->available_at = 0; $crashedPolicy->created_at = $crashedPolicy->updated_at = time() - 30; $crashedPolicy->save(); $beforeCrashRequests = count($history); $mock->append(new Response(200, [], json_encode([ 'operation_id' => $crashedPolicy->operation_id, 'status' => 'uncertain', ]))); $worker->tick(); $crashRequests = array_slice($history, $beforeCrashRequests); $crashLookupRequest = count($crashRequests) === 1 ? $agentRequest($crashRequests[0]) : []; $assert(count($crashRequests) === 1 && $crashLookupRequest['method'] === 'GET' && !str_contains($crashLookupRequest['request_target'], '?') && CloudJob::find($crashedPolicy->id)->status === 'uncertain' && $instance->fresh()->network_status === 'restore_pending', 'An obsolete uncertain target was replayed or allowed to overwrite the newer desired state.'); $crashedPolicy->refresh(); $crashedPolicy->available_at = 0; $crashedPolicy->save(); $mock->append(new Response(200, [], json_encode([ 'operation_id' => $crashedPolicy->operation_id, 'status' => 'succeeded', 'result' => ['network_suspended' => true], ]))); $worker->tick(); $assert(CloudJob::find($crashedPolicy->id)->status === 'succeeded' && $instance->fresh()->network_status === 'restore_pending', 'A later terminal Agent record overwrote the newer desired state.'); // The reverse-target assertions above intentionally leave the fixture in // a lifecycle transition. Restore a neutral state before testing an // unrelated tenant mutation. $instance->network_status = 'active'; $instance->save(); // A non-policy mutation whose durable record has vanished must remain // uncertain and cannot be replayed or converted into terminal failure. $missingAction = CloudJobService::enqueue($instance, $actor, 'action', 'POST', '/actions', ['action' => 'reboot']); $missingAction->status = 'uncertain'; $missingAction->dispatched_at = time() - 30; $missingAction->available_at = 0; $missingAction->save(); $beforeMissingAction = count($history); $mock->append(new Response(200, [], json_encode([ 'operation_id' => $actionJob->operation_id, 'status' => 'succeeded', 'result' => ['state' => 'running'], ]))); $worker->tick(); $assert(count($history) === $beforeMissingAction + 1 && CloudJob::find($missingAction->id)->status === 'uncertain', 'A GET response for another operation advanced a tenant mutation.'); $missingAction->refresh(); $missingAction->available_at = 0; $missingAction->save(); $beforeMissingAction = count($history); $mock->append(new Response(404, [], '{}')); $worker->tick(); $missingActionRequest = $agentRequest(end($history)); $assert(count($history) === $beforeMissingAction + 1 && $missingActionRequest['method'] === 'GET' && CloudJob::find($missingAction->id)->status === 'uncertain', 'A vanished non-policy Agent operation was replayed or marked failed.'); $missingAction->status = 'failed'; $missingAction->available_at = time() + 3600; $missingAction->save(); // Expiry networking, graceful purge shutdown, and irreversible deletion // deliberately use separate states. Only the four target-state policy // jobs may converge; the final disk deletion is a one-shot mutation. $makeLifecycleInstance = function (string $uuid, string $name, string $serviceStatus, string $networkStatus) use ($instance) { $copy = $instance->replicate(); $copy->order_id = null; $copy->current_period_id = null; $copy->uuid = $uuid; $copy->name = $name; $copy->status = 'active'; $copy->service_status = $serviceStatus; $copy->network_status = $networkStatus; $copy->suspended_at = null; $copy->network_suspended_at = null; $copy->sealed_credential = \App\Services\Cloud\CloudSecrets::encrypt([ 'password' => 'LifecycleStrongPassword!123', 'login_name' => 'root', ], 'instance-credential'); $copy->created_at = $copy->updated_at = time(); $copy->save(); return $copy; }; $queuePolicy = function (CloudInstance $target, string $kind, string $suffix, ?string $reason = null) { $job = new CloudJob(); $job->operation_id = bin2hex(random_bytes(16)); $job->instance_id = $target->id; $job->user_id = $target->user_id; $job->host_id = $target->host_id; $job->kind = $kind; $job->status = 'queued'; $job->sealed_payload = \App\Services\Cloud\CloudSecrets::encrypt([ 'method' => 'POST', 'request_target' => '/v1/vms/' . $target->uuid . '/' . $suffix, 'body' => $reason === null ? [] : ['reason' => $reason], 'owner_id' => (int) $target->user_id, 'system_policy' => true, 'suspend_reason' => $reason, ], 'job-payload'); $job->available_at = $job->created_at = $job->updated_at = time(); $job->save(); return $job; }; $expiredNetwork = $makeLifecycleInstance( '55555555-5555-4555-8555-555555555555', 'expired-network-vm', 'expired_grace', 'blocking_expired'); $expiredNetworkJob = $queuePolicy( $expiredNetwork, 'network_suspend', 'network/suspend', 'expired'); $mock->append(new Response(200, [], json_encode([ 'operation_id' => $expiredNetworkJob->operation_id, 'status' => 'succeeded', 'result' => ['network_suspended' => true], ]))); $worker->tick(); $assert(CloudJob::find($expiredNetworkJob->id)->status === 'succeeded' && $expiredNetwork->fresh()->network_status === 'blocked_expired', 'Expiry network suspension was recorded as a traffic quota block.'); $purgeStopped = $makeLifecycleInstance( '66666666-6666-4666-8666-666666666666', 'purge-stop-success-vm', 'purge_stopping', 'blocked_expired'); $purgeStopped->status = 'deleting'; $purgeStopped->save(); $purgeStopJob = $queuePolicy( $purgeStopped, 'service_suspend', 'service/suspend', 'purge'); $mock->append(new Response(200, [], json_encode([ 'operation_id' => $purgeStopJob->operation_id, 'status' => 'succeeded', 'result' => ['service_was_running' => true], ]))); $worker->tick(); $purgeStopped->refresh(); $assert(CloudJob::find($purgeStopJob->id)->status === 'succeeded' && $purgeStopped->status === 'deleting' && $purgeStopped->service_status === 'purge_deleting' && $purgeStopped->suspended_at === null, 'Successful expiry shutdown did not advance to disk deletion safely.'); $purgeStopFailed = $makeLifecycleInstance( '77777777-7777-4777-8777-777777777777', 'purge-stop-failed-vm', 'purge_stopping', 'blocked_expired'); $purgeStopFailed->status = 'deleting'; $purgeStopFailed->save(); $purgeStopFailedJob = $queuePolicy( $purgeStopFailed, 'service_suspend', 'service/suspend', 'purge'); $mock->append(new Response(409, [], json_encode([ 'operation_id' => $purgeStopFailedJob->operation_id, 'status' => 'failed', 'error' => ['code' => 'conflict', 'message' => 'shutdown rejected'], 'replayed' => false, 'retryable' => false, ]))); $worker->tick(); $purgeStopFailed->refresh(); $assert(CloudJob::find($purgeStopFailedJob->id)->status === 'failed' && $purgeStopFailed->status === 'deleting' && $purgeStopFailed->service_status === 'purge_stop_failed', 'Failed expiry shutdown incorrectly advanced or failed the disk purge.'); $expiryDeleted = $makeLifecycleInstance( '88888888-8888-4888-8888-888888888888', 'expiry-delete-success-vm', 'purge_deleting', 'blocked_expired'); $expiryDeleted->status = 'deleting'; $expiryDeleted->save(); $olderBacklog = CloudJobService::enqueue($instance, $actor, 'action', 'POST', '/actions', ['action' => 'reboot']); $expiryDeleteJob = CloudJobService::enqueueExpiryDelete($expiryDeleted); $beforeExpiryDeleteRequests = count($history); $user->enable = 0; $user->save(); $mock->append(new Response(200, [], json_encode([ 'operation_id' => $expiryDeleteJob->operation_id, 'status' => 'succeeded', 'result' => ['deleted' => true], ]))); $worker->tick(); $user->enable = 1; $user->save(); $expiryDeleteRequests = array_slice($history, $beforeExpiryDeleteRequests); $expiryDeleteRequest = count($expiryDeleteRequests) === 1 ? $agentRequest($expiryDeleteRequests[0]) : []; $expiryDeleteActor = count($expiryDeleteRequests) === 1 ? json_decode(base64_decode(strtr( $expiryDeleteRequest['headers']['X-Virt-Actor'], '-_', '+/' )), true) : []; $expiryDeleted->refresh(); $assert(count($expiryDeleteRequests) === 1 && $expiryDeleteRequest['method'] === 'DELETE' && !str_contains($expiryDeleteRequest['request_target'], '?') && json_decode($expiryDeleteRequest['body'], true) === ['remove_managed_disks' => true] && ($expiryDeleteActor['can_manage'] ?? false) === true && ($expiryDeleteActor['operation_id'] ?? null) === $expiryDeleteJob->operation_id && !array_key_exists('system_cleanup', $expiryDeleteActor) && $expiryDeleteActor['is_admin'] === false && CloudJob::find($expiryDeleteJob->id)->status === 'succeeded' && $expiryDeleted->status === 'deleted' && $expiryDeleted->sealed_credential === null, 'Expiry cleanup did not delete disks once or clear the saved credential.'); $olderBacklog->status = 'failed'; $olderBacklog->save(); $forgedCleanup = $makeLifecycleInstance( '89898989-8989-4898-8898-898989898989', 'forged-expiry-delete-vm', 'purge_deleting', 'blocked_expired'); $forgedCleanup->status = 'deleting'; $forgedCleanup->save(); $forgedCleanupJob = CloudJobService::enqueueExpiryDelete($forgedCleanup); $forgedPayload = \App\Services\Cloud\CloudSecrets::decrypt( $forgedCleanupJob->sealed_payload, 'job-payload' ); $forgedPayload['request_target'] = '/v1/vms/' . $instance->uuid; $forgedCleanupJob->sealed_payload = \App\Services\Cloud\CloudSecrets::encrypt( $forgedPayload, 'job-payload' ); $forgedCleanupJob->save(); $beforeForgedCleanupRequests = count($history); $worker->tick(); $forgedCleanup->refresh(); $assert(count($history) === $beforeForgedCleanupRequests && CloudJob::find($forgedCleanupJob->id)->status === 'failed' && $forgedCleanup->status === 'delete_failed' && $forgedCleanup->service_status === 'purge_failed', 'A forged cleanup payload crossed its bound instance or reached the Agent.'); $expiryDeleteFailed = $makeLifecycleInstance( '99999999-9999-4999-8999-999999999999', 'expiry-delete-failed-vm', 'purge_deleting', 'blocked_expired'); $expiryDeleteFailed->status = 'deleting'; $expiryDeleteFailed->save(); $expiryDeleteFailedJob = CloudJobService::enqueueExpiryDelete($expiryDeleteFailed); $mock->append(new Response(409, [], json_encode([ 'operation_id' => $expiryDeleteFailedJob->operation_id, 'status' => 'failed', 'error' => [ 'code' => 'conflict', 'message' => 'volume delete rejected before undefine', ], 'replayed' => false, 'retryable' => false, ]))); $worker->tick(); $expiryDeleteFailed->refresh(); $assert(CloudJob::find($expiryDeleteFailedJob->id)->status === 'failed' && $expiryDeleteFailed->status === 'delete_failed' && $expiryDeleteFailed->service_status === 'purge_failed' && $expiryDeleteFailed->sealed_credential !== null, 'Explicit expiry deletion failure did not enter the manual-review state.'); $expiryDeleteUncertain = $makeLifecycleInstance( 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa', 'expiry-delete-uncertain-vm', 'purge_deleting', 'blocked_expired'); $expiryDeleteUncertain->status = 'deleting'; $expiryDeleteUncertain->save(); $expiryDeleteUncertainJob = CloudJobService::enqueueExpiryDelete( $expiryDeleteUncertain); $expiryDeleteUncertainJob->status = 'uncertain'; $expiryDeleteUncertainJob->attempts = 1; $expiryDeleteUncertainJob->dispatched_at = time() - 30; $expiryDeleteUncertainJob->available_at = 0; $expiryDeleteUncertainJob->save(); $beforeMissingDeleteCheck = count($history); $mock->append(new Response(404, [], '{}')); $worker->tick(); $missingDeleteRequests = array_slice($history, $beforeMissingDeleteCheck); $expiryDeleteUncertainJob->refresh(); $expiryDeleteUncertain->refresh(); $missingDeleteRequest = count($missingDeleteRequests) === 1 ? $agentRequest($missingDeleteRequests[0]) : []; $assert(count($missingDeleteRequests) === 1 && $missingDeleteRequest['method'] === 'GET' && !str_contains($missingDeleteRequest['request_target'], '?') && $expiryDeleteUncertainJob->status === 'uncertain' && $expiryDeleteUncertain->status === 'deleting' && $expiryDeleteUncertain->service_status === 'purge_deleting', 'Missing expiry operation was replayed or closed without manual review.'); $expiryDeleteUncertainJob->available_at = 0; $expiryDeleteUncertainJob->save(); $beforeNonterminalDeleteCheck = count($history); $nonterminalExpiryOperation = json_encode([ 'operation_id' => $expiryDeleteUncertainJob->operation_id, 'status' => 'uncertain', ]); $mock->append(new Response(200, [], $nonterminalExpiryOperation)); $mock->append(new Response(200, [], $nonterminalExpiryOperation)); $worker->tick(); $nonterminalDeleteRequests = array_slice($history, $beforeNonterminalDeleteCheck); $expiryDeleteUncertainJob->refresh(); $expiryDeleteUncertain->refresh(); $nonterminalDeleteLookup = count($nonterminalDeleteRequests) === 2 ? $agentRequest($nonterminalDeleteRequests[0]) : []; $nonterminalDeleteReconcile = count($nonterminalDeleteRequests) === 2 ? $agentRequest($nonterminalDeleteRequests[1]) : []; $assert(count($nonterminalDeleteRequests) === 2 && $nonterminalDeleteLookup['method'] === 'GET' && !str_contains($nonterminalDeleteLookup['request_target'], '?') && $nonterminalDeleteReconcile['method'] === 'POST' && str_ends_with($nonterminalDeleteReconcile['request_target'], '/operations/' . $expiryDeleteUncertainJob->operation_id . '/reconcile') && $expiryDeleteUncertainJob->status === 'uncertain' && $expiryDeleteUncertain->status === 'deleting' && $expiryDeleteUncertain->service_status === 'purge_deleting', 'Nonterminal expiry deletion was replayed or marked failed automatically.'); // Once the Agent's durable record confirms deletion, PHP must close the // same one-shot expiry job without issuing DELETE a second time. $expiryDeleteUncertainJob->available_at = 0; $expiryDeleteUncertainJob->save(); $beforeClaimCleanup = count($history); $mock->append(new Response(200, [], json_encode([ 'operation_id' => $expiryDeleteUncertainJob->operation_id, 'status' => 'succeeded', 'result' => [ 'uuid' => $expiryDeleteUncertain->uuid, 'deleted' => true, ], ]))); $worker->tick(); $claimCleanupRequests = array_slice($history, $beforeClaimCleanup); $expiryDeleteUncertainJob->refresh(); $expiryDeleteUncertain->refresh(); $claimCleanupRequest = count($claimCleanupRequests) === 1 ? $agentRequest($claimCleanupRequests[0]) : []; $assert(count($claimCleanupRequests) === 1 && $claimCleanupRequest['method'] === 'GET' && !str_contains($claimCleanupRequest['request_target'], '?') && $expiryDeleteUncertainJob->status === 'succeeded' && $expiryDeleteUncertain->status === 'deleted' && $expiryDeleteUncertain->sealed_credential === null, 'Expiry deletion did not close after claim-only reconciliation succeeded.'); $reinstall = CloudJobService::enqueue($instance, $actor, 'reinstall', 'POST', '/reinstall', ['template_id' => $reinstallTemplate->id, 'confirmation_name' => $instance->name, 'root_password' => 'NewStrongPassword!123'], ['password' => 'NewStrongPassword!123', 'login_name' => 'root']); $oldCredential = $instance->sealed_credential; $queuedReinstall = \App\Services\Cloud\CloudSecrets::decrypt( (string) $reinstall->sealed_payload, 'job-payload' ); $assert(($queuedReinstall['template_metadata']['template_id'] ?? null) === $reinstallTemplate->id && ($queuedReinstall['template_metadata']['os_source'] ?? null) === 'cloud_template' && ($queuedReinstall['template_metadata']['os_name'] ?? null) === 'Debian 13' && ($queuedReinstall['template_metadata']['login_name'] ?? null) === 'root' && !array_key_exists('os_source', $queuedReinstall['body'] ?? []) && !array_key_exists('os_name', $queuedReinstall['body'] ?? []) && !array_key_exists('login_name', $queuedReinstall['body'] ?? []), 'Reinstall did not keep PHP template metadata outside the strict Agent DTO.'); $mock->append(new Response(202, [], json_encode([ 'operation_id' => $reinstall->operation_id, 'status' => 'reserved', 'replayed' => false, 'retryable' => false, ]))); $worker->tick(); $reinstallRequest = $agentRequest(end($history)); $reinstallBody = json_decode( (string) ($reinstallRequest['body'] ?? ''), true, 16, JSON_THROW_ON_ERROR ); $reinstallKeys = array_keys($reinstallBody); sort($reinstallKeys); $expectedReinstallKeys = [ 'root_password', 'template_generation_token', 'template_id', 'template_revision', ]; sort($expectedReinstallKeys); $assert(CloudJob::find($reinstall->id)->status === 'running' && $instance->fresh()->sealed_credential === $oldCredential, 'Pending reinstall replaced password prematurely.'); $assert($reinstallKeys === $expectedReinstallKeys && !array_key_exists('confirmation_name', $reinstallBody), 'Browser reinstall confirmation leaked into the strict Agent DTO.'); CloudJob::where('id', $reinstall->id)->update(['available_at' => 0]); $mock->append(new Response(200, [], json_encode([ 'operation_id' => $reinstall->operation_id, 'status' => 'succeeded', 'replayed' => false, 'retryable' => false, 'result' => [ 'generated_password' => 'never-public', ], ]))); $worker->tick(); $reinstalled = $instance->fresh(); $reinstalledSpec = json_decode((string) $reinstalled->spec_json, true, flags: JSON_THROW_ON_ERROR); $assert(CloudJob::find($reinstall->id)->status === 'succeeded' && $reinstalled->sealed_credential !== $oldCredential && !str_contains(CloudJob::find($reinstall->id)->result_json, 'never-public') && ($reinstalledSpec['template_id'] ?? null) === $reinstallTemplate->id && ($reinstalledSpec['public_ip_cidr'] ?? null) === '203.0.113.18/28' && ($reinstalledSpec['os_source'] ?? null) === 'cloud_template' && ($reinstalledSpec['os_name'] ?? null) === 'Debian 13' && ($reinstalledSpec['login_name'] ?? null) === 'root' && ($reinstalledSpec['managed'] ?? null) === true && !isset($reinstalledSpec['install_iso'], $reinstalledSpec['agent_metadata_version']), 'Completed reinstall lost its trusted template metadata.'); // A replacement guest may be running while old-volume cleanup remains // unresolved. The worker must keep the job uncertain but still persist // the new credential and template metadata; raw host warnings stay out of // result_json. $partialReinstall = CloudJobService::enqueue($instance->fresh(), $actor, 'reinstall', 'POST', '/reinstall', [ 'template_id' => $input['template_id'], 'confirmation_name' => $instance->name, 'root_password' => 'PartialStrongPassword!123', ], ['password' => 'PartialStrongPassword!123', 'login_name' => 'root']); $mock->append(new Response(202, [], json_encode([ 'operation_id' => $partialReinstall->operation_id, 'status' => 'reserved', 'replayed' => false, 'retryable' => false, ]))); $worker->tick(); CloudJob::where('id', $partialReinstall->id)->update(['available_at' => 0]); $beforePartialReconcile = count($history); $partialOperation = json_encode([ 'operation_id' => $partialReinstall->operation_id, // Older Agents could persist a terminal success before deterministic // old-root cleanup completed. PHP must still call reconciliation for // that same operation instead of looping on GET or replaying reinstall. 'status' => 'succeeded', 'replayed' => false, 'retryable' => false, 'error' => [ 'code' => 'reinstall_cleanup_pending', 'message' => 'replacement committed; cleanup pending', ], 'result' => [ 'reinstall_committed' => true, 'cleanup_pending' => true, 'vm' => ['uuid' => (string) $instance->uuid, 'name' => $instance->name, 'state' => 'running'], 'warning' => '旧存储卷 /srv/private.qcow2 删除失败', ], ]); $mock->append(new Response(200, [], $partialOperation)); $mock->append(new Response(200, [], $partialOperation)); $worker->tick(); $partialRequests = array_slice($history, $beforePartialReconcile); $partialJob = CloudJob::find($partialReinstall->id); $partialInstance = $instance->fresh(); $partialSpec = json_decode((string) $partialInstance->spec_json, true, flags: JSON_THROW_ON_ERROR); $partialLookupRequest = count($partialRequests) === 2 ? $agentRequest($partialRequests[0]) : []; $partialReconcileRequest = count($partialRequests) === 2 ? $agentRequest($partialRequests[1]) : []; $assert($partialJob->status === 'uncertain' && $partialJob->error_code === 'reinstall_cleanup_pending' && $partialInstance->sealed_credential !== $reinstalled->sealed_credential && !str_contains((string) $partialJob->result_json, 'private.qcow2') && ($partialSpec['template_id'] ?? null) === $input['template_id'] && ($partialSpec['os_source'] ?? null) === 'cloud_template' && ($partialSpec['os_name'] ?? null) === 'AlmaLinux 10' && ($partialSpec['login_name'] ?? null) === 'root' && ($partialSpec['managed'] ?? null) === true && !isset($partialSpec['install_iso'], $partialSpec['agent_metadata_version']) && count($partialRequests) === 2 && $partialLookupRequest['method'] === 'GET' && $partialReconcileRequest['method'] === 'POST' && str_ends_with($partialReconcileRequest['request_target'], '/operations/' . $partialReinstall->operation_id . '/reconcile'), 'Pending reinstall cleanup was reported as success or left the old credential.'); // A later durable terminal record closes the same operation. It must not // submit the destructive reinstall request a second time. CloudJob::where('id', $partialReinstall->id)->update(['available_at' => 0]); $beforePartialResolved = count($history); $mock->append(new Response(200, [], json_encode([ 'operation_id' => $partialReinstall->operation_id, 'status' => 'succeeded', 'replayed' => true, 'retryable' => false, 'result' => [ 'vm' => ['uuid' => (string) $instance->uuid, 'name' => $instance->name, 'state' => 'running'], ], ]))); $worker->tick(); $partialResolvedRequests = array_slice($history, $beforePartialResolved); $partialResolvedRequest = count($partialResolvedRequests) === 1 ? $agentRequest($partialResolvedRequests[0]) : []; $assert(CloudJob::find($partialReinstall->id)->status === 'succeeded' && count($partialResolvedRequests) === 1 && $partialResolvedRequest['method'] === 'GET' && str_ends_with( $partialResolvedRequest['request_target'], '/operations/' . $partialReinstall->operation_id ), 'Resolved reinstall cleanup replayed the destructive mutation.'); // A network change reserves only a locally available target. The encrypted // job keeps both ledger IDs, while the Agent receives only its existing // request body. An ambiguous response keeps both addresses unavailable and // a reconciled success atomically moves the reservation to the target. try { CloudJobService::enqueue($instance->fresh(), $actor, 'network_change', 'POST', '/network', [ 'public_ip_cidr' => '203.0.113.34/28', 'confirm_restart' => true, ]); throw new RuntimeException('Network change accepted an implicit public pool.'); } catch (CloudError $expected) { $assert($expected->httpStatus === 422, 'Missing public pool key returned the wrong status.'); } try { CloudJobService::enqueue($instance->fresh(), $actor, 'network_change', 'POST', '/network', [ 'public_ip_cidr' => '203.0.113.34/28', 'public_pool_key' => $sourcePoolKey, 'confirm_restart' => true, ]); throw new RuntimeException('Network change accepted an IP from another pool.'); } catch (CloudError $expected) { $assert($expected->httpStatus === 409, 'Pool/IP mismatch returned the wrong status.'); } $attachment->line_id = (int) $line->id + 100; $attachment->save(); try { CloudJobService::enqueue($instance->fresh(), $actor, 'network_change', 'POST', '/network', [ 'public_ip_cidr' => '203.0.113.34/28', 'public_pool_key' => $targetPoolKey, 'confirm_restart' => true, ]); throw new RuntimeException('Network change crossed the instance sales line.'); } catch (CloudError $expected) { $assert($expected->httpStatus === 409, 'Sales-line mismatch returned the wrong status.'); } $attachment->line_id = $line->id; $attachment->save(); $reservationUpdatedAtBeforeNetwork = (int) $openingReservation->fresh()->updated_at; $networkTarget = CloudPublicIp::where('address', '203.0.113.34')->first(); $networkChange = CloudJobService::enqueue($instance->fresh(), $actor, 'network_change', 'POST', '/network', [ 'public_ip_cidr' => '203.0.113.34/28', 'public_pool_key' => $targetPoolKey, 'confirm_restart' => true, ]); $networkPayload = \App\Services\Cloud\CloudSecrets::decrypt( $networkChange->sealed_payload, 'job-payload' ); $networkTarget->refresh(); $assert((int) ($networkPayload['source_public_ip_id'] ?? 0) === (int) $openingReservation->public_ip_id && (int) ($networkPayload['target_public_ip_id'] ?? 0) === (int) $networkTarget->id && (int) ($networkPayload['network_attachment_id'] ?? 0) === (int) $attachment->id && (int) ($networkPayload['source_public_ip_pool_id'] ?? 0) === (int) $sourcePublicPool->id && (int) ($networkPayload['target_public_ip_pool_id'] ?? 0) === (int) $targetPublicPool->id && $networkTarget->state === 'changing' && $networkTarget->reservation_id === null && (int) $networkTarget->instance_id === (int) $instance->id && (string) $networkTarget->operation_id === (string) $networkChange->operation_id, 'Network-change enqueue did not durably reserve the target without violating the reservation uniqueness constraint.'); $beforeNetwork = count($history); $mock->append(new Response(200, [], json_encode([ 'operation_id' => $networkChange->operation_id, 'status' => 'uncertain', 'safe_to_refund' => false, ]))); $worker->tick(); $networkRequest = $agentRequest($history[$beforeNetwork]); $networkBody = json_decode($networkRequest['body'], true); $networkTarget->refresh(); $assert($networkBody === [ 'public_ip_cidr' => '203.0.113.34/28', 'public_pool_key' => $targetPoolKey, 'confirm_restart' => true, 'network' => 'public', 'public_pool_cidr' => '203.0.113.32/29', 'gateway' => '203.0.113.33', 'dns_servers' => ['1.0.0.1'], ] && $networkTarget->state === 'uncertain' && (int) CloudResourceReservation::find( $openingReservation->id )->public_ip_pool_id === (int) $sourcePublicPool->id && CloudPublicIp::find($openingReservation->public_ip_id)->state === 'assigned', 'Ambiguous network change exposed ledger metadata to the Agent or released either address.'); $networkChange->refresh(); $networkChange->available_at = 0; $networkChange->save(); $mock->append(new Response(200, [], json_encode([ 'operation_id' => $networkChange->operation_id, 'status' => 'succeeded', 'result' => [ 'public_ip_cidr' => '203.0.113.34/28', 'public_pool_key' => $sourcePoolKey, 'network_changed' => true, ], ]))); $worker->tick(); $networkChange->refresh(); $networkTarget->refresh(); $assert($networkChange->status === 'uncertain' && $networkTarget->state === 'uncertain' && (int) CloudResourceReservation::find( $openingReservation->id )->public_ip_pool_id === (int) $sourcePublicPool->id, 'A network success for another public pool moved the local ledger.'); $networkChange->available_at = 0; $networkChange->save(); $mock->append(new Response(200, [], json_encode([ 'operation_id' => $networkChange->operation_id, 'status' => 'succeeded', 'result' => [ 'public_ip_cidr' => '203.0.113.34/28', 'public_pool_key' => $targetPoolKey, 'network_changed' => true, ], ]))); $worker->tick(); $networkReservation = CloudResourceReservation::find($openingReservation->id); $networkTarget->refresh(); $assert(CloudJob::find($networkChange->id)->status === 'succeeded' && (int) $networkReservation->public_ip_id === (int) $networkTarget->id && (int) $networkReservation->network_attachment_id === (int) $attachment->id && (int) $networkReservation->public_ip_pool_id === (int) $targetPublicPool->id && (int) $networkReservation->updated_at === $reservationUpdatedAtBeforeNetwork && $networkTarget->state === 'assigned' && (int) $networkTarget->reservation_id === (int) $networkReservation->id && (int) $networkTarget->instance_id === (int) $instance->id && CloudPublicIp::find($openingReservation->public_ip_id)->state === 'available' && (json_decode((string) $instance->fresh()->spec_json, true)['public_ip_cidr'] ?? null) === '203.0.113.34/28', 'Reconciled network success did not atomically swap the reservation and instance metadata.'); $failedNetworkTarget = CloudPublicIp::where('address', '203.0.113.35')->first(); $failedNetwork = CloudJobService::enqueue($instance->fresh(), $actor, 'network_change', 'POST', '/network', [ 'public_ip_cidr' => '203.0.113.35/28', 'public_pool_key' => $targetPoolKey, 'confirm_restart' => true, ]); $mock->append(new Response(422, [], json_encode([ 'operation_id' => $failedNetwork->operation_id, 'status' => 'failed', 'error' => ['code' => 'invalid_request', 'message' => 'network change rejected'], 'replayed' => false, 'retryable' => false, ]))); $worker->tick(); $failedNetworkTarget->refresh(); $networkTarget->refresh(); $assert(CloudJob::find($failedNetwork->id)->status === 'failed' && $failedNetworkTarget->state === 'available' && $failedNetworkTarget->reservation_id === null && $failedNetworkTarget->instance_id === null && $failedNetworkTarget->operation_id === null && $networkTarget->state === 'assigned' && (int) CloudResourceReservation::find($openingReservation->id)->public_ip_id === (int) $networkTarget->id && (int) CloudResourceReservation::find( $openingReservation->id )->public_ip_pool_id === (int) $targetPublicPool->id, 'Explicit network failure changed the source assignment or retained its target hold.'); $delete = CloudJobService::enqueue($instance, $actor, 'delete', 'DELETE', '', ['remove_managed_disks' => true]); $mock->append(new Response(200, [], json_encode([ 'operation_id' => $delete->operation_id, 'status' => 'succeeded', 'result' => [ 'uuid' => '11111111-1111-4111-8111-111111111111', 'deleted' => true, ], ]))); $worker->tick(); $last = $agentRequest(end($history)); $assert(json_decode($last['body'], true) === ['remove_managed_disks' => true] && !str_contains($last['request_target'], '?') && $instance->fresh()->status === 'deleted' && $instance->fresh()->sealed_credential === null && CloudResourceReservation::where('order_id', $first->id)->first()->state === 'released' && CloudPublicIp::find($openingReservation->public_ip_id)->state === 'available' && CloudPublicIp::find($networkTarget->id)->state === 'available' && CloudPublicIp::find($failedNetworkTarget->id)->state === 'available', 'Delete payload/query, credential removal, or capacity release failed.'); // Provider callbacks credit the wallet before the invoice bridge runs. // That credit must be consumed once, queue one create job, and remain // idempotent when the payment provider retries its callback. $providerOrder = CloudOrderService::purchase(array_replace($input, [ 'name' => 'vm-provider-payment', 'idempotency_key' => 'test-request-key-provider-payment', ]), $actor); $providerInvoice = CloudInvoice::where('order_id', $providerOrder->id)->first(); $providerPayment = new CloudInvoicePayment(); $providerPayment->invoice_id = $providerInvoice->id; $providerPayment->user_id = 1; $providerPayment->provider = 'paytaro'; $providerPayment->provider_order_no = 'PT-PROVISION-0001'; $providerPayment->idempotency_key = 'provision-provider-payment-0001'; $providerPayment->amount = '9.99'; $providerPayment->status = 'pending'; $providerPayment->created_at = $providerPayment->updated_at = time(); $providerPayment->save(); $balanceBeforeProviderCredit = CloudMoney::cents((string) User::find(1)->money); $user = User::find(1); $user->money = CloudMoney::decimal($balanceBeforeProviderCredit + 999); $user->save(); $providerOutcome = CloudInvoicePaymentService::settleForProvider( 'paytaro', 'PT-PROVISION-0001'); $providerOutcomeAgain = CloudInvoicePaymentService::settleForProvider( 'paytaro', 'PT-PROVISION-0001'); $providerQueuedCreate = \App\Services\Cloud\CloudSecrets::decrypt( (string) CloudJob::where('order_id', $providerOrder->id) ->first()->sealed_payload, 'job-payload' ); $providerPolicy = $providerQueuedCreate['body'] ['platform_network_policy_snapshot'] ?? null; $assert($providerOutcome === 'paid_invoice' && $providerOutcomeAgain === 'paid_invoice' && CloudMoney::cents((string) User::find(1)->money) === $balanceBeforeProviderCredit && CloudInvoice::find($providerInvoice->id)->status === 'paid' && CloudOrder::find($providerOrder->id)->status === 'queued' && CloudJob::where('order_id', $providerOrder->id)->count() === 1 && ($providerPolicy['catalog_revision'] ?? null) === 2 && count($providerPolicy['rules'] ?? []) === 9, 'Provider callback did not settle an opening invoice exactly once.'); // A blank optional label is normalized before hashing, but generated only // for a genuinely new order. Replaying the same request with an explicit // empty field must therefore return the original generated display name. $automaticInput = array_replace($input, [ 'idempotency_key' => 'test-request-key-automatic-name', ]); unset($automaticInput['name']); $automatic = CloudOrderService::purchase($automaticInput, $actor); $automaticReplay = CloudOrderService::purchase( $automaticInput + ['name' => ''], $actor ); $automaticSpec = json_decode((string) $automatic->spec_json, true, flags: JSON_THROW_ON_ERROR); $assert($automatic->id === $automaticReplay->id && preg_match('/^vm-[abcdefghjkmnpqrstuvwxyz23456789]{6}$/D', (string) ($automaticSpec['name'] ?? '')) === 1, 'Blank instance names were not generated securely or idempotently.'); echo "Cloud SQLite purchase, provisioning, refund, ambiguity, quota and tenant checks passed (crypto double only).\n"; }