feat: provision dual-stack public networks
parent
cb8260ab56
commit
c0ee66f369
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Cloud;
|
||||
|
||||
/**
|
||||
* The selected host has no complete allocatable IPv4 + IPv6 pair.
|
||||
*
|
||||
* Placement may continue with the next host only for this exact outcome.
|
||||
* Invalid pool configuration and inconsistent address ledgers remain hard
|
||||
* failures and must never be converted into scheduler fallback.
|
||||
*/
|
||||
final class CloudPublicNetworkCapacityUnavailable extends CloudError
|
||||
{
|
||||
}
|
||||
@ -0,0 +1,251 @@
|
||||
<?php
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
use App\Services\Cloud\CloudPlacementNetworkService;
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
$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');
|
||||
$table->string('default_network')->default('bridge:br0');
|
||||
$table->string('endpoint')->default('https://host.invalid');
|
||||
});
|
||||
$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');
|
||||
$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->integer('max_allocations');
|
||||
$table->string('state');
|
||||
$table->integer('priority');
|
||||
$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->integer('prefix_length');
|
||||
$table->string('state');
|
||||
$table->integer('reservation_id')->nullable();
|
||||
$table->integer('instance_id')->nullable();
|
||||
$table->string('operation_id')->nullable();
|
||||
});
|
||||
|
||||
Capsule::table('cloud_hosts')->insert(['id' => 1]);
|
||||
Capsule::table('cloud_host_agents')->insert([
|
||||
'host_id' => 1,
|
||||
'enabled' => 1,
|
||||
'revision' => 5,
|
||||
'tested_revision' => 5,
|
||||
'last_test_status' => 'healthy',
|
||||
'network_selectors_json' => '["bridge:br0","bridge:br1"]',
|
||||
'public_network_capabilities_json' => json_encode([
|
||||
'contract' => 1,
|
||||
'dual_stack_required' => true,
|
||||
'project_owned_filter' => true,
|
||||
'ndp_guard' => true,
|
||||
], JSON_THROW_ON_ERROR),
|
||||
]);
|
||||
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' => 5,
|
||||
'last_verified_at' => time(),
|
||||
],
|
||||
[
|
||||
'id' => 11,
|
||||
'host_id' => 1,
|
||||
'line_id' => 7,
|
||||
'network_selector' => 'bridge:br1',
|
||||
'state' => 'active',
|
||||
'priority' => 1,
|
||||
'verification_state' => 'verified',
|
||||
'verified_agent_revision' => 5,
|
||||
'last_verified_at' => time(),
|
||||
],
|
||||
]);
|
||||
$common = [
|
||||
'host_id' => 1,
|
||||
'state' => 'active',
|
||||
'priority' => 0,
|
||||
'desired_revision' => 1,
|
||||
'observed_revision' => 1,
|
||||
'sync_state' => 'applied',
|
||||
'reserved_ips_json' => '[]',
|
||||
];
|
||||
Capsule::table('cloud_public_ip_pools')->insert([
|
||||
array_merge($common, [
|
||||
'id' => 100,
|
||||
'pool_key' => str_repeat('a', 32),
|
||||
'network_attachment_id' => 10,
|
||||
'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"]',
|
||||
'allocation_start' => null,
|
||||
'allocation_end' => null,
|
||||
'max_allocations' => 0,
|
||||
]),
|
||||
array_merge($common, [
|
||||
'id' => 200,
|
||||
'pool_key' => str_repeat('b', 32),
|
||||
'network_attachment_id' => 11,
|
||||
'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"]',
|
||||
'allocation_start' => '2404:c140:156:3a::10',
|
||||
'allocation_end' => '2404:c140:156:3a::ff',
|
||||
'max_allocations' => 2,
|
||||
]),
|
||||
]);
|
||||
Capsule::table('cloud_public_ips')->insert([
|
||||
'id' => 1,
|
||||
'host_id' => 1,
|
||||
'public_ip_pool_id' => 100,
|
||||
'address_family' => 4,
|
||||
'address' => '8.8.8.10',
|
||||
'prefix_length' => 24,
|
||||
'state' => 'available',
|
||||
]);
|
||||
$candidate = collect([
|
||||
(object) ['id' => 1, 'resource_pool_line_id' => 7],
|
||||
]);
|
||||
|
||||
$assert(CloudPlacementNetworkService::advisoryDualStackAvailableHosts(
|
||||
$candidate
|
||||
) === [],
|
||||
'Pools split across two attachments were advertised as one dual-stack path.');
|
||||
Capsule::table('cloud_public_ip_pools')->where('id', 200)->update([
|
||||
'network_attachment_id' => 10,
|
||||
]);
|
||||
$connection = Capsule::connection('default');
|
||||
$connection->flushQueryLog();
|
||||
$connection->enableQueryLog();
|
||||
$available = CloudPlacementNetworkService::advisoryDualStackAvailableHosts(
|
||||
$candidate
|
||||
);
|
||||
$queryCount = count($connection->getQueryLog());
|
||||
$connection->disableQueryLog();
|
||||
$assert($available === [1 => true] && $queryCount <= 5,
|
||||
'A sparse IPv6 pool with capacity was not paired with available IPv4.');
|
||||
Capsule::table('cloud_host_agents')->where('host_id', 1)->update([
|
||||
'public_network_capabilities_json' => '{}',
|
||||
]);
|
||||
$assert(CloudPlacementNetworkService::advisoryDualStackAvailableHosts(
|
||||
$candidate
|
||||
) === [],
|
||||
'A host without current guarded-network capability remained sellable.');
|
||||
Capsule::table('cloud_host_agents')->where('host_id', 1)->update([
|
||||
'public_network_capabilities_json' => json_encode([
|
||||
'contract' => 1,
|
||||
'dual_stack_required' => true,
|
||||
'project_owned_filter' => true,
|
||||
'ndp_guard' => true,
|
||||
], JSON_THROW_ON_ERROR),
|
||||
]);
|
||||
Capsule::table('cloud_public_ip_pools')->where('id', 100)->update([
|
||||
'observed_revision' => 2,
|
||||
]);
|
||||
$assert(CloudPlacementNetworkService::advisoryDualStackAvailableHosts(
|
||||
$candidate
|
||||
) === [],
|
||||
'A pool ahead of its desired revision remained visible as sellable.');
|
||||
Capsule::table('cloud_public_ip_pools')->where('id', 100)->update([
|
||||
'observed_revision' => 1,
|
||||
]);
|
||||
Capsule::table('cloud_public_ips')->insert([
|
||||
[
|
||||
'id' => 2,
|
||||
'host_id' => 1,
|
||||
'public_ip_pool_id' => 200,
|
||||
'address_family' => 6,
|
||||
'address' => '2404:c140:156:3a::10',
|
||||
'prefix_length' => 64,
|
||||
'state' => 'assigned',
|
||||
'reservation_id' => null,
|
||||
'instance_id' => 90,
|
||||
'operation_id' => null,
|
||||
],
|
||||
[
|
||||
'id' => 3,
|
||||
'host_id' => 1,
|
||||
'public_ip_pool_id' => 200,
|
||||
'address_family' => 6,
|
||||
'address' => '2404:c140:156:3a::11',
|
||||
'prefix_length' => 64,
|
||||
'state' => 'changing',
|
||||
'reservation_id' => 91,
|
||||
'instance_id' => null,
|
||||
'operation_id' => str_repeat('c', 32),
|
||||
],
|
||||
]);
|
||||
$assert(CloudPlacementNetworkService::advisoryDualStackAvailableHosts(
|
||||
$candidate
|
||||
) === [],
|
||||
'A changing sparse IPv6 address was omitted from provisioning capacity.');
|
||||
|
||||
echo "Dual-stack placement advisory tests passed.\n";
|
||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
$root = dirname(__DIR__);
|
||||
$read = static function (string $path) use ($root): string {
|
||||
$contents = file_get_contents($root . '/' . $path);
|
||||
if (!is_string($contents)) {
|
||||
throw new RuntimeException('Unreadable contract source: ' . $path);
|
||||
}
|
||||
return $contents;
|
||||
};
|
||||
$assert = static function (bool $condition, string $message): void {
|
||||
if (!$condition) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
};
|
||||
|
||||
$order = $read('app/Services/Cloud/CloudOrderService.php');
|
||||
$placement = $read('app/Services/Cloud/CloudPlacementService.php');
|
||||
$worker = $read('app/Services/Cloud/CloudWorkerService.php');
|
||||
$invoice = $read('app/Services/Cloud/CloudInvoiceService.php');
|
||||
$recovery = $read('app/Services/Cloud/CloudOperationRecoveryIdentity.php');
|
||||
$reservation = $read('app/Models/CloudResourceReservation.php');
|
||||
|
||||
$assert(!str_contains($order, "'public_pool_key' => ''")
|
||||
&& !str_contains($order, "'public_pool_cidr' => ''")
|
||||
&& !str_contains($order, "'public_ip_cidr' => ''")
|
||||
&& !str_contains($order, "'gateway' => ''"),
|
||||
'Unpaid orders still define the retired scalar public-network contract.');
|
||||
$snapshotGate = strpos($order,
|
||||
"if (\$source === 'deployment_snapshot') {");
|
||||
$purchaseTransaction = strpos($order,
|
||||
"return (new User())->getConnection()->transaction(");
|
||||
$assert($snapshotGate !== false && $purchaseTransaction !== false
|
||||
&& $snapshotGate < $purchaseTransaction
|
||||
&& str_contains(substr($order, $snapshotGate,
|
||||
$purchaseTransaction - $snapshotGate),
|
||||
'云快照创建实例正在升级双栈网络,暂不可用'),
|
||||
'Deferred cloud-snapshot provisioning is not blocked before billing state is created.');
|
||||
$paymentStart = strpos($order,
|
||||
'public static function queuePaidProvision(');
|
||||
$paymentPlacement = strpos($order,
|
||||
'CloudPlacementService::reserve(',
|
||||
$paymentStart === false ? 0 : $paymentStart);
|
||||
$paymentGate = $paymentStart === false || $paymentPlacement === false
|
||||
? '' : substr($order, $paymentStart,
|
||||
$paymentPlacement - $paymentStart);
|
||||
$assert(str_contains($paymentGate,
|
||||
"if (\$source === 'deployment_snapshot')")
|
||||
&& str_contains($paymentGate,
|
||||
'云快照创建实例正在升级双栈网络,暂不可用'),
|
||||
'A pre-existing unpaid cloud-snapshot order can cross the payment boundary.');
|
||||
$compactOrder = preg_replace('/\s+/', ' ', $order) ?? $order;
|
||||
$assert(str_contains($compactOrder,
|
||||
"\$orderSpec['public_network'] = \$placement['public_network']->toArray();")
|
||||
&& str_contains($order,
|
||||
"\$placement['payload']['body']['public_network']"),
|
||||
'Paid order and dispatched job do not freeze the same public-network snapshot.');
|
||||
|
||||
foreach (['reserveBundle(', 'commitBundle(', 'fenceBundle(',
|
||||
'releaseBundle(', 'releaseCommittedBundle('] as $call) {
|
||||
$assert(preg_match(
|
||||
'/CloudPublicNetworkAllocationService::\s*'
|
||||
. preg_quote($call, '/') . '/',
|
||||
$placement
|
||||
) === 1,
|
||||
'Provisioning placement does not use bundle operation: ' . $call);
|
||||
}
|
||||
$assert(str_contains($placement,
|
||||
"\$payload['body']['public_network'] = \$publicNetwork->toArray();")
|
||||
&& str_contains($placement,
|
||||
"'public_network' => \$publicNetwork"),
|
||||
'Placement does not return and dispatch one immutable dual-stack snapshot.');
|
||||
$assert(!str_contains($reservation,
|
||||
"foreach (['network_attachment_id', 'public_ip_pool_id',")
|
||||
&& str_contains($reservation, 'cloud_resource_reservation_ips'),
|
||||
'Resource reservations still treat one public_ip_id as network authority.');
|
||||
|
||||
$compactWorker = preg_replace('/\s+/', ' ', $worker) ?? $worker;
|
||||
$workerFinalizationStart = strpos($worker,
|
||||
'$provisionInvoice = CloudInvoice::where(');
|
||||
$workerCommit = strpos($worker, 'CloudPlacementService::commit(',
|
||||
$workerFinalizationStart === false ? 0 : $workerFinalizationStart);
|
||||
$workerPreCommit = $workerFinalizationStart === false || $workerCommit === false
|
||||
? '' : substr($worker, $workerFinalizationStart,
|
||||
$workerCommit - $workerFinalizationStart);
|
||||
$assert($workerPreCommit !== ''
|
||||
&& !str_contains($workerPreCommit, 'CloudResourceReservation::where('),
|
||||
'Create success locks the reservation before canonical placement commit.');
|
||||
$tryHostStart = strpos($placement, 'private static function tryHost(');
|
||||
$allocatorCall = strpos(
|
||||
$placement,
|
||||
'CloudPublicNetworkAllocationService::reserveBundle(',
|
||||
$tryHostStart === false ? 0 : $tryHostStart
|
||||
);
|
||||
$preAllocator = $tryHostStart === false || $allocatorCall === false
|
||||
? '' : substr($placement, $tryHostStart,
|
||||
$allocatorCall - $tryHostStart);
|
||||
$assert($preAllocator !== ''
|
||||
&& !str_contains($preAllocator, 'lockedCandidates(')
|
||||
&& !str_contains($preAllocator, 'hasCompleteDualStackCandidate('),
|
||||
'tryHost can bypass the canonical allocator through a stale precheck.');
|
||||
$assert(substr_count($worker,
|
||||
'CloudPublicNetworkSnapshot::fromArray(') >= 2
|
||||
&& str_contains($compactWorker,
|
||||
"\$instanceSpec['public_network'] = \$confirmedPublicNetwork->toArray();")
|
||||
&& str_contains($worker,
|
||||
"\$requestedPublicNetwork->fingerprint()")
|
||||
&& str_contains($worker,
|
||||
"\$confirmedPublicNetwork->fingerprint()"),
|
||||
'Create finalization does not compare and persist the exact dual-stack identity.');
|
||||
$assert(str_contains($invoice, 'CloudPublicNetworkSnapshot::fromArray(')
|
||||
&& str_contains($invoice, "\$spec['public_network']")
|
||||
&& !str_contains($invoice, "\$spec['public_ip_cidr']"),
|
||||
'Provision-success mail still treats scalar public_ip_cidr as authoritative.');
|
||||
$assert(substr_count($recovery,
|
||||
'CloudPublicNetworkSnapshot::fromArray(') >= 2
|
||||
&& str_contains($recovery, "\$orderSpec['public_network']")
|
||||
&& str_contains($recovery, "\$body['public_network']"),
|
||||
'Create recovery identity is not bound to the frozen dual-stack snapshot.');
|
||||
|
||||
echo "Dual-stack provisioning call-chain contract passed.\n";
|
||||
@ -0,0 +1,379 @@
|
||||
<?php
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
use App\Models\CloudInstance;
|
||||
use App\Models\CloudOrder;
|
||||
use App\Services\Cloud\CloudPlacementService;
|
||||
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_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_orders', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->integer('user_id');
|
||||
$table->integer('offer_id');
|
||||
$table->integer('resource_pool_id');
|
||||
$table->integer('host_id');
|
||||
$table->integer('reservation_id');
|
||||
});
|
||||
$schema->create('cloud_resource_reservations', static function (
|
||||
Blueprint $table
|
||||
): void {
|
||||
$table->increments('id');
|
||||
$table->string('reservation_key');
|
||||
$table->string('operation_id');
|
||||
$table->integer('order_id')->nullable();
|
||||
$table->integer('backup_restore_id')->nullable();
|
||||
$table->integer('offer_id')->nullable();
|
||||
$table->integer('resource_pool_id');
|
||||
$table->integer('host_id');
|
||||
$table->integer('user_id');
|
||||
$table->integer('instance_id')->nullable();
|
||||
$table->integer('network_attachment_id')->nullable();
|
||||
$table->integer('public_ip_pool_id')->nullable();
|
||||
$table->integer('public_ip_id')->nullable();
|
||||
$table->integer('cpu_cores');
|
||||
$table->integer('memory_mib');
|
||||
$table->integer('disk_gib');
|
||||
$table->string('state');
|
||||
$table->integer('expires_at')->nullable();
|
||||
$table->integer('released_at')->nullable();
|
||||
$table->string('release_reason')->default('');
|
||||
$table->integer('created_at');
|
||||
$table->integer('updated_at');
|
||||
});
|
||||
$schema->create('cloud_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']);
|
||||
});
|
||||
$schema->create('cloud_instances', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->integer('host_id');
|
||||
$table->integer('order_id')->nullable();
|
||||
$table->integer('resource_pool_id');
|
||||
$table->integer('reservation_id');
|
||||
$table->string('status');
|
||||
});
|
||||
|
||||
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),
|
||||
]);
|
||||
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(),
|
||||
]);
|
||||
$poolCommon = [
|
||||
'host_id' => 1,
|
||||
'network_attachment_id' => 10,
|
||||
'state' => 'active',
|
||||
'priority' => 0,
|
||||
'desired_revision' => 1,
|
||||
'observed_revision' => 1,
|
||||
'sync_state' => 'applied',
|
||||
'reserved_ips_json' => '[]',
|
||||
];
|
||||
Capsule::table('cloud_public_ip_pools')->insert([
|
||||
array_merge($poolCommon, [
|
||||
'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"]',
|
||||
'allocation_start' => null,
|
||||
'allocation_end' => null,
|
||||
'allocation_cursor' => null,
|
||||
'max_allocations' => 0,
|
||||
]),
|
||||
array_merge($poolCommon, [
|
||||
'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"]',
|
||||
'allocation_start' => '2404:c140:156:3a::10',
|
||||
'allocation_end' => '2404:c140:156:3a::ff',
|
||||
'allocation_cursor' => null,
|
||||
'max_allocations' => 8,
|
||||
]),
|
||||
]);
|
||||
$now = time();
|
||||
foreach (range(10, 13) 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,
|
||||
]);
|
||||
}
|
||||
|
||||
$reserve = static function (int $id, string $operationId): array {
|
||||
Capsule::table('cloud_orders')->insert([
|
||||
'id' => $id,
|
||||
'user_id' => 40,
|
||||
'offer_id' => 50,
|
||||
'resource_pool_id' => 60,
|
||||
'host_id' => 1,
|
||||
'reservation_id' => 1000 + $id,
|
||||
]);
|
||||
Capsule::table('cloud_resource_reservations')->insert([
|
||||
'id' => 1000 + $id,
|
||||
'reservation_key' => str_repeat((string) ($id % 10), 32),
|
||||
'operation_id' => $operationId,
|
||||
'order_id' => $id,
|
||||
'backup_restore_id' => null,
|
||||
'offer_id' => 50,
|
||||
'resource_pool_id' => 60,
|
||||
'host_id' => 1,
|
||||
'user_id' => 40,
|
||||
'instance_id' => null,
|
||||
'network_attachment_id' => null,
|
||||
'public_ip_pool_id' => null,
|
||||
'public_ip_id' => null,
|
||||
'cpu_cores' => 2,
|
||||
'memory_mib' => 2048,
|
||||
'disk_gib' => 20,
|
||||
'state' => 'held',
|
||||
'expires_at' => null,
|
||||
'released_at' => null,
|
||||
'release_reason' => '',
|
||||
'created_at' => time(),
|
||||
'updated_at' => time(),
|
||||
]);
|
||||
return [
|
||||
CloudOrder::findOrFail($id),
|
||||
CloudPublicNetworkAllocationService::reserveBundle(
|
||||
1,
|
||||
7,
|
||||
1000 + $id,
|
||||
$operationId
|
||||
),
|
||||
];
|
||||
};
|
||||
|
||||
[$committedOrder, $committedSnapshot] = $reserve(1, str_repeat('1', 32));
|
||||
CloudPlacementService::commit(
|
||||
$committedOrder,
|
||||
900,
|
||||
$committedSnapshot->toArray()
|
||||
);
|
||||
$assert(Capsule::table('cloud_resource_reservations')->where('id', 1001)
|
||||
->value('state') === 'committed'
|
||||
&& Capsule::table('cloud_public_ips')->where('instance_id', 900)
|
||||
->where('state', 'assigned')->count() === 2,
|
||||
'Provisioning commit did not bind the complete public-network bundle.');
|
||||
CloudPlacementService::markUncertain($committedOrder->fresh());
|
||||
$assert(Capsule::table('cloud_resource_reservations')->where('id', 1001)
|
||||
->value('state') === 'committed'
|
||||
&& Capsule::table('cloud_public_ips')->where('instance_id', 900)
|
||||
->where('state', 'assigned')->count() === 2,
|
||||
'A late uncertainty marker rolled back a committed network bundle.');
|
||||
|
||||
[$failedOrder] = $reserve(2, str_repeat('2', 32));
|
||||
CloudPlacementService::markUncertain($failedOrder);
|
||||
$assert(Capsule::table('cloud_resource_reservations')->where('id', 1002)
|
||||
->value('state') === 'uncertain'
|
||||
&& Capsule::table('cloud_public_ips')->where('reservation_id', 1002)
|
||||
->where('state', 'uncertain')->count() === 2,
|
||||
'Unknown effect did not fence the complete provisioning bundle.');
|
||||
CloudPlacementService::release($failedOrder, 'confirmed-not-applied');
|
||||
$assert(Capsule::table('cloud_resource_reservations')->where('id', 1002)
|
||||
->value('state') === 'released'
|
||||
&& Capsule::table('cloud_resource_reservation_ips')
|
||||
->where('reservation_id', 1002)->where('state', 'released')->count() === 2
|
||||
&& Capsule::table('cloud_public_ips')->where('reservation_id', 1002)
|
||||
->count() === 0,
|
||||
'Confirmed non-application did not release both address families.');
|
||||
|
||||
[$recoveredOrder, $recoveredSnapshot] = $reserve(3, str_repeat('3', 32));
|
||||
CloudPlacementService::markUncertain($recoveredOrder);
|
||||
CloudPlacementService::commit(
|
||||
$recoveredOrder,
|
||||
901,
|
||||
$recoveredSnapshot->toArray()
|
||||
);
|
||||
$assert(Capsule::table('cloud_resource_reservations')->where('id', 1003)
|
||||
->value('state') === 'committed'
|
||||
&& Capsule::table('cloud_public_ips')->where('instance_id', 901)
|
||||
->where('state', 'assigned')->count() === 2,
|
||||
'A recovered success did not commit its previously fenced bundle.');
|
||||
|
||||
[$mismatchOrder, $mismatchSnapshot] = $reserve(4, str_repeat('4', 32));
|
||||
$wrongSnapshot = $mismatchSnapshot->toArray();
|
||||
$wrongSnapshot['ipv4']['address_cidr'] = '8.8.8.99/24';
|
||||
$rejects(static fn () => CloudPlacementService::commit(
|
||||
$mismatchOrder,
|
||||
902,
|
||||
$wrongSnapshot
|
||||
), 'A create receipt committed a different dual-stack identity.');
|
||||
$assert(Capsule::table('cloud_resource_reservations')->where('id', 1004)
|
||||
->value('state') === 'held'
|
||||
&& Capsule::table('cloud_public_ips')->where('reservation_id', 1004)
|
||||
->where('state', 'reserved')->count() === 2,
|
||||
'Rejecting a mismatched receipt partially committed the bundle.');
|
||||
|
||||
Capsule::table('cloud_instances')->insert([
|
||||
'id' => 900,
|
||||
'host_id' => 1,
|
||||
'order_id' => 1,
|
||||
'resource_pool_id' => 60,
|
||||
'reservation_id' => 1001,
|
||||
'status' => 'deleted',
|
||||
]);
|
||||
CloudPlacementService::releaseDeletedInstance(
|
||||
CloudInstance::findOrFail(900),
|
||||
'instance_deleted'
|
||||
);
|
||||
$assert(Capsule::table('cloud_resource_reservations')->where('id', 1001)
|
||||
->value('state') === 'released'
|
||||
&& Capsule::table('cloud_resource_reservation_ips')
|
||||
->where('reservation_id', 1001)->where('state', 'released')->count() === 2
|
||||
&& Capsule::table('cloud_public_ips')->where('instance_id', 900)
|
||||
->count() === 0,
|
||||
'Confirmed instance deletion did not release its committed network bundle.');
|
||||
|
||||
echo "Provisioning dual-stack public-network ledger tests passed.\n";
|
||||
Loading…
Reference in New Issue