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

1239 lines
43 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudPublicNetworkAllocationService;
use App\Services\Cloud\CloudPublicNetworkCapacityUnavailable;
use App\Services\Cloud\CloudError;
use Illuminate\Database\Capsule\Manager as Capsule;
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): void {
try {
$callback();
} catch (Throwable) {
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('cloud_hosts', static function (Blueprint $table): void {
$table->increments('id');
});
$schema->create('cloud_host_agents', static function (Blueprint $table): void {
$table->integer('host_id')->primary();
$table->boolean('enabled');
$table->integer('revision');
$table->integer('tested_revision');
$table->string('last_test_status');
$table->text('network_selectors_json');
$table->text('public_network_capabilities_json');
});
$schema->create('cloud_host_network_attachments', static function (
Blueprint $table
): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('line_id');
$table->string('network_selector');
$table->string('state');
$table->integer('priority')->default(0);
$table->string('verification_state');
$table->integer('verified_agent_revision');
$table->integer('last_verified_at');
});
$schema->create('cloud_public_ip_pools', static function (
Blueprint $table
): void {
$table->increments('id');
$table->string('pool_key');
$table->integer('host_id');
$table->integer('network_attachment_id');
$table->integer('address_family');
$table->string('cidr');
$table->string('gateway');
$table->boolean('gateway_on_link');
$table->integer('guest_prefix_length');
$table->text('dns_servers_json');
$table->text('reserved_ips_json');
$table->string('allocation_start')->nullable();
$table->string('allocation_end')->nullable();
$table->string('allocation_cursor')->nullable();
$table->integer('max_allocations');
$table->string('state');
$table->integer('priority')->default(0);
$table->integer('desired_revision');
$table->integer('observed_revision');
$table->string('sync_state');
});
$schema->create('cloud_public_ips', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('public_ip_pool_id');
$table->integer('address_family');
$table->string('address');
$table->binary('address_binary');
$table->integer('prefix_length');
$table->string('state');
$table->integer('reservation_id')->nullable();
$table->integer('instance_id')->nullable();
$table->string('operation_id')->nullable();
$table->integer('created_at');
$table->integer('updated_at');
$table->unique(['address_family', 'address_binary']);
});
$schema->create('cloud_resource_reservations', static function (
Blueprint $table
): void {
$table->increments('id');
$table->integer('host_id');
$table->string('operation_id');
$table->string('state');
$table->integer('order_id')->nullable();
$table->integer('backup_restore_id')->nullable();
$table->integer('instance_id')->nullable();
$table->integer('network_attachment_id')->nullable();
$table->integer('public_ip_pool_id')->nullable();
$table->integer('public_ip_id')->nullable();
});
$schema->create('cloud_instance_migrations', static function (
Blueprint $table
): void {
$table->increments('id');
$table->integer('instance_id');
$table->integer('restore_id');
$table->integer('source_host_id');
$table->integer('source_reservation_id');
$table->integer('target_host_id');
$table->integer('target_reservation_id');
$table->string('state');
});
$schema->create('cloud_resource_reservation_ips', static function (
Blueprint $table
): void {
$table->increments('id');
$table->integer('reservation_id');
$table->integer('address_family');
$table->integer('network_attachment_id');
$table->integer('public_ip_pool_id');
$table->integer('public_ip_id');
$table->string('operation_id');
$table->string('state');
$table->integer('created_at');
$table->integer('updated_at');
$table->integer('released_at')->nullable();
$table->string('release_reason')->default('');
$table->unique(['reservation_id', 'address_family']);
});
Capsule::table('cloud_host_network_attachments')->insert([
'id' => 10,
'host_id' => 1,
'line_id' => 7,
'network_selector' => 'bridge:br0',
'state' => 'active',
'priority' => 0,
'verification_state' => 'verified',
'verified_agent_revision' => 1,
'last_verified_at' => time(),
]);
Capsule::table('cloud_hosts')->insert(['id' => 1]);
Capsule::table('cloud_host_agents')->insert([
'host_id' => 1,
'enabled' => 1,
'revision' => 1,
'tested_revision' => 1,
'last_test_status' => 'healthy',
'network_selectors_json' => '["bridge:br0"]',
'public_network_capabilities_json' => json_encode([
'contract' => 1,
'dual_stack_required' => true,
'project_owned_filter' => true,
'ndp_guard' => true,
], JSON_THROW_ON_ERROR),
]);
$common = [
'host_id' => 1,
'network_attachment_id' => 10,
'state' => 'active',
'priority' => 0,
'desired_revision' => 1,
'observed_revision' => 1,
'sync_state' => 'applied',
];
Capsule::table('cloud_public_ip_pools')->insert([
array_merge($common, [
'id' => 100,
'pool_key' => str_repeat('a', 32),
'address_family' => 4,
'cidr' => '8.8.8.0/24',
'gateway' => '8.8.8.1',
'gateway_on_link' => 0,
'guest_prefix_length' => 24,
'dns_servers_json' => '["1.1.1.1"]',
'reserved_ips_json' => '[]',
'allocation_start' => null,
'allocation_end' => null,
'allocation_cursor' => null,
'max_allocations' => 0,
]),
array_merge($common, [
'id' => 200,
'pool_key' => str_repeat('b', 32),
'address_family' => 6,
'cidr' => '2404:c140:156:3a::/64',
'gateway' => '2404:c140:156::1',
'gateway_on_link' => 1,
'guest_prefix_length' => 64,
'dns_servers_json' => '["2606:4700:4700::1111"]',
'reserved_ips_json' => json_encode(array_map(
static fn (int $suffix): string =>
'2404:c140:156:3a::' . dechex($suffix),
range(0x1000, 0x1013)
), JSON_THROW_ON_ERROR),
'allocation_start' => '2404:c140:156:3a::1000',
'allocation_end' => '2404:c140:156:3a::1060',
'allocation_cursor' => null,
'max_allocations' => 4,
]),
]);
$now = time();
foreach ([10, 11] as $suffix) {
$address = '8.8.8.' . $suffix;
Capsule::table('cloud_public_ips')->insert([
'host_id' => 1,
'public_ip_pool_id' => 100,
'address_family' => 4,
'address' => $address,
'address_binary' => inet_pton($address),
'prefix_length' => 24,
'state' => 'available',
'created_at' => $now,
'updated_at' => $now,
]);
}
foreach (range(0x1014, 0x102c) as $suffix) {
$address = '2404:c140:156:3a::' . dechex($suffix);
Capsule::table('cloud_public_ips')->insert([
'host_id' => 1,
'public_ip_pool_id' => 200,
'address_family' => 6,
'address' => $address,
'address_binary' => inet_pton($address),
'prefix_length' => 64,
'state' => 'disabled',
'created_at' => $now,
'updated_at' => $now,
]);
}
$operations = [
500 => str_repeat('c', 32),
501 => str_repeat('d', 32),
502 => str_repeat('e', 32),
503 => str_repeat('f', 32),
504 => str_repeat('9', 32),
];
foreach ($operations as $id => $operationId) {
Capsule::table('cloud_resource_reservations')->insert([
'id' => $id,
'host_id' => 1,
'operation_id' => $operationId,
'state' => 'held',
]);
}
$materializedBefore = Capsule::table('cloud_public_ips')->count();
$first = CloudPublicNetworkAllocationService::reserveBundle(
1,
7,
500,
$operations[500]
);
$assert($first->address(4) === '8.8.8.10/24'
&& $first->address(6) === '2404:c140:156:3a::102d/64',
'The first atomic bundle did not contain one address from each family.');
$assert(Capsule::table('cloud_public_ips')->count() === $materializedBefore + 1,
'The sparse /64 allocator materialized more than the claimed IPv6 address.');
$assert(Capsule::table('cloud_resource_reservation_ips')
->where('reservation_id', 500)->count() === 2,
'One reservation did not retain both address claims.');
$replay = CloudPublicNetworkAllocationService::reserveBundle(
1,
7,
500,
$operations[500]
);
$assert(hash_equals($first->fingerprint(), $replay->fingerprint())
&& Capsule::table('cloud_public_ips')->count() === $materializedBefore + 1,
'Idempotent bundle reservation allocated a second address.');
$committed = CloudPublicNetworkAllocationService::commitBundle(
500,
900,
$operations[500]
);
$assert(hash_equals($first->fingerprint(), $committed->fingerprint())
&& Capsule::table('cloud_public_ips')->where('instance_id', 900)
->where('state', 'assigned')->count() === 2,
'Commit did not bind the complete dual-stack identity to the instance.');
$replacementAttempt = CloudPublicNetworkAllocationService::reserveBundle(
1,
7,
500,
$operations[500]
);
$assert(hash_equals($committed->fingerprint(),
$replacementAttempt->fingerprint())
&& Capsule::table('cloud_public_ips')->count() === $materializedBefore + 1,
'A normal reservation was reused to overwrite a committed source bundle.');
$recommitted = CloudPublicNetworkAllocationService::commitBundle(
500,
900,
$operations[500]
);
$assert(hash_equals($committed->fingerprint(), $recommitted->fingerprint()),
'An already committed bundle was not an idempotent success.');
$rejects(static fn () => CloudPublicNetworkAllocationService::releaseBundle(
500,
$operations[500],
'confirmed_not_applied',
true
), 'A committed public-network bundle was released.');
$assert(Capsule::table('cloud_public_ips')->where('instance_id', 900)
->where('state', 'assigned')->count() === 2,
'Rejecting a committed release changed its assigned address bundle.');
$second = CloudPublicNetworkAllocationService::reserveBundle(
1,
7,
501,
$operations[501]
);
$assert($second->address(4) === '8.8.8.11/24'
&& $second->address(6) === '2404:c140:156:3a::102e/64',
'The allocation cursor did not advance across a second reservation.');
CloudPublicNetworkAllocationService::fenceBundle(501, $operations[501]);
$assert(Capsule::table('cloud_public_ips')->where('reservation_id', 501)
->where('state', 'uncertain')->count() === 2,
'Unknown effect did not fence both public addresses.');
$rejects(static fn () => CloudPublicNetworkAllocationService::releaseBundle(
501,
$operations[501],
'confirmed_not_applied',
false
), 'An unconfirmed destructive result released a public address bundle.');
CloudPublicNetworkAllocationService::releaseBundle(
501,
$operations[501],
'confirmed_not_applied',
true
);
$assert(Capsule::table('cloud_public_ips')->where('reservation_id', 501)
->count() === 0
&& Capsule::table('cloud_resource_reservation_ips')
->where('reservation_id', 501)->where('state', 'released')->count() === 2,
'Confirmed non-application did not release the complete bundle.');
$uncertainSuccess = CloudPublicNetworkAllocationService::reserveBundle(
1,
7,
504,
$operations[504]
);
CloudPublicNetworkAllocationService::fenceBundle(504, $operations[504]);
$rejects(static fn () => CloudPublicNetworkAllocationService::commitBundle(
504,
901,
str_repeat('8', 32)
), 'An uncertain bundle accepted a different operation identity.');
$recoveredSuccess = CloudPublicNetworkAllocationService::commitBundle(
504,
901,
$operations[504]
);
$assert(hash_equals(
$uncertainSuccess->fingerprint(),
$recoveredSuccess->fingerprint()
) && Capsule::table('cloud_public_ips')->where('instance_id', 901)
->where('state', 'assigned')->count() === 2
&& Capsule::table('cloud_resource_reservation_ips')
->where('reservation_id', 504)->where('state', 'committed')->count() === 2,
'A recovered success did not atomically commit its uncertain dual-stack bundle.');
$rejects(static fn () => CloudPublicNetworkAllocationService::commitBundle(
504,
902,
$operations[504]
), 'A committed bundle was rebound to a different instance.');
Capsule::table('cloud_public_ip_pools')->where('id', 200)->update([
'max_allocations' => 1,
]);
$rejects(static fn () => CloudPublicNetworkAllocationService::reserveBundle(
1,
7,
502,
$operations[502]
), 'A reusable sparse IPv6 row bypassed the pool allocation limit.');
$assert(Capsule::table('cloud_resource_reservation_ips')
->where('reservation_id', 502)->count() === 0,
'A failed upper-bound check retained a partial address claim.');
$rejects(static fn () =>
CloudPublicNetworkAllocationService::releaseCommittedBundle(
500,
900,
$operations[500],
'instance_deleted',
false
), 'An assigned bundle was released before deletion was confirmed.');
CloudPublicNetworkAllocationService::releaseCommittedBundle(
500,
900,
$operations[500],
'instance_deleted',
true
);
$assert(Capsule::table('cloud_public_ips')->where('instance_id', 900)
->count() === 0
&& Capsule::table('cloud_resource_reservation_ips')
->where('reservation_id', 500)->where('state', 'released')->count() === 2,
'Confirmed instance deletion did not release its committed dual-stack bundle.');
Capsule::table('cloud_public_ip_pools')->where('id', 100)->update([
'cidr' => '198.51.100.0/24',
'gateway' => '198.51.100.1',
]);
$invalidPoolError = null;
try {
CloudPublicNetworkAllocationService::reserveBundle(
1,
7,
503,
$operations[503]
);
} catch (CloudError $error) {
$invalidPoolError = $error;
}
$assert($invalidPoolError instanceof CloudError
&& !$invalidPoolError instanceof CloudPublicNetworkCapacityUnavailable,
'Invalid pool configuration was downgraded to scheduler exhaustion.');
$assert(Capsule::table('cloud_resource_reservation_ips')
->where('reservation_id', 503)->count() === 0,
'Rejecting a non-public pool retained a partial address claim.');
$addAgent = static function (int $hostId, array $selectors,
array $overrides = []): void {
Capsule::table('cloud_host_agents')->insert(array_replace([
'host_id' => $hostId,
'enabled' => 1,
'revision' => 1,
'tested_revision' => 1,
'last_test_status' => 'healthy',
'network_selectors_json' => json_encode(
$selectors,
JSON_THROW_ON_ERROR
),
'public_network_capabilities_json' => json_encode([
'contract' => 1,
'dual_stack_required' => true,
'project_owned_filter' => true,
'ndp_guard' => true,
], JSON_THROW_ON_ERROR),
], $overrides));
};
$addAttachment = static function (int $id, int $hostId, int $lineId,
int $priority, array $overrides = []): void {
Capsule::table('cloud_host_network_attachments')->insert(array_replace([
'id' => $id,
'host_id' => $hostId,
'line_id' => $lineId,
'network_selector' => 'bridge:br' . $id,
'state' => 'active',
'priority' => $priority,
'verification_state' => 'verified',
'verified_agent_revision' => 1,
'last_verified_at' => time(),
], $overrides));
};
$addPool = static function (int $id, int $hostId, int $attachmentId,
int $family, int $priority, string $cidr, string $gateway,
?string $rangeStart = null, ?string $rangeEnd = null,
int $maximum = 0): void {
Capsule::table('cloud_public_ip_pools')->insert([
'id' => $id,
'pool_key' => str_pad(dechex($id), 32, '0', STR_PAD_LEFT),
'host_id' => $hostId,
'network_attachment_id' => $attachmentId,
'address_family' => $family,
'cidr' => $cidr,
'gateway' => $gateway,
'gateway_on_link' => $family === 6 ? 1 : 0,
'guest_prefix_length' => (int) substr(strrchr($cidr, '/'), 1),
'dns_servers_json' => $family === 6
? '["2606:4700:4700::1111"]' : '["1.1.1.1"]',
'reserved_ips_json' => '[]',
'allocation_start' => $rangeStart,
'allocation_end' => $rangeEnd,
'allocation_cursor' => null,
'max_allocations' => $maximum,
'state' => 'active',
'priority' => $priority,
'desired_revision' => 1,
'observed_revision' => 1,
'sync_state' => 'applied',
]);
};
$addIp = static function (int $hostId, int $poolId, int $family,
string $address, int $prefix, string $state = 'available'): void {
Capsule::table('cloud_public_ips')->insert([
'host_id' => $hostId,
'public_ip_pool_id' => $poolId,
'address_family' => $family,
'address' => $address,
'address_binary' => inet_pton($address),
'prefix_length' => $prefix,
'state' => $state,
'reservation_id' => $state === 'assigned' ? 9999 : null,
'instance_id' => $state === 'assigned' ? 9999 : null,
'operation_id' => $state === 'assigned' ? str_repeat('7', 32) : null,
'created_at' => time(),
'updated_at' => time(),
]);
};
$addReservation = static function (int $id, int $hostId,
string $operationId): void {
Capsule::table('cloud_resource_reservations')->insert([
'id' => $id,
'host_id' => $hostId,
'operation_id' => $operationId,
'state' => 'held',
]);
};
$assertHardFailure = static function (callable $callback,
string $message) use ($assert): void {
$failure = null;
try {
$callback();
} catch (CloudError $error) {
$failure = $error;
}
$assert($failure instanceof CloudError
&& !$failure instanceof CloudPublicNetworkCapacityUnavailable,
$message);
};
$addPairHost = static function (int $hostId, int $lineId,
int $attachmentId, int $ipv4PoolId, int $ipv6PoolId,
string $ipv4Cidr, string $ipv6Cidr, array $agentOverrides = [],
array $attachmentOverrides = []) use (
$addAgent,
$addAttachment,
$addPool,
$addIp
): void {
$ipv4Prefix = substr($ipv4Cidr, 0, strrpos($ipv4Cidr, '.'));
$ipv6Network = substr($ipv6Cidr, 0, -3);
$selector = 'bridge:br' . $attachmentId;
Capsule::table('cloud_hosts')->insert(['id' => $hostId]);
$addAgent($hostId, [$selector], $agentOverrides);
$addAttachment(
$attachmentId,
$hostId,
$lineId,
0,
$attachmentOverrides
);
$addPool(
$ipv4PoolId,
$hostId,
$attachmentId,
4,
0,
$ipv4Cidr,
$ipv4Prefix . '.1'
);
$addIp(
$hostId,
$ipv4PoolId,
4,
$ipv4Prefix . '.10',
24
);
$addPool(
$ipv6PoolId,
$hostId,
$attachmentId,
6,
0,
$ipv6Cidr,
'2404:c140:156::1',
$ipv6Network . '10',
$ipv6Network . '20',
4
);
};
// A complete attachment may have more than one pool per family. Exhaustion
// of a higher-priority pool must not hide capacity in its next sibling.
Capsule::table('cloud_hosts')->insert(['id' => 2]);
$addAgent(2, ['bridge:br20', 'bridge:br30', 'bridge:br31']);
$addAttachment(20, 2, 8, 0);
$addPool(110, 2, 20, 4, 0, '9.9.9.0/24', '9.9.9.1');
Capsule::table('cloud_public_ip_pools')->where('id', 110)->update([
'reserved_ips_json' => '["9.9.9.10"]',
]);
$addPool(111, 2, 20, 4, 1, '11.0.0.0/24', '11.0.0.1');
$addIp(2, 111, 4, '11.0.0.10', 24);
$addPool(210, 2, 20, 6, 0, '2404:c140:156:3b::/64',
'2404:c140:156::1', '2404:c140:156:3b::10',
'2404:c140:156:3b::20', 1);
$addIp(2, 210, 6, '2404:c140:156:3b::10', 64, 'assigned');
$addPool(211, 2, 20, 6, 1, '2404:c140:156:3c::/64',
'2404:c140:156::1', '2404:c140:156:3c::10',
'2404:c140:156:3c::20', 2);
$fallbackOperation = str_repeat('6', 32);
$addReservation(600, 2, $fallbackOperation);
$fallback = CloudPublicNetworkAllocationService::reserveBundle(
2, 8, 600, $fallbackOperation
);
$assert($fallback->address(4) === '11.0.0.10/24'
&& $fallback->address(6) === '2404:c140:156:3c::10/64',
'Allocation did not skip exhausted higher-priority family pools.');
// The same deterministic search must continue with the next attachment when
// the first complete attachment has no usable pair.
$addAttachment(30, 2, 9, 0);
$addPool(120, 2, 30, 4, 0, '12.0.0.0/24', '12.0.0.1');
$addIp(2, 120, 4, '12.0.0.10', 24);
$addPool(220, 2, 30, 6, 0, '2404:c140:156:3d::/64',
'2404:c140:156::1', '2404:c140:156:3d::10',
'2404:c140:156:3d::20', 1);
$addIp(2, 220, 6, '2404:c140:156:3d::10', 64, 'assigned');
$addAttachment(31, 2, 9, 1);
$addPool(121, 2, 31, 4, 0, '13.0.0.0/24', '13.0.0.1');
$addIp(2, 121, 4, '13.0.0.10', 24);
$addPool(221, 2, 31, 6, 0, '2404:c140:156:3e::/64',
'2404:c140:156::1', '2404:c140:156:3e::10',
'2404:c140:156:3e::20', 2);
$attachmentFallbackOperation = str_repeat('5', 32);
$addReservation(601, 2, $attachmentFallbackOperation);
$attachmentFallback = CloudPublicNetworkAllocationService::reserveBundle(
2, 9, 601, $attachmentFallbackOperation
);
$attachmentClaims = Capsule::table('cloud_resource_reservation_ips')
->where('reservation_id', 601)->orderBy('address_family')->get();
$assert($attachmentFallback->address(4) === '13.0.0.10/24'
&& $attachmentFallback->address(6) === '2404:c140:156:3e::10/64'
&& $attachmentClaims->count() === 2
&& $attachmentClaims->every(
static fn ($claim): bool => (int) $claim->network_attachment_id === 31
), 'Allocation did not continue with the next complete attachment.');
// An unavailable IPv6 leg must reject the whole bundle before the available
// IPv4 leg is claimed.
Capsule::table('cloud_hosts')->insert(['id' => 3]);
$addAgent(3, ['bridge:br40']);
$addAttachment(40, 3, 10, 0);
$addPool(130, 3, 40, 4, 0, '14.0.0.0/24', '14.0.0.1');
$addIp(3, 130, 4, '14.0.0.10', 24);
$addPool(230, 3, 40, 6, 0, '2404:c140:156:3f::/64',
'2404:c140:156::1', '2404:c140:156:3f::10',
'2404:c140:156:3f::20', 1);
$addIp(3, 230, 6, '2404:c140:156:3f::10', 64, 'assigned');
$exhaustedOperation = str_repeat('4', 32);
$addReservation(602, 3, $exhaustedOperation);
$capacityUnavailable = false;
try {
CloudPublicNetworkAllocationService::reserveBundle(
3, 10, 602, $exhaustedOperation
);
} catch (CloudPublicNetworkCapacityUnavailable) {
$capacityUnavailable = true;
}
$assert($capacityUnavailable,
'Pair exhaustion did not produce the dedicated capacity result.');
$assert(Capsule::table('cloud_resource_reservation_ips')
->where('reservation_id', 602)->count() === 0
&& Capsule::table('cloud_public_ips')->where('public_ip_pool_id', 130)
->where('state', 'available')->whereNull('reservation_id')->count() === 1,
'A failed pair selection left a partial IPv4 claim.');
// Exact allocation must ignore an unverified high-priority attachment and
// continue with a lower-priority attachment backed by the same locked current
// Agent evidence.
Capsule::table('cloud_hosts')->insert(['id' => 4]);
$addAgent(4, ['bridge:br51']);
$addAttachment(50, 4, 11, 0, [
'verification_state' => 'pending',
'verified_agent_revision' => 0,
]);
$addPool(140, 4, 50, 4, 0, '15.0.0.0/24', '15.0.0.1');
$addIp(4, 140, 4, '15.0.0.10', 24);
$addPool(240, 4, 50, 6, 0, '2404:c140:156:40::/64',
'2404:c140:156::1', '2404:c140:156:40::10',
'2404:c140:156:40::20', 4);
$addAttachment(51, 4, 11, 1);
$addPool(141, 4, 51, 4, 0, '16.0.0.0/24', '16.0.0.1');
$addIp(4, 141, 4, '16.0.0.10', 24);
$addPool(241, 4, 51, 6, 0, '2404:c140:156:41::/64',
'2404:c140:156::1', '2404:c140:156:41::10',
'2404:c140:156:41::20', 4);
$verifiedFallbackOperation = str_repeat('3', 32);
$addReservation(603, 4, $verifiedFallbackOperation);
$verifiedFallback = CloudPublicNetworkAllocationService::reserveBundle(
4, 11, 603, $verifiedFallbackOperation
);
$verifiedFallbackClaims = Capsule::table('cloud_resource_reservation_ips')
->where('reservation_id', 603)->get();
$assert($verifiedFallback->address(4) === '16.0.0.10/24'
&& $verifiedFallbackClaims->count() === 2
&& $verifiedFallbackClaims->every(
static fn ($claim): bool =>
(int) $claim->network_attachment_id === 51
), 'Allocator selected an unverified higher-priority attachment.');
// Missing capabilities, an untested selector and stale revision evidence are
// configuration/evidence failures. They must never be downgraded to ordinary
// address exhaustion and silently sent to the next host.
$addPairHost(
5,
12,
60,
150,
250,
'17.0.0.0/24',
'2404:c140:156:50::/64'
);
$evidenceCases = [
'capabilities' => static function (): void {
Capsule::table('cloud_host_agents')->where('host_id', 5)->update([
'public_network_capabilities_json' => '{}',
]);
},
'selector' => static function (): void {
Capsule::table('cloud_host_agents')->where('host_id', 5)->update([
'network_selectors_json' => '[]',
]);
},
'agent revision' => static function (): void {
Capsule::table('cloud_host_agents')->where('host_id', 5)->update([
'tested_revision' => 0,
]);
},
'attachment revision' => static function (): void {
Capsule::table('cloud_host_network_attachments')->where('id', 60)
->update(['verified_agent_revision' => 2]);
},
];
$validCapabilities = json_encode([
'contract' => 1,
'dual_stack_required' => true,
'project_owned_filter' => true,
'ndp_guard' => true,
], JSON_THROW_ON_ERROR);
$evidenceReservationId = 604;
foreach ($evidenceCases as $label => $mutateEvidence) {
Capsule::table('cloud_host_agents')->where('host_id', 5)->update([
'enabled' => 1,
'revision' => 1,
'tested_revision' => 1,
'last_test_status' => 'healthy',
'network_selectors_json' => '["bridge:br60"]',
'public_network_capabilities_json' => $validCapabilities,
]);
Capsule::table('cloud_host_network_attachments')->where('id', 60)
->update([
'verification_state' => 'verified',
'verified_agent_revision' => 1,
'last_verified_at' => time(),
]);
$mutateEvidence();
$operation = str_pad(dechex($evidenceReservationId), 32, 'a');
$addReservation($evidenceReservationId, 5, $operation);
$assertHardFailure(
static fn () => CloudPublicNetworkAllocationService::reserveBundle(
5,
12,
$evidenceReservationId,
$operation
),
'Stale ' . $label . ' evidence was treated as address exhaustion.'
);
$assert(Capsule::table('cloud_resource_reservation_ips')
->where('reservation_id', $evidenceReservationId)->count() === 0,
'Stale ' . $label . ' evidence retained an address claim.');
$evidenceReservationId++;
}
// An active pool whose observed revision is ahead of the desired catalog is
// not a capacity miss; it is a non-converged configuration that must stop the
// paid placement transaction.
Capsule::table('cloud_host_agents')->where('host_id', 5)->update([
'tested_revision' => 1,
'network_selectors_json' => '["bridge:br60"]',
'public_network_capabilities_json' => $validCapabilities,
]);
Capsule::table('cloud_host_network_attachments')->where('id', 60)->update([
'verification_state' => 'verified',
'verified_agent_revision' => 1,
]);
Capsule::table('cloud_public_ip_pools')->where('id', 150)->update([
'observed_revision' => 2,
]);
$revisionOperation = str_repeat('2', 32);
$addReservation(608, 5, $revisionOperation);
$assertHardFailure(
static fn () => CloudPublicNetworkAllocationService::reserveBundle(
5,
12,
608,
$revisionOperation
),
'A pool ahead of desired revision was treated as address exhaustion.'
);
$assert(Capsule::table('cloud_resource_reservation_ips')
->where('reservation_id', 608)->count() === 0,
'A non-converged pool retained an address claim.');
// Every materialized row is validated before capacity is evaluated. Shape,
// topology and concrete address corruption must therefore hard-fail rather
// than make a host look merely full.
$addPairHost(
6,
13,
70,
160,
260,
'18.0.0.0/24',
'2404:c140:156:60::/64'
);
$ledgerIpv4 = Capsule::table('cloud_public_ips')
->where('public_ip_pool_id', 160)->first();
$assert($ledgerIpv4 !== null, 'Ledger corruption fixture is missing IPv4.');
$ledgerBaseline = [
'host_id' => 6,
'public_ip_pool_id' => 160,
'address_family' => 4,
'address' => '18.0.0.10',
'address_binary' => inet_pton('18.0.0.10'),
'prefix_length' => 24,
'state' => 'available',
'reservation_id' => null,
'instance_id' => null,
'operation_id' => null,
];
$ledgerCases = [
'dirty available ownership' => [
'reservation_id' => 9999,
],
'active row without ownership' => [
'state' => 'reserved',
],
'wrong host' => [
'host_id' => 999,
],
'wrong family' => [
'address_family' => 6,
],
'wrong pool' => [
'public_ip_pool_id' => 260,
],
'invalid prefix' => [
'prefix_length' => 23,
],
'address outside pool' => [
'address' => '19.0.0.10',
'address_binary' => inet_pton('19.0.0.10'),
],
'same text with different binary' => [
'address_binary' => inet_pton('18.0.0.11'),
],
];
$ledgerReservationId = 609;
foreach ($ledgerCases as $label => $mutation) {
Capsule::table('cloud_public_ips')->where('id', (int) $ledgerIpv4->id)
->update(array_replace($ledgerBaseline, $mutation));
$operation = str_pad(dechex($ledgerReservationId), 32, 'b');
$addReservation($ledgerReservationId, 6, $operation);
$assertHardFailure(
static fn () => CloudPublicNetworkAllocationService::reserveBundle(
6,
13,
$ledgerReservationId,
$operation
),
'Corrupt ' . $label . ' ledger was treated as address exhaustion.'
);
$assert(Capsule::table('cloud_resource_reservation_ips')
->where('reservation_id', $ledgerReservationId)->count() === 0,
'Corrupt ' . $label . ' ledger retained a partial claim.');
$ledgerReservationId++;
}
// Canonical text is part of the concrete identity just like the packed bytes.
Capsule::table('cloud_public_ips')->where('id', (int) $ledgerIpv4->id)
->update($ledgerBaseline);
$nonCanonicalIpv6Id = Capsule::table('cloud_public_ips')->insertGetId([
'host_id' => 6,
'public_ip_pool_id' => 260,
'address_family' => 6,
'address' => '2404:C140:156:60::10',
'address_binary' => inet_pton('2404:c140:156:60::10'),
'prefix_length' => 64,
'state' => 'available',
'reservation_id' => null,
'instance_id' => null,
'operation_id' => null,
'created_at' => time(),
'updated_at' => time(),
]);
$canonicalOperation = str_repeat('1', 32);
$addReservation(617, 6, $canonicalOperation);
$assertHardFailure(
static fn () => CloudPublicNetworkAllocationService::reserveBundle(
6,
13,
617,
$canonicalOperation
),
'Non-canonical concrete address text was treated as address exhaustion.'
);
$assert(Capsule::table('cloud_resource_reservation_ips')
->where('reservation_id', 617)->count() === 0,
'Rejecting non-canonical text retained a partial claim.');
Capsule::table('cloud_public_ips')->where('id', $nonCanonicalIpv6Id)->delete();
// A retained maintenance-migration source is a deliberate capacity debt, not
// a corrupt assigned address. Accept only the exact migration + restore
// reservation evidence, and keep that address unavailable to new placement.
$addPairHost(
8,
15,
90,
180,
280,
'21.0.0.0/24',
'2404:c140:156:80::/64'
);
foreach ([
['21.0.0.14', 'changing', null, 8000, str_repeat('6', 32)],
['21.0.0.15', 'uncertain', null, 8001, str_repeat('7', 32)],
['21.0.0.16', 'disabled', null, null, null],
] as [$address, $state, $reservationId, $instanceId, $operationId]) {
Capsule::table('cloud_public_ips')->insert([
'host_id' => 8,
'public_ip_pool_id' => 180,
'address_family' => 4,
'address' => $address,
'address_binary' => inet_pton($address),
'prefix_length' => 24,
'state' => $state,
'reservation_id' => $reservationId,
'instance_id' => $instanceId,
'operation_id' => $operationId,
'created_at' => time(),
'updated_at' => time(),
]);
}
$retainedOperation = str_repeat('8', 32);
$retainedIpId = Capsule::table('cloud_public_ips')->insertGetId([
'host_id' => 8,
'public_ip_pool_id' => 180,
'address_family' => 4,
'address' => '21.0.0.11',
'address_binary' => inet_pton('21.0.0.11'),
'prefix_length' => 24,
'state' => 'assigned',
'reservation_id' => 700,
'instance_id' => null,
'operation_id' => $retainedOperation,
'created_at' => time(),
'updated_at' => time(),
]);
Capsule::table('cloud_resource_reservations')->insert([
'id' => 700,
'host_id' => 8,
'operation_id' => $retainedOperation,
'state' => 'committed',
'order_id' => null,
'backup_restore_id' => 800,
'instance_id' => null,
'network_attachment_id' => 90,
'public_ip_pool_id' => 180,
'public_ip_id' => $retainedIpId,
]);
Capsule::table('cloud_instance_migrations')->insert([
'id' => 800,
'instance_id' => 8000,
'restore_id' => 800,
'source_host_id' => 8,
'source_reservation_id' => 700,
'target_host_id' => 9,
'target_reservation_id' => 701,
'state' => 'source_retained',
]);
$sourceRetainedOperation = str_repeat('9', 32);
$addReservation(621, 8, $sourceRetainedOperation);
$sourceRetainedBundle = CloudPublicNetworkAllocationService::reserveBundle(
8,
15,
621,
$sourceRetainedOperation
);
$assert($sourceRetainedBundle->address(4) === '21.0.0.10/24'
&& Capsule::table('cloud_public_ips')->where('id', $retainedIpId)
->where('state', 'assigned')->whereNull('instance_id')
->where('reservation_id', 700)->count() === 1,
'Exact source-retained debt blocked placement or was reallocated.');
// The same debt remains valid while its cleanup mutation is in flight.
Capsule::table('cloud_instance_migrations')->where('id', 800)->update([
'state' => 'cleanup_running',
]);
$addIp(8, 180, 4, '21.0.0.12', 24);
$cleanupRunningOperation = str_repeat('a', 32);
$addReservation(622, 8, $cleanupRunningOperation);
$cleanupRunningBundle = CloudPublicNetworkAllocationService::reserveBundle(
8,
15,
622,
$cleanupRunningOperation
);
$assert($cleanupRunningBundle->address(4) === '21.0.0.12/24'
&& Capsule::table('cloud_public_ips')->where('id', $retainedIpId)
->where('state', 'assigned')->whereNull('instance_id')
->where('reservation_id', 700)->count() === 1,
'Cleanup-running retained debt blocked placement or was reallocated.');
// An orphan or any material identity mismatch must remain a hard ledger
// failure. It must not become ordinary capacity exhaustion or claim a spare.
$addIp(8, 180, 4, '21.0.0.13', 24);
$retainedMigrationBaseline = (array) Capsule::table(
'cloud_instance_migrations'
)->where('id', 800)->first();
$retainedReservationBaseline = (array) Capsule::table(
'cloud_resource_reservations'
)->where('id', 700)->first();
$evidenceFailureReservationId = 623;
$assertRetainedEvidenceFailure = static function (
string $label
) use (
&$evidenceFailureReservationId,
$addReservation,
$assertHardFailure,
$assert
): void {
$operation = str_pad(
dechex($evidenceFailureReservationId),
32,
'b'
);
$addReservation($evidenceFailureReservationId, 8, $operation);
$assertHardFailure(
static fn () => CloudPublicNetworkAllocationService::reserveBundle(
8,
15,
$evidenceFailureReservationId,
$operation
),
$label . ' was downgraded to ordinary capacity exhaustion.'
);
$assert(Capsule::table('cloud_resource_reservation_ips')
->where('reservation_id', $evidenceFailureReservationId)
->count() === 0,
$label . ' retained a partial public-network claim.');
$evidenceFailureReservationId++;
};
Capsule::table('cloud_instance_migrations')->where('id', 800)->delete();
$assertRetainedEvidenceFailure('Orphan retained-source address');
Capsule::table('cloud_instance_migrations')->insert(
$retainedMigrationBaseline
);
$retainedEvidenceMutations = [
'Unsupported retained-source state' => static function (): void {
Capsule::table('cloud_instance_migrations')->where('id', 800)
->update(['state' => 'completed']);
},
'Migration restore identity mismatch' => static function (): void {
Capsule::table('cloud_instance_migrations')->where('id', 800)
->update(['restore_id' => 801]);
},
'Reservation host identity mismatch' => static function (): void {
Capsule::table('cloud_resource_reservations')->where('id', 700)
->update(['host_id' => 999]);
},
'Reservation pool identity mismatch' => static function (): void {
Capsule::table('cloud_resource_reservations')->where('id', 700)
->update(['public_ip_pool_id' => 280]);
},
'Reservation IP identity mismatch' => static function (): void {
Capsule::table('cloud_resource_reservations')->where('id', 700)
->update(['public_ip_id' => 999999]);
},
'Reservation operation identity mismatch' => static function (): void {
Capsule::table('cloud_resource_reservations')->where('id', 700)
->update(['operation_id' => str_repeat('f', 32)]);
},
];
foreach ($retainedEvidenceMutations as $label => $mutate) {
Capsule::table('cloud_instance_migrations')->where('id', 800)->update(
$retainedMigrationBaseline
);
Capsule::table('cloud_resource_reservations')->where('id', 700)->update(
$retainedReservationBaseline
);
$mutate();
$assertRetainedEvidenceFailure($label);
}
Capsule::table('cloud_instance_migrations')->where('id', 800)->update(
$retainedMigrationBaseline
);
Capsule::table('cloud_resource_reservations')->where('id', 700)->update(
$retainedReservationBaseline
);
// Every state transition repeats the same concrete identity checks. A valid
// text paired with different packed bytes, or an address moved to another
// host, cannot be committed, fenced or released.
$addPairHost(
7,
14,
80,
170,
270,
'20.0.0.0/24',
'2404:c140:156:70::/64'
);
$commitIdentityOperation = str_repeat('c', 32);
$addReservation(618, 7, $commitIdentityOperation);
CloudPublicNetworkAllocationService::reserveBundle(
7,
14,
618,
$commitIdentityOperation
);
$commitIpv4 = Capsule::table('cloud_public_ips')
->where('reservation_id', 618)->where('address_family', 4)->first();
$assert($commitIpv4 !== null, 'Commit identity fixture is missing IPv4.');
Capsule::table('cloud_public_ips')->where('id', (int) $commitIpv4->id)
->update(['address_binary' => inet_pton('20.0.0.11')]);
$assertHardFailure(
static fn () => CloudPublicNetworkAllocationService::commitBundle(
618,
918,
$commitIdentityOperation
),
'Commit accepted same-text/different-binary concrete identity.'
);
Capsule::table('cloud_public_ips')->where('id', (int) $commitIpv4->id)
->update(['address_binary' => inet_pton('20.0.0.10')]);
CloudPublicNetworkAllocationService::commitBundle(
618,
918,
$commitIdentityOperation
);
Capsule::table('cloud_public_ips')->where('id', (int) $commitIpv4->id)
->update(['host_id' => 999]);
$assertHardFailure(
static fn () =>
CloudPublicNetworkAllocationService::releaseCommittedBundle(
618,
918,
$commitIdentityOperation,
'instance_deleted',
true
),
'Committed release accepted a concrete address from another host.'
);
Capsule::table('cloud_public_ips')->where('id', (int) $commitIpv4->id)
->update(['host_id' => 7]);
CloudPublicNetworkAllocationService::releaseCommittedBundle(
618,
918,
$commitIdentityOperation,
'instance_deleted',
true
);
$fenceIdentityOperation = str_repeat('d', 32);
$addReservation(619, 7, $fenceIdentityOperation);
CloudPublicNetworkAllocationService::reserveBundle(
7,
14,
619,
$fenceIdentityOperation
);
$fenceIpv4 = Capsule::table('cloud_public_ips')
->where('reservation_id', 619)->where('address_family', 4)->first();
$assert($fenceIpv4 !== null, 'Fence identity fixture is missing IPv4.');
Capsule::table('cloud_public_ips')->where('id', (int) $fenceIpv4->id)
->update(['host_id' => 999]);
$assertHardFailure(
static fn () => CloudPublicNetworkAllocationService::fenceBundle(
619,
$fenceIdentityOperation
),
'Fence accepted a concrete address from another host.'
);
Capsule::table('cloud_public_ips')->where('id', (int) $fenceIpv4->id)
->update(['host_id' => 7]);
CloudPublicNetworkAllocationService::fenceBundle(
619,
$fenceIdentityOperation
);
CloudPublicNetworkAllocationService::releaseBundle(
619,
$fenceIdentityOperation,
'confirmed_not_applied',
true
);
$releaseIdentityOperation = str_repeat('e', 32);
$addReservation(620, 7, $releaseIdentityOperation);
CloudPublicNetworkAllocationService::reserveBundle(
7,
14,
620,
$releaseIdentityOperation
);
$releaseIpv4 = Capsule::table('cloud_public_ips')
->where('reservation_id', 620)->where('address_family', 4)->first();
$assert($releaseIpv4 !== null, 'Release identity fixture is missing IPv4.');
Capsule::table('cloud_public_ips')->where('id', (int) $releaseIpv4->id)
->update(['address_binary' => inet_pton('20.0.0.11')]);
$assertHardFailure(
static fn () => CloudPublicNetworkAllocationService::releaseBundle(
620,
$releaseIdentityOperation,
'confirmed_not_applied',
true
),
'Release accepted same-text/different-binary concrete identity.'
);
Capsule::table('cloud_public_ips')->where('id', (int) $releaseIpv4->id)
->update(['address_binary' => inet_pton('20.0.0.10')]);
CloudPublicNetworkAllocationService::releaseBundle(
620,
$releaseIdentityOperation,
'confirmed_not_applied',
true
);
echo "Atomic dual-stack public network allocation tests passed.\n";