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

303 lines
12 KiB
PHTML

<?php
declare(strict_types=1);
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\CloudHostNetworkAttachment;
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudHostNetworkAttachmentService;
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);
}
};
$expect = static function (callable $operation, int $status,
string $message) use ($assert): void {
try {
$operation();
} catch (CloudError $error) {
$assert($error->httpStatus === $status
&& str_contains($error->getMessage(), $message),
'Unexpected error: ' . $error->getMessage());
return;
}
throw new RuntimeException('Expected CloudError was not thrown.');
};
$capsule = new Capsule();
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:']);
$capsule->setAsGlobal();
$capsule->bootEloquent();
$schema = $capsule->schema();
$schema->create('cloud_hosts', static function (Blueprint $table): void {
$table->increments('id');
$table->string('name');
$table->string('default_network');
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_host_agents', static function (Blueprint $table): void {
$table->integer('host_id')->primary();
$table->text('sealed_api_secret');
$table->boolean('enabled');
$table->integer('revision');
$table->integer('tested_revision');
$table->string('last_test_status');
$table->string('last_error_code');
$table->string('last_agent_version');
$table->text('network_selectors_json');
$table->integer('last_tested_at');
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_network_lines', static function (Blueprint $table): void {
$table->increments('id');
$table->string('name');
$table->string('status');
$table->integer('sort');
});
$schema->create('cloud_resource_pools', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('line_id');
$table->string('status');
});
$schema->create('cloud_host_resource_pool_memberships',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('resource_pool_id');
$table->string('scheduler_state');
$table->integer('priority');
$table->integer('weight');
$table->integer('revision')->default(1);
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_host_network_attachments',
static function (Blueprint $table): void {
$table->increments('id');
$table->string('attachment_key');
$table->integer('host_id');
$table->integer('line_id');
$table->string('name');
$table->string('network_mode');
$table->string('network_selector');
$table->integer('vlan_id')->nullable();
$table->integer('routing_table')->nullable();
$table->string('state');
$table->integer('priority');
$table->integer('config_revision');
$table->integer('verified_agent_revision');
$table->string('verification_state');
$table->integer('last_verified_at');
$table->string('verification_error_code');
$table->integer('desired_revision');
$table->integer('observed_revision');
$table->string('sync_state');
$table->string('last_error_code');
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_public_ip_pools', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('network_attachment_id');
});
$now = time();
Capsule::table('cloud_hosts')->insert([
'id' => 1, 'name' => 'HKG-A', 'default_network' => 'bridge:br0',
'created_at' => $now, 'updated_at' => $now,
]);
Capsule::table('cloud_host_agents')->insert([
'host_id' => 1, 'sealed_api_secret' => 'sealed', 'enabled' => 1,
'revision' => 7, 'tested_revision' => 7,
'last_test_status' => 'healthy', 'last_error_code' => '',
'last_agent_version' => 'v3',
'network_selectors_json' =>
'["bridge:br0","bridge:br1","network:default"]',
'last_tested_at' => $now, 'created_at' => $now, 'updated_at' => $now,
]);
Capsule::table('cloud_network_lines')->insert([
['id' => 11, 'name' => 'Premium', 'status' => 'active', 'sort' => 10],
['id' => 12, 'name' => 'Standard', 'status' => 'active', 'sort' => 20],
['id' => 13, 'name' => 'Unowned', 'status' => 'active', 'sort' => 30],
['id' => 14, 'name' => 'Draining membership', 'status' => 'active',
'sort' => 40],
['id' => 15, 'name' => 'Disabled line', 'status' => 'disabled',
'sort' => 50],
]);
Capsule::table('cloud_resource_pools')->insert([
['id' => 21, 'line_id' => 11, 'status' => 'active'],
['id' => 22, 'line_id' => 12, 'status' => 'active'],
['id' => 23, 'line_id' => 14, 'status' => 'active'],
['id' => 24, 'line_id' => 15, 'status' => 'active'],
]);
Capsule::table('cloud_host_resource_pool_memberships')->insert([
['host_id' => 1, 'resource_pool_id' => 21, 'scheduler_state' => 'active',
'priority' => 0, 'weight' => 100, 'created_at' => $now,
'updated_at' => $now],
['host_id' => 1, 'resource_pool_id' => 22, 'scheduler_state' => 'active',
'priority' => 10, 'weight' => 100, 'created_at' => $now,
'updated_at' => $now],
['host_id' => 1, 'resource_pool_id' => 23, 'scheduler_state' => 'disabled',
'priority' => 20, 'weight' => 100, 'created_at' => $now,
'updated_at' => $now],
['host_id' => 1, 'resource_pool_id' => 24, 'scheduler_state' => 'active',
'priority' => 30, 'weight' => 100, 'created_at' => $now,
'updated_at' => $now],
]);
$probeSelectors = ['bridge:br0', 'bridge:br1', 'network:default'];
$probe = static function (int $hostId, array $actor, bool $requireReady) use (
&$probeSelectors
): array {
return [
'host_id' => $hostId,
'agent_revision' => 7,
'tested_revision' => 7,
'network_selectors' => $probeSelectors,
];
};
$service = new CloudHostNetworkAttachmentService($probe);
$actor = [
'user_id' => 1, 'is_admin' => true,
'is_super_admin' => true, 'can_manage' => true,
];
$readOnlyAdmin = [
'user_id' => 2, 'is_admin' => true,
'is_super_admin' => false, 'can_manage' => false,
];
$tenant = [
'user_id' => 3, 'is_admin' => false,
'is_super_admin' => false, 'can_manage' => false,
];
$body = static fn (string $name, int $lineId, string $selector): array => [
'name' => $name, 'line_id' => $lineId,
'network_selector' => $selector, 'state' => 'active', 'priority' => 0,
];
$expect(static fn () => $service->read(1, $tenant), 403, '管理员');
$assert($service->read(1, $readOnlyAdmin)['host']['id'] === 1,
'A read-only administrator could not inspect network attachments.');
foreach ([
static fn () => $service->discover(1, $readOnlyAdmin),
static fn () => $service->create(1, $readOnlyAdmin, []),
static fn () => $service->update(
1,
str_repeat('a', 32),
$readOnlyAdmin,
[]
),
static fn () => $service->test(
1,
str_repeat('a', 32),
$readOnlyAdmin,
[]
),
] as $operation) {
$expect($operation, 403, '超级管理员');
}
$discovered = $service->discover(1, $actor);
$assert($discovered['network_selectors'] === $probeSelectors,
'Discovery did not return the bounded Agent selector evidence.');
$firstKey = str_repeat('a', 32);
$first = $service->create(1, $actor,
$body('Primary', 11, 'bridge:br0') + ['request_key' => $firstKey]);
$assert($first['saved_attachment_key'] === $firstKey
&& $first['replay'] === false && count($first['items']) === 1
&& $first['items'][0]['verification_state'] === 'never'
&& $first['items'][0]['ready'] === false,
'A newly saved attachment bypassed its independent verification gate.');
$replay = $service->create(1, $actor,
$body('Primary', 11, 'bridge:br0') + ['request_key' => $firstKey]);
$assert($replay['replay'] === true && count($replay['items']) === 1,
'Identical attachment creation was not idempotent.');
$secondKey = str_repeat('b', 32);
$service->create(1, $actor,
$body('Secondary', 12, 'bridge:br1') + ['request_key' => $secondKey]);
$assert(CloudHostNetworkAttachment::where('host_id', 1)->count() === 2,
'A second line/selector attachment was not saved.');
$expect(static fn () => $service->create(1, $actor,
$body('Duplicate', 12, 'bridge:br0')
+ ['request_key' => str_repeat('c', 32)]), 409, '相同网络选择器');
$expect(static fn () => $service->create(1, $actor,
$body('Unknown selector', 12, 'bridge:br9')
+ ['request_key' => str_repeat('d', 32)]), 409, '最近一次完整能力测试');
$expect(static fn () => $service->create(1, $actor,
$body('Unowned line', 13, 'network:default')
+ ['request_key' => str_repeat('e', 32)]), 409, '资源池成员关系');
$expect(static fn () => $service->create(1, $actor,
$body('Disabled membership', 14, 'network:default')
+ ['request_key' => str_repeat('f', 32)]), 409, '资源池成员关系');
$expect(static fn () => $service->create(1, $actor,
$body('Disabled line', 15, 'network:default')
+ ['request_key' => str_repeat('1', 32)]), 409, '资源池成员关系');
$tested = $service->test(1, $firstKey, $actor, ['expected_revision' => 1]);
$testedItem = array_values(array_filter($tested['items'],
static fn (array $item): bool => $item['attachment_key'] === $firstKey))[0];
$assert($testedItem['ready'] === true
&& $testedItem['verified_agent_revision'] === 7,
'Exact live selector evidence did not verify the attachment.');
$disabledBody = $body('Primary', 11, 'bridge:br0');
$disabledBody['state'] = 'disabled';
$disabled = $service->update(1, $firstKey, $actor,
$disabledBody + ['expected_revision' => 1]);
$disabledItem = array_values(array_filter($disabled['items'],
static fn (array $item): bool => $item['attachment_key'] === $firstKey))[0];
$assert($disabledItem['verification_state'] === 'verified'
&& $disabledItem['ready'] === false,
'A disabled attachment was exposed as ready.');
$updated = $service->update(1, $firstKey, $actor,
$body('Primary renamed', 11, 'bridge:br0') + ['expected_revision' => 2]);
$updatedItem = array_values(array_filter($updated['items'],
static fn (array $item): bool => $item['attachment_key'] === $firstKey))[0];
$assert($updatedItem['config_revision'] === 3
&& $updatedItem['ready'] === true,
'A metadata-only CAS update lost valid selector evidence.');
$expect(static fn () => $service->update(1, $firstKey, $actor,
$body('Stale', 11, 'bridge:br0') + ['expected_revision' => 2]),
409, '版本已变化');
$probeSelectors = ['bridge:br0', 'network:default'];
$expect(static fn () => $service->test(1, $secondKey, $actor,
['expected_revision' => 1]), 409, '当前未发现');
$second = CloudHostNetworkAttachment::where('attachment_key', $secondKey)
->first();
$assert((string) $second->verification_state === 'failed'
&& (string) $second->verification_error_code
=== 'agent_network_selector_missing',
'A missing live selector did not persist independent failure evidence.');
Capsule::table('cloud_public_ip_pools')->insert([
'host_id' => 1, 'network_attachment_id' =>
(int) CloudHostNetworkAttachment::where('attachment_key', $firstKey)
->value('id'),
]);
$expect(static fn () => $service->update(1, $firstKey, $actor,
$body('Rebound', 12, 'network:default') + ['expected_revision' => 3]),
409, '已经绑定公网地址池');
Capsule::table('cloud_host_agents')->where('host_id', 1)->update([
'revision' => 8, 'tested_revision' => 0, 'last_test_status' => 'never',
'network_selectors_json' => '[]',
]);
$stale = $service->read(1, $actor);
$firstStale = array_values(array_filter($stale['items'],
static fn (array $item): bool => $item['attachment_key'] === $firstKey))[0];
$assert($firstStale['verification_state'] === 'stale'
&& $firstStale['ready'] === false,
'An Agent generation change did not make old attachment evidence stale.');
echo "Host network attachment configuration and Agent evidence passed.\n";