You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/CloudPlanUpgradeServiceTest...

1122 lines
47 KiB
PHP

<?php
namespace App\Services\Cloud {
// Isolated in-memory cipher for this service test only.
final class CloudSecrets
{
private static array $values = [];
public static function encrypt(array $value, string $purpose): string
{
$key = bin2hex(random_bytes(16));
self::$values[$key] = $value;
return 'upgrade-test:' . $key;
}
public static function decrypt(string $value, string $purpose): array
{
$key = substr($value, strlen('upgrade-test:'));
if (!array_key_exists($key, self::$values)) {
throw new \RuntimeException('Unknown upgrade test payload.');
}
return self::$values[$key];
}
}
}
namespace {
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\CloudInstance;
use App\Models\CloudInvoice;
use App\Models\CloudJob;
use App\Models\CloudOffer;
use App\Models\CloudResourceReservation;
use App\Models\CloudServicePeriod;
use App\Models\User;
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudInstanceMetadata;
use App\Services\Cloud\CloudInvoicePaymentService;
use App\Services\Cloud\CloudInvoiceService;
use App\Services\Cloud\CloudLifecycleService;
use App\Services\Cloud\CloudPlanUpgradeService;
use App\Services\Cloud\CloudSecrets;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$expectError = static function (
callable $callable,
int $status,
string $message
) use ($assert): void {
try {
$callable();
} catch (CloudError $error) {
$assert($error->httpStatus === $status,
$message . ' Unexpected HTTP status.');
return;
}
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', static function (Blueprint $table): void {
$table->increments('id');
$table->string('user_name')->default('');
$table->string('email')->default('');
$table->string('money');
$table->integer('enable')->default(1);
$table->integer('is_admin')->default(0);
});
$schema->create('shop', static function (Blueprint $table): void {
$table->increments('id');
$table->string('name');
foreach ([
'cpu_cores', 'memory_mib', 'bandwidth_mbps', 'disk_gib',
'validity_days', 'traffic_quota_bytes', 'status', 'sort',
'created_at', 'updated_at',
] as $column) {
$table->integer($column)->default(0);
}
});
$schema->create('cloud_regions', static function (Blueprint $table): void {
$table->increments('id');
$table->string('code');
$table->string('name');
$table->string('country_code');
$table->string('status');
$table->integer('sort')->default(0);
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_network_lines', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('region_id');
$table->string('code');
$table->string('name');
$table->integer('is_default')->default(0);
$table->string('status');
$table->integer('sort')->default(0);
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_resource_pools', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('line_id');
$table->string('code');
$table->string('name');
$table->string('status');
$table->string('scheduling_policy')->default('balanced');
$table->integer('sort')->default(0);
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_hosts', static function (Blueprint $table): void {
$table->increments('id');
$table->string('name');
$table->string('region')->default('');
$table->string('country_code')->default('');
$table->integer('enabled');
$table->integer('resource_pool_id')->nullable();
$table->string('scheduler_state')->default('disabled');
$table->string('default_network')->default('default');
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_host_agents', static function (Blueprint $table): void {
$table->integer('host_id')->primary();
$table->text('sealed_api_secret');
$table->integer('enabled');
$table->integer('revision');
$table->integer('tested_revision');
$table->string('last_test_status');
$table->string('last_error_code')->default('');
$table->string('last_agent_version')->default('');
$table->integer('last_tested_at')->default(0);
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_host_storage_configurations',
static function (Blueprint $table): void {
$table->integer('host_id')->primary();
$table->integer('revision');
$table->string('state');
$table->integer('created_at')->default(0);
$table->integer('updated_at')->default(0);
});
$schema->create('cloud_host_resource_pool_memberships',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('resource_pool_id');
$table->string('scheduler_state');
$table->integer('priority')->default(0);
$table->integer('weight')->default(100);
$table->integer('revision')->default(1);
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_host_capacity_policies',
static function (Blueprint $table): void {
$table->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 $column) {
$table->integer($column)->default(0);
}
});
$schema->create('cloud_offers', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('plan_id');
$table->integer('resource_pool_id');
$table->string('code')->unique();
$table->string('name');
$table->string('price');
$table->string('backup_price')->default('0.00');
$table->string('currency')->default('USD');
$table->integer('price_version');
$table->integer('revision');
$table->integer('status');
$table->integer('stock');
$table->integer('sort')->default(0);
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_backup_subscriptions',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('instance_id')->unique();
$table->integer('user_id');
$table->integer('offer_id');
$table->boolean('enabled');
$table->boolean('renew_next_period');
$table->string('frequency')->default('daily');
$table->integer('weekday')->default(0);
$table->integer('retention_count')->default(2);
$table->string('timezone')->default('UTC');
$table->string('price');
$table->string('currency')->default('USD');
$table->integer('price_version');
$table->integer('state_revision')->default(1);
$table->integer('next_run_at')->default(0);
$table->integer('deferred_until')->default(0);
$table->string('defer_reason')->default('');
$table->integer('deferred_at')->nullable();
$table->integer('defer_count')->default(0);
$table->integer('last_scheduled_at')->nullable();
$table->integer('last_backup_id')->nullable();
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_instances', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('user_id');
foreach ([
'order_id', 'plan_id', 'offer_id', 'resource_pool_id',
'reservation_id', 'current_period_id', 'expires_at',
'suspended_at', 'network_suspended_at',
] as $column) {
$table->integer($column)->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 $column) {
$table->integer($column)->default(0);
}
$table->string('renewal_price')->nullable();
$table->string('uuid');
$table->string('name');
$table->string('status');
$table->string('service_status');
$table->string('network_status');
$table->text('spec_json');
$table->text('sealed_credential')->nullable();
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_instance_network_policy_states',
static function (Blueprint $table): void {
$table->integer('instance_id')->primary();
$table->integer('user_id');
$table->boolean('desired_suspended')->default(false);
$table->boolean('applied_suspended')->default(false);
$table->integer('revision')->default(1);
$table->string('state')->default('applied');
$table->string('operation_id')->nullable()->unique();
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_instance_network_restrictions',
static function (Blueprint $table): void {
$table->integer('instance_id');
$table->integer('user_id');
$table->string('reason_code');
$table->boolean('desired_active')->default(false);
$table->integer('revision')->default(1);
$table->string('source_type');
$table->string('source_id');
$table->integer('created_at');
$table->integer('updated_at');
$table->primary(['instance_id', 'reason_code']);
});
$schema->create('cloud_resource_reservations',
static function (Blueprint $table): void {
$table->increments('id');
$table->string('reservation_key')->unique();
$table->string('operation_id')->unique();
foreach ([
'order_id', 'offer_id', 'resource_pool_id', 'host_id',
'user_id',
] as $column) {
$table->integer($column);
}
$table->integer('instance_id')->nullable();
$table->integer('network_attachment_id');
$table->integer('public_ip_pool_id');
$table->integer('public_ip_id');
$table->integer('cpu_cores');
$table->integer('memory_mib');
$table->integer('disk_gib');
$table->string('state');
$table->integer('expires_at')->nullable();
$table->integer('released_at')->nullable();
$table->string('release_reason')->default('');
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_service_periods',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('instance_id');
$table->integer('user_id');
$table->integer('plan_id')->nullable();
$table->integer('order_id')->nullable();
$table->integer('renewal_id')->nullable();
$table->integer('traffic_pool_group_id')->nullable();
$table->string('traffic_quota_mode')->default('finite');
$table->integer('starts_at');
$table->integer('ends_at');
$table->integer('traffic_quota_bytes');
$table->integer('download_base_bytes')->nullable();
$table->integer('upload_base_bytes')->nullable();
$table->integer('download_used_bytes')->default(0);
$table->integer('upload_used_bytes')->default(0);
$table->string('status');
$table->integer('activated_at')->nullable();
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_instance_snapshots',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('instance_id');
$table->integer('user_id');
$table->integer('host_id');
$table->string('state');
$table->integer('size_bytes')->nullable();
});
$schema->create('cloud_invoices', static function (Blueprint $table): void {
$table->increments('id');
$table->string('invoice_no')->unique();
$table->string('purpose');
$table->integer('instance_id')->nullable();
$table->integer('order_id')->nullable();
$table->integer('user_id');
$table->integer('plan_id')->nullable();
$table->integer('offer_id')->nullable();
$table->integer('renewal_id')->nullable();
$table->integer('renews_expires_at')->nullable();
$table->integer('due_at');
$table->string('amount');
$table->string('currency')->default('USD');
$table->integer('validity_days');
$table->integer('traffic_quota_bytes');
$table->string('status');
$table->string('paid_via')->nullable();
$table->string('payment_reference')->nullable();
$table->integer('paid_at')->nullable();
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_invoice_items', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('invoice_id');
$table->integer('order_item_id')->nullable();
$table->string('code');
$table->string('label');
$table->string('amount');
$table->string('currency')->default('USD');
$table->text('snapshot_json');
$table->integer('created_at');
});
$schema->create('cloud_invoice_payments',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('invoice_id');
$table->integer('user_id');
$table->string('status');
});
$schema->create('cloud_mail_outbox',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('invoice_id');
$table->string('kind');
$table->string('status');
$table->integer('attempts');
$table->integer('available_at');
$table->string('lease_token')->nullable();
$table->integer('lease_until');
$table->integer('sent_at')->nullable();
$table->string('last_error');
$table->integer('created_at');
$table->integer('updated_at');
$table->unique(['invoice_id', 'kind']);
});
$schema->create('cloud_jobs', static function (Blueprint $table): void {
$table->increments('id');
$table->string('operation_id')->unique();
$table->string('request_idempotency_key');
$table->string('request_fingerprint');
foreach ([
'parent_job_id', 'order_id', 'invoice_id', 'instance_id',
'snapshot_id',
] as $column) {
$table->integer($column)->nullable();
}
$table->integer('user_id');
$table->integer('host_id')->nullable();
$table->string('kind');
$table->string('execution_mode');
$table->string('phase');
$table->string('outcome')->nullable();
$table->string('status');
$table->text('sealed_payload');
$table->integer('attempts')->default(0);
$table->integer('dispatch_attempts')->default(0);
$table->integer('dispatch_deadline_at')->default(0);
$table->integer('dispatch_started_at')->nullable();
$table->integer('agent_accepted_at')->nullable();
$table->integer('receipt_deadline_at')->nullable();
$table->integer('terminal_at')->nullable();
$table->boolean('effect_may_have_applied')->nullable();
$table->boolean('safe_to_resubmit')->nullable();
$table->string('effect_resolution');
$table->integer('effect_resolved_at')->nullable();
$table->integer('effect_resolved_by')->nullable();
$table->text('effect_resolution_reason')->nullable();
$table->string('completion_event_id')->nullable();
$table->integer('read_at')->nullable();
$table->integer('dispatched_at')->nullable();
$table->string('lease_token')->nullable();
$table->integer('lease_until')->default(0);
$table->integer('available_at');
$table->string('error_code')->nullable();
$table->boolean('finalization_incident')->default(0);
$table->string('finalization_error_code')->nullable();
$table->text('result_json')->nullable();
$table->integer('created_at');
$table->integer('updated_at');
});
$now = time();
$connection = $capsule->getConnection('default');
$connection->table('user')->insert([
'id' => 1,
'user_name' => 'upgrade-user',
'email' => 'upgrade@example.test',
'money' => '100.00',
'enable' => 1,
'is_admin' => 0,
]);
$connection->table('cloud_regions')->insert([
'id' => 1, 'code' => 'hkg', 'name' => 'Hong Kong',
'country_code' => 'HK', 'status' => 'active', 'sort' => 1,
'created_at' => $now, 'updated_at' => $now,
]);
$connection->table('cloud_network_lines')->insert([
'id' => 1, 'region_id' => 1, 'code' => 'premium',
'name' => 'Premium', 'is_default' => 1, 'status' => 'active',
'sort' => 1, 'created_at' => $now, 'updated_at' => $now,
]);
$connection->table('cloud_resource_pools')->insert([
'id' => 1, 'line_id' => 1, 'code' => 'pool-a', 'name' => 'Pool A',
'status' => 'active', 'scheduling_policy' => 'balanced', 'sort' => 1,
'created_at' => $now, 'updated_at' => $now,
]);
$connection->table('cloud_hosts')->insert([
'id' => 1, 'name' => 'host-a', 'region' => 'hkg',
'country_code' => 'HK', 'enabled' => 1, 'resource_pool_id' => 1,
'scheduler_state' => 'active', 'default_network' => 'default',
'created_at' => $now, 'updated_at' => $now,
]);
$connection->table('cloud_host_agents')->insert([
'host_id' => 1,
'sealed_api_secret' => 'test-only',
'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,
]);
$connection->table('cloud_host_storage_configurations')->insert([
'host_id' => 1, 'revision' => 1, 'state' => 'applied',
'created_at' => $now, 'updated_at' => $now,
]);
$connection->table('cloud_host_resource_pool_memberships')->insert([
'host_id' => 1, 'resource_pool_id' => 1,
'scheduler_state' => 'active', 'priority' => 0, 'weight' => 100,
'created_at' => $now, 'updated_at' => $now,
]);
$connection->table('cloud_host_capacity_policies')->insert([
'host_id' => 1,
'configured_cpu_cores' => 64,
'configured_memory_mib' => 131072,
'configured_disk_gib' => 4096,
'safety_cpu_cores' => 0,
'safety_memory_mib' => 0,
'safety_disk_gib' => 0,
'max_instances' => 100,
'created_at' => $now,
'updated_at' => $now,
]);
$fixture = static function (int $sequence, bool $exhausted = false) use (
$connection,
$now
): array {
$sourcePlanId = $sequence * 10 + 1;
$targetPlanId = $sequence * 10 + 2;
$connection->table('shop')->insert([
[
'id' => $sourcePlanId, 'name' => 'Source ' . $sequence,
'cpu_cores' => 2, 'memory_mib' => 2048,
'bandwidth_mbps' => 100, 'disk_gib' => 40,
'validity_days' => 30, 'traffic_quota_bytes' => 1000,
'status' => 1, 'sort' => 1, 'created_at' => $now,
'updated_at' => $now,
],
[
'id' => $targetPlanId, 'name' => 'Target ' . $sequence,
'cpu_cores' => 4, 'memory_mib' => 4096,
'bandwidth_mbps' => 200, 'disk_gib' => 80,
'validity_days' => 30, 'traffic_quota_bytes' => 2000,
'status' => 1, 'sort' => 2, 'created_at' => $now,
'updated_at' => $now,
],
]);
$sourceOfferId = $sequence * 10 + 1;
$targetOfferId = $sequence * 10 + 2;
$connection->table('cloud_offers')->insert([
[
'id' => $sourceOfferId, 'plan_id' => $sourcePlanId,
'resource_pool_id' => 1, 'code' => 'source-' . $sequence,
'name' => 'Source Offer ' . $sequence, 'price' => '10.00',
'backup_price' => '2.00',
'currency' => 'USD', 'price_version' => 1, 'revision' => 1,
'status' => 1, 'stock' => 5, 'sort' => 1,
'created_at' => $now, 'updated_at' => $now,
],
[
'id' => $targetOfferId, 'plan_id' => $targetPlanId,
'resource_pool_id' => 1, 'code' => 'target-' . $sequence,
'name' => 'Target Offer ' . $sequence, 'price' => '20.00',
'backup_price' => '3.00',
'currency' => 'USD', 'price_version' => 3, 'revision' => 4,
'status' => 1, 'stock' => 3, 'sort' => 2,
'created_at' => $now, 'updated_at' => $now,
],
]);
$expiresAt = $now + (15 * 86400);
$spec = json_encode([
'plan_id' => $sourcePlanId,
'plan_name' => 'Source ' . $sequence,
'offer_id' => $sourceOfferId,
'offer_name' => 'Source Offer ' . $sequence,
'stock_mode' => 'finite',
'price_version' => 1,
'validity_days' => 30,
'renewal_price' => '10.00',
'traffic_quota_bytes' => 1000,
'vcpu' => 2,
'cpu_cores' => 2,
'memory_mib' => 2048,
'disk_gib' => 40,
'bandwidth_mbps' => 100,
], JSON_THROW_ON_ERROR);
$instanceId = $connection->table('cloud_instances')->insertGetId([
'host_id' => 1, 'user_id' => 1, 'order_id' => $sequence,
'plan_id' => $sourcePlanId, 'offer_id' => $sourceOfferId,
'resource_pool_id' => 1, 'reservation_id' => null,
'current_period_id' => null, 'expires_at' => $expiresAt,
'renewal_price' => '10.00', 'renewal_days' => 30,
'renewal_traffic_bytes' => 1000, 'traffic_quota_bytes' => 1000,
'uuid' => sprintf('00000000-0000-4000-8000-%012d', $sequence),
'name' => 'vm-upgrade-' . $sequence, 'status' => 'active',
'service_status' => 'active',
'network_status' => $exhausted ? 'blocked_traffic' : 'active',
'spec_json' => $spec, 'created_at' => $now,
'updated_at' => $now,
]);
$reservationId = $connection->table(
'cloud_resource_reservations'
)->insertGetId([
'reservation_key' => 'reservation-' . $sequence,
'operation_id' => md5('reservation-' . $sequence),
'order_id' => $sequence, 'offer_id' => $sourceOfferId,
'resource_pool_id' => 1, 'host_id' => 1, 'user_id' => 1,
'instance_id' => $instanceId, 'network_attachment_id' => 1,
'public_ip_pool_id' => 1, 'public_ip_id' => $sequence,
'cpu_cores' => 2, 'memory_mib' => 2048, 'disk_gib' => 40,
'state' => 'committed', 'created_at' => $now,
'updated_at' => $now,
]);
$periodId = $connection->table('cloud_service_periods')->insertGetId([
'instance_id' => $instanceId, 'user_id' => 1,
'plan_id' => $sourcePlanId, 'order_id' => $sequence,
'starts_at' => $now - (15 * 86400), 'ends_at' => $expiresAt,
'traffic_quota_bytes' => 1000, 'download_base_bytes' => 10,
'upload_base_bytes' => 20,
'download_used_bytes' => $exhausted ? 700 : 100,
'upload_used_bytes' => $exhausted ? 600 : 100,
'status' => $exhausted ? 'exhausted' : 'active',
'activated_at' => $now - (15 * 86400),
'created_at' => $now, 'updated_at' => $now,
]);
$connection->table('cloud_instances')->where('id', $instanceId)
->update([
'reservation_id' => $reservationId,
'current_period_id' => $periodId,
]);
$renewalInvoiceId = $connection->table('cloud_invoices')->insertGetId([
'invoice_no' => 'RENEW-' . $sequence,
'purpose' => 'renewal', 'instance_id' => $instanceId,
'user_id' => 1, 'plan_id' => $sourcePlanId,
'offer_id' => $sourceOfferId, 'renews_expires_at' => $expiresAt,
'due_at' => $expiresAt, 'amount' => '10.00', 'currency' => 'USD',
'validity_days' => 30, 'traffic_quota_bytes' => 1000,
'status' => 'open', 'created_at' => $now, 'updated_at' => $now,
]);
return compact(
'instanceId', 'sourcePlanId', 'targetPlanId', 'sourceOfferId',
'targetOfferId', 'reservationId', 'periodId', 'expiresAt',
'renewalInvoiceId'
);
};
$optionFor = static function (array $options, int $offerId) use ($assert): array {
foreach ($options['offers'] as $option) {
if ((int) $option['offer_id'] === $offerId) {
return $option;
}
}
$assert(false, 'Expected upgrade option was not returned.');
};
$actor = static fn (string $key): array => [
'user_id' => 1,
'is_admin' => false,
'is_super_admin' => false,
'can_manage' => false,
'request_idempotency_key' => $key,
];
// A finite entitlement that already belongs to a shared pool cannot be
// converted to unlimited by an in-place upgrade. Reject it before the
// wallet, job, capacity and Agent-dispatch fences are created.
$pooledFinite = $fixture(9);
$connection->table('cloud_service_periods')
->where('id', $pooledFinite['periodId'])
->update([
'traffic_pool_group_id' => 77,
'traffic_quota_mode' => 'finite',
]);
$pooledInstance = CloudInstance::find($pooledFinite['instanceId']);
$finiteOptions = CloudPlanUpgradeService::options(
$pooledInstance,
User::find(1)
);
$optionFor($finiteOptions, $pooledFinite['targetOfferId']);
$connection->table('cloud_service_periods')
->where('id', $pooledFinite['periodId'])
->update(['traffic_quota_mode' => 'unlimited']);
$expectError(static fn () => CloudPlanUpgradeService::options(
$pooledInstance,
User::find(1)
), 409, 'A corrupt pooled quota-mode snapshot remained upgradeable.');
$connection->table('cloud_service_periods')
->where('id', $pooledFinite['periodId'])
->update(['traffic_quota_mode' => 'finite']);
$connection->table('shop')
->where('id', $pooledFinite['targetPlanId'])
->update(['traffic_quota_bytes' => 0]);
$pooledOptions = CloudPlanUpgradeService::options(
$pooledInstance,
User::find(1)
);
$assert(array_filter(
$pooledOptions['offers'],
static fn (array $option): bool =>
(int) $option['offer_id'] === $pooledFinite['targetOfferId']
) === [],
'A pooled finite period exposed an unlimited in-place upgrade.');
$beforeRejectedUpgrade = [
'balance' => (string) User::find(1)->money,
'jobs' => CloudJob::count(),
'invoices' => CloudInvoice::count(),
'target_stock' => (int) CloudOffer::find(
$pooledFinite['targetOfferId']
)->stock,
'reservation_offer' => (int) CloudResourceReservation::find(
$pooledFinite['reservationId']
)->offer_id,
];
$expectError(static fn () => CloudPlanUpgradeService::enqueue(
$pooledInstance,
User::find(1),
$actor('upgrade-pooled-unlimited-0009'),
$pooledFinite['targetOfferId'],
'5.00'
), 409, 'A pooled finite period was charged for an unlimited upgrade.');
$assert((string) User::find(1)->money
=== $beforeRejectedUpgrade['balance']
&& CloudJob::count() === $beforeRejectedUpgrade['jobs']
&& CloudInvoice::count() === $beforeRejectedUpgrade['invoices']
&& (int) CloudOffer::find($pooledFinite['targetOfferId'])->stock
=== $beforeRejectedUpgrade['target_stock']
&& (int) CloudResourceReservation::find(
$pooledFinite['reservationId']
)->offer_id === $beforeRejectedUpgrade['reservation_offer'],
'Rejected pooled finite-to-unlimited upgrade changed paid fences.');
// Unknown keeps every paid fence; later not_applied restores capacity,
// inventory and balance exactly once.
$failed = $fixture(1);
$instance = CloudInstance::find($failed['instanceId']);
$user = User::find(1);
$options = CloudPlanUpgradeService::options($instance, $user);
$option = $optionFor($options, $failed['targetOfferId']);
$assert($option['amount'] === '5.00'
&& $option['renewal_price'] === '20.00',
'Upgrade quote or target renewal price was not derived from the immutable offer.');
$job = CloudPlanUpgradeService::enqueue(
$instance,
$user,
$actor('upgrade-failure-0001'),
$failed['targetOfferId'],
$option['amount']
);
$payload = CloudSecrets::decrypt((string) $job->sealed_payload, 'job-payload');
$pendingUpgradeBill = CloudInvoiceService::display(
CloudInvoice::find((int) $job->invoice_id)
);
$assert($pendingUpgradeBill['plan_name'] === 'Target 1'
&& $pendingUpgradeBill['cpu_cores'] === 4
&& $pendingUpgradeBill['memory_mib'] === 4096
&& $pendingUpgradeBill['disk_gib'] === 80
&& $pendingUpgradeBill['bandwidth_mbps'] === 200
&& $pendingUpgradeBill['payment_deadline_at'] === null,
'Pending upgrade bill used mutable source resources or invented a payment deadline.');
$assert($payload['method'] === 'POST'
&& $payload['request_target']
=== '/v1/vms/' . $instance->uuid . '/resources'
&& ($payload['body']['from_vcpu'] ?? null) === 2
&& ($payload['body']['target_vcpu'] ?? null) === 4
&& ($payload['body']['confirm_shutdown'] ?? null) === true,
'Upgrade job lost the exact POST /resources Agent contract.');
$sourceSpec = (string) $instance->spec_json;
CloudInstanceMetadata::persistBatch([
['id' => (string) $instance->id, 'uuid' => (string) $instance->uuid],
], [
(string) $instance->uuid => [
'uuid' => (string) $instance->uuid,
'vcpu' => 4,
'memory_mib' => 4096,
'disks' => [['capacity_gib' => 80]],
'autostart' => true,
],
]);
$assert((string) CloudInstance::find($failed['instanceId'])->spec_json
=== $sourceSpec
&& hash('sha256', $sourceSpec)
=== (string) $payload['upgrade_snapshot']['source']['spec_hash'],
'Live metadata rewrote the immutable source while upgrade owned the instance fence.');
$upgradeInvoice = CloudInvoice::find((int) $job->invoice_id);
$renewalInvoice = CloudInvoice::find($failed['renewalInvoiceId']);
$assert((CloudInvoiceService::display($renewalInvoice)['payable'] ?? null)
=== false,
'A renewal bill remained publicly payable while upgrade owned the instance fence.');
$expectError(static fn () => CloudInvoiceService::payBalance(
$upgradeInvoice,
$actor('upgrade-invoice-pay-0001'),
'upgrade-invoice-pay-0001'
), 409, 'A settled upgrade audit invoice was accepted by balance payment.');
$expectError(static fn () => CloudInvoicePaymentService::enqueue(
$upgradeInvoice,
User::find(1),
['method' => 'paytaro',
'idempotency_key' => 'upgrade-online-pay-0001'],
'127.0.0.1',
'test'
), 409, 'A settled upgrade audit invoice was accepted by online payment.');
$expectError(static fn () => CloudInvoicePaymentService::create(
$renewalInvoice,
User::find(1),
['method' => 'paytaro',
'idempotency_key' => 'renewal-during-upgrade-0001'],
'127.0.0.1',
'test'
), 409, 'Renewal online payment crossed an active upgrade fence.');
$reservation = CloudResourceReservation::find($failed['reservationId']);
$assert((int) $reservation->offer_id === $failed['targetOfferId']
&& (int) $reservation->cpu_cores === 4
&& (int) CloudOffer::find($failed['targetOfferId'])->stock === 2
&& (string) User::find(1)->money === '95.00',
'Enqueue did not atomically charge and hold target capacity/stock.');
CloudPlanUpgradeService::finishJobLocked(
$job,
'uncertain',
null,
$payload,
User::find(1)
);
$assert((int) CloudResourceReservation::find(
$failed['reservationId']
)->offer_id === $failed['targetOfferId']
&& (int) CloudOffer::find($failed['sourceOfferId'])->stock === 5
&& (int) CloudOffer::find($failed['targetOfferId'])->stock === 2
&& (string) User::find(1)->money === '95.00',
'Unknown effect released a paid capacity, money or inventory fence.');
CloudPlanUpgradeService::finishJobLocked(
$job,
'failed',
null,
$payload,
User::find(1)
);
$assert((int) CloudResourceReservation::find(
$failed['reservationId']
)->offer_id === $failed['sourceOfferId']
&& (int) CloudOffer::find($failed['targetOfferId'])->stock === 3
&& (string) User::find(1)->money === '100.00'
&& (string) CloudInvoice::find((int) $job->invoice_id)->status
=== 'refunded',
'Confirmed not_applied did not restore every paid upgrade ledger.');
CloudPlanUpgradeService::finishJobLocked(
$job,
'failed',
null,
$payload,
User::find(1)
);
$job->sealed_payload = CloudSecrets::encrypt(
['terminal' => true],
'job-payload'
);
$job->phase = 'terminal';
$job->outcome = 'failed';
$job->status = 'failed';
$job->save();
$replayedFailure = CloudPlanUpgradeService::enqueue(
CloudInstance::find($failed['instanceId']),
User::find(1),
$actor('upgrade-failure-0001'),
$failed['targetOfferId'],
$option['amount']
);
$assert((int) $replayedFailure->id === (int) $job->id
&& (string) User::find(1)->money === '100.00'
&& CloudJob::where('request_idempotency_key',
'upgrade-failure-0001')->count() === 1,
'Terminal refunded replay depended on erased payload or charged twice.');
$expectError(static fn () => CloudPlanUpgradeService::enqueue(
CloudInstance::find($failed['instanceId']),
User::find(1),
$actor('upgrade-failure-0001'),
$failed['sourceOfferId'],
$option['amount']
), 409, 'One idempotency key was accepted for a different offer.');
$expectError(static fn () => CloudPlanUpgradeService::enqueue(
CloudInstance::find($failed['instanceId']),
User::find(1),
$actor('upgrade-failure-0001'),
$failed['targetOfferId'],
'5.01'
), 409, 'One idempotency key was accepted for a different amount.');
// Applied upgrades change the current/renewal snapshots without extending
// time or resetting usage, release source stock and void stale renewal.
$applied = $fixture(2, true);
$connection->table('cloud_backup_subscriptions')->insert([
'instance_id' => $applied['instanceId'], 'user_id' => 1,
'offer_id' => $applied['sourceOfferId'], 'enabled' => 0,
'renew_next_period' => 1, 'frequency' => 'daily', 'weekday' => 0,
'retention_count' => 2, 'timezone' => 'UTC',
'price' => '2.00', 'currency' => 'USD', 'price_version' => 1,
'next_run_at' => $now + 86400, 'last_scheduled_at' => null,
'last_backup_id' => null, 'created_at' => $now,
'updated_at' => $now,
]);
$instance = CloudInstance::find($applied['instanceId']);
$options = CloudPlanUpgradeService::options($instance, User::find(1));
$option = $optionFor($options, $applied['targetOfferId']);
$job = CloudPlanUpgradeService::enqueue(
$instance,
User::find(1),
$actor('upgrade-success-0002'),
$applied['targetOfferId'],
$option['amount']
);
$payload = CloudSecrets::decrypt((string) $job->sealed_payload, 'job-payload');
$result = [
'uuid' => (string) $instance->uuid,
'resources_resized' => true,
'status' => 'running',
'originally_running' => true,
'power_state_restored' => true,
'from_vcpu' => 2,
'target_vcpu' => 4,
'applied_vcpu' => 4,
'from_memory_mib' => 2048,
'target_memory_mib' => 4096,
'applied_memory_mib' => 4096,
'from_disk_gib' => 40,
'target_disk_gib' => 80,
'applied_disk_gib' => 80,
'from_bandwidth_mbps' => 100,
'target_bandwidth_mbps' => 200,
'applied_bandwidth_mbps' => 200,
];
CloudPlanUpgradeService::finishJobLocked(
$job,
'succeeded',
$result,
$payload,
User::find(1)
);
$upgraded = CloudInstance::find($applied['instanceId']);
$period = CloudServicePeriod::find($applied['periodId']);
$spec = json_decode((string) $upgraded->spec_json, true,
32, JSON_THROW_ON_ERROR);
$assert((int) $upgraded->plan_id === $applied['targetPlanId']
&& (int) $upgraded->offer_id === $applied['targetOfferId']
&& (int) $upgraded->expires_at === $applied['expiresAt']
&& (string) $upgraded->renewal_price === '20.00'
&& (int) $upgraded->renewal_traffic_bytes === 2000
&& (int) $period->traffic_quota_bytes === 2000
&& $period->traffic_pool_group_id === null
&& (string) $period->traffic_quota_mode === 'finite'
&& (int) $period->download_used_bytes === 700
&& (int) $period->upload_used_bytes === 600
&& (string) $period->status === 'active'
&& (string) $upgraded->network_status === 'restore_pending'
&& ($spec['vcpu'] ?? null) === 4
&& ($spec['memory_mib'] ?? null) === 4096
&& ($spec['disk_gib'] ?? null) === 80
&& ($spec['bandwidth_mbps'] ?? null) === 200,
'Applied upgrade did not converge instance/current-period snapshots.');
$assert((int) CloudOffer::find($applied['sourceOfferId'])->stock === 6
&& (int) CloudOffer::find($applied['targetOfferId'])->stock === 2
&& (string) CloudInvoice::find(
$applied['renewalInvoiceId']
)->status === 'void',
'Applied upgrade did not release source stock or void stale renewal.');
$upgradedBackup = $connection->table('cloud_backup_subscriptions')
->where('instance_id', $applied['instanceId'])->first();
$assert((int) $upgradedBackup->offer_id === $applied['targetOfferId']
&& (string) $upgradedBackup->price === '3.00'
&& (int) $upgradedBackup->price_version === 3
&& (int) $upgradedBackup->enabled === 0
&& (int) $upgradedBackup->renew_next_period === 1,
'Upgrade did not move a mid-cycle backup subscription to the target offer price.');
$mutated = json_decode((string) $upgraded->spec_json, true,
32, JSON_THROW_ON_ERROR);
$mutated['plan_name'] = 'Later mutable plan';
$mutated['vcpu'] = $mutated['cpu_cores'] = 32;
$mutated['memory_mib'] = 65536;
$mutated['disk_gib'] = 1024;
$upgraded->spec_json = json_encode($mutated, JSON_THROW_ON_ERROR);
$upgraded->save();
$historicalBill = CloudInvoiceService::display(
CloudInvoice::find((int) $job->invoice_id)
);
$assert($historicalBill['plan_name'] === 'Target 2'
&& $historicalBill['cpu_cores'] === 4
&& $historicalBill['memory_mib'] === 4096
&& $historicalBill['disk_gib'] === 80,
'Historical upgrade bill changed with the instance current specification.');
$balanceAfterSuccess = (string) User::find(1)->money;
$job->sealed_payload = CloudSecrets::encrypt(
['terminal' => true],
'job-payload'
);
$job->phase = 'terminal';
$job->outcome = 'succeeded';
$job->status = 'succeeded';
$job->save();
$replayedSuccess = CloudPlanUpgradeService::enqueue(
$upgraded,
User::find(1),
$actor('upgrade-success-0002'),
$applied['targetOfferId'],
$option['amount']
);
$assert((int) $replayedSuccess->id === (int) $job->id
&& (string) User::find(1)->money === $balanceAfterSuccess
&& CloudJob::where('request_idempotency_key',
'upgrade-success-0002')->count() === 1,
'Terminal successful replay depended on erased payload or charged twice.');
// A nominal Agent success without the complete exact evidence must leave
// every paid fence intact instead of advancing or compensating a ledger.
$incomplete = $fixture(3);
$instance = CloudInstance::find($incomplete['instanceId']);
$options = CloudPlanUpgradeService::options($instance, User::find(1));
$option = $optionFor($options, $incomplete['targetOfferId']);
$job = CloudPlanUpgradeService::enqueue(
$instance,
User::find(1),
$actor('upgrade-evidence-0003'),
$incomplete['targetOfferId'],
$option['amount']
);
$payload = CloudSecrets::decrypt((string) $job->sealed_payload, 'job-payload');
$incompleteResult = [
'uuid' => (string) $instance->uuid,
'resources_resized' => true,
'status' => 'running',
'originally_running' => true,
'power_state_restored' => true,
'from_vcpu' => 2,
'target_vcpu' => 4,
'applied_vcpu' => 4,
'from_memory_mib' => 2048,
'target_memory_mib' => 4096,
'applied_memory_mib' => 4096,
'from_disk_gib' => 40,
'target_disk_gib' => 80,
'applied_disk_gib' => 80,
'from_bandwidth_mbps' => 100,
'target_bandwidth_mbps' => 200,
// Deliberately omit applied_bandwidth_mbps.
];
$expectError(static fn () => CloudPlanUpgradeService::finishJobLocked(
$job,
'succeeded',
$incompleteResult,
$payload,
User::find(1)
), 502, 'Incomplete Agent success evidence advanced the upgrade.');
$stillSource = CloudInstance::find($incomplete['instanceId']);
$assert((int) $stillSource->offer_id === $incomplete['sourceOfferId']
&& (int) CloudResourceReservation::find(
$incomplete['reservationId']
)->offer_id === $incomplete['targetOfferId']
&& (int) CloudOffer::find($incomplete['sourceOfferId'])->stock === 5
&& (int) CloudOffer::find($incomplete['targetOfferId'])->stock === 2
&& (string) CloudInvoice::find((int) $job->invoice_id)->status === 'paid'
&& (string) User::find(1)->money === '90.00',
'Incomplete success evidence mutated or released a paid fence.');
CloudPlanUpgradeService::finishJobLocked(
$job,
'uncertain',
null,
$payload,
User::find(1)
);
$job->phase = 'terminal';
$job->outcome = 'failed';
$job->status = 'failed';
$job->dispatch_attempts = 1;
$job->effect_may_have_applied = 1;
$job->safe_to_resubmit = 0;
$job->effect_resolution = 'unknown';
$job->terminal_at = time();
$job->save();
$period = CloudServicePeriod::find($incomplete['periodId']);
$expiredAt = time() - 1;
$period->ends_at = $expiredAt;
$period->save();
$stillSource->expires_at = $expiredAt;
$stillSource->save();
$staleRenewal = CloudInvoice::find($incomplete['renewalInvoiceId']);
$staleRenewal->renews_expires_at = $expiredAt;
$staleRenewal->due_at = $expiredAt;
$staleRenewal->save();
$jobCount = CloudJob::count();
$lifecycle = new CloudLifecycleService();
$reconcile = (new ReflectionClass(CloudLifecycleService::class))
->getMethod('reconcile');
$queued = $reconcile->invoke($lifecycle, $incomplete['instanceId']);
$frozen = CloudInstance::find($incomplete['instanceId']);
$period->refresh();
$assert($queued === false
&& (string) $period->status === 'active'
&& (int) $frozen->current_period_id === $incomplete['periodId']
&& (int) $frozen->expires_at === $expiredAt
&& (string) $frozen->service_status === 'active'
&& (string) $frozen->network_status === 'active'
&& CloudJob::count() === $jobCount,
'Lifecycle mutated an expired instance while upgrade effect was unknown.');
$resolutionStartedAt = time();
CloudPlanUpgradeService::finishJobLocked(
$job,
'failed',
null,
$payload,
User::find(1)
);
$job->effect_resolution = 'not_applied';
$job->effect_resolved_at = time();
$job->save();
$replacementRenewal = CloudInvoice::where(
'instance_id',
$incomplete['instanceId']
)->where('purpose', 'renewal')->where('status', 'open')
->orderByDesc('id')->first();
$assert((string) $staleRenewal->fresh()->status === 'void'
&& $replacementRenewal !== null
&& (int) $replacementRenewal->renews_expires_at === $expiredAt
&& (int) $replacementRenewal->due_at >= $resolutionStartedAt
&& CloudInvoiceService::paymentDeadline($replacementRenewal)
=== (int) $replacementRenewal->due_at
+ CloudInvoiceService::PAYMENT_GRACE_SECONDS
&& (int) CloudInstance::find($incomplete['instanceId'])->expires_at
=== $expiredAt,
'Resolved upgrade did not restore a full renewal grace window without extending service.');
$reconcile->invoke($lifecycle, $incomplete['instanceId']);
$afterResolution = CloudInstance::find($incomplete['instanceId']);
$assert(!in_array((string) $afterResolution->status,
['deleting', 'delete_failed', 'deleted'], true)
&& CloudJob::where('instance_id', $incomplete['instanceId'])
->where('kind', 'expiry_delete')->count() === 0,
'Lifecycle deleted the instance immediately after delayed upgrade recovery.');
echo "Cloud plan upgrade service tests passed.\n";
}