addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default'); $capsule->setAsGlobal(); $capsule->bootEloquent(); $schema = $capsule->schema(); $schema->create('user', static function (Blueprint $t): void { $t->increments('id'); $t->string('money'); $t->integer('enable')->default(1); $t->boolean('is_admin')->default(false); }); $schema->create('cloud_hosts', static function (Blueprint $t): void { $t->increments('id'); }); $schema->create('cloud_offers', static function (Blueprint $t): void { $t->increments('id'); $t->integer('stock'); $t->integer('updated_at')->default(0); }); $schema->create('cloud_orders', static function (Blueprint $t): void { $t->increments('id'); $t->integer('user_id'); $t->integer('host_id'); $t->integer('offer_id'); $t->string('amount'); $t->string('status'); $t->integer('stock_reserved_at'); $t->integer('stock_released_at')->nullable(); $t->integer('refunded_at')->nullable(); $t->integer('updated_at')->default(0); }); $schema->create('cloud_invoices', static function (Blueprint $t): void { $t->increments('id'); $t->integer('order_id'); $t->string('purpose'); $t->string('status'); $t->integer('updated_at')->default(0); }); $schema->create('cloud_resource_reservations', static function (Blueprint $t): void { $t->increments('id'); $t->integer('order_id'); $t->integer('host_id'); $t->integer('public_ip_id'); $t->integer('network_attachment_id'); $t->integer('public_ip_pool_id'); $t->string('operation_id'); $t->string('state'); $t->integer('released_at')->nullable(); $t->string('release_reason')->nullable(); $t->integer('expires_at')->nullable(); $t->integer('updated_at')->default(0); }); $schema->create('cloud_public_ips', static function (Blueprint $t): void { $t->increments('id'); $t->integer('host_id'); $t->integer('public_ip_pool_id'); $t->integer('reservation_id')->nullable(); $t->integer('instance_id')->nullable(); $t->string('operation_id')->nullable(); $t->string('state'); $t->integer('updated_at')->default(0); }); $schema->create('cloud_jobs', static function (Blueprint $t): void { $t->increments('id'); $t->string('operation_id')->unique(); $t->integer('order_id')->nullable(); $t->integer('instance_id')->nullable(); $t->integer('snapshot_id')->nullable(); $t->integer('user_id'); $t->integer('host_id'); $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->integer('created_at'); $t->integer('updated_at'); $t->string('error_code')->nullable(); $t->text('result_json')->nullable(); }); Capsule::table('user')->insert(['id' => 1, 'money' => '100.00']); Capsule::table('cloud_hosts')->insert(['id' => 7]); Capsule::table('cloud_offers')->insert(['id' => 1, 'stock' => 10]); $history = []; $mock = new MockHandler(); $handler = HandlerStack::create($mock); $handler->push(Middleware::history($history)); $worker = new CloudWorkerService(new CloudEngineClient(new Client(['handler' => $handler]))); // Begin at the paid, reserved boundary. All subsequent transitions use // the real worker, finance services, Eloquent and HTTP outcome validation. $paidCreate = static function (int $id): CloudJob { $operation = bin2hex(random_bytes(16)); $now = time(); Capsule::table('user')->where('id', 1)->decrement('money', 9.99); Capsule::table('cloud_offers')->where('id', 1)->decrement('stock'); Capsule::table('cloud_orders')->insert(['id' => $id, 'user_id' => 1, 'host_id' => 7, 'offer_id' => 1, 'amount' => '9.99', 'status' => 'queued', 'stock_reserved_at' => $now]); Capsule::table('cloud_invoices')->insert(['id' => $id, 'order_id' => $id, 'purpose' => 'provision', 'status' => 'paid']); Capsule::table('cloud_resource_reservations')->insert(['id' => $id, 'order_id' => $id, 'host_id' => 7, 'public_ip_id' => $id, 'network_attachment_id' => 1, 'public_ip_pool_id' => 1, 'operation_id' => $operation, 'state' => 'held', 'expires_at' => $now + 3600]); Capsule::table('cloud_public_ips')->insert(['id' => $id, 'host_id' => 7, 'public_ip_pool_id' => 1, 'reservation_id' => $id, 'operation_id' => $operation, 'state' => 'reserved']); Capsule::table('cloud_jobs')->insert(['id' => $id, 'operation_id' => $operation, 'order_id' => $id, 'user_id' => 1, 'host_id' => 7, 'kind' => 'create', 'status' => 'queued', 'payload_cipher' => CloudSecrets::encrypt([ 'method' => 'POST', 'resource' => 'vms', 'body' => ['name' => 'preflight-' . $id], ]), 'available_at' => $now, 'created_at' => $now, 'updated_at' => $now]); return CloudJob::find($id); }; $job = $paidCreate(1); $mock->append(new Response(503, [], json_encode([ 'operation_id' => $job->operation_id, 'status' => 'failed', 'execution_outcome' => 'not_dispatched', 'mutation_started' => false, 'safe_to_refund' => true, 'error_code' => 'execution_result_uncertain', ]))); $assert($worker->tick(), 'Preflight failure was not consumed.'); $assert(CloudJob::find(1)->status === 'failed' && (string) Capsule::table('user')->value('money') === '100.00' && Capsule::table('cloud_offers')->value('stock') === 10 && Capsule::table('cloud_orders')->where('id', 1)->value('refunded_at') !== null && Capsule::table('cloud_invoices')->where('id', 1)->value('status') === 'refunded' && Capsule::table('cloud_resource_reservations')->where('id', 1)->value('state') === 'released' && Capsule::table('cloud_public_ips')->where('id', 1)->value('state') === 'available', 'Proven no-write 503 retained a create lock, wallet charge, stock or IP.'); $assert(!$worker->tick() && count($history) === 1 && (string) Capsule::table('user')->value('money') === '100.00', 'Terminal preflight failure was dispatched or refunded twice.'); foreach ([['unknown', 'uncertain', true], ['not_dispatched', 'failed', false]] as $index => [$outcome, $status, $started]) { $id = $index + 2; $job = $paidCreate($id); $mock->append(new Response(503, [], json_encode([ 'operation_id' => $job->operation_id, 'status' => $status, 'execution_outcome' => $outcome, 'mutation_started' => $started, 'safe_to_refund' => false, 'error_code' => 'libvirt_execution_uncertain', ]))); $worker->tick(); $assert(CloudJob::find($id)->status === 'uncertain' && Capsule::table('cloud_orders')->where('id', $id)->value('refunded_at') === null && Capsule::table('cloud_invoices')->where('id', $id)->value('status') === 'paid' && Capsule::table('cloud_resource_reservations')->where('id', $id)->value('state') === 'uncertain' && Capsule::table('cloud_public_ips')->where('id', $id)->value('state') === 'uncertain' && Capsule::table('cloud_offers')->value('stock') === 11 - $id, 'Unknown write or missing absence proof released a create reservation.'); } $assert((string) Capsule::table('user')->value('money') === '80.02', 'Unproven creation outcomes refunded the wallet.'); echo "Prewrite creation failure workflow tests passed.\n"; }