20, 'cloud_engine_url' => 'http://127.0.0.1:8000', 'super_admin' => '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 { $id = bin2hex(random_bytes(16)); self::$memory[$id] = $payload; return 'test-only:' . $id; } public static function decrypt(string $cipher): 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\CloudHostCapacitySnapshot; 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\CloudTemplateRetirement; use App\Services\Cloud\CloudInvoiceService; use App\Services\Cloud\CloudInvoicePaymentService; use App\Services\Cloud\CloudOrderService; use App\Services\Cloud\CloudWorkerService; use App\Services\Cloud\CloudEngineClient; use App\Services\Cloud\CloudAccess; use App\Services\Cloud\CloudJobService; use App\Services\Cloud\CloudInstanceDiscovery; use App\Services\Cloud\CloudError; use App\Services\Cloud\CloudMoney; use App\Services\Cloud\CloudDeploymentPageService; use App\Services\Cloud\CloudTemplateDeletionService; 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); } }; $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('health_state')->default('unknown'); $t->integer('last_seen_at')->nullable(); $t->integer('health_checked_at')->nullable(); $t->string('health_error_code')->default(''); $t->string('default_network'); $t->integer('created_at')->default(0); $t->integer('updated_at')->default(0); }); $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('bridge'); $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_host_capacity_snapshots', function (Blueprint $t) { $t->integer('host_id')->primary(); foreach (['cpu_total_cores', 'cpu_allocated_cores', 'cpu_available_cores', 'memory_total_mib', 'memory_allocated_mib', 'memory_available_mib', 'storage_total_bytes', 'storage_allocated_bytes', 'storage_available_bytes', 'instance_total_count', 'instance_running_count', 'public_ip_configured', 'public_ip_total_count', 'public_ip_reserved_count', 'public_ip_allocated_count', 'public_ip_available_count'] as $v) { $t->integer($v)->default(0); } $t->string('sync_state')->default('never'); foreach (['observed_at', 'last_attempt_at', 'revision', 'attempt_generation'] as $v) { $t->integer($v)->default(0); } $t->string('last_error_code')->default(''); $t->integer('created_at'); $t->integer('updated_at'); }); $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_template_retirements', function (Blueprint $t) { $t->string('template_id')->primary(); $t->string('state')->default('deleting'); $t->integer('template_created_at')->default(0); $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_template_catalog_locks', function (Blueprint $t) { $t->integer('id')->primary(); $t->integer('updated_at'); $t->string('lease_token')->nullable(); $t->string('lease_kind')->nullable(); $t->integer('lease_until')->default(0); }); $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('payload_cipher')->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('payload_cipher'); $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('credential_cipher')->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'); }); $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->health_state = 'healthy'; $host->last_seen_at = $now; $host->health_checked_at = $now; $host->health_error_code = ''; $host->default_network = 'bridge:br0'; $host->created_at = $host->updated_at = $now; $host->save(); $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 bridge'; $attachment->network_mode = 'bridge'; $attachment->network_selector = 'bridge:br0'; $attachment->state = 'active'; $attachment->priority = 0; $attachment->desired_revision = 1; $attachment->observed_revision = 1; $attachment->sync_state = 'fresh'; $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 = 'fresh'; $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 = 'fresh'; $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(); $snapshot = new CloudHostCapacitySnapshot(); $snapshot->host_id = $host->id; $snapshot->cpu_total_cores = 64; $snapshot->cpu_allocated_cores = 0; $snapshot->cpu_available_cores = 64; $snapshot->memory_total_mib = 131072; $snapshot->memory_allocated_mib = 0; $snapshot->memory_available_mib = 131072; $snapshot->storage_total_bytes = 4096 * 1073741824; $snapshot->storage_allocated_bytes = 0; $snapshot->storage_available_bytes = 4096 * 1073741824; $snapshot->instance_total_count = 0; $snapshot->instance_running_count = 0; $snapshot->public_ip_configured = true; $snapshot->public_ip_total_count = 4; $snapshot->public_ip_reserved_count = 0; $snapshot->public_ip_allocated_count = 0; $snapshot->public_ip_available_count = 4; $snapshot->sync_state = 'fresh'; $snapshot->observed_at = $now; $snapshot->last_attempt_at = $now; $snapshot->revision = 1; $snapshot->last_error_code = ''; $snapshot->created_at = $snapshot->updated_at = $now; $snapshot->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(); $actor = ['user_id' => 1, 'is_admin' => false, 'is_super_admin' => false, 'can_manage' => false, 'session_id' => '']; $wrongOperationClient = new CloudEngineClient(new Client(['handler' => HandlerStack::create(new MockHandler([ new Response(422, [], '{"operation_id":"another-operation","status":"failed","safe_to_refund":true}') ]))])); try { $wrongOperationClient->request('POST', '/internal/v1/hosts/1/vms', $actor, [], 'expected-operation'); throw new RuntimeException('Mismatched operation response was trusted.'); } catch (CloudError $expected) { $assert($expected->httpStatus === 502, 'Mismatched operation response exposed an unexpected status.'); } $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.'); $host->health_state = 'unhealthy'; $host->updated_at = time(); $host->save(); $unavailableCatalog = CloudDeploymentPageService::saleCatalog(); $assert(count($unavailableCatalog['offers'] ?? []) === 2 && ($unavailableCatalog['offers'][0]['sellable'] ?? null) === false && ($unavailableCatalog['offers'][1]['sellable'] ?? null) === false, 'An unavailable pool was hidden or exposed as sellable.'); try { CloudOrderService::purchase(array_replace($input, [ 'name' => 'vm-unavailable', 'idempotency_key' => 'test-unavailable-offer-0001', ]), $actor); throw new RuntimeException('Unavailable capacity created an order.'); } catch (CloudError $e) { $assert($e->httpStatus === 409, 'Unavailable capacity returned the wrong status.'); } $assert(CloudOrder::count() === 0 && CloudInvoice::count() === 0 && CloudOffer::find(1)->stock === 10, 'Rejected availability admission created finance state or consumed stock.'); $host->health_state = 'healthy'; $host->updated_at = time(); $host->save(); $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(); $attachment->sync_state = 'failed'; $attachment->updated_at = time(); $attachment->save(); $staleNetworkCatalog = CloudDeploymentPageService::saleCatalog(); $assert(($staleNetworkCatalog['offers'][0]['sellable'] ?? null) === false, 'A pool on a stale network attachment remained sellable.'); $attachment->sync_state = 'fresh'; $attachment->updated_at = time(); $attachment->save(); 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(); $snapshot = CloudHostCapacitySnapshot::find($host->id); $snapshot->observed_at = time() - 1000; $snapshot->last_attempt_at = time() - 1000; $snapshot->save(); try { CloudInvoiceService::payBalance( $openingInvoice, $actor, 'provision-stale-capacity-0001'); throw new RuntimeException('Stale host capacity was scheduled.'); } catch (CloudError $e) { $assert($e->httpStatus === 409, 'Stale capacity returned the wrong status.'); } $assert(User::find(1)->money === '100.00' && CloudInvoice::find($openingInvoice->id)->status === 'open' && CloudResourceReservation::where('order_id', $first->id)->count() === 0, 'A failed placement charged the wallet or retained resources.'); $snapshot->observed_at = time(); $snapshot->last_attempt_at = time(); $snapshot->updated_at = time(); $snapshot->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(); $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(); $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 && $openingOrder->payload_cipher === null, 'Opening invoice payment was not an atomic, exactly-once transition into the provisioning queue.'); $changedPlan->cpu_cores = 2; $changedPlan->memory_mib = 2048; $changedPlan->disk_gib = 20; $changedPlan->save(); $history = []; $createdVm = [ 'status' => 'succeeded', 'uuid' => '11111111-1111-4111-8111-111111111111', 'name' => 'vm-one', 'vcpu' => 2, 'memory_mib' => 2048, 'disks' => [[ 'capacity_gib' => 20, 'source' => '/home/kvm/vm-one.qcow2', 'target' => 'vda', ]], 'autostart' => true, 'managed' => true, 'public_pool_key' => $sourcePoolKey, 'public_ip_cidr' => '203.0.113.18/28', 'os_source' => 'cloud_template', 'os_name' => 'AlmaLinux 10', 'login_name' => 'root', '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($mismatchedVm)), ]); $stack = HandlerStack::create($mock); $stack->push(Middleware::history($history)); $worker = new CloudWorkerService(new CloudEngineClient(new Client(['handler' => $stack]))); $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(); unset($createdVm['status']); $poolMismatchedVm = array_replace($createdVm, [ 'public_pool_key' => str_repeat('f', 32), ]); $mock->append(new Response(200, [], json_encode([ '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([ '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.'); $posted = json_decode((string) $history[0]['request']->getBody(), true); $signedActor = json_decode(base64_decode(strtr($history[0]['request']->getHeaderLine('X-Cloud-Actor'), '-_', '+/')), true); $assert($posted['vcpu'] === 2 && $posted['bandwidth_mbps'] === 100 && $posted['network'] === 'bridge:br0' && $posted['public_pool_key'] === $sourcePoolKey && $signedActor['can_provision'] === true && $signedActor['can_manage'] === true, 'Plan or provisioning authority lost.'); $assert(!str_contains(CloudJob::first()->result_json, 'must-not-persist'), 'Plain password saved in result.'); $terminalPayload = \App\Services\Cloud\CloudSecrets::decrypt( CloudJob::first()->payload_cipher ); $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(($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['engine_metadata_version'] ?? null) === 1 && ($instanceSpec['disks'] ?? null) === [['capacity_gib' => 20]] && !isset($instanceSpec['console_port']) && !isset($instanceSpec['disks'][0]['source']), 'Successful provisioning did not persist the safe engine 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.'); // One active instance plus one unpaid reservation must exhaust a limit of // two. Expiry releases finite stock and the encrypted credential exactly // once without ever entering the worker queue or touching the wallet. \App\Services\Config::$values['cloud_max_instances_per_user'] = 2; $abandoned = CloudOrderService::purchase(array_replace($input, [ 'name' => 'vm-abandoned', 'idempotency_key' => 'test-request-key-abandoned', ]), $actor); try { CloudOrderService::purchase(array_replace($input, [ 'name' => 'vm-over-limit', 'idempotency_key' => 'test-request-key-over-limit', ]), $actor); throw new RuntimeException('Unpaid provisioning reservation bypassed the instance limit.'); } catch (CloudError $e) { $assert($e->httpStatus === 409, 'Reserved instance limit returned the wrong status.'); } $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)->payload_cipher === 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.'); \App\Services\Config::$values['cloud_max_instances_per_user'] = 20; $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(['operation_id' => $failedJob->operation_id, 'status' => 'failed', 'safe_to_refund' => true, 'error' => '虚拟机参数校验失败']))); $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, 'Structured non-2xx operation failure 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 job 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()); $assert($uncertainJob->status === 'uncertain' && CloudOrder::find($uncertain->id)->refunded_at === null && CloudResourceReservation::where('order_id', $uncertain->id)->first()->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(); $mock->append(new Response(404, [], '{}')); $worker->tick(); $last = end($history)['request']; $assert($last->getMethod() === 'GET' && str_contains($last->getUri()->getPath(), '/operations/') && User::find(1)->money === '80.02', 'Ambiguous timeout recreated or refunded on unknown operation.'); $uncertainJob->refresh(); $uncertainJob->available_at = 0; $uncertainJob->save(); $mock->append(new Response(200, [], '{"status":"succeeded","result":{"uuid":"33333333-3333-4333-8333-333333333333"}}')); $worker->tick(); $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.'); \App\Services\Config::$values['cloud_max_instances_per_user'] = 2; try { CloudOrderService::purchase(array_replace($input, ['name' => 'vm-four', 'idempotency_key' => 'test-request-key-0004']), $actor); throw new RuntimeException('Quota bypass.'); } catch (CloudError $e) { $assert($e->httpStatus === 409, 'Quota response incorrect.'); } $assert(User::find(1)->money === '80.02' && CloudOrder::count() === 4, 'Rejected order altered finance.'); $actionJob = CloudJobService::enqueue($instance, $actor, 'action', 'POST', '/actions', ['action' => 'reboot']); $mock->append(new Response(200, [], '{"status":"succeeded","uuid":"11111111-1111-4111-8111-111111111111","state":"running"}')); $worker->tick(); $last = end($history)['request']; $signedActor = json_decode(base64_decode(strtr($last->getHeaderLine('X-Cloud-Actor'), '-_', '+/')), true); $assert(CloudJob::find($actionJob->id)->status === 'succeeded' && $signedActor['can_manage'] === true && !isset($signedActor['can_provision']), 'Power task delegated provision authority or did not complete.'); // A policy request whose first POST was lost before the engine operation // record existed is safe to replay with the same operation ID. A stale // suspend target must instead be skipped after the authoritative 404. $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->payload_cipher = \App\Services\Cloud\CloudSecrets::encrypt([ 'method' => 'POST', 'resource' => 'vms/' . $instance->uuid . '/network-suspend', 'body' => [], 'owner_id' => (int) $instance->user_id, 'system_policy' => true, ]); $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(404, [], '{}')); $mock->append(new Response(200, [], '{"status":"succeeded","result":{}}')); $worker->tick(); $policyRequests = array_slice($history, $beforePolicyRequests); $assert(count($policyRequests) === 2 && $policyRequests[0]['request']->getMethod() === 'GET' && $policyRequests[0]['request']->getUri()->getQuery() === 'reconcile=false' && $policyRequests[1]['request']->getMethod() === 'POST' && CloudJob::find($policy->id)->status === 'succeeded' && $instance->fresh()->network_status === 'blocked_traffic', 'Missing target-state operation was not replayed safely with the same desired state.'); $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(); $assert(count($history) === $beforeStaleRequests + 1 && end($history)['request']->getUri()->getQuery() === 'reconcile=false' && CloudJob::find($stalePolicy->id)->status === 'succeeded' && $instance->fresh()->network_status === 'active', 'A stale policy suspend was replayed after the desired state changed.'); // Desired state may reverse while the old engine 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([ 'status' => 'uncertain', 'executor_active' => true, ]))); $worker->tick(); $activePolicy->refresh(); $assert(count($history) === $beforeActiveRequests + 1 && end($history)['request']->getUri()->getQuery() === 'reconcile=false' && $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([ 'status' => 'succeeded', 'executor_active' => false, '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.'); // After an engine restart executor_active is false, but an existing // nonterminal operation may have only partly changed libvirt. Reconcile // that old target before the pending reverse target is allowed to run. $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([ 'status' => 'uncertain', 'executor_active' => false, ]))); $mock->append(new Response(200, [], json_encode([ 'status' => 'succeeded', 'executor_active' => false, 'result' => ['network_suspended' => true], ]))); $worker->tick(); $crashRequests = array_slice($history, $beforeCrashRequests); $assert(count($crashRequests) === 2 && $crashRequests[0]['request']->getMethod() === 'GET' && $crashRequests[0]['request']->getUri()->getQuery() === 'reconcile=false' && $crashRequests[1]['request']->getMethod() === 'POST' && CloudJob::find($crashedPolicy->id)->status === 'succeeded' && $instance->fresh()->network_status === 'restore_pending', 'A crash-interrupted stale target was skipped instead of converged before reversal.'); // The reverse-target assertion above intentionally leaves 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 authoritative engine operation has vanished // cannot be replayed safely and must become terminal instead of blocking // expiry enforcement forever. $missingAction = CloudJobService::enqueue($instance, $actor, 'action', 'POST', '/actions', ['action' => 'reboot']); $missingAction->status = 'uncertain'; $missingAction->dispatched_at = time() - 30; $missingAction->available_at = 0; $missingAction->save(); $mock->append(new Response(404, [], '{}')); $worker->tick(); $assert(CloudJob::find($missingAction->id)->status === 'failed', 'A vanished non-policy engine operation remained permanently uncertain.'); // 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->credential_cipher = \App\Services\Cloud\CloudSecrets::encrypt([ 'password' => 'LifecycleStrongPassword!123', 'login_name' => 'root', ]); $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->payload_cipher = \App\Services\Cloud\CloudSecrets::encrypt([ 'method' => 'POST', 'resource' => 'vms/' . $target->uuid . '/' . $suffix, 'body' => [], 'owner_id' => (int) $target->user_id, 'system_policy' => true, 'suspend_reason' => $reason, ]); $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, [], '{"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.'); // Online upgrade retires the old "stop during grace" target. A queued // job must be skipped without contacting the engine. If a previously // dispatched executor may already have stopped a running guest, preserve // that fact for a later network-first safe resume, but never replay stop. $legacyQueued = $makeLifecycleInstance( '56565656-5656-4656-8656-565656565656', 'legacy-queued-stop-vm', 'suspending_expired', 'active'); $legacyQueued->expires_at = time() - 1; $legacyQueued->save(); $legacyQueuedJob = $queuePolicy( $legacyQueued, 'service_suspend', 'service-suspend', 'expired'); $beforeLegacyQueuedRequests = count($history); $worker->tick(); $legacyQueued->refresh(); $assert(count($history) === $beforeLegacyQueuedRequests && CloudJob::find($legacyQueuedJob->id)->status === 'succeeded' && $legacyQueued->service_status === 'expired_grace' && $legacyQueued->suspended_at === null, 'A queued legacy expiry shutdown was sent after the network-only upgrade.'); $legacyUncertain = $makeLifecycleInstance( '57575757-5757-4757-8757-575757575757', 'legacy-uncertain-stop-vm', 'expired_grace', 'blocking_expired'); $legacyUncertain->expires_at = time() - 1; $legacyUncertain->save(); $legacyUncertainJob = $queuePolicy( $legacyUncertain, 'service_suspend', 'service-suspend', 'expired'); $legacyUncertainJob->status = 'uncertain'; $legacyUncertainJob->attempts = 1; $legacyUncertainJob->dispatched_at = time() - 30; $legacyUncertainJob->available_at = 0; $legacyUncertainJob->save(); $beforeLegacyUncertainRequests = count($history); $mock->append(new Response(200, [], json_encode([ 'status' => 'uncertain', 'executor_active' => false, 'service_was_running_hint' => true, ]))); $worker->tick(); $legacyUncertainRequests = array_slice($history, $beforeLegacyUncertainRequests); $legacyUncertain->refresh(); $assert(count($legacyUncertainRequests) === 1 && $legacyUncertainRequests[0]['request']->getMethod() === 'GET' && $legacyUncertainRequests[0]['request']->getUri()->getQuery() === 'reconcile=false' && CloudJob::find($legacyUncertainJob->id)->status === 'succeeded' && $legacyUncertain->service_status === 'resume_pending' && $legacyUncertain->suspended_at !== null, 'A crash-interrupted legacy expiry shutdown was replayed or lost its safe-resume marker.'); $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, [], '{"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', 'safe_to_refund' => false, 'error' => 'shutdown rejected', ]))); $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, [], '{"status":"succeeded","result":{"deleted":true}}')); $worker->tick(); $user->enable = 1; $user->save(); $expiryDeleteRequests = array_slice($history, $beforeExpiryDeleteRequests); $expiryDeleteActor = count($expiryDeleteRequests) === 1 ? json_decode(base64_decode(strtr( $expiryDeleteRequests[0]['request']->getHeaderLine('X-Cloud-Actor'), '-_', '+/' )), true) : []; $expiryDeleted->refresh(); $assert(count($expiryDeleteRequests) === 1 && $expiryDeleteRequests[0]['request']->getMethod() === 'DELETE' && $expiryDeleteRequests[0]['request']->getUri()->getQuery() === 'delete_disks=true' && (string) $expiryDeleteRequests[0]['request']->getBody() === '{}' && ($expiryDeleteActor['system_cleanup'] ?? false) === true && $expiryDeleteActor['is_admin'] === false && CloudJob::find($expiryDeleteJob->id)->status === 'succeeded' && $expiryDeleted->status === 'deleted' && $expiryDeleted->credential_cipher === 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->payload_cipher ); $forgedPayload['resource'] = 'vms/' . $instance->uuid . '?delete_disks=true'; $forgedCleanupJob->payload_cipher = \App\Services\Cloud\CloudSecrets::encrypt( $forgedPayload ); $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 engine.'); $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', 'safe_to_refund' => false, 'error' => 'volume delete rejected before undefine', ]))); $worker->tick(); $expiryDeleteFailed->refresh(); $assert(CloudJob::find($expiryDeleteFailedJob->id)->status === 'failed' && $expiryDeleteFailed->status === 'delete_failed' && $expiryDeleteFailed->service_status === 'purge_failed' && $expiryDeleteFailed->credential_cipher !== 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(); $assert(count($missingDeleteRequests) === 1 && $missingDeleteRequests[0]['request']->getMethod() === 'GET' && $missingDeleteRequests[0]['request']->getUri()->getQuery() === '' && $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); $mock->append(new Response(200, [], '{"status":"running","executor_active":false}')); $worker->tick(); $nonterminalDeleteRequests = array_slice($history, $beforeNonterminalDeleteCheck); $expiryDeleteUncertainJob->refresh(); $expiryDeleteUncertain->refresh(); $assert(count($nonterminalDeleteRequests) === 1 && $nonterminalDeleteRequests[0]['request']->getMethod() === 'GET' && $nonterminalDeleteRequests[0]['request']->getUri()->getQuery() === '' && $expiryDeleteUncertainJob->status === 'uncertain' && $expiryDeleteUncertain->status === 'deleting' && $expiryDeleteUncertain->service_status === 'purge_deleting', 'Nonterminal expiry deletion was replayed or marked failed automatically.'); // If the VM deletion itself was durable and only the engine's public-IP // claim cleanup failed, its reconcile endpoint retries that cleanup alone. // The PHP lifecycle must then 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([ 'status' => 'succeeded', 'result' => [ 'uuid' => $expiryDeleteUncertain->uuid, 'deleted' => true, 'public_ip_claims_released' => true, ], ]))); $worker->tick(); $claimCleanupRequests = array_slice($history, $beforeClaimCleanup); $expiryDeleteUncertainJob->refresh(); $expiryDeleteUncertain->refresh(); $assert(count($claimCleanupRequests) === 1 && $claimCleanupRequests[0]['request']->getMethod() === 'GET' && $claimCleanupRequests[0]['request']->getUri()->getQuery() === '' && $expiryDeleteUncertainJob->status === 'succeeded' && $expiryDeleteUncertain->status === 'deleted' && $expiryDeleteUncertain->credential_cipher === null, 'Expiry deletion did not close after claim-only reconciliation succeeded.'); $reinstall = CloudJobService::enqueue($instance, $actor, 'reinstall', 'POST', '/reinstall', ['template_id' => $input['template_id'], 'confirmation_name' => $instance->name, 'root_password' => 'NewStrongPassword!123'], ['password' => 'NewStrongPassword!123', 'login_name' => 'root']); $oldCredential = $instance->credential_cipher; $mock->append(new Response(202, [], '{"status":"pending","job_id":"remote-reinstall-id"}')); $worker->tick(); $assert(CloudJob::find($reinstall->id)->status === 'running' && $instance->fresh()->credential_cipher === $oldCredential, 'Pending reinstall replaced password prematurely.'); CloudJob::where('id', $reinstall->id)->update(['available_at' => 0]); $mock->append(new Response(200, [], '{"status":"succeeded","result":{"job_id":"remote-reinstall-id","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->credential_cipher !== $oldCredential && !str_contains(CloudJob::find($reinstall->id)->result_json, 'never-public') && ($reinstalledSpec['template_id'] ?? null) === $input['template_id'] && ($reinstalledSpec['public_ip_cidr'] ?? null) === '203.0.113.18/28' && !isset($reinstalledSpec['os_source'], $reinstalledSpec['os_name'], $reinstalledSpec['install_iso'], $reinstalledSpec['login_name'], $reinstalledSpec['engine_metadata_version']), 'Completed reinstall password, sanitization or metadata invalidation failed.'); // A network change reserves only a locally available target. The encrypted // job keeps both ledger IDs, while the engine 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->payload_cipher ); $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([ 'status' => 'uncertain', 'safe_to_refund' => false, ]))); $worker->tick(); $networkRequest = $history[$beforeNetwork]['request']; $networkBody = json_decode((string) $networkRequest->getBody(), true); $networkTarget->refresh(); $assert($networkBody === [ 'public_ip_cidr' => '203.0.113.34/28', 'public_pool_key' => $targetPoolKey, 'confirm_restart' => true, ] && $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 engine or released either address.'); $networkChange->refresh(); $networkChange->available_at = 0; $networkChange->save(); $mock->append(new Response(200, [], json_encode([ '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([ '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', 'safe_to_refund' => 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', '?delete_disks=true', []); $mock->append(new Response(200, [], '{"status":"succeeded","uuid":"11111111-1111-4111-8111-111111111111"}')); $worker->tick(); $last = end($history)['request']; $assert((string) $last->getBody() === '{}' && $last->getUri()->getQuery() === 'delete_disks=true' && $instance->fresh()->status === 'deleted' && $instance->fresh()->credential_cipher === 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.'); $manualUuid = '44444444-4444-4444-8444-444444444444'; $inventory = ['total_count' => 1, 'claimed_count' => 1, 'untracked_managed_count' => 0, 'items' => [[ 'uuid' => $manualUuid, 'owner_user_id' => 99, 'name' => 'manual-vm', 'state' => 'running', 'vcpu' => 4, 'memory_mib' => 4096, 'disks' => [['capacity_gib' => 40]], 'managed' => false, ]]]; $sync = CloudInstanceDiscovery::synchronize($host, $inventory); $imported = CloudInstance::where('uuid', $manualUuid)->first(); $assert($sync['scanned'] === 1 && $sync['imported'] === 1 && $imported !== null && (int) $imported->user_id === 99 && $imported->order_id === null && $imported->credential_cipher === null, 'Discovered VM was not assigned to the configuring administrator without a fake order.'); $repeat = CloudInstanceDiscovery::synchronize($host, $inventory); $assert($repeat['imported'] === 0 && $repeat['existing'] === 1 && CloudInstance::where('uuid', $manualUuid)->count() === 1, 'Repeated VM discovery duplicated or rewrote the imported instance.'); // 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. \App\Services\Config::$values['cloud_max_instances_per_user'] = 20; $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'); $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, '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. \App\Services\Config::$values['cloud_max_instances_per_user'] = 100; $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.'); try { CloudTemplateDeletionService::begin($input['template_id'], 1); throw new RuntimeException('An actively referenced cloud template was retired.'); } catch (CloudError $error) { $assert($error->httpStatus === 409 && CloudTemplateRetirement::find($input['template_id'])->state === 'active', 'A rejected template deletion did not restore the active template row.'); } $retiredTemplate = 'debian12-fedcba98-0123456789abcdef'; CloudTemplateDeletionService::begin($retiredTemplate, 1); try { CloudTemplateDeletionService::assertImportAllowed(); throw new RuntimeException('Import was allowed during a pending template delete.'); } catch (CloudError $error) { $assert($error->httpStatus === 409, 'Pending template deletion returned the wrong import-gate status.'); } CloudTemplateDeletionService::complete($retiredTemplate, 1); CloudTemplateDeletionService::assertImportAllowed(); $leaseObserved = false; $importResult = CloudTemplateDeletionService::queueImport( function () use (&$leaseObserved, $retiredTemplate, $assert): array { $lease = \App\Models\CloudTemplateCatalogLock::find(1); $leaseObserved = $lease !== null && preg_match('/^[a-f0-9]{32}$/D', (string) $lease->lease_token) === 1 && (string) $lease->lease_kind === 'upload' && (int) $lease->lease_until > time() && $lease->getConnection()->transactionLevel() === 0; try { CloudTemplateDeletionService::queueImport( static fn (): array => ['queued' => true] ); throw new RuntimeException( 'A concurrent cloud-template import bypassed the lease.' ); } catch (CloudError $error) { $assert($error->httpStatus === 409, 'A concurrent import lease returned the wrong status.'); } try { CloudTemplateDeletionService::begin($retiredTemplate, 1); throw new RuntimeException( 'Template deletion bypassed an active import lease.' ); } catch (CloudError $error) { $assert($error->httpStatus === 409, 'An import-gated deletion returned the wrong status.'); } return ['queued' => true]; }, 'upload' ); $clearedLease = \App\Models\CloudTemplateCatalogLock::find(1); $assert($leaseObserved && ($importResult['queued'] ?? false) === true && $clearedLease !== null && $clearedLease->lease_token === null && $clearedLease->lease_kind === null && (int) $clearedLease->lease_until === 0, 'Cloud-template import did not use and clear its durable lease.'); try { CloudTemplateDeletionService::queueImport( static function (): array { throw new CloudError('synthetic import failure', 502); }, 'upload' ); throw new RuntimeException('A synthetic import failure was swallowed.'); } catch (CloudError $error) { $failedLease = \App\Models\CloudTemplateCatalogLock::find(1); $assert($error->getMessage() === 'synthetic import failure' && $failedLease !== null && $failedLease->lease_token === null && (int) $failedLease->lease_until === 0, 'A failed import did not release its durable lease.'); } \App\Models\CloudTemplateCatalogLock::where('id', 1)->update([ 'lease_token' => str_repeat('a', 32), 'lease_kind' => 'upload', 'lease_until' => time() - 1, ]); CloudTemplateDeletionService::queueImport( static fn (): array => ['queued' => true] ); $recoveredLease = \App\Models\CloudTemplateCatalogLock::find(1); $assert($recoveredLease !== null && $recoveredLease->lease_token === null && $recoveredLease->lease_kind === null && (int) $recoveredLease->lease_until === 0, 'An expired import lease was not recovered after a PHP crash.'); CloudTemplateDeletionService::begin($retiredTemplate, 1); $assert(CloudTemplateRetirement::find($retiredTemplate)->state === 'deleting', 'A retry did not reopen the durable engine deletion request.'); CloudTemplateDeletionService::complete($retiredTemplate, 1); CloudTemplateDeletionService::reactivateImportedTemplate($retiredTemplate, 2); $assert(CloudTemplateRetirement::find($retiredTemplate)->state === 'active' && (int) CloudTemplateRetirement::find($retiredTemplate) ->template_created_at === 2, 'A completed same-content reimport remained retired or reused its generation.'); CloudTemplateRetirement::where('template_id', $retiredTemplate)->update([ 'state' => 'deleted', 'updated_at' => time(), ]); CloudTemplateDeletionService::reconcileAuthoritativeCatalog([[ 'id' => $retiredTemplate, 'created_at' => 2, ]]); $assert(CloudTemplateRetirement::find($retiredTemplate)->state === 'active', 'An authoritative live catalog did not repair a stale deleted gate.'); $staleImportAccepted = CloudTemplateDeletionService::reactivateImportedTemplate( $retiredTemplate, 1 ); $assert($staleImportAccepted === false && (int) CloudTemplateRetirement::find($retiredTemplate) ->template_created_at === 2, 'A delayed old import result rolled back the active template generation.'); try { CloudTemplateDeletionService::begin($retiredTemplate, 1); throw new RuntimeException('A stale deletion generation was accepted.'); } catch (CloudError $error) { $state = CloudTemplateRetirement::find($retiredTemplate); $assert($error->httpStatus === 409 && $state->state === 'active' && (int) $state->template_created_at === 2, 'A stale delete changed the active reimport generation.'); } $largeCatalog = []; for ($index = 1; $index <= 257; $index++) { $largeCatalog[] = [ 'id' => 'bulk-template-' . $index, 'created_at' => 1000 + $index, ]; } CloudTemplateDeletionService::reactivateImportedTemplate( 'bulk-template-257', 1257 ); CloudTemplateDeletionService::reconcileAuthoritativeCatalog($largeCatalog); $largeCatalogState = CloudTemplateRetirement::find('bulk-template-257'); $assert($largeCatalogState->state === 'active' && (int) $largeCatalogState->template_created_at === 1257, 'Catalog reconciliation truncated and retired a template after row 256.'); echo "Cloud SQLite purchase, provisioning, refund, ambiguity, quota and tenant checks passed (crypto double only).\n"; }