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.
295 lines
9.8 KiB
PHP
295 lines
9.8 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Services\Cloud\CloudError;
|
|
use App\Services\Cloud\CloudPublicNetworkBundleConstraint;
|
|
use App\Services\Cloud\CloudPublicNetworkAddressPolicy;
|
|
use App\Services\Cloud\CloudPublicNetworkCandidateCatalog;
|
|
|
|
$assert = static function (bool $condition, string $message): void {
|
|
if (!$condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
};
|
|
$available = static function (int $id, object $pool, string $address): object {
|
|
return (object) [
|
|
'id' => $id,
|
|
'host_id' => (int) $pool->host_id,
|
|
'public_ip_pool_id' => (int) $pool->id,
|
|
'address_family' => (int) $pool->address_family,
|
|
'address' => $address,
|
|
'address_binary' => inet_pton($address),
|
|
'prefix_length' => (int) $pool->guest_prefix_length,
|
|
'state' => 'available',
|
|
'reservation_id' => null,
|
|
'reservation_ip_claim_id' => null,
|
|
'instance_id' => null,
|
|
'operation_id' => null,
|
|
];
|
|
};
|
|
$pool = static function (int $id, int $attachmentId, int $family,
|
|
int $priority): object {
|
|
$v6 = $family === 6;
|
|
return (object) [
|
|
'id' => $id,
|
|
'pool_key' => str_repeat(dechex($id % 16), 32),
|
|
'host_id' => 1,
|
|
'network_attachment_id' => $attachmentId,
|
|
'address_family' => $family,
|
|
'cidr' => $v6 ? "2404:c140:156:$id::/64" : "8.8.$id.0/24",
|
|
'gateway' => $v6 ? "2404:c140:156:$id::1" : "8.8.$id.1",
|
|
'gateway_on_link' => 0,
|
|
'guest_prefix_length' => $v6 ? 64 : 24,
|
|
'dns_servers_json' => $v6
|
|
? '["2606:4700:4700::1111"]' : '["1.1.1.1"]',
|
|
'reserved_ips_json' => '[]',
|
|
'allocation_start' => $v6 ? "2404:c140:156:$id::10" : null,
|
|
'allocation_end' => $v6 ? "2404:c140:156:$id::1f" : null,
|
|
'allocation_cursor' => $v6 ? "2404:c140:156:$id::10" : null,
|
|
'max_allocations' => $v6 ? 4 : 0,
|
|
'priority' => $priority,
|
|
'desired_revision' => 1,
|
|
'observed_revision' => 1,
|
|
];
|
|
};
|
|
|
|
$attachment1 = (object) [
|
|
'id' => 10,
|
|
'host_id' => 1,
|
|
'line_id' => 1,
|
|
'attachment_key' => str_repeat('a', 32),
|
|
'network_selector' => 'bridge:br0',
|
|
'config_revision' => 1,
|
|
'verified_agent_revision' => 1,
|
|
'priority' => 5,
|
|
];
|
|
$attachment2 = (object) [
|
|
'id' => 20,
|
|
'host_id' => 1,
|
|
'line_id' => 1,
|
|
'attachment_key' => str_repeat('b', 32),
|
|
'network_selector' => 'bridge:br1',
|
|
'config_revision' => 1,
|
|
'verified_agent_revision' => 1,
|
|
'priority' => 1,
|
|
];
|
|
$v4a = $pool(11, 10, 4, 0);
|
|
$v6a = $pool(12, 10, 6, 0);
|
|
$v4b = $pool(13, 20, 4, 0);
|
|
$v6b = $pool(14, 20, 6, 0);
|
|
$contracts = [];
|
|
foreach ([$v4a, $v6a, $v4b, $v6b] as $candidatePool) {
|
|
$contracts[(int) $candidatePool->id] =
|
|
CloudPublicNetworkAddressPolicy::contract($candidatePool);
|
|
}
|
|
$v4aRow = $available(101, $v4a, '8.8.11.10');
|
|
$v4bRow = $available(102, $v4b, '8.8.13.10');
|
|
$rows = [
|
|
11 => [$v4aRow],
|
|
12 => [],
|
|
13 => [$v4bRow],
|
|
14 => [],
|
|
];
|
|
$catalog = CloudPublicNetworkCandidateCatalog::fromLocked(
|
|
[10 => $attachment1, 20 => $attachment2],
|
|
[10 => [$v4a, $v6a], 20 => [$v4b, $v6b]],
|
|
$contracts,
|
|
$rows,
|
|
null
|
|
);
|
|
|
|
$allocation = $catalog->planAllocation();
|
|
$assert($allocation !== null
|
|
&& (int) $allocation->attachment()->id === 20
|
|
&& (int) $allocation->pools()[4]->id === 13
|
|
&& (int) $allocation->pools()[6]->id === 14
|
|
&& (int) $allocation->candidates()[4]['row']->id === 102
|
|
&& $allocation->candidates()[6]['candidate']->text()
|
|
=== '2404:c140:156:14::10',
|
|
'Allocation did not preserve attachment/pool priority and sparse proposal.');
|
|
$selection = $allocation->allocationSelection();
|
|
$assert(array_keys($selection) === [
|
|
'attachment',
|
|
'pools',
|
|
'contracts',
|
|
'address_candidates',
|
|
]
|
|
&& $selection['attachment'] === $allocation->attachment()
|
|
&& $selection['pools'] === $allocation->pools()
|
|
&& $selection['contracts'] === [
|
|
4 => $contracts[13],
|
|
6 => $contracts[14],
|
|
]
|
|
&& $selection['address_candidates'] === $allocation->candidates(),
|
|
'Allocation selection contract no longer preserves internal contracts.');
|
|
$excluded = [
|
|
bin2hex($allocation->candidates()[6]['candidate']->binary()) => true,
|
|
];
|
|
$replanned = $catalog->planAllocation(null, $excluded);
|
|
$assert($replanned !== null
|
|
&& $replanned->candidates()[6]['candidate']->text()
|
|
=== '2404:c140:156:14::11',
|
|
'Global collision exclusion did not advance the pure sparse proposal.');
|
|
|
|
$constraint = CloudPublicNetworkBundleConstraint::fromArray([
|
|
'host_id' => 1,
|
|
'line_id' => 1,
|
|
'network_attachment_id' => 10,
|
|
'attachment_key' => (string) $attachment1->attachment_key,
|
|
'network_selector' => (string) $attachment1->network_selector,
|
|
'attachment_config_revision' => 1,
|
|
'attachment_verified_agent_revision' => 1,
|
|
'ipv4_pool' => [
|
|
'public_ip_pool_id' => 11,
|
|
'pool_key' => (string) $v4a->pool_key,
|
|
'network_attachment_id' => 10,
|
|
'address_family' => 4,
|
|
'desired_revision' => 1,
|
|
'observed_revision' => 1,
|
|
],
|
|
'ipv6_pool' => [
|
|
'public_ip_pool_id' => 12,
|
|
'pool_key' => (string) $v6a->pool_key,
|
|
'network_attachment_id' => 10,
|
|
'address_family' => 6,
|
|
'desired_revision' => 1,
|
|
'observed_revision' => 1,
|
|
],
|
|
]);
|
|
$constrained = $catalog->planAllocation($constraint);
|
|
$assert($constrained !== null
|
|
&& (int) $constrained->attachment()->id === 10
|
|
&& (int) $constrained->pools()[4]->id === 11
|
|
&& (int) $constrained->pools()[6]->id === 12,
|
|
'Exact allocation constraint fell back to a higher-priority bundle.');
|
|
$occupiedV6 = [];
|
|
foreach (range(1, 4) as $offset) {
|
|
$row = $available(
|
|
300 + $offset,
|
|
$v6a,
|
|
'2404:c140:156:12::' . dechex(15 + $offset)
|
|
);
|
|
$row->state = 'assigned';
|
|
$occupiedV6[] = $row;
|
|
}
|
|
$fullCatalog = CloudPublicNetworkCandidateCatalog::fromLocked(
|
|
[10 => $attachment1, 20 => $attachment2],
|
|
[10 => [$v4a, $v6a], 20 => [$v4b, $v6b]],
|
|
$contracts,
|
|
[
|
|
11 => [$v4aRow],
|
|
12 => $occupiedV6,
|
|
13 => [$v4bRow],
|
|
14 => [],
|
|
],
|
|
null
|
|
);
|
|
$assert($fullCatalog->planAllocation($constraint) === null
|
|
&& (int) $fullCatalog->planAllocation()->attachment()->id === 20,
|
|
'Sparse-capacity exhaustion allowed an exact bundle fallback.');
|
|
|
|
$sourceV4 = clone $v4aRow;
|
|
$sourceV4->id = 201;
|
|
$sourceV4->state = 'assigned';
|
|
$sourceV4->reservation_id = 1;
|
|
$sourceV4->reservation_ip_claim_id = 1;
|
|
$sourceV4->instance_id = 1;
|
|
$sourceV4->operation_id = str_repeat('a', 32);
|
|
$sourceV6 = $available(202, $v6a, '2404:c140:156:12::12');
|
|
$sourceV6->state = 'assigned';
|
|
$sourceV6->reservation_id = 1;
|
|
$sourceV6->reservation_ip_claim_id = 2;
|
|
$sourceV6->instance_id = 1;
|
|
$sourceV6->operation_id = str_repeat('a', 32);
|
|
$tornV4 = $available(203, $v4a, '8.8.11.19');
|
|
$tornV4->address_binary = inet_pton('8.8.11.18');
|
|
$replacementV4 = $available(204, $v4a, '8.8.11.20');
|
|
$tornV6 = $available(205, $v6a, '2404:c140:156:12::19');
|
|
$tornV6->address_binary = inet_pton('2404:c140:156:12::17');
|
|
$transitionCatalog = CloudPublicNetworkCandidateCatalog::fromLocked(
|
|
[10 => $attachment1, 20 => $attachment2],
|
|
[10 => [$v4a, $v6a], 20 => [$v4b, $v6b]],
|
|
$contracts,
|
|
[
|
|
11 => [$sourceV4, $tornV4, $replacementV4],
|
|
12 => [$sourceV6, $tornV6],
|
|
13 => [$v4bRow],
|
|
14 => [],
|
|
],
|
|
null
|
|
);
|
|
$automatic = $transitionCatalog->planTransition(
|
|
[4 => null],
|
|
10,
|
|
[4 => $sourceV4, 6 => $sourceV6]
|
|
);
|
|
$assert((int) $automatic->attachment()->id === 10
|
|
&& (int) $automatic->candidates()[4]['row']->id === 204
|
|
&& $automatic->changedFamilies() === [4 => true],
|
|
'Automatic transition did not skip a torn row for the next valid row.');
|
|
$exact = $transitionCatalog->planTransition(
|
|
[6 => [
|
|
'pool_key' => (string) $v6a->pool_key,
|
|
'address_cidr' => '2404:c140:156:12::18/64',
|
|
]],
|
|
10,
|
|
[4 => $sourceV4, 6 => $sourceV6]
|
|
);
|
|
$assert($exact->candidates()[6]['row'] === null
|
|
&& $exact->candidates()[6]['candidate']->text()
|
|
=== '2404:c140:156:12::18',
|
|
'Exact IPv6 transition did not return a pure sparse proposal.');
|
|
$tornExactRejected = false;
|
|
try {
|
|
$transitionCatalog->planTransition(
|
|
[6 => [
|
|
'pool_key' => (string) $v6a->pool_key,
|
|
'address_cidr' => '2404:c140:156:12::19/64',
|
|
]],
|
|
10,
|
|
[4 => $sourceV4, 6 => $sourceV6]
|
|
);
|
|
} catch (CloudError $error) {
|
|
$tornExactRejected = $error->httpStatus === 409;
|
|
}
|
|
$assert($tornExactRejected,
|
|
'Exact IPv6 target duplicated an existing torn identity.');
|
|
|
|
$crossRejected = false;
|
|
try {
|
|
$transitionCatalog->planTransition(
|
|
[4 => [
|
|
'pool_key' => (string) $v4b->pool_key,
|
|
'address_cidr' => '8.8.13.10/24',
|
|
]],
|
|
10,
|
|
[4 => $sourceV4, 6 => $sourceV6]
|
|
);
|
|
} catch (CloudError $error) {
|
|
$crossRejected = $error->httpStatus === 409;
|
|
}
|
|
$assert($crossRejected,
|
|
'A single-family cross-attachment transition was accepted.');
|
|
|
|
$source = file_get_contents(dirname(__DIR__)
|
|
. '/app/Services/Cloud/CloudPublicNetworkCandidateCatalog.php');
|
|
$planSource = file_get_contents(dirname(__DIR__)
|
|
. '/app/Services/Cloud/CloudPublicNetworkCandidatePlan.php');
|
|
$assert(is_string($source)
|
|
&& is_string($planSource)
|
|
&& !str_contains($source, 'lockForUpdate(')
|
|
&& !str_contains($source, 'DB::')
|
|
&& !str_contains($source, '::find(')
|
|
&& !str_contains($source, '->save(')
|
|
&& !str_contains($source, '->insert(')
|
|
&& !str_contains($source, '->update(')
|
|
&& !str_contains($planSource, 'DB::')
|
|
&& !str_contains($planSource, 'lockForUpdate(')
|
|
&& (new ReflectionClass(
|
|
App\Services\Cloud\CloudPublicNetworkCandidatePlan::class
|
|
))->isReadOnly(),
|
|
'Candidate catalog contains persistence or lock operations.');
|
|
|
|
echo "Public-network candidate catalog tests passed.\n";
|