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

364 lines
14 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudAdminResourceService;
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);
}
};
$capsule = new Capsule();
$capsule->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
], 'default');
$capsule->setAsGlobal();
$capsule->bootEloquent();
$schema = $capsule->schema('default');
$schema->create('cloud_regions', static function (Blueprint $table): void {
$table->increments('id');
$table->string('name');
});
$schema->create('cloud_network_lines', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('region_id');
$table->string('name');
});
$schema->create('cloud_resource_pools', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('line_id');
$table->string('name');
});
$schema->create('cloud_offers', static function (Blueprint $table): void {
$table->increments('id');
$table->string('name');
});
$schema->create('cloud_hosts', static function (Blueprint $table): void {
$table->increments('id');
$table->string('name');
});
$schema->create('cloud_instances', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('reservation_id');
$table->integer('host_id');
$table->integer('resource_pool_id');
$table->integer('user_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');
});
$schema->create('cloud_public_ip_pools', static function (
Blueprint $table
): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('network_attachment_id');
$table->integer('address_family');
$table->string('pool_key');
$table->string('cidr');
$table->string('gateway');
$table->boolean('gateway_on_link');
$table->integer('guest_prefix_length');
$table->text('dns_servers_json');
});
$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();
});
$schema->create('cloud_resource_reservations', static function (
Blueprint $table
): void {
$table->increments('id');
$table->string('reservation_key');
$table->integer('order_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->string('operation_id');
$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');
$table->integer('created_at');
$table->integer('updated_at');
});
$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');
$table->integer('active_claim')->nullable();
$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('released_at')->nullable();
$table->string('release_reason');
$table->integer('created_at');
$table->integer('updated_at');
});
Capsule::table('cloud_regions')->insert(['id' => 1, 'name' => '香港']);
Capsule::table('cloud_network_lines')->insert([
'id' => 2, 'region_id' => 1, 'name' => '国际线路',
]);
Capsule::table('cloud_resource_pools')->insert([
'id' => 3, 'line_id' => 2, 'name' => '通用池',
]);
Capsule::table('cloud_offers')->insert(['id' => 4, 'name' => '双栈计划']);
Capsule::table('cloud_hosts')->insert(['id' => 5, 'name' => 'HKG-A']);
Capsule::table('cloud_host_network_attachments')->insert([
'id' => 10,
'host_id' => 5,
'line_id' => 2,
'network_selector' => 'bridge:br0',
]);
Capsule::table('cloud_public_ip_pools')->insert([
[
'id' => 20, 'host_id' => 5, 'network_attachment_id' => 10,
'address_family' => 4, 'pool_key' => str_repeat('a', 32),
'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"]',
],
[
'id' => 21, 'host_id' => 5, 'network_attachment_id' => 10,
'address_family' => 6, 'pool_key' => str_repeat('b', 32),
'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"]',
],
]);
$now = time();
Capsule::table('cloud_resource_reservations')->insert([
'id' => 100,
'reservation_key' => str_repeat('c', 32),
'order_id' => 200,
'offer_id' => 4,
'resource_pool_id' => 3,
'host_id' => 5,
'user_id' => 7,
'instance_id' => 88,
// Deliberately corrupt retired scalar fields. They must not affect output.
'network_attachment_id' => 999,
'public_ip_pool_id' => 999,
'public_ip_id' => 999,
'operation_id' => str_repeat('d', 32),
'cpu_cores' => 2,
'memory_mib' => 2048,
'disk_gib' => 20,
'state' => 'committed',
'expires_at' => null,
'released_at' => null,
'release_reason' => '',
'created_at' => $now,
'updated_at' => $now,
]);
Capsule::table('cloud_instances')->insert([
'id' => 88,
'reservation_id' => 100,
'host_id' => 5,
'resource_pool_id' => 3,
'user_id' => 7,
]);
$claims = [
4 => ['id' => 301, 'generation' => 1, 'pool' => 20, 'ip' => 501,
'operation' => str_repeat('e', 32), 'address' => '8.8.8.10',
'prefix' => 24],
6 => ['id' => 302, 'generation' => 3, 'pool' => 21, 'ip' => 502,
'operation' => str_repeat('f', 32),
'address' => '2404:c140:156:3a::10', 'prefix' => 64],
];
foreach ($claims as $family => $claim) {
Capsule::table('cloud_resource_reservation_ips')->insert([
'id' => $claim['id'], 'reservation_id' => 100,
'address_family' => $family,
'claim_generation' => $claim['generation'], 'active_claim' => 1,
'network_attachment_id' => 10,
'public_ip_pool_id' => $claim['pool'],
'public_ip_id' => $claim['ip'],
'operation_id' => $claim['operation'], 'state' => 'committed',
'released_at' => null, 'release_reason' => '',
'created_at' => $now, 'updated_at' => $now,
]);
Capsule::table('cloud_public_ips')->insert([
'id' => $claim['ip'], 'host_id' => 5,
'public_ip_pool_id' => $claim['pool'], 'address_family' => $family,
'address' => $claim['address'],
'address_binary' => inet_pton($claim['address']),
'prefix_length' => $claim['prefix'],
'state' => 'assigned', 'reservation_id' => 100,
'reservation_ip_claim_id' => $claim['id'], 'instance_id' => 88,
'operation_id' => $claim['operation'],
]);
}
// Historical claims must never leak into the current projection.
Capsule::table('cloud_resource_reservation_ips')->insert([
'id' => 303, 'reservation_id' => 100, 'address_family' => 4,
'claim_generation' => 1, 'active_claim' => null,
'network_attachment_id' => 10, 'public_ip_pool_id' => 20,
'public_ip_id' => 503, 'operation_id' => str_repeat('1', 32),
'state' => 'released', 'released_at' => $now,
'release_reason' => 'replaced', 'created_at' => $now,
'updated_at' => $now,
]);
Capsule::table('cloud_resource_reservations')->insert([
'id' => 101, 'reservation_key' => str_repeat('2', 32),
'order_id' => 201, 'offer_id' => 4, 'resource_pool_id' => 3,
'host_id' => 5, 'user_id' => 7, 'instance_id' => null,
'network_attachment_id' => 999, 'public_ip_pool_id' => 999,
'public_ip_id' => 999, 'operation_id' => str_repeat('3', 32),
'cpu_cores' => 1, 'memory_mib' => 1024, 'disk_gib' => 10,
'state' => 'held', 'expires_at' => null, 'released_at' => null,
'release_reason' => '', 'created_at' => $now, 'updated_at' => $now,
]);
foreach ([
4 => [401, 601, 20, str_repeat('4', 32), '8.8.8.11', 24],
6 => [402, 602, 21, str_repeat('5', 32),
'2404:c140:156:3a::11', 64],
] as $family => [$claimId, $ipId, $poolId, $operationId, $address, $prefix]) {
Capsule::table('cloud_resource_reservation_ips')->insert([
'id' => $claimId, 'reservation_id' => 101,
'address_family' => $family, 'claim_generation' => 1,
'active_claim' => 1, 'network_attachment_id' => 10,
'public_ip_pool_id' => $poolId, 'public_ip_id' => $ipId,
'operation_id' => $operationId, 'state' => 'reserved',
'released_at' => null, 'release_reason' => '',
'created_at' => $now, 'updated_at' => $now,
]);
Capsule::table('cloud_public_ips')->insert([
'id' => $ipId, 'host_id' => 5, 'public_ip_pool_id' => $poolId,
'address_family' => $family, 'address' => $address,
'address_binary' => inet_pton($address),
'prefix_length' => $prefix, 'state' => 'reserved',
'reservation_id' => 101, 'reservation_ip_claim_id' => $claimId,
'instance_id' => null, 'operation_id' => $operationId,
]);
}
Capsule::table('cloud_resource_reservations')->insert([
'id' => 102, 'reservation_key' => str_repeat('6', 32),
'order_id' => 202, 'offer_id' => 4, 'resource_pool_id' => 3,
'host_id' => 5, 'user_id' => 7, 'instance_id' => null,
'network_attachment_id' => null, 'public_ip_pool_id' => null,
'public_ip_id' => null, 'operation_id' => str_repeat('7', 32),
'cpu_cores' => 1, 'memory_mib' => 1024, 'disk_gib' => 10,
'state' => 'released', 'expires_at' => null, 'released_at' => $now,
'release_reason' => 'failed', 'created_at' => $now,
'updated_at' => $now,
]);
Capsule::table('cloud_resource_reservation_ips')->insert([
'id' => 403, 'reservation_id' => 102, 'address_family' => 4,
'claim_generation' => 1, 'active_claim' => 1,
'network_attachment_id' => 10, 'public_ip_pool_id' => 20,
'public_ip_id' => 603, 'operation_id' => str_repeat('8', 32),
'state' => 'reserved', 'released_at' => null, 'release_reason' => '',
'created_at' => $now, 'updated_at' => $now,
]);
Capsule::table('cloud_public_ips')->insert([
'id' => 603, 'host_id' => 5, 'public_ip_pool_id' => 20,
'address_family' => 4, 'address' => '8.8.8.12',
'address_binary' => inet_pton('8.8.8.12'), 'prefix_length' => 24,
'state' => 'reserved', 'reservation_id' => 102,
'reservation_ip_claim_id' => 403, 'instance_id' => null,
'operation_id' => str_repeat('8', 32),
]);
$page = CloudAdminResourceService::reservations(['q' => '501']);
$item = $page['items'][0] ?? null;
$assert(is_array($item)
&& $item['public_ip_id'] === 501
&& $item['public_ip'] === '8.8.8.10'
&& $item['public_ipv4'] === '8.8.8.10'
&& $item['public_ipv6'] === '2404:c140:156:3a::10'
&& count($item['public_ips']) === 2
&& $item['public_ips'][0]['claim_generation'] === 1
&& $item['public_ips'][1]['claim_generation'] === 3,
'Administrator reservation projection did not use the active dual-stack claims.');
$assert(CloudAdminResourceService::reservations(['q' => '999'])[
'pagination'
]['total'] === 0,
'Reservation search still treated the retired singular IP field as authority.');
$held = CloudAdminResourceService::reservation('101')['item'];
$assert($held['state'] === 'held'
&& $held['public_ipv4'] === '8.8.8.11'
&& $held['public_ipv6'] === '2404:c140:156:3a::11',
'Held reservation did not map to its reserved active claim pair.');
$releasedLeakRejected = false;
try {
CloudAdminResourceService::reservation('102');
} catch (CloudError $error) {
$releasedLeakRejected = $error->httpStatus === 409;
}
$assert($releasedLeakRejected,
'Released reservation hid an active public-address leak.');
Capsule::table('cloud_resource_reservation_ips')->where('id', 403)->update([
'state' => 'released',
'active_claim' => null,
'released_at' => $now,
'release_reason' => 'failed',
]);
Capsule::table('cloud_public_ips')->where('id', 603)->update([
'state' => 'available',
'reservation_id' => null,
// The reverse pointer alone is still an ownership leak.
'reservation_ip_claim_id' => 403,
'instance_id' => null,
'operation_id' => null,
]);
$releasedReverseLeakRejected = false;
try {
CloudAdminResourceService::reservation('102');
} catch (CloudError $error) {
$releasedReverseLeakRejected = $error->httpStatus === 409;
}
$assert($releasedReverseLeakRejected,
'Released reservation hid a reverse claim-to-address leak.');
Capsule::table('cloud_public_ips')->where('id', 603)->update([
'reservation_ip_claim_id' => null,
]);
$released = CloudAdminResourceService::reservation('102')['item'];
$assert($released['public_ip_id'] === null
&& $released['public_ip'] === ''
&& $released['public_ipv4'] === ''
&& $released['public_ipv6'] === ''
&& $released['public_ips'] === [],
'Clean released reservation did not project an empty current authority.');
echo "Administrator active-claim reservation projection tests passed.\n";