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

384 lines
13 KiB
PHP

<?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('reservation_ip_claim_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('claim_generation')->default(1);
$table->integer('active_claim')->nullable()->default(1);
$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', 'active_claim']);
$table->unique(['reservation_id', 'claim_generation', '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";