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

526 lines
19 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudPublicNetworkAllocationService;
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_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);
});
$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');
});
$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,
]);
Capsule::table('cloud_hosts')->insert(['id' => 1]);
$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',
]);
$rejects(static fn () => CloudPublicNetworkAllocationService::reserveBundle(
1,
7,
503,
$operations[503]
), 'A special-use range crossed the public-network allocator.');
$assert(Capsule::table('cloud_resource_reservation_ips')
->where('reservation_id', 503)->count() === 0,
'Rejecting a non-public pool retained a partial address claim.');
$addAttachment = static function (int $id, int $hostId, int $lineId,
int $priority): void {
Capsule::table('cloud_host_network_attachments')->insert([
'id' => $id,
'host_id' => $hostId,
'line_id' => $lineId,
'network_selector' => 'bridge:br' . $id,
'state' => 'active',
'priority' => $priority,
]);
};
$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',
]);
};
// 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]);
$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"]',
]);
$addIp(2, 110, 4, '9.9.9.10', 24);
$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]);
$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);
$rejects(static fn () => CloudPublicNetworkAllocationService::reserveBundle(
3, 10, 602, $exhaustedOperation
), 'A host without a complete usable pool pair was accepted.');
$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.');
echo "Atomic dual-stack public network allocation tests passed.\n";