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/CloudPublicIpPoolReplacemen...

1767 lines
68 KiB
PHTML

<?php
namespace App\Services\Cloud {
final class CloudSecrets
{
public static function encrypt(array $value, string $purpose): string
{
return json_encode(
$value,
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
);
}
public static function decrypt(string $value, string $purpose): array
{
$decoded = json_decode($value, true, 64, JSON_THROW_ON_ERROR);
return is_array($decoded) ? $decoded : [];
}
}
final class CloudPanelUsageService
{
public static function managementAddress($host): ?string
{
return null;
}
}
}
namespace {
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\CloudJob;
use App\Models\CloudPublicNetworkTransition;
use App\Models\CloudPublicIpPoolReplacementBatch;
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudIpAddress;
use App\Services\Cloud\CloudPublicIpPoolReplacementBatchService;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Schema\Blueprint;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$rejects = static function (
callable $callback,
string $message,
array $allowedStatuses = [404, 409]
) use ($assert): void {
try {
$callback();
} catch (CloudError $error) {
$assert(in_array($error->httpStatus, $allowedStatuses, true),
$message . ' (unexpected HTTP status)');
return;
}
throw new RuntimeException($message . ' (request was accepted)');
};
$snapshot = static function (): array {
$tables = [
'cloud_jobs',
'cloud_public_ip_pool_replacement_batches',
'cloud_public_ip_pool_replacement_batch_items',
'cloud_public_network_transitions',
'cloud_public_network_transition_ips',
'cloud_resource_reservation_ips',
'cloud_public_ips',
];
$result = [];
foreach ($tables as $table) {
$result[$table] = DB::table($table)->orderBy('id')->get()
->map(static fn ($row): array => (array) $row)->all();
}
$result['pools'] = DB::table('cloud_public_ip_pools')
->orderBy('id')->get()->map(static fn ($row): array => (array) $row)
->all();
return $result;
};
$capsule = new DB();
$capsule->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
], 'default');
$capsule->setAsGlobal();
$capsule->bootEloquent();
$schema = $capsule->schema('default');
$schema->create('user', static function (Blueprint $t): void {
$t->increments('id');
$t->string('money')->default('0.00');
$t->boolean('enable')->default(true);
});
$schema->create('cloud_hosts', static function (Blueprint $t): void {
$t->increments('id');
$t->string('name');
$t->boolean('enabled');
$t->integer('created_at')->default(0);
$t->integer('updated_at')->default(0);
});
$schema->create('cloud_host_agents', static function (Blueprint $t): void {
$t->integer('host_id')->primary();
$t->text('sealed_api_secret');
$t->boolean('enabled');
$t->integer('revision');
$t->integer('tested_revision');
$t->string('last_test_status');
$t->string('last_error_code');
$t->string('last_agent_version');
$t->text('network_selectors_json');
$t->text('public_network_capabilities_json');
$t->integer('last_tested_at');
$t->integer('created_at');
$t->integer('updated_at');
});
$schema->create('cloud_resource_pools', static function (Blueprint $t): void {
$t->increments('id');
$t->integer('line_id');
$t->string('code');
$t->string('name');
$t->string('status');
});
$schema->create('cloud_host_network_attachments', static function (
Blueprint $t
): void {
$t->increments('id');
$t->string('attachment_key');
$t->integer('host_id');
$t->integer('line_id');
$t->string('name');
$t->string('network_selector');
$t->string('state');
$t->integer('priority');
$t->integer('verified_agent_revision');
$t->string('verification_state');
$t->integer('last_verified_at');
$t->integer('created_at')->default(0);
$t->integer('updated_at')->default(0);
});
$schema->create('cloud_public_ip_pools', static function (
Blueprint $t
): void {
$t->increments('id');
$t->string('pool_key', 32)->unique();
$t->integer('host_id');
$t->integer('network_attachment_id');
$t->integer('address_family');
$t->string('name');
$t->string('cidr');
$t->string('gateway');
$t->boolean('gateway_on_link');
$t->integer('guest_prefix_length');
$t->text('dns_servers_json');
$t->text('reserved_ips_json');
$t->string('allocation_start')->nullable();
$t->string('allocation_end')->nullable();
$t->string('allocation_cursor')->nullable();
$t->integer('max_allocations');
$t->string('state');
$t->integer('priority');
$t->integer('desired_revision');
$t->integer('observed_revision');
$t->string('sync_state');
$t->string('last_error_code')->default('');
$t->integer('created_at')->default(0);
$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('address_family');
$t->string('address');
$t->binary('address_binary');
$t->integer('prefix_length');
$t->string('state');
$t->integer('reservation_id')->nullable();
$t->integer('reservation_ip_claim_id')->nullable()->unique();
$t->integer('instance_id')->nullable();
$t->string('operation_id')->nullable();
$t->integer('created_at');
$t->integer('updated_at');
$t->unique(['address_family', 'address_binary']);
});
$schema->create('cloud_resource_reservations', static function (
Blueprint $t
): void {
$t->increments('id');
$t->string('reservation_key');
$t->string('operation_id')->unique();
$t->integer('order_id')->nullable();
$t->integer('backup_restore_id')->nullable();
$t->integer('offer_id')->nullable();
$t->integer('resource_pool_id');
$t->integer('host_id');
$t->integer('user_id');
$t->integer('instance_id')->nullable();
$t->integer('network_attachment_id')->nullable();
$t->integer('public_ip_pool_id')->nullable();
$t->integer('public_ip_id')->nullable();
$t->integer('cpu_cores');
$t->integer('memory_mib');
$t->integer('disk_gib');
$t->string('state');
$t->integer('expires_at')->nullable();
$t->integer('released_at')->nullable();
$t->string('release_reason');
$t->integer('created_at');
$t->integer('updated_at');
});
$schema->create('cloud_resource_reservation_ips', static function (
Blueprint $t
): void {
$t->increments('id');
$t->integer('reservation_id');
$t->integer('address_family');
$t->integer('claim_generation');
$t->integer('active_claim')->nullable();
$t->integer('network_attachment_id');
$t->integer('public_ip_pool_id');
$t->integer('public_ip_id');
$t->string('operation_id');
$t->string('state');
$t->integer('released_at')->nullable();
$t->string('release_reason');
$t->integer('created_at');
$t->integer('updated_at');
$t->unique(['reservation_id', 'address_family', 'active_claim']);
$t->unique(['reservation_id', 'claim_generation', 'address_family']);
$t->unique(['operation_id', 'address_family']);
});
$schema->create('cloud_instances', static function (Blueprint $t): void {
$t->increments('id');
$t->integer('host_id');
$t->integer('user_id');
$t->integer('order_id')->nullable();
$t->integer('plan_id')->nullable();
$t->integer('offer_id')->nullable();
$t->integer('resource_pool_id');
$t->integer('reservation_id');
$t->string('uuid');
$t->string('name');
$t->string('status');
$t->string('service_status');
$t->string('network_status');
$t->integer('expires_at')->nullable();
$t->text('spec_json');
$t->integer('created_at');
$t->integer('updated_at');
});
$schema->create('cloud_public_network_transitions', static function (
Blueprint $t
): void {
$t->increments('id');
$t->string('transition_key')->unique();
$t->string('operation_id')->unique();
$t->string('kind');
$t->integer('instance_id');
$t->integer('host_id');
$t->integer('reservation_id');
$t->integer('invoice_id')->nullable()->unique();
$t->integer('pool_replacement_batch_item_id')->nullable()->unique();
$t->integer('requested_by_user_id');
$t->integer('source_network_attachment_id');
$t->integer('target_network_attachment_id');
$t->string('source_claim_set_sha256');
$t->string('target_claim_set_sha256');
$t->text('source_public_network_json');
$t->string('source_public_network_sha256');
$t->text('target_public_network_json');
$t->string('target_public_network_sha256');
$t->string('state');
$t->string('effect_resolution');
$t->integer('active_transition')->nullable();
$t->string('last_error_code');
$t->integer('effect_resolved_at')->nullable();
$t->integer('terminal_at')->nullable();
$t->integer('created_at');
$t->integer('updated_at');
$t->unique(['instance_id', 'active_transition']);
$t->unique(['reservation_id', 'active_transition']);
});
$schema->create('cloud_public_network_transition_ips', static function (
Blueprint $t
): void {
$t->increments('id');
$t->integer('transition_id');
$t->integer('address_family');
$t->integer('source_claim_id');
$t->integer('target_claim_id')->nullable()->unique();
$t->boolean('changed');
$t->integer('created_at');
$t->integer('updated_at');
$t->unique(['transition_id', 'address_family']);
});
$schema->create('cloud_jobs', static function (Blueprint $t): void {
$t->increments('id');
$t->string('operation_id', 32)->unique();
$t->string('request_idempotency_key', 128);
$t->string('request_fingerprint', 64);
foreach (['parent_job_id', 'supersedes_job_id',
'vpc_repair_root_job_id', 'order_id', 'invoice_id', 'instance_id',
'snapshot_id', 'backup_id', 'backup_restore_id',
'backup_recovery_id', 'public_network_transition_id', 'vpc_id',
'vpc_port_id', 'vpc_batch_id'] as $column) {
$t->integer($column)->nullable();
}
$t->integer('user_id');
$t->integer('host_id')->nullable();
$t->string('kind');
$t->string('execution_mode');
$t->string('phase');
$t->string('outcome')->nullable();
$t->string('status');
$t->text('sealed_payload');
$t->integer('attempts');
$t->integer('dispatch_attempts');
$t->integer('dispatch_deadline_at');
$t->integer('dispatch_started_at')->nullable();
$t->integer('agent_accepted_at')->nullable();
$t->integer('receipt_deadline_at')->nullable();
$t->integer('terminal_at')->nullable();
$t->boolean('effect_may_have_applied')->nullable();
$t->boolean('safe_to_resubmit')->nullable();
$t->string('effect_resolution');
$t->integer('effect_resolved_at')->nullable();
$t->integer('effect_resolved_by')->nullable();
$t->text('effect_resolution_reason')->nullable();
$t->boolean('finalization_incident');
$t->string('finalization_error_code')->nullable();
$t->string('completion_event_id')->nullable();
$t->integer('read_at')->nullable();
$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(['user_id', 'request_idempotency_key']);
});
$schema->create('cloud_public_ip_pool_replacement_batches',
static function (Blueprint $t): void {
$t->increments('id');
$t->string('batch_key', 32)->unique();
$t->integer('host_id');
$t->integer('source_public_ip_pool_id');
$t->integer('target_public_ip_pool_id');
$t->string('source_initial_state');
$t->integer('parent_job_id')->unique();
$t->integer('requested_by_user_id');
$t->string('state');
$t->integer('active_claim')->nullable();
$t->string('last_error_code')->default('');
$t->integer('terminal_at')->nullable();
$t->integer('created_at');
$t->integer('updated_at');
});
$schema->create('cloud_instance_migrations', static function (
Blueprint $t
): void {
$t->increments('id');
$t->integer('instance_id');
$t->string('state');
});
$schema->create('cloud_backup_recoveries', static function (
Blueprint $t
): void {
$t->increments('id');
$t->integer('instance_id');
$t->string('state');
});
DB::statement(<<<'SQL'
CREATE TABLE cloud_public_ip_pool_replacement_batch_items (
id INTEGER PRIMARY KEY AUTOINCREMENT,
batch_id INTEGER NOT NULL,
instance_id INTEGER NOT NULL,
address_family INTEGER NOT NULL,
source_reservation_id INTEGER NOT NULL,
source_public_ip_id INTEGER NOT NULL,
target_public_ip_id INTEGER NOT NULL,
source_network_attachment_id INTEGER NOT NULL,
target_network_attachment_id INTEGER NOT NULL,
child_job_id INTEGER NULL UNIQUE,
state TEXT NOT NULL,
last_error_code TEXT NOT NULL DEFAULT '',
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL,
UNIQUE (batch_id, instance_id),
UNIQUE (batch_id, target_public_ip_id),
CHECK (source_public_ip_id <> target_public_ip_id),
CHECK (address_family IN (4,6)),
CHECK (
(state = 'preparing' AND child_job_id IS NULL)
OR (state IN ('queued','succeeded','failed','unknown')
AND child_job_id IS NOT NULL)
)
)
SQL);
$now = time();
DB::table('user')->insert([
['id' => 99, 'money' => '0.00', 'enable' => 1],
['id' => 501, 'money' => '0.00', 'enable' => 1],
['id' => 502, 'money' => '0.00', 'enable' => 1],
]);
DB::table('cloud_hosts')->insert([
['id' => 1, 'name' => 'HKG-A', 'enabled' => 1,
'created_at' => $now, 'updated_at' => $now],
['id' => 2, 'name' => 'HKG-B', 'enabled' => 1,
'created_at' => $now, 'updated_at' => $now],
]);
$capabilities = json_encode([
'contract' => 1,
'dual_stack_required' => true,
'project_owned_filter' => true,
'ndp_guard' => true,
], JSON_THROW_ON_ERROR);
foreach ([1, 2] as $hostId) {
DB::table('cloud_host_agents')->insert([
'host_id' => $hostId,
'sealed_api_secret' => 'unused',
'enabled' => 1,
'revision' => 3,
'tested_revision' => 3,
'last_test_status' => 'healthy',
'last_error_code' => '',
'last_agent_version' => 'test',
'network_selectors_json' =>
'["bridge:br0","bridge:br1","bridge:br2"]',
'public_network_capabilities_json' => $capabilities,
'last_tested_at' => $now,
'created_at' => $now,
'updated_at' => $now,
]);
}
DB::table('cloud_resource_pools')->insert([
['id' => 1, 'line_id' => 10, 'code' => 'line-a',
'name' => 'Line A', 'status' => 'active'],
['id' => 2, 'line_id' => 20, 'code' => 'line-b',
'name' => 'Line B', 'status' => 'active'],
]);
foreach ([
[11, 'a', 1, 10, 'bridge:br0'],
[12, 'b', 1, 10, 'bridge:br1'],
[13, 'c', 1, 20, 'bridge:br2'],
[14, 'd', 2, 10, 'bridge:br0'],
] as [$id, $key, $hostId, $lineId, $selector]) {
DB::table('cloud_host_network_attachments')->insert([
'id' => $id,
'attachment_key' => str_repeat($key, 32),
'host_id' => $hostId,
'line_id' => $lineId,
'name' => 'Attachment ' . $id,
'network_selector' => $selector,
'state' => 'active',
'priority' => $id,
'verified_agent_revision' => 3,
'verification_state' => 'verified',
'last_verified_at' => $now,
'created_at' => $now,
'updated_at' => $now,
]);
}
$poolKey = static fn (string $character): string => str_repeat(
$character,
32
);
$pool = static function (int $id, string $key, int $hostId,
int $attachmentId, int $family, string $cidr, string $gateway,
int $revision = 1, ?string $start = null, ?string $end = null,
?string $cursor = null, int $maximum = 0
) use ($now): array {
return [
'id' => $id,
'pool_key' => str_repeat($key, 32),
'host_id' => $hostId,
'network_attachment_id' => $attachmentId,
'address_family' => $family,
'name' => 'Pool ' . $id,
'cidr' => $cidr,
'gateway' => $gateway,
'gateway_on_link' => 0,
'guest_prefix_length' => $family === 4 ? 29 : 64,
'dns_servers_json' => '["1.1.1.1"]',
'reserved_ips_json' => '[]',
'allocation_start' => $start,
'allocation_end' => $end,
'allocation_cursor' => $cursor,
'max_allocations' => $maximum,
'state' => 'active',
'priority' => $id,
'desired_revision' => $revision,
'observed_revision' => $revision,
'sync_state' => 'applied',
'last_error_code' => '',
'created_at' => $now,
'updated_at' => $now,
];
};
DB::table('cloud_public_ip_pools')->insert([
$pool(11, 'a', 1, 11, 4, '8.8.8.0/29', '8.8.8.1', 11),
$pool(12, 'b', 1, 11, 6, '2404:c140:156:3a::/64',
'2404:c140:156:3a::1', 12, '2404:c140:156:3a::1000',
'2404:c140:156:3a::10ff', '2404:c140:156:3a::1000', 100),
$pool(13, 'c', 1, 11, 4, '8.8.8.8/29', '8.8.8.9', 13),
$pool(14, 'd', 1, 11, 6, '2404:c140:156:3b::/64',
'2404:c140:156:3b::1', 14, '2404:c140:156:3b::1000',
'2404:c140:156:3b::10ff', '2404:c140:156:3b::1000', 100),
$pool(15, 'e', 1, 12, 4, '8.8.8.16/29', '8.8.8.17', 15),
$pool(16, 'f', 1, 13, 4, '8.8.8.24/29', '8.8.8.25', 16),
$pool(17, '1', 2, 14, 4, '9.9.9.0/29', '9.9.9.1', 17),
$pool(21, '2', 1, 11, 4, '9.9.9.8/29', '9.9.9.9', 21),
$pool(22, '3', 1, 11, 6, '2404:c140:156:3c::/64',
'2404:c140:156:3c::1', 22, '2404:c140:156:3c::1000',
'2404:c140:156:3c::10ff', '2404:c140:156:3c::1000', 100),
$pool(23, '4', 1, 11, 6, '2404:c140:156:3d::/64',
'2404:c140:156:3d::1', 23, '2404:c140:156:3d::1000',
'2404:c140:156:3d::1003', '2404:c140:156:3d::1000', 4),
]);
$operation = static fn (int $id): string => str_pad(
dechex($id),
32,
'0',
STR_PAD_LEFT
);
$ipRow = static function (int $id, int $poolId, int $family,
string $address, string $state, ?int $reservationId,
?int $claimId, ?int $instanceId, ?string $operationId
) use ($now): array {
return [
'id' => $id,
'host_id' => 1,
'public_ip_pool_id' => $poolId,
'address_family' => $family,
'address' => $address,
'address_binary' => CloudIpAddress::parse($address)->binary(),
'prefix_length' => $family === 4 ? 29 : 64,
'state' => $state,
'reservation_id' => $reservationId,
'reservation_ip_claim_id' => $claimId,
'instance_id' => $instanceId,
'operation_id' => $operationId,
'created_at' => $now,
'updated_at' => $now,
];
};
DB::table('cloud_public_ips')->insert([
$ipRow(1101, 11, 4, '8.8.8.2', 'assigned', 1001, 10011,
101, $operation(1001)),
$ipRow(1102, 11, 4, '8.8.8.3', 'assigned', 1002, 10021,
102, $operation(1002)),
$ipRow(1201, 12, 6, '2404:c140:156:3a::1000', 'assigned', 1001,
10012, 101, $operation(1001)),
$ipRow(1202, 12, 6, '2404:c140:156:3a::1001', 'assigned', 1002,
10022, 102, $operation(1002)),
$ipRow(1301, 13, 4, '8.8.8.10', 'available', null, null,
null, null),
$ipRow(1302, 13, 4, '8.8.8.11', 'available', null, null,
null, null),
$ipRow(1501, 15, 4, '8.8.8.18', 'available', null, null,
null, null),
$ipRow(1502, 15, 4, '8.8.8.19', 'available', null, null,
null, null),
$ipRow(1601, 16, 4, '8.8.8.26', 'available', null, null,
null, null),
$ipRow(1602, 16, 4, '8.8.8.27', 'available', null, null,
null, null),
$ipRow(2101, 21, 4, '9.9.9.10', 'assigned', 2001, 20011,
201, $operation(2001)),
$ipRow(2102, 21, 4, '9.9.9.11', 'assigned', 2002, 20021,
202, $operation(2002)),
$ipRow(2201, 22, 6, '2404:c140:156:3c::1000', 'assigned', 2001,
20012, 201, $operation(2001)),
$ipRow(2202, 22, 6, '2404:c140:156:3c::1001', 'assigned', 2002,
20022, 202, $operation(2002)),
]);
DB::table('cloud_resource_reservations')->insert(array_map(
static function (array $row) use ($operation, $now): array {
[$id, $userId, $instanceId, $resourcePool] = $row;
return [
'id' => $id,
'reservation_key' => str_pad((string) $id, 32, '0'),
'operation_id' => $operation($id),
'order_id' => $id,
'backup_restore_id' => null,
'offer_id' => 1,
'resource_pool_id' => $resourcePool,
'host_id' => 1,
'user_id' => $userId,
'instance_id' => $instanceId,
'network_attachment_id' => 999,
'public_ip_pool_id' => 999,
'public_ip_id' => 999,
'cpu_cores' => 1,
'memory_mib' => 1024,
'disk_gib' => 20,
'state' => 'committed',
'expires_at' => null,
'released_at' => null,
'release_reason' => '',
'created_at' => $now,
'updated_at' => $now,
];
},
[
[1001, 501, 101, 1],
[1002, 502, 102, 1],
[2001, 501, 201, 1],
[2002, 502, 202, 1],
]
));
$claim = static function (int $id, int $reservationId, int $family,
int $poolId, int $ipId
) use ($operation, $now): array {
return [
'id' => $id,
'reservation_id' => $reservationId,
'address_family' => $family,
'claim_generation' => 1,
'active_claim' => 1,
'network_attachment_id' => 11,
'public_ip_pool_id' => $poolId,
'public_ip_id' => $ipId,
'operation_id' => $operation($reservationId),
'state' => 'committed',
'released_at' => null,
'release_reason' => '',
'created_at' => $now,
'updated_at' => $now,
];
};
DB::table('cloud_resource_reservation_ips')->insert([
$claim(10011, 1001, 4, 11, 1101),
$claim(10012, 1001, 6, 12, 1201),
$claim(10021, 1002, 4, 11, 1102),
$claim(10022, 1002, 6, 12, 1202),
$claim(20011, 2001, 4, 21, 2101),
$claim(20012, 2001, 6, 22, 2201),
$claim(20021, 2002, 4, 21, 2102),
$claim(20022, 2002, 6, 22, 2202),
]);
$publicNetwork = static function (int $instanceId): array {
$v4 = $instanceId < 200
? ['pool' => str_repeat('a', 32), 'cidr' => '8.8.8.0/29',
'address' => $instanceId === 101
? '8.8.8.2/29' : '8.8.8.3/29',
'gateway' => '8.8.8.1']
: ['pool' => str_repeat('2', 32), 'cidr' => '9.9.9.8/29',
'address' => $instanceId === 201
? '9.9.9.10/29' : '9.9.9.11/29',
'gateway' => '9.9.9.9'];
$v6 = $instanceId < 200
? ['pool' => str_repeat('b', 32),
'cidr' => '2404:c140:156:3a::/64',
'address' => $instanceId === 101
? '2404:c140:156:3a::1000/64'
: '2404:c140:156:3a::1001/64',
'gateway' => '2404:c140:156:3a::1']
: ['pool' => str_repeat('3', 32),
'cidr' => '2404:c140:156:3c::/64',
'address' => $instanceId === 201
? '2404:c140:156:3c::1000/64'
: '2404:c140:156:3c::1001/64',
'gateway' => '2404:c140:156:3c::1'];
return [
'contract' => 1,
'network_selector' => 'bridge:br0',
'ipv4' => [
'pool_key' => $v4['pool'],
'pool_cidr' => $v4['cidr'],
'address_cidr' => $v4['address'],
'gateway' => $v4['gateway'],
'gateway_on_link' => false,
],
'ipv6' => [
'pool_key' => $v6['pool'],
'pool_cidr' => $v6['cidr'],
'address_cidr' => $v6['address'],
'gateway' => $v6['gateway'],
'gateway_on_link' => false,
],
'dns_servers' => ['1.1.1.1'],
];
};
foreach ([101, 102, 201, 202] as $instanceId) {
$reservationId = $instanceId < 200
? 900 + $instanceId : 1800 + $instanceId;
DB::table('cloud_instances')->insert([
'id' => $instanceId,
'host_id' => 1,
'user_id' => $instanceId % 2 === 1 ? 501 : 502,
'order_id' => $reservationId,
'plan_id' => 1,
'offer_id' => 1,
'resource_pool_id' => 1,
'reservation_id' => $reservationId,
'uuid' => sprintf('00000000-0000-4000-8000-%012d', $instanceId),
'name' => 'vm-' . $instanceId,
'status' => 'active',
'service_status' => 'active',
'network_status' => 'active',
'expires_at' => $now + 86400,
'spec_json' => json_encode([
'managed' => true,
'os_source' => 'cloud_template',
'public_network' => $publicNetwork($instanceId),
], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES),
'created_at' => $now,
'updated_at' => $now,
]);
}
$admin = [
'user_id' => 99,
'is_admin' => true,
'is_super_admin' => true,
'can_manage' => true,
];
$readAdmin = [
'user_id' => 99,
'is_admin' => true,
'is_super_admin' => true,
'can_manage' => false,
];
$input = static function (string $targetKey, int $sourceRevision,
int $targetRevision, string $requestKey): array {
return [
'target_pool_key' => $targetKey,
'expected_source_revision' => $sourceRevision,
'expected_target_revision' => $targetRevision,
'confirm_replacement' => true,
'request_key' => $requestKey,
];
};
$beforeAuthorizationReject = $snapshot();
$rejects(static fn () =>
CloudPublicIpPoolReplacementBatchService::start(
1,
$poolKey('a'),
$readAdmin,
$input(
$poolKey('c'),
11,
13,
'replacement-read-only-admin-0001'
)
), 'Read-only administrator started a pool replacement.', [403]);
$assert($snapshot() === $beforeAuthorizationReject,
'Read-only administrator rejection left durable writes.');
foreach (['false', 'missing'] as $confirmationShape) {
$unconfirmed = $input(
$poolKey('c'),
11,
13,
'replacement-unconfirmed-' . $confirmationShape
);
if ($confirmationShape === 'false') {
$unconfirmed['confirm_replacement'] = false;
} else {
unset($unconfirmed['confirm_replacement']);
}
$beforeConfirmationReject = $snapshot();
$rejects(static fn () =>
CloudPublicIpPoolReplacementBatchService::start(
1,
$poolKey('a'),
$admin,
$unconfirmed
), 'Unconfirmed pool replacement was accepted: '
. $confirmationShape);
$assert($snapshot() === $beforeConfirmationReject,
'Confirmation rejection left durable writes: '
. $confirmationShape);
}
foreach ([10 => 'behind', 12 => 'ahead'] as $observed => $label) {
DB::table('cloud_public_ip_pools')->where('id', 11)->update([
'observed_revision' => $observed,
]);
$driftOverview = CloudPublicIpPoolReplacementBatchService::overview(
1,
$poolKey('a'),
$readAdmin
);
$driftTargets = array_column(
$driftOverview['target_options'],
null,
'pool_key'
);
$assert(($driftOverview['can_start'] ?? null) === false
&& ($driftOverview['source_pool']['expected_revision'] ?? null)
=== 11
&& ($driftTargets[$poolKey('c')]['eligible'] ?? null) === false,
'Source pool revision ' . $label . ' drift was advertised.');
$beforeRevisionReject = $snapshot();
$rejects(static fn () =>
CloudPublicIpPoolReplacementBatchService::start(
1,
$poolKey('a'),
$admin,
$input(
$poolKey('c'),
11,
13,
'replacement-source-revision-' . $label
)
), 'Source pool revision ' . $label . ' drift was accepted.');
$assert($snapshot() === $beforeRevisionReject,
'Source revision rejection left durable writes: ' . $label);
}
DB::table('cloud_public_ip_pools')->where('id', 11)->update([
'observed_revision' => 11,
]);
foreach ([12 => 'behind', 14 => 'ahead'] as $observed => $label) {
DB::table('cloud_public_ip_pools')->where('id', 13)->update([
'observed_revision' => $observed,
]);
$driftOverview = CloudPublicIpPoolReplacementBatchService::overview(
1,
$poolKey('a'),
$readAdmin
);
$driftTargets = array_column(
$driftOverview['target_options'],
null,
'pool_key'
);
$assert(!isset($driftTargets[$poolKey('c')]),
'Target pool revision ' . $label . ' drift was advertised.');
$beforeRevisionReject = $snapshot();
$rejects(static fn () =>
CloudPublicIpPoolReplacementBatchService::start(
1,
$poolKey('a'),
$admin,
$input(
$poolKey('c'),
11,
13,
'replacement-target-revision-' . $label
)
), 'Target pool revision ' . $label . ' drift was accepted.');
$assert($snapshot() === $beforeRevisionReject,
'Target revision rejection left durable writes: ' . $label);
}
DB::table('cloud_public_ip_pools')->where('id', 13)->update([
'observed_revision' => 13,
]);
DB::table('cloud_host_agents')->where('host_id', 1)->update([
'public_network_capabilities_json' => '{}',
]);
$capabilityOverview = CloudPublicIpPoolReplacementBatchService::overview(
1,
$poolKey('a'),
$readAdmin
);
$assert(($capabilityOverview['can_start'] ?? null) === false
&& ($capabilityOverview['target_options'] ?? []) === [],
'Missing Agent public-network capability was advertised.');
$beforeCapabilityReject = $snapshot();
$rejects(static fn () =>
CloudPublicIpPoolReplacementBatchService::start(
1,
$poolKey('a'),
$admin,
$input(
$poolKey('c'),
11,
13,
'replacement-agent-capability-0001'
)
), 'Missing Agent public-network capability was accepted.');
$assert($snapshot() === $beforeCapabilityReject,
'Agent-capability rejection left durable writes.');
DB::table('cloud_host_agents')->where('host_id', 1)->update([
'public_network_capabilities_json' => $capabilities,
]);
foreach ([
'migration' => static function (): void {
DB::table('cloud_instance_migrations')->insert([
'id' => 1,
'instance_id' => 101,
'state' => 'preparing_source',
]);
},
'backup-recovery' => static function (): void {
DB::table('cloud_backup_recoveries')->insert([
'id' => 1,
'instance_id' => 101,
'state' => 'recovering',
]);
},
] as $label => $arrange) {
$arrange();
$blockedOverview =
CloudPublicIpPoolReplacementBatchService::overview(
1,
$poolKey('a'),
$readAdmin
);
$assert(($blockedOverview['can_start'] ?? null) === false,
'Batch overview ignored active ' . $label . '.');
$beforeAdmissionReject = $snapshot();
$rejects(static fn () =>
CloudPublicIpPoolReplacementBatchService::start(
1,
$poolKey('a'),
$admin,
$input(
$poolKey('c'),
11,
13,
'replacement-admission-' . $label
)
), 'Batch start ignored active ' . $label . '.');
$assert($snapshot() === $beforeAdmissionReject,
'Admission rejection left durable writes: ' . $label);
DB::table('cloud_instance_migrations')->delete();
DB::table('cloud_backup_recoveries')->delete();
}
$blockingJob = new CloudJob();
$blockingJob->instance_id = 101;
$blockingJob->user_id = 501;
$blockingJob->host_id = 1;
$blockingJob->kind = 'action';
$blockingJob->execution_mode = 'agent';
$blockingJob->sealed_payload = '{}';
$blockingJob->available_at = $now;
$blockingJob->created_at = $blockingJob->updated_at = $now;
$blockingJob->save();
$jobBlockedOverview = CloudPublicIpPoolReplacementBatchService::overview(
1,
$poolKey('a'),
$readAdmin
);
$assert(($jobBlockedOverview['can_start'] ?? null) === false,
'Batch overview ignored a blocking instance job.');
$beforeJobReject = $snapshot();
$rejects(static fn () =>
CloudPublicIpPoolReplacementBatchService::start(
1,
$poolKey('a'),
$admin,
$input(
$poolKey('c'),
11,
13,
'replacement-admission-job-0001'
)
), 'Batch start ignored a blocking instance job.');
$assert($snapshot() === $beforeJobReject,
'Blocking-job rejection left durable writes.');
$blockingJob->delete();
$frozenNetwork = json_encode(
$publicNetwork(101),
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
);
DB::table('cloud_public_network_transitions')->insert([
'transition_key' => str_repeat('8', 32),
'operation_id' => str_repeat('9', 32),
'kind' => 'admin_exact',
'instance_id' => 101,
'host_id' => 1,
'reservation_id' => 1001,
'invoice_id' => null,
'pool_replacement_batch_item_id' => null,
'requested_by_user_id' => 99,
'source_network_attachment_id' => 11,
'target_network_attachment_id' => 11,
'source_claim_set_sha256' => str_repeat('a', 64),
'target_claim_set_sha256' => str_repeat('b', 64),
'source_public_network_json' => $frozenNetwork,
'source_public_network_sha256' => hash('sha256', $frozenNetwork),
'target_public_network_json' => $frozenNetwork,
'target_public_network_sha256' => hash('sha256', $frozenNetwork),
'state' => 'queued',
'effect_resolution' => 'not_applicable',
'active_transition' => 1,
'last_error_code' => '',
'effect_resolved_at' => null,
'terminal_at' => null,
'created_at' => $now,
'updated_at' => $now,
]);
$transitionBlockedOverview =
CloudPublicIpPoolReplacementBatchService::overview(
1,
$poolKey('a'),
$readAdmin
);
$assert(($transitionBlockedOverview['can_start'] ?? null) === false,
'Batch overview ignored an active public-network transition.');
$beforeTransitionReject = $snapshot();
$rejects(static fn () =>
CloudPublicIpPoolReplacementBatchService::start(
1,
$poolKey('a'),
$admin,
$input(
$poolKey('c'),
11,
13,
'replacement-admission-transition-0001'
)
), 'Batch start ignored an active public-network transition.');
$assert($snapshot() === $beforeTransitionReject,
'Active-transition rejection left durable writes.');
DB::table('cloud_public_network_transitions')->delete();
$v4Overview = CloudPublicIpPoolReplacementBatchService::overview(
1,
$poolKey('a'),
$readAdmin
);
$v4Targets = array_column($v4Overview['target_options'], null, 'pool_key');
$assert($v4Overview['source_pool']['assigned_count'] === 2
&& $v4Targets[$poolKey('c')]['available_count'] === 2
&& $v4Targets[$poolKey('c')]['eligible'] === true
&& !isset($v4Targets[$poolKey('d')])
&& !isset($v4Targets[$poolKey('e')])
&& !isset($v4Targets[$poolKey('f')]),
'Overview accepted a cross-family/attachment/line target: '
. json_encode($v4Overview, JSON_UNESCAPED_SLASHES));
$v6Overview = CloudPublicIpPoolReplacementBatchService::overview(
1,
$poolKey('3'),
$readAdmin
);
$v6Targets = array_column($v6Overview['target_options'], null, 'pool_key');
$assert($v6Targets[$poolKey('4')]['available_count'] === 4
&& $v6Targets[$poolKey('4')]['eligible'] === true,
'Sparse IPv6 overview used materialized row count instead of capacity.');
foreach ([
[$poolKey('d'), 14, 'cross-family'],
[$poolKey('e'), 15, 'cross-attachment'],
[$poolKey('f'), 16, 'cross-line'],
[$poolKey('1'), 17, 'cross-host'],
] as [$targetKey, $revision, $label]) {
$before = $snapshot();
$rejects(static fn () =>
CloudPublicIpPoolReplacementBatchService::start(
1,
$poolKey('a'),
$admin,
$input($targetKey, 11, $revision,
'replacement-reject-' . $label)
), 'Replacement accepted ' . $label . ' target.');
$assert($snapshot() === $before,
'Rejected ' . $label . ' target wrote durable state.');
}
DB::table('cloud_public_ips')->where('id', 1302)->update([
'state' => 'reserved',
'operation_id' => str_repeat('9', 32),
]);
$beforeShortage = $snapshot();
$rejects(static fn () => CloudPublicIpPoolReplacementBatchService::start(
1,
$poolKey('a'),
$admin,
$input($poolKey('c'), 11, 13, 'replacement-v4-shortage-0001')
), 'IPv4 target shortage was accepted.');
$assert($snapshot() === $beforeShortage,
'IPv4 target shortage left partial batch writes.');
DB::table('cloud_public_ips')->where('id', 1302)->update([
'state' => 'available',
'operation_id' => null,
]);
DB::statement(<<<'SQL'
CREATE TRIGGER fail_second_pool_replacement_child
BEFORE INSERT ON cloud_jobs
WHEN NEW.kind = 'network_change' AND NEW.instance_id = 102
BEGIN
SELECT RAISE(ABORT, 'injected second child failure');
END
SQL);
$beforeFault = $snapshot();
try {
CloudPublicIpPoolReplacementBatchService::start(
1,
$poolKey('a'),
$admin,
$input($poolKey('c'), 11, 13,
'replacement-v4-second-fault')
);
throw new RuntimeException('Second-child fault was not raised.');
} catch (Throwable $error) {
$assert(str_contains($error->getMessage(), 'second child failure'),
'Unexpected error replaced the injected second-child fault: '
. $error->getMessage());
}
$assert($snapshot() === $beforeFault,
'Second-child fault did not roll back the entire batch transaction.');
DB::statement('DROP TRIGGER fail_second_pool_replacement_child');
$companionBefore = DB::table('cloud_resource_reservation_ips')
->whereIn('id', [10012, 10022])->orderBy('id')->get()
->map(static fn ($row): array => (array) $row)->all();
$companionIpsBefore = DB::table('cloud_public_ips')
->whereIn('id', [1201, 1202])->orderBy('id')->get()
->map(static fn ($row): array => (array) $row)->all();
$v4Input = $input(
$poolKey('c'),
11,
13,
'replacement-v4-success-0001'
);
$started = CloudPublicIpPoolReplacementBatchService::start(
1,
$poolKey('a'),
$admin,
$v4Input
);
$parentId = (int) $started['job']->id;
$batchId = (int) CloudPublicIpPoolReplacementBatch::where(
'parent_job_id',
$parentId
)->value('id');
$items = DB::table('cloud_public_ip_pool_replacement_batch_items')
->where('batch_id', $batchId)->orderBy('instance_id')->get();
$projectChild = static function (int $childId): void {
$hint = CloudJob::find($childId);
if (!$hint instanceof CloudJob) {
throw new RuntimeException('Projection child fixture is missing.');
}
DB::connection()->transaction(static function () use (
$hint,
$childId
): void {
$parent = CloudJob::where(
'id',
(int) $hint->parent_job_id
)->lockForUpdate()->first();
$child = CloudJob::where('id', $childId)
->lockForUpdate()->first();
$transition = CloudPublicNetworkTransition::where(
'id',
(int) $child->public_network_transition_id
)->lockForUpdate()->first();
CloudPublicIpPoolReplacementBatchService::
projectTransitionEvidenceLocked(
$parent,
$child,
$transition
);
});
};
$assert($started['unchanged'] === false && $items->count() === 2
&& $items->every(static fn ($item): bool =>
(int) $item->address_family === 4
&& (string) $item->state === 'queued'
&& $item->child_job_id !== null),
'IPv4 replacement did not queue two production-shaped items.');
foreach ($items as $item) {
$child = DB::table('cloud_jobs')->where(
'id',
(int) $item->child_job_id
)->first();
$transition = DB::table('cloud_public_network_transitions')->where(
'id',
(int) $child->public_network_transition_id
)->first();
$legs = DB::table('cloud_public_network_transition_ips')->where(
'transition_id',
(int) $transition->id
)->orderBy('address_family')->get();
$assert((int) $child->parent_job_id === $parentId
&& (int) $child->instance_id === (int) $item->instance_id
&& (int) $transition->pool_replacement_batch_item_id
=== (int) $item->id
&& (int) $transition->instance_id === (int) $item->instance_id
&& $legs->count() === 2
&& (int) $legs[0]->address_family === 4
&& (int) $legs[0]->changed === 1
&& $legs[0]->target_claim_id !== null
&& (int) $legs[1]->address_family === 6
&& (int) $legs[1]->changed === 0
&& $legs[1]->target_claim_id === null,
'Item/transition/child dual-stack topology is incomplete.');
}
$assert(DB::table('cloud_resource_reservation_ips')
->whereIn('id', [10012, 10022])->orderBy('id')->get()
->map(static fn ($row): array => (array) $row)->all()
=== $companionBefore
&& DB::table('cloud_public_ips')->whereIn('id', [1201, 1202])
->orderBy('id')->get()
->map(static fn ($row): array => (array) $row)->all()
=== $companionIpsBefore,
'IPv4 replacement mutated its IPv6 companion family.');
$activeSnapshot = $snapshot();
$replay = CloudPublicIpPoolReplacementBatchService::start(
1,
$poolKey('a'),
$admin,
$v4Input
);
$assert($replay['unchanged'] === true
&& (int) $replay['job']->id === $parentId
&& $snapshot() === $activeSnapshot,
'Exact active replay duplicated or changed durable topology.');
$firstChildId = (int) $items->first()->child_job_id;
DB::table('cloud_jobs')->where('id', $firstChildId)->update([
'parent_job_id' => null,
]);
$rejects(static fn () => CloudPublicIpPoolReplacementBatchService::start(
1,
$poolKey('a'),
$admin,
$v4Input
), 'Active replay accepted a corrupted child-parent topology.');
DB::table('cloud_jobs')->where('id', $firstChildId)->update([
'parent_job_id' => $parentId,
]);
DB::table('cloud_resource_reservation_ips')->where('id', 10012)->update([
'state' => 'uncertain',
]);
$rejects(static fn () => CloudPublicIpPoolReplacementBatchService::start(
1,
$poolKey('a'),
$admin,
$v4Input
), 'Active replay accepted a drifted companion claim.');
DB::table('cloud_resource_reservation_ips')->where('id', 10012)->update([
'state' => 'committed',
]);
// Finishing refuses a child whose frozen topology has drifted. The
// poisoned scalar reservation mirrors remain irrelevant throughout.
DB::table('cloud_jobs')->where('id', $firstChildId)->update([
'host_id' => 2,
]);
$rejects(static fn () => $projectChild($firstChildId),
'Projection accepted a child with a drifted frozen host.');
DB::table('cloud_jobs')->where('id', $firstChildId)->update([
'host_id' => 1,
]);
$firstTransitionId = (int) DB::table('cloud_jobs')
->where('id', $firstChildId)
->value('public_network_transition_id');
foreach (['reserved', 'queued', 'awaiting_receipt'] as $activeState) {
DB::table('cloud_public_network_transitions')
->where('id', $firstTransitionId)->update([
'state' => $activeState,
'effect_resolution' => 'not_applicable',
'active_transition' => 1,
'effect_resolved_at' => null,
'terminal_at' => null,
'last_error_code' => '',
]);
$projectChild($firstChildId);
$assert(DB::table('cloud_public_ip_pool_replacement_batch_items')
->where('child_job_id', $firstChildId)
->value('state') === 'queued'
&& (string) CloudPublicIpPoolReplacementBatch::find($batchId)->state
=== 'running',
'Active transition state ' . $activeState
. ' prematurely terminated its batch item.');
}
DB::table('cloud_public_network_transitions')
->where('id', $firstTransitionId)->update([
'kind' => 'admin_manual',
]);
$corruptKindSnapshot = $snapshot();
$rejects(static fn () => $projectChild($firstChildId),
'Projection silently ignored a corrupted replacement kind.');
$assert($snapshot() === $corruptKindSnapshot,
'Corrupted replacement kind caused partial projection writes.');
DB::table('cloud_public_network_transitions')
->where('id', $firstTransitionId)->update([
'kind' => 'pool_replacement',
]);
DB::table('cloud_public_network_transitions')
->where('id', $firstTransitionId)->update([
'state' => 'succeeded',
'effect_resolution' => 'applied',
'active_transition' => 1,
'effect_resolved_at' => null,
'terminal_at' => null,
]);
$invalidShapeSnapshot = $snapshot();
$rejects(static fn () => $projectChild($firstChildId),
'Projection accepted an invalid pseudo-terminal transition shape.');
$assert($snapshot() === $invalidShapeSnapshot,
'Invalid transition shape caused partial batch writes.');
$resolvedAt = time();
DB::table('cloud_public_network_transitions')
->where('id', $firstTransitionId)->update([
'state' => 'finalization_pending',
'effect_resolution' => 'applied',
'active_transition' => 1,
'effect_resolved_at' => $resolvedAt,
'terminal_at' => null,
'last_error_code' => '',
]);
DB::table('cloud_jobs')->where('id', $firstChildId)->update([
'status' => 'failed',
'error_code' => 'catalog_poison',
]);
$projectChild($firstChildId);
$assert(DB::table('cloud_public_ip_pool_replacement_batch_items')
->where('child_job_id', $firstChildId)->value('state') === 'queued'
&& (string) CloudPublicIpPoolReplacementBatch::find($batchId)->state
=== 'running',
'Finalization-pending evidence prematurely terminated a batch item.');
DB::table('cloud_public_network_transitions')
->where('id', $firstTransitionId)->update([
'state' => 'effect_unknown',
'effect_resolution' => 'unknown',
'active_transition' => 1,
'effect_resolved_at' => null,
'terminal_at' => null,
'last_error_code' => 'durable_effect_unknown',
]);
$unknownIdentity = [
'job_count' => DB::table('cloud_jobs')->count(),
'transition_count' => DB::table(
'cloud_public_network_transitions'
)->count(),
'operation_id' => (string) DB::table('cloud_jobs')
->where('id', $firstChildId)->value('operation_id'),
];
$projectChild($firstChildId);
$assert((string) CloudPublicIpPoolReplacementBatch::find($batchId)->state
=== 'blocked'
&& (int) CloudPublicIpPoolReplacementBatch::find($batchId)->active_claim
=== 1
&& DB::table('cloud_public_ip_pool_replacement_batch_items')
->where('child_job_id', $firstChildId)->value('state') === 'unknown'
&& DB::table('cloud_public_ip_pool_replacement_batch_items')
->where('child_job_id', $firstChildId)
->value('last_error_code') === 'durable_effect_unknown'
&& DB::table('cloud_jobs')->count() === $unknownIdentity['job_count']
&& DB::table('cloud_public_network_transitions')->count()
=== $unknownIdentity['transition_count']
&& (string) DB::table('cloud_jobs')->where('id', $firstChildId)
->value('operation_id') === $unknownIdentity['operation_id']
&& (string) CloudJob::find($parentId)->phase !== 'terminal',
'Durable unknown evidence did not retain batch and parent fences.');
$applyChild = static function (int $childId): CloudJob {
$child = CloudJob::find($childId);
$transition = DB::table('cloud_public_network_transitions')->where(
'id',
(int) $child->public_network_transition_id
)->first();
$changed = DB::table('cloud_public_network_transition_ips')
->where('transition_id', (int) $transition->id)
->where('changed', 1)->first();
$sourceClaim = DB::table('cloud_resource_reservation_ips')->where(
'id',
(int) $changed->source_claim_id
)->first();
$targetClaim = DB::table('cloud_resource_reservation_ips')->where(
'id',
(int) $changed->target_claim_id
)->first();
$sourceIpId = (int) $sourceClaim->public_ip_id;
$targetIpId = (int) $targetClaim->public_ip_id;
$finishedAt = time();
DB::table('cloud_resource_reservation_ips')->where(
'id',
(int) $sourceClaim->id
)->update([
'active_claim' => null,
'state' => 'released',
'released_at' => $finishedAt,
'release_reason' => 'network_change_succeeded',
'updated_at' => $finishedAt,
]);
DB::table('cloud_public_ips')->where('id', $sourceIpId)->update([
'state' => 'available',
'reservation_id' => null,
'reservation_ip_claim_id' => null,
'instance_id' => null,
'operation_id' => null,
'updated_at' => $finishedAt,
]);
DB::table('cloud_resource_reservation_ips')->where(
'id',
(int) $targetClaim->id
)->update([
'active_claim' => 1,
'state' => 'committed',
'updated_at' => $finishedAt,
]);
DB::table('cloud_public_ips')->where('id', $targetIpId)->update([
'state' => 'assigned',
'reservation_id' => (int) $targetClaim->reservation_id,
'reservation_ip_claim_id' => (int) $targetClaim->id,
'instance_id' => (int) $child->instance_id,
'operation_id' => (string) $targetClaim->operation_id,
'updated_at' => $finishedAt,
]);
DB::table('cloud_public_network_transitions')->where(
'id',
(int) $transition->id
)->update([
'state' => 'succeeded',
'effect_resolution' => 'applied',
'active_transition' => null,
'effect_resolved_at' => $finishedAt,
'terminal_at' => $finishedAt,
'updated_at' => $finishedAt,
]);
$targetNetwork = json_decode(
(string) $transition->target_public_network_json,
true,
64,
JSON_THROW_ON_ERROR
);
$spec = json_decode(
(string) DB::table('cloud_instances')->where(
'id',
(int) $child->instance_id
)->value('spec_json'),
true,
64,
JSON_THROW_ON_ERROR
);
$spec['public_network'] = $targetNetwork;
DB::table('cloud_instances')->where(
'id',
(int) $child->instance_id
)->update([
'spec_json' => json_encode(
$spec,
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
),
'updated_at' => $finishedAt,
]);
DB::table('cloud_jobs')->where('id', $childId)->update([
'phase' => 'terminal',
'outcome' => 'succeeded',
'status' => 'succeeded',
'terminal_at' => $finishedAt,
'effect_may_have_applied' => 0,
'safe_to_resubmit' => 0,
'effect_resolution' => 'applied',
'effect_resolved_at' => $finishedAt,
'error_code' => null,
'updated_at' => $finishedAt,
]);
return CloudJob::find($childId);
};
$applyChild($firstChildId);
$projectChild($firstChildId);
$assert(DB::table('cloud_public_ip_pool_replacement_batch_items')
->where('child_job_id', $firstChildId)
->value('state') === 'succeeded'
&& (string) CloudPublicIpPoolReplacementBatch::find($batchId)->state
=== 'running'
&& (string) CloudJob::find($parentId)->phase !== 'terminal',
'One resolved child did not release blocked state in one hook.');
DB::table('cloud_public_network_transitions')
->where('id', $firstTransitionId)->update([
'state' => 'failed',
'effect_resolution' => 'not_applied',
]);
$terminalConflictSnapshot = $snapshot();
$rejects(static fn () => $projectChild($firstChildId),
'Projection rewrote a terminal item while its sibling was pending.');
$assert($snapshot() === $terminalConflictSnapshot,
'Conflicting terminal item evidence caused partial writes.');
DB::table('cloud_public_network_transitions')
->where('id', $firstTransitionId)->update([
'state' => 'succeeded',
'effect_resolution' => 'applied',
]);
$secondChildId = (int) $items->last()->child_job_id;
$applyChild($secondChildId);
$projectChild($secondChildId);
$terminalBatch = CloudPublicIpPoolReplacementBatch::find($batchId);
$terminalParent = CloudJob::find($parentId);
$assert((string) $terminalBatch->state === 'succeeded'
&& $terminalBatch->active_claim === null
&& (string) $terminalParent->phase === 'terminal'
&& (string) $terminalParent->outcome === 'succeeded'
&& DB::table('cloud_public_ip_pools')->where('id', 11)
->value('state') === 'disabled'
&& DB::table('cloud_public_ips')->where('public_ip_pool_id', 11)
->where(function ($query): void {
$query->where('state', '<>', 'available')
->orWhereNotNull('reservation_id')
->orWhereNotNull('reservation_ip_claim_id')
->orWhereNotNull('instance_id')
->orWhereNotNull('operation_id');
})->count() === 0,
'All-success children did not converge parent/batch/source in one pass.');
$terminalProjectionSnapshot = $snapshot();
$projectChild($secondChildId);
$assert($snapshot() === $terminalProjectionSnapshot,
'Repeated terminal transition projection was not idempotent.');
// A strict terminal topology remains replayable after the instance has
// legitimately acquired a later companion-family generation.
$terminalAt = time() + 10;
DB::table('cloud_resource_reservation_ips')->where('id', 10012)->update([
'active_claim' => null,
'state' => 'released',
'released_at' => $terminalAt,
'release_reason' => 'later_manual_change',
'updated_at' => $terminalAt,
]);
DB::table('cloud_public_ips')->where('id', 1201)->update([
'state' => 'available',
'reservation_id' => null,
'reservation_ip_claim_id' => null,
'instance_id' => null,
'operation_id' => null,
'updated_at' => $terminalAt,
]);
$newCompanionOperation = str_repeat('8', 32);
DB::table('cloud_public_ips')->insert([
'id' => 1203,
'host_id' => 1,
'public_ip_pool_id' => 12,
'address_family' => 6,
'address' => '2404:c140:156:3a::1002',
'address_binary' => CloudIpAddress::parse(
'2404:c140:156:3a::1002'
)->binary(),
'prefix_length' => 64,
'state' => 'assigned',
'reservation_id' => 1001,
'reservation_ip_claim_id' => 10013,
'instance_id' => 101,
'operation_id' => $newCompanionOperation,
'created_at' => $terminalAt,
'updated_at' => $terminalAt,
]);
DB::table('cloud_resource_reservation_ips')->insert([
'id' => 10013,
'reservation_id' => 1001,
'address_family' => 6,
'claim_generation' => 2,
'active_claim' => 1,
'network_attachment_id' => 11,
'public_ip_pool_id' => 12,
'public_ip_id' => 1203,
'operation_id' => $newCompanionOperation,
'state' => 'committed',
'released_at' => null,
'release_reason' => '',
'created_at' => $terminalAt,
'updated_at' => $terminalAt,
]);
$instanceSpec = json_decode(
(string) DB::table('cloud_instances')->where('id', 101)
->value('spec_json'),
true,
64,
JSON_THROW_ON_ERROR
);
$instanceSpec['public_network']['ipv6']['address_cidr'] =
'2404:c140:156:3a::1002/64';
DB::table('cloud_instances')->where('id', 101)->update([
'spec_json' => json_encode(
$instanceSpec,
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
),
'updated_at' => $terminalAt,
]);
$terminalSnapshot = $snapshot();
$terminalReplay = CloudPublicIpPoolReplacementBatchService::start(
1,
$poolKey('a'),
$admin,
$v4Input
);
$assert($terminalReplay['unchanged'] === true
&& (int) $terminalReplay['job']->id === $parentId
&& $snapshot() === $terminalSnapshot,
'Strict terminal replay rejected a later companion generation.');
// Sparse IPv6 shortage is a zero-write rejection even though the target
// catalog has no pre-materialized rows.
DB::table('cloud_public_ip_pools')->where('id', 23)->update([
'max_allocations' => 1,
]);
$beforeV6Shortage = $snapshot();
$rejects(static fn () => CloudPublicIpPoolReplacementBatchService::start(
1,
$poolKey('3'),
$admin,
$input($poolKey('4'), 22, 23, 'replacement-v6-shortage-0001')
), 'Sparse IPv6 capacity shortage was accepted.');
$assert($snapshot() === $beforeV6Shortage,
'Sparse IPv6 shortage materialized rows or wrote batch state.');
DB::table('cloud_public_ip_pools')->where('id', 23)->update([
'max_allocations' => 4,
]);
$v4CompanionBefore = DB::table('cloud_resource_reservation_ips')
->whereIn('id', [20011, 20021])->orderBy('id')->get()
->map(static fn ($row): array => (array) $row)->all();
$v4CompanionIpsBefore = DB::table('cloud_public_ips')
->whereIn('id', [2101, 2102])->orderBy('id')->get()
->map(static fn ($row): array => (array) $row)->all();
$v6Input = $input(
$poolKey('4'),
22,
23,
'replacement-v6-success-0001'
);
$v6Started = CloudPublicIpPoolReplacementBatchService::start(
1,
$poolKey('3'),
$admin,
$v6Input
);
$v6ParentId = (int) $v6Started['job']->id;
$v6BatchId = (int) CloudPublicIpPoolReplacementBatch::where(
'parent_job_id',
$v6ParentId
)->value('id');
$v6Items = DB::table('cloud_public_ip_pool_replacement_batch_items')
->where('batch_id', $v6BatchId)->orderBy('instance_id')->get();
$targetV6Ids = $v6Items->pluck('target_public_ip_id')->all();
$targetV6 = DB::table('cloud_public_ips')->whereIn('id', $targetV6Ids)
->orderBy('id')->get();
$assert($v6Items->count() === 2
&& $v6Items->every(static fn ($item): bool =>
(int) $item->address_family === 6
&& (string) $item->state === 'queued'
&& $item->child_job_id !== null)
&& count(array_unique($targetV6Ids)) === 2
&& $targetV6->pluck('address')->all() === [
'2404:c140:156:3d::1000',
'2404:c140:156:3d::1001',
]
&& DB::table('cloud_public_ip_pools')->where('id', 23)
->value('allocation_cursor') === '2404:c140:156:3d::1002',
'Sparse IPv6 replacement did not materialize bounded unique targets.');
$assert(DB::table('cloud_resource_reservation_ips')
->whereIn('id', [20011, 20021])->orderBy('id')->get()
->map(static fn ($row): array => (array) $row)->all()
=== $v4CompanionBefore
&& DB::table('cloud_public_ips')->whereIn('id', [2101, 2102])
->orderBy('id')->get()
->map(static fn ($row): array => (array) $row)->all()
=== $v4CompanionIpsBefore,
'IPv6 replacement mutated its IPv4 companion family.');
foreach ($v6Items as $item) {
$child = DB::table('cloud_jobs')->where(
'id',
(int) $item->child_job_id
)->first();
$transition = DB::table('cloud_public_network_transitions')->where(
'id',
(int) $child->public_network_transition_id
)->first();
$legs = DB::table('cloud_public_network_transition_ips')->where(
'transition_id',
(int) $transition->id
)->orderBy('address_family')->get();
$assert((int) $child->parent_job_id === $v6ParentId
&& (int) $transition->pool_replacement_batch_item_id
=== (int) $item->id
&& $legs->count() === 2
&& (int) $legs[0]->address_family === 4
&& (int) $legs[0]->changed === 0
&& $legs[0]->target_claim_id === null
&& (int) $legs[1]->address_family === 6
&& (int) $legs[1]->changed === 1
&& $legs[1]->target_claim_id !== null,
'Sparse IPv6 child did not freeze the companion IPv4 leg.');
}
$failedV6ChildId = (int) $v6Items->first()->child_job_id;
$failedV6TransitionId = (int) DB::table('cloud_jobs')
->where('id', $failedV6ChildId)
->value('public_network_transition_id');
$failedV6Leg = DB::table('cloud_public_network_transition_ips')
->where('transition_id', $failedV6TransitionId)
->where('changed', 1)->first();
$failedV6TargetClaim = DB::table('cloud_resource_reservation_ips')
->where('id', (int) $failedV6Leg->target_claim_id)->first();
$failedAt = time() + 20;
DB::table('cloud_resource_reservation_ips')
->where('id', (int) $failedV6TargetClaim->id)->update([
'active_claim' => null,
'state' => 'released',
'released_at' => $failedAt,
'release_reason' => 'public_network_transition_not_applied',
'updated_at' => $failedAt,
]);
DB::table('cloud_public_ips')
->where('id', (int) $failedV6TargetClaim->public_ip_id)->update([
'state' => 'available',
'reservation_id' => null,
'reservation_ip_claim_id' => null,
'instance_id' => null,
'operation_id' => null,
'updated_at' => $failedAt,
]);
DB::table('cloud_public_network_transitions')
->where('id', $failedV6TransitionId)->update([
'state' => 'failed',
'effect_resolution' => 'not_applied',
'active_transition' => null,
'effect_resolved_at' => $failedAt,
'terminal_at' => $failedAt,
'last_error_code' => 'durable_not_applied',
'updated_at' => $failedAt,
]);
DB::table('cloud_jobs')->where('id', $failedV6ChildId)->update([
'status' => 'succeeded',
'error_code' => null,
]);
$projectChild($failedV6ChildId);
$assert(DB::table('cloud_public_ip_pool_replacement_batch_items')
->where('child_job_id', $failedV6ChildId)->value('state')
=== 'failed'
&& DB::table('cloud_public_ip_pool_replacement_batch_items')
->where('child_job_id', $failedV6ChildId)
->value('last_error_code') === 'durable_not_applied'
&& (string) CloudPublicIpPoolReplacementBatch::find($v6BatchId)->state
=== 'running',
'Strict failed/not-applied transition did not override job catalog drift.');
$jobService = file_get_contents(
dirname(__DIR__) . '/app/Services/Cloud/CloudJobService.php'
);
$batchService = file_get_contents(
dirname(__DIR__)
. '/app/Services/Cloud/CloudPublicIpPoolReplacementBatchService.php'
);
$assert(is_string($jobService) && is_string($batchService)
&& !str_contains(
$jobService,
'enqueuePoolReplacementNetworkChangeLocked'
)
&& !str_contains(
$batchService,
'enqueuePoolReplacementNetworkChangeLocked'
), 'Legacy scalar pool-replacement child enqueuer remains reachable.');
echo "Cloud public IP pool replacement dual-stack behavior tests passed\n";
}