feat: support multiple host pool memberships
parent
a200195928
commit
aa408ed8bc
@ -1,2 +1,2 @@
|
||||
{* Shared Venix CSS/JS cache key. Increment whenever a shared browser asset changes. *}
|
||||
{assign var=venix_asset_version value='2026110203' scope='global'}
|
||||
{assign var=venix_asset_version value='2026110204' scope='global'}
|
||||
|
||||
@ -0,0 +1,187 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import vm from 'node:vm';
|
||||
|
||||
const adminSource = await readFile(
|
||||
new URL('../public/cloud/admin-hosts.js', import.meta.url), 'utf8');
|
||||
const source = adminSource.slice(adminSource.indexOf('const root ='))
|
||||
.replace('void initialize();', '')
|
||||
+ `\nglobalThis.hostSchedulingUI = {
|
||||
renderHostScheduling, schedulingBody, saveHostScheduling,
|
||||
createHostMembership, saveHostMembership, deleteHostMembership, state,
|
||||
};`;
|
||||
|
||||
const descendants = node => [node, ...(node?.children || []).flatMap(descendants)];
|
||||
|
||||
function fixture(canManage = true) {
|
||||
const elements = new Map();
|
||||
const requests = [];
|
||||
let response = null;
|
||||
|
||||
class Element {
|
||||
constructor(tag = 'input') {
|
||||
this.tag = tag; this.value = ''; this.textContent = ''; this.className = '';
|
||||
this.name = ''; this.type = ''; this.dataset = {}; this.children = [];
|
||||
this.disabled = false; this.hidden = false; this.isConnected = true;
|
||||
this.listeners = new Map();
|
||||
this.classList = { add() {}, remove() {}, toggle() {} };
|
||||
}
|
||||
get elements() {
|
||||
return { namedItem: name => descendants(this).find(node => node.name === name) || null };
|
||||
}
|
||||
append(...children) { this.children.push(...children); }
|
||||
replaceChildren(...children) {
|
||||
for (const child of this.children) for (const node of descendants(child)) node.isConnected = false;
|
||||
this.children = children;
|
||||
}
|
||||
addEventListener(name, listener) { this.listeners.set(name, listener); }
|
||||
querySelector(selector) {
|
||||
if (selector === '[data-host-membership-save]') {
|
||||
return descendants(this).find(node => Object.hasOwn(node.dataset, 'hostMembershipSave')) || null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
reportValidity() { return true; }
|
||||
}
|
||||
|
||||
const selectIds = new Set([
|
||||
'host-membership-pool', 'host-membership-new-state',
|
||||
]);
|
||||
const get = id => {
|
||||
if (!elements.has(id)) elements.set(id, new Element(selectIds.has(id) ? 'select' : 'input'));
|
||||
return elements.get(id);
|
||||
};
|
||||
const root = { dataset: { cloudAdmin: 'hosts', canManage: canManage ? '1' : '0' } };
|
||||
const runtime = {
|
||||
create: () => ({
|
||||
read: async path => ({ payload: response, path }),
|
||||
mutate: async (path, options) => {
|
||||
requests.push({ path, ...options });
|
||||
return { payload: response };
|
||||
},
|
||||
}),
|
||||
};
|
||||
const context = {
|
||||
document: {
|
||||
querySelector: () => root,
|
||||
getElementById: get,
|
||||
createElement: tag => new Element(tag),
|
||||
},
|
||||
ADMIN_CSRF_TOKEN: 'a'.repeat(64),
|
||||
VenixAdminRequestRuntime: runtime,
|
||||
VenixAdminDangerDialog: { confirm: async () => true },
|
||||
VenixDrawerMotion: {},
|
||||
taskUrl: () => '',
|
||||
location: { assign() {} },
|
||||
Headers, AbortController, setTimeout, clearTimeout,
|
||||
};
|
||||
context.window = context;
|
||||
vm.runInNewContext(source, context, { filename: 'admin-hosts.js' });
|
||||
return {
|
||||
...context.hostSchedulingUI,
|
||||
get,
|
||||
requests,
|
||||
descendants: id => descendants(get(id)),
|
||||
respond(payload) { response = payload; },
|
||||
};
|
||||
}
|
||||
|
||||
const capacity = {
|
||||
cpu: { allocated: 1, reserved: 0, free: 7 },
|
||||
memory: { allocated: 1024, reserved: 0, free: 7168 },
|
||||
disk: { allocated: 20, reserved: 0, free: 180 },
|
||||
instances: { allocated: 1, reserved: 0 },
|
||||
ip: { available: 4, total: 8 },
|
||||
};
|
||||
const policy = {
|
||||
configured_cpu_cores: 8, configured_memory_mib: 8192,
|
||||
configured_disk_gib: 200, safety_cpu_cores: 0,
|
||||
safety_memory_mib: 0, safety_disk_gib: 0, max_instances: 20,
|
||||
};
|
||||
const pools = [
|
||||
{ id: 11, name: 'Premium', status: 'active', line_name: 'CN2', line_status: 'active', region_name: 'Hong Kong', region_status: 'active' },
|
||||
{ id: 12, name: 'Standard', status: 'active', line_name: 'Global', line_status: 'active', region_name: 'Hong Kong', region_status: 'active' },
|
||||
];
|
||||
const membership = {
|
||||
resource_pool_id: 11, pool_name: 'Premium', line_name: 'CN2',
|
||||
region_name: 'Hong Kong', scheduler_state: 'active', priority: 10,
|
||||
weight: 200, revision: 4, warnings: [],
|
||||
};
|
||||
const payload = {
|
||||
host: { id: 7, name: 'HKG-A', enabled: true, resource_pool_id: 11, scheduler_state: 'active' },
|
||||
policy, capacity, pools, memberships: [membership], warnings: [],
|
||||
network_lines_enabled: true,
|
||||
};
|
||||
|
||||
const test = fixture();
|
||||
test.respond(payload);
|
||||
test.renderHostScheduling(payload);
|
||||
|
||||
assert.deepEqual(test.get('host-membership-pool').children.map(option => option.value), ['', '12'],
|
||||
'Assigned resource pools remained selectable as new memberships.');
|
||||
const cards = test.descendants('host-membership-list');
|
||||
assert.ok(cards.some(node => node.dataset.hostMembership === '11'));
|
||||
assert.ok(cards.some(node => /Hong Kong \/ CN2 \/ Premium/.test(node.textContent)));
|
||||
const memberForm = cards.find(node => node.dataset.hostMembershipForm === '');
|
||||
assert.equal(memberForm.dataset.revision, '4');
|
||||
assert.equal(memberForm.elements.namedItem('scheduler_state').value, 'active');
|
||||
assert.equal(memberForm.elements.namedItem('priority').value, '10');
|
||||
assert.equal(memberForm.elements.namedItem('weight').value, '200');
|
||||
|
||||
assert.deepEqual({ ...test.schedulingBody() }, policy,
|
||||
'The global capacity payload still contained membership fields.');
|
||||
await test.saveHostScheduling({ preventDefault() {}, currentTarget: test.get('host-scheduling-form') });
|
||||
assert.equal(test.requests.at(-1).path, '/api/v1/admin/hosts/7/scheduling');
|
||||
assert.equal(test.requests.at(-1).method, 'PUT');
|
||||
assert.deepEqual({ ...test.requests.at(-1).body }, policy);
|
||||
|
||||
const withSecond = {
|
||||
...payload,
|
||||
memberships: [membership, { ...membership, resource_pool_id: 12, pool_name: 'Standard', line_name: 'Global', scheduler_state: 'disabled', priority: 0, weight: 100, revision: 1 }],
|
||||
};
|
||||
test.respond(withSecond);
|
||||
test.get('host-membership-pool').value = '12';
|
||||
test.get('host-membership-new-state').value = 'disabled';
|
||||
test.get('host-membership-new-priority').value = '0';
|
||||
test.get('host-membership-new-weight').value = '100';
|
||||
await test.createHostMembership({ preventDefault() {}, currentTarget: test.get('host-membership-create-form') });
|
||||
assert.equal(test.requests.at(-1).path, '/api/v1/admin/hosts/7/scheduling/memberships/12');
|
||||
assert.equal(test.requests.at(-1).method, 'PUT');
|
||||
assert.deepEqual({ ...test.requests.at(-1).body }, {
|
||||
scheduler_state: 'disabled', priority: 0, weight: 100, expected_revision: 0,
|
||||
});
|
||||
|
||||
const updatedForms = test.descendants('host-membership-list');
|
||||
const firstForm = updatedForms.find(node => node.dataset.hostMembershipForm === '' && node.dataset.poolId === '11');
|
||||
firstForm.elements.namedItem('scheduler_state').value = 'draining';
|
||||
firstForm.elements.namedItem('priority').value = '30';
|
||||
firstForm.elements.namedItem('weight').value = '300';
|
||||
test.respond({ ...withSecond, memberships: [{ ...membership, scheduler_state: 'draining', priority: 30, weight: 300, revision: 5 }, withSecond.memberships[1]] });
|
||||
await test.saveHostMembership({ preventDefault() {}, currentTarget: firstForm });
|
||||
assert.equal(test.requests.at(-1).path, '/api/v1/admin/hosts/7/scheduling/memberships/11');
|
||||
assert.deepEqual({ ...test.requests.at(-1).body }, {
|
||||
scheduler_state: 'draining', priority: 30, weight: 300, expected_revision: 4,
|
||||
});
|
||||
|
||||
test.respond({ ...payload, memberships: [] });
|
||||
await test.deleteHostMembership({ ...membership, revision: 5 }, null);
|
||||
assert.equal(test.requests.at(-1).method, 'DELETE');
|
||||
assert.equal(test.requests.at(-1).path, '/api/v1/admin/hosts/7/scheduling/memberships/11');
|
||||
assert.deepEqual({ ...test.requests.at(-1).body }, { expected_revision: 5 });
|
||||
|
||||
const viewer = fixture(false);
|
||||
viewer.respond(payload);
|
||||
viewer.renderHostScheduling(payload);
|
||||
const viewerForm = viewer.descendants('host-membership-list')
|
||||
.find(node => node.dataset.hostMembershipForm === '');
|
||||
assert.equal(viewerForm.elements.namedItem('scheduler_state').disabled, true);
|
||||
assert.equal(viewerForm.elements.namedItem('priority').disabled, true);
|
||||
assert.equal(viewerForm.elements.namedItem('weight').disabled, true);
|
||||
assert.equal(viewer.descendants('host-membership-list')
|
||||
.some(node => Object.hasOwn(node.dataset, 'hostMembershipSave')), false);
|
||||
await viewer.saveHostScheduling({ preventDefault() {}, currentTarget: viewer.get('host-scheduling-form') });
|
||||
await viewer.createHostMembership({ preventDefault() {}, currentTarget: viewer.get('host-membership-create-form') });
|
||||
assert.equal(viewer.requests.length, 0,
|
||||
'Read-only administrators could submit host scheduling mutations.');
|
||||
|
||||
console.log('Administrator host capacity, independent membership and read-only UI behavior passed.');
|
||||
@ -0,0 +1,364 @@
|
||||
<?php
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
use App\Models\CloudHost;
|
||||
use App\Services\Cloud\CloudAdminHostProjectionService;
|
||||
use App\Services\Cloud\CloudAdminResourceService;
|
||||
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('configurations', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->string('name')->unique();
|
||||
$table->text('value');
|
||||
});
|
||||
$schema->create('cloud_hosts', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->string('name');
|
||||
$table->string('region');
|
||||
$table->string('country_code', 2);
|
||||
$table->string('default_network');
|
||||
$table->boolean('enabled');
|
||||
$table->integer('created_at');
|
||||
$table->integer('updated_at');
|
||||
});
|
||||
$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');
|
||||
$table->integer('created_at');
|
||||
$table->integer('updated_at');
|
||||
});
|
||||
$schema->create('cloud_regions', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->string('code');
|
||||
$table->string('name');
|
||||
$table->string('country_code', 2);
|
||||
$table->string('status');
|
||||
$table->integer('sort');
|
||||
$table->integer('created_at');
|
||||
$table->integer('updated_at');
|
||||
});
|
||||
$schema->create('cloud_network_lines', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->integer('region_id');
|
||||
$table->string('code');
|
||||
$table->string('name');
|
||||
$table->boolean('is_default');
|
||||
$table->string('status');
|
||||
$table->integer('sort');
|
||||
$table->integer('created_at');
|
||||
$table->integer('updated_at');
|
||||
});
|
||||
$schema->create('cloud_resource_pools', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->integer('line_id');
|
||||
$table->string('code');
|
||||
$table->string('name');
|
||||
$table->string('status');
|
||||
$table->string('scheduling_policy');
|
||||
$table->integer('sort');
|
||||
$table->integer('created_at');
|
||||
$table->integer('updated_at');
|
||||
});
|
||||
$schema->create('cloud_offers', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->integer('resource_pool_id');
|
||||
});
|
||||
$schema->create('cloud_host_capacity_policies',
|
||||
static function (Blueprint $table): void {
|
||||
$table->integer('host_id')->primary();
|
||||
$table->integer('configured_cpu_cores');
|
||||
$table->integer('configured_memory_mib');
|
||||
$table->integer('configured_disk_gib');
|
||||
$table->integer('safety_cpu_cores');
|
||||
$table->integer('safety_memory_mib');
|
||||
$table->integer('safety_disk_gib');
|
||||
$table->integer('max_instances');
|
||||
$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_host_storage_configurations',
|
||||
static function (Blueprint $table): void {
|
||||
$table->integer('host_id')->primary();
|
||||
$table->integer('revision');
|
||||
$table->string('state');
|
||||
$table->integer('created_at');
|
||||
$table->integer('updated_at');
|
||||
});
|
||||
$schema->create('cloud_resource_reservations',
|
||||
static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->integer('resource_pool_id');
|
||||
$table->integer('host_id');
|
||||
$table->string('state');
|
||||
$table->integer('instance_id')->nullable();
|
||||
$table->integer('cpu_cores');
|
||||
$table->integer('memory_mib');
|
||||
$table->integer('disk_gib');
|
||||
});
|
||||
$schema->create('cloud_instances', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->integer('host_id');
|
||||
$table->string('status');
|
||||
$table->text('spec_json');
|
||||
});
|
||||
$schema->create('cloud_instance_snapshots',
|
||||
static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->integer('instance_id');
|
||||
$table->integer('host_id');
|
||||
$table->string('state');
|
||||
$table->integer('size_bytes')->nullable();
|
||||
});
|
||||
$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->string('sync_state');
|
||||
$table->integer('verified_agent_revision');
|
||||
$table->string('verification_state');
|
||||
$table->integer('observed_revision');
|
||||
$table->integer('desired_revision');
|
||||
});
|
||||
$schema->create('cloud_public_ip_pools',
|
||||
static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->integer('host_id');
|
||||
$table->integer('network_attachment_id');
|
||||
$table->string('pool_key');
|
||||
$table->string('cidr');
|
||||
$table->integer('guest_prefix_length');
|
||||
$table->string('state');
|
||||
$table->string('sync_state');
|
||||
$table->integer('observed_revision');
|
||||
$table->integer('desired_revision');
|
||||
});
|
||||
$schema->create('cloud_public_ips', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->integer('host_id');
|
||||
$table->integer('public_ip_pool_id');
|
||||
$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('configurations')->insert([
|
||||
'name' => 'cloud_network_lines_enabled',
|
||||
'value' => 'true',
|
||||
]);
|
||||
Capsule::table('cloud_hosts')->insert([
|
||||
'id' => 1,
|
||||
'name' => 'HKG-A',
|
||||
'region' => 'Hong Kong',
|
||||
'country_code' => 'HK',
|
||||
'default_network' => 'bridge:retired-default',
|
||||
'enabled' => 1,
|
||||
'created_at' => 1,
|
||||
'updated_at' => 1,
|
||||
]);
|
||||
Capsule::table('cloud_regions')->insert([
|
||||
'id' => 1, 'code' => 'hkg', 'name' => 'Hong Kong',
|
||||
'country_code' => 'HK', 'status' => 'active', 'sort' => 1,
|
||||
'created_at' => 1, 'updated_at' => 1,
|
||||
]);
|
||||
Capsule::table('cloud_network_lines')->insert([
|
||||
['id' => 10, 'region_id' => 1, 'code' => 'premium',
|
||||
'name' => 'Premium', 'is_default' => 0, 'status' => 'active',
|
||||
'sort' => 10, 'created_at' => 1, 'updated_at' => 1],
|
||||
['id' => 11, 'region_id' => 1, 'code' => 'standard',
|
||||
'name' => 'Standard', 'is_default' => 1, 'status' => 'active',
|
||||
'sort' => 20, 'created_at' => 1, 'updated_at' => 1],
|
||||
]);
|
||||
Capsule::table('cloud_resource_pools')->insert([
|
||||
['id' => 20, 'line_id' => 10, 'code' => 'premium-pool',
|
||||
'name' => 'Premium Pool', 'status' => 'active',
|
||||
'scheduling_policy' => 'balanced', 'sort' => 10,
|
||||
'created_at' => 1, 'updated_at' => 1],
|
||||
['id' => 21, 'line_id' => 11, 'code' => 'standard-pool',
|
||||
'name' => 'Standard Pool', 'status' => 'active',
|
||||
'scheduling_policy' => 'balanced', 'sort' => 20,
|
||||
'created_at' => 1, 'updated_at' => 1],
|
||||
]);
|
||||
Capsule::table('cloud_host_resource_pool_memberships')->insert([
|
||||
['id' => 30, 'host_id' => 1, 'resource_pool_id' => 20,
|
||||
'scheduler_state' => 'active', 'priority' => 20, 'weight' => 100,
|
||||
'revision' => 1, 'created_at' => 1, 'updated_at' => 1],
|
||||
['id' => 31, 'host_id' => 1, 'resource_pool_id' => 21,
|
||||
'scheduler_state' => 'active', 'priority' => 10, 'weight' => 100,
|
||||
'revision' => 2, 'created_at' => 1, 'updated_at' => 1],
|
||||
]);
|
||||
Capsule::table('cloud_host_capacity_policies')->insert([
|
||||
'host_id' => 1, 'configured_cpu_cores' => 16,
|
||||
'configured_memory_mib' => 32768, 'configured_disk_gib' => 800,
|
||||
'safety_cpu_cores' => 2, 'safety_memory_mib' => 1024,
|
||||
'safety_disk_gib' => 50, 'max_instances' => 50,
|
||||
'created_at' => 1, 'updated_at' => 1,
|
||||
]);
|
||||
Capsule::table('cloud_host_agents')->insert([
|
||||
'host_id' => 1, 'sealed_api_secret' => 'sealed', 'enabled' => 1,
|
||||
'revision' => 1, 'tested_revision' => 1,
|
||||
'last_test_status' => 'healthy', 'last_error_code' => '',
|
||||
'last_agent_version' => 'v3', 'last_tested_at' => 1,
|
||||
'network_selectors_json' => json_encode([
|
||||
'bridge:br-premium', 'bridge:br-standard',
|
||||
], JSON_THROW_ON_ERROR),
|
||||
'created_at' => 1, 'updated_at' => 1,
|
||||
]);
|
||||
Capsule::table('cloud_host_storage_configurations')->insert([
|
||||
'host_id' => 1, 'revision' => 1, 'state' => 'applied',
|
||||
'created_at' => 1, 'updated_at' => 1,
|
||||
]);
|
||||
Capsule::table('cloud_host_network_attachments')->insert([
|
||||
['id' => 40, 'host_id' => 1, 'line_id' => 10,
|
||||
'network_selector' => 'bridge:br-premium', 'state' => 'active',
|
||||
'sync_state' => 'never', 'verified_agent_revision' => 1,
|
||||
'verification_state' => 'verified', 'observed_revision' => 0,
|
||||
'desired_revision' => 2],
|
||||
['id' => 41, 'host_id' => 1, 'line_id' => 11,
|
||||
'network_selector' => 'bridge:br-standard', 'state' => 'active',
|
||||
'sync_state' => 'never', 'verified_agent_revision' => 1,
|
||||
'verification_state' => 'verified', 'observed_revision' => 0,
|
||||
'desired_revision' => 3],
|
||||
]);
|
||||
Capsule::table('cloud_public_ip_pools')->insert([
|
||||
['id' => 50, 'host_id' => 1, 'network_attachment_id' => 40,
|
||||
'pool_key' => str_repeat('a', 32), 'cidr' => '203.0.113.0/29',
|
||||
'guest_prefix_length' => 29,
|
||||
'state' => 'active', 'sync_state' => 'applied',
|
||||
'observed_revision' => 2, 'desired_revision' => 2],
|
||||
['id' => 51, 'host_id' => 1, 'network_attachment_id' => 41,
|
||||
'pool_key' => str_repeat('b', 32), 'cidr' => '198.51.100.0/29',
|
||||
'guest_prefix_length' => 29,
|
||||
'state' => 'active', 'sync_state' => 'applied',
|
||||
'observed_revision' => 3, 'desired_revision' => 3],
|
||||
]);
|
||||
Capsule::table('cloud_public_ips')->insert([
|
||||
['id' => 60, 'host_id' => 1, 'public_ip_pool_id' => 50,
|
||||
'address' => '203.0.113.2', 'prefix_length' => 29,
|
||||
'state' => 'available', 'reservation_id' => null,
|
||||
'instance_id' => null, 'operation_id' => null],
|
||||
['id' => 61, 'host_id' => 1, 'public_ip_pool_id' => 51,
|
||||
'address' => '198.51.100.2', 'prefix_length' => 29,
|
||||
'state' => 'available', 'reservation_id' => null,
|
||||
'instance_id' => null, 'operation_id' => null],
|
||||
]);
|
||||
|
||||
$projection = CloudAdminHostProjectionService::listing();
|
||||
$assert(count($projection['hosts']) === 1,
|
||||
'Expected exactly one physical host projection.');
|
||||
$host = $projection['hosts'][0];
|
||||
$assert($host['resource_pool_id'] === 21
|
||||
&& $host['scheduler_state'] === 'active'
|
||||
&& $host['active_memberships'] === 2
|
||||
&& $host['draining_memberships'] === 0,
|
||||
'Compact fields were not a deterministic primary-membership summary.');
|
||||
$assert(array_column($host['memberships'], 'resource_pool_id') === [20, 21]
|
||||
&& count(array_filter($host['memberships'],
|
||||
static fn (array $row): bool => $row['placement_ready'])) === 2
|
||||
&& array_column($host['lines'], 'id') === [10, 11]
|
||||
&& $host['readiness']['network_ready'] === true
|
||||
&& $host['readiness']['placement_ready'] === true,
|
||||
'The host did not expose every exact membership and verified target line.');
|
||||
$assert($host['capacity']['cpu']['configured'] === 16
|
||||
&& $host['capacity']['ip']['available'] === 2
|
||||
&& $projection['summary']['capacity']['cpu']['configured'] === 16,
|
||||
'Physical capacity was duplicated across active memberships.');
|
||||
|
||||
$resources = CloudAdminResourceService::page();
|
||||
$pools = [];
|
||||
foreach ($resources['pools'] as $pool) {
|
||||
$pools[(int) $pool['id']] = $pool;
|
||||
}
|
||||
$assert(count($resources['hosts']) === 2
|
||||
&& array_column($resources['hosts'], 'resource_pool_id') === [20, 21]
|
||||
&& $pools[20]['health']['host_count'] === 1
|
||||
&& $pools[21]['health']['host_count'] === 1,
|
||||
'Resource catalog did not map each membership to its exact pool.');
|
||||
$assert($pools[20]['capacity_scope'] === 'shared_host'
|
||||
&& $pools[21]['capacity_shared'] === true
|
||||
&& $pools[20]['capacity']['cpu']['configured'] === 16
|
||||
&& $pools[21]['capacity']['cpu']['configured'] === 16
|
||||
&& $resources['summary']['hosts_total'] === 1
|
||||
&& $resources['summary']['placement_ready_hosts'] === 1
|
||||
&& $resources['summary']['capacity']['cpu']['configured'] === 16
|
||||
&& $resources['summary']['placement_capacity']['cpu']['configured'] === 16,
|
||||
'Shared pool views or platform totals duplicated physical host capacity.');
|
||||
|
||||
$single = CloudAdminHostProjectionService::one(CloudHost::find(1));
|
||||
$assert(count($single['memberships']) === 2 && count($single['lines']) === 2,
|
||||
'Single-host projection drifted from the multi-membership list DTO.');
|
||||
|
||||
Capsule::table('cloud_host_agents')->where('host_id', 1)->update([
|
||||
'network_selectors_json' => '[]',
|
||||
]);
|
||||
$missingSelector = CloudAdminHostProjectionService::listing()['hosts'][0];
|
||||
$assert($missingSelector['readiness']['network_ready'] === false
|
||||
&& $missingSelector['readiness']['placement_ready'] === false
|
||||
&& $missingSelector['capacity']['ip']['available'] === 0,
|
||||
'A selector missing from current Agent evidence was advertised as ready.');
|
||||
$missingSelectorResources = CloudAdminResourceService::page();
|
||||
$assert(count(array_filter(
|
||||
$missingSelectorResources['hosts'],
|
||||
static fn (array $row): bool => $row['network_ready']
|
||||
)) === 0
|
||||
&& $missingSelectorResources['summary']['placement_ready_hosts'] === 0
|
||||
&& $missingSelectorResources['summary']['placement_capacity']['cpu']
|
||||
['configured'] === 0,
|
||||
'Resource catalog accepted an attachment absent from Agent evidence.');
|
||||
|
||||
Capsule::table('cloud_host_agents')->where('host_id', 1)->update([
|
||||
'network_selectors_json' => json_encode([
|
||||
'bridge:br-premium', 'bridge:br-standard',
|
||||
], JSON_THROW_ON_ERROR),
|
||||
]);
|
||||
Capsule::table('cloud_host_network_attachments')->update([
|
||||
'verified_agent_revision' => 0,
|
||||
]);
|
||||
$staleRevision = CloudAdminHostProjectionService::listing()['hosts'][0];
|
||||
$assert($staleRevision['readiness']['network_ready'] === false
|
||||
&& $staleRevision['capacity']['ip']['available'] === 0,
|
||||
'Stale attachment verification was advertised as ready.');
|
||||
|
||||
echo "Administrator host multi-membership projection passed.\n";
|
||||
@ -0,0 +1,334 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
use App\Services\Cloud\CloudAdminHostSchedulingService;
|
||||
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);
|
||||
}
|
||||
};
|
||||
$expectError = static function (callable $callback, int $status,
|
||||
string $message): void {
|
||||
try {
|
||||
$callback();
|
||||
} catch (CloudError $error) {
|
||||
if ($error->httpStatus !== $status) {
|
||||
throw new RuntimeException(
|
||||
$message . ': expected HTTP ' . $status . ', got '
|
||||
. $error->httpStatus
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
throw new RuntimeException($message . ': expected CloudError');
|
||||
};
|
||||
$membership = static function (array $payload, int $poolId): array {
|
||||
foreach ($payload['memberships'] as $row) {
|
||||
if ((int) $row['resource_pool_id'] === $poolId) {
|
||||
return $row;
|
||||
}
|
||||
}
|
||||
throw new RuntimeException('Missing membership for pool ' . $poolId);
|
||||
};
|
||||
|
||||
$capsule = new Capsule();
|
||||
$capsule->addConnection([
|
||||
'driver' => 'sqlite',
|
||||
'database' => ':memory:',
|
||||
], 'default');
|
||||
$capsule->setAsGlobal();
|
||||
$capsule->bootEloquent();
|
||||
$schema = $capsule->schema('default');
|
||||
|
||||
$schema->create('configurations', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->string('name')->unique();
|
||||
$table->text('value');
|
||||
});
|
||||
$schema->create('cloud_hosts', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->string('name');
|
||||
$table->boolean('enabled');
|
||||
$table->integer('created_at');
|
||||
$table->integer('updated_at');
|
||||
});
|
||||
$schema->create('cloud_regions', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->string('code');
|
||||
$table->string('name');
|
||||
$table->string('country_code', 2);
|
||||
$table->string('status');
|
||||
$table->integer('sort');
|
||||
$table->integer('created_at');
|
||||
$table->integer('updated_at');
|
||||
});
|
||||
$schema->create('cloud_network_lines', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->integer('region_id');
|
||||
$table->string('code');
|
||||
$table->string('name');
|
||||
$table->boolean('is_default');
|
||||
$table->string('status');
|
||||
$table->integer('sort');
|
||||
$table->integer('created_at');
|
||||
$table->integer('updated_at');
|
||||
});
|
||||
$schema->create('cloud_resource_pools', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->integer('line_id');
|
||||
$table->string('code');
|
||||
$table->string('name');
|
||||
$table->string('status');
|
||||
$table->string('scheduling_policy');
|
||||
$table->integer('sort');
|
||||
$table->integer('created_at');
|
||||
$table->integer('updated_at');
|
||||
});
|
||||
$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');
|
||||
$table->unique(['host_id', 'resource_pool_id']);
|
||||
});
|
||||
$schema->create('cloud_host_capacity_policies',
|
||||
static function (Blueprint $table): void {
|
||||
$table->integer('host_id')->primary();
|
||||
foreach (['configured_cpu_cores', 'configured_memory_mib',
|
||||
'configured_disk_gib', 'safety_cpu_cores', 'safety_memory_mib',
|
||||
'safety_disk_gib', 'max_instances', 'created_at',
|
||||
'updated_at'] as $field) {
|
||||
$table->integer($field);
|
||||
}
|
||||
});
|
||||
$schema->create('cloud_resource_reservations',
|
||||
static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->integer('host_id');
|
||||
$table->integer('resource_pool_id');
|
||||
$table->string('state');
|
||||
$table->integer('instance_id')->nullable();
|
||||
$table->integer('cpu_cores');
|
||||
$table->integer('memory_mib');
|
||||
$table->integer('disk_gib');
|
||||
});
|
||||
$schema->create('cloud_instances', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->integer('host_id');
|
||||
$table->string('status');
|
||||
$table->text('spec_json');
|
||||
});
|
||||
$schema->create('cloud_instance_snapshots',
|
||||
static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->integer('instance_id');
|
||||
$table->integer('host_id');
|
||||
$table->string('state');
|
||||
$table->integer('size_bytes')->nullable();
|
||||
});
|
||||
$schema->create('cloud_host_network_attachments',
|
||||
static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->integer('host_id');
|
||||
$table->integer('line_id');
|
||||
$table->string('state');
|
||||
$table->string('sync_state');
|
||||
$table->integer('observed_revision');
|
||||
$table->integer('desired_revision');
|
||||
});
|
||||
$schema->create('cloud_public_ip_pools',
|
||||
static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->integer('host_id');
|
||||
$table->integer('network_attachment_id');
|
||||
$table->string('state');
|
||||
$table->string('sync_state');
|
||||
$table->integer('observed_revision');
|
||||
$table->integer('desired_revision');
|
||||
});
|
||||
$schema->create('cloud_public_ips', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->integer('host_id');
|
||||
$table->integer('public_ip_pool_id');
|
||||
$table->string('state');
|
||||
$table->integer('reservation_id')->nullable();
|
||||
$table->integer('instance_id')->nullable();
|
||||
$table->string('operation_id')->nullable();
|
||||
});
|
||||
|
||||
$assert(!$schema->hasColumn('cloud_hosts', 'default_network'),
|
||||
'Test schema must prove scheduling no longer reads default_network.');
|
||||
Capsule::table('configurations')->insert([
|
||||
'name' => 'cloud_network_lines_enabled', 'value' => 'true',
|
||||
]);
|
||||
Capsule::table('cloud_hosts')->insert([
|
||||
'id' => 1, 'name' => 'HKG-A', 'enabled' => 1,
|
||||
'created_at' => 1, 'updated_at' => 1,
|
||||
]);
|
||||
Capsule::table('cloud_regions')->insert([
|
||||
['id' => 1, 'code' => 'hkg', 'name' => 'Hong Kong',
|
||||
'country_code' => 'HK', 'status' => 'active', 'sort' => 10,
|
||||
'created_at' => 1, 'updated_at' => 1],
|
||||
['id' => 2, 'code' => 'nrt', 'name' => 'Tokyo',
|
||||
'country_code' => 'JP', 'status' => 'active', 'sort' => 20,
|
||||
'created_at' => 1, 'updated_at' => 1],
|
||||
]);
|
||||
Capsule::table('cloud_network_lines')->insert([
|
||||
['id' => 10, 'region_id' => 1, 'code' => 'hkg-a', 'name' => 'HKG A',
|
||||
'is_default' => 1, 'status' => 'active', 'sort' => 10,
|
||||
'created_at' => 1, 'updated_at' => 1],
|
||||
['id' => 11, 'region_id' => 1, 'code' => 'hkg-b', 'name' => 'HKG B',
|
||||
'is_default' => 0, 'status' => 'active', 'sort' => 20,
|
||||
'created_at' => 1, 'updated_at' => 1],
|
||||
['id' => 20, 'region_id' => 2, 'code' => 'nrt-a', 'name' => 'NRT A',
|
||||
'is_default' => 1, 'status' => 'active', 'sort' => 30,
|
||||
'created_at' => 1, 'updated_at' => 1],
|
||||
]);
|
||||
Capsule::table('cloud_resource_pools')->insert([
|
||||
['id' => 100, 'line_id' => 10, 'code' => 'hkg-a', 'name' => 'HKG A',
|
||||
'status' => 'active', 'scheduling_policy' => 'balanced', 'sort' => 10,
|
||||
'created_at' => 1, 'updated_at' => 1],
|
||||
['id' => 101, 'line_id' => 11, 'code' => 'hkg-b', 'name' => 'HKG B',
|
||||
'status' => 'active', 'scheduling_policy' => 'balanced', 'sort' => 20,
|
||||
'created_at' => 1, 'updated_at' => 1],
|
||||
['id' => 200, 'line_id' => 20, 'code' => 'nrt-a', 'name' => 'NRT A',
|
||||
'status' => 'active', 'scheduling_policy' => 'balanced', 'sort' => 30,
|
||||
'created_at' => 1, 'updated_at' => 1],
|
||||
]);
|
||||
Capsule::table('cloud_host_network_attachments')->insert([
|
||||
['id' => 1, 'host_id' => 1, 'line_id' => 10, 'state' => 'active',
|
||||
'sync_state' => 'applied', 'observed_revision' => 1,
|
||||
'desired_revision' => 1],
|
||||
['id' => 2, 'host_id' => 1, 'line_id' => 11, 'state' => 'active',
|
||||
'sync_state' => 'applied', 'observed_revision' => 1,
|
||||
'desired_revision' => 1],
|
||||
]);
|
||||
Capsule::table('cloud_public_ip_pools')->insert([
|
||||
['id' => 1, 'host_id' => 1, 'network_attachment_id' => 1,
|
||||
'state' => 'active', 'sync_state' => 'applied',
|
||||
'observed_revision' => 1, 'desired_revision' => 1],
|
||||
['id' => 2, 'host_id' => 1, 'network_attachment_id' => 2,
|
||||
'state' => 'active', 'sync_state' => 'applied',
|
||||
'observed_revision' => 1, 'desired_revision' => 1],
|
||||
]);
|
||||
Capsule::table('cloud_public_ips')->insert([
|
||||
['id' => 1, 'host_id' => 1, 'public_ip_pool_id' => 1,
|
||||
'state' => 'available', 'reservation_id' => null,
|
||||
'instance_id' => null, 'operation_id' => null],
|
||||
['id' => 2, 'host_id' => 1, 'public_ip_pool_id' => 2,
|
||||
'state' => 'available', 'reservation_id' => null,
|
||||
'instance_id' => null, 'operation_id' => null],
|
||||
['id' => 3, 'host_id' => 1, 'public_ip_pool_id' => 2,
|
||||
'state' => 'available', 'reservation_id' => null,
|
||||
'instance_id' => null, 'operation_id' => null],
|
||||
]);
|
||||
|
||||
$capacityBody = [
|
||||
'configured_cpu_cores' => 32,
|
||||
'configured_memory_mib' => 65536,
|
||||
'configured_disk_gib' => 1000,
|
||||
'safety_cpu_cores' => 2,
|
||||
'safety_memory_mib' => 2048,
|
||||
'safety_disk_gib' => 50,
|
||||
'max_instances' => 100,
|
||||
];
|
||||
$initial = CloudAdminHostSchedulingService::saveCapacity(1, $capacityBody);
|
||||
$assert($initial['memberships'] === []
|
||||
&& Capsule::table('cloud_host_capacity_policies')->count() === 1,
|
||||
'Capacity save must not create or replace a membership.');
|
||||
|
||||
$first = CloudAdminHostSchedulingService::saveMembership(1, 100, [
|
||||
'scheduler_state' => 'active', 'priority' => 10, 'weight' => 100,
|
||||
'expected_revision' => 0,
|
||||
]);
|
||||
$second = CloudAdminHostSchedulingService::saveMembership(1, 101, [
|
||||
'scheduler_state' => 'active', 'priority' => 20, 'weight' => 200,
|
||||
'expected_revision' => 0,
|
||||
]);
|
||||
$poolA = $membership($second, 100);
|
||||
$poolB = $membership($second, 101);
|
||||
$assert(count($second['memberships']) === 2
|
||||
&& $second['host']['active_memberships'] === 2
|
||||
&& $poolA['revision'] === 1 && $poolB['revision'] === 1
|
||||
&& $poolA['region_id'] === 1 && $poolB['region_id'] === 1
|
||||
&& $poolA['line_id'] === 10 && $poolB['line_id'] === 11
|
||||
&& $poolA['ip_capacity']['available'] === 1
|
||||
&& $poolB['ip_capacity']['available'] === 2
|
||||
&& $second['capacity']['ip']['available'] === 3,
|
||||
'Read did not expose complete enriched same-region memberships and IP capacity.');
|
||||
|
||||
$afterCapacity = CloudAdminHostSchedulingService::saveCapacity(1,
|
||||
array_replace($capacityBody, ['configured_cpu_cores' => 64]));
|
||||
$assert(count($afterCapacity['memberships']) === 2
|
||||
&& $afterCapacity['policy']['configured_cpu_cores'] === 64
|
||||
&& $membership($afterCapacity, 100)['revision'] === 1
|
||||
&& $membership($afterCapacity, 101)['revision'] === 1
|
||||
&& Capsule::table('cloud_host_capacity_policies')->count() === 1,
|
||||
'Host-global capacity save changed independently revisioned memberships.');
|
||||
|
||||
$draining = CloudAdminHostSchedulingService::saveMembership(1, 100, [
|
||||
'scheduler_state' => 'draining', 'priority' => 10, 'weight' => 100,
|
||||
'expected_revision' => 1,
|
||||
]);
|
||||
$assert($membership($draining, 100)['scheduler_state'] === 'draining'
|
||||
&& $membership($draining, 100)['revision'] === 2
|
||||
&& $membership($draining, 101)['scheduler_state'] === 'active'
|
||||
&& $membership($draining, 101)['revision'] === 1,
|
||||
'Updating one membership changed its sibling.');
|
||||
$expectError(static function (): void {
|
||||
CloudAdminHostSchedulingService::saveMembership(1, 100, [
|
||||
'scheduler_state' => 'active', 'priority' => 10, 'weight' => 100,
|
||||
'expected_revision' => 1,
|
||||
]);
|
||||
}, 409, 'Stale membership revision was accepted');
|
||||
$expectError(static function (): void {
|
||||
CloudAdminHostSchedulingService::saveMembership(1, 200, [
|
||||
'scheduler_state' => 'active', 'priority' => 30, 'weight' => 100,
|
||||
'expected_revision' => 0,
|
||||
]);
|
||||
}, 409, 'Cross-region active membership was accepted');
|
||||
|
||||
Capsule::table('cloud_resource_reservations')->insert([
|
||||
'id' => 1, 'host_id' => 1, 'resource_pool_id' => 100,
|
||||
'state' => 'committed', 'instance_id' => null,
|
||||
'cpu_cores' => 1, 'memory_mib' => 512, 'disk_gib' => 5,
|
||||
]);
|
||||
$expectError(static function (): void {
|
||||
CloudAdminHostSchedulingService::saveMembership(1, 100, [
|
||||
'scheduler_state' => 'disabled', 'priority' => 10, 'weight' => 100,
|
||||
'expected_revision' => 2,
|
||||
]);
|
||||
}, 409, 'Exact-pool reservation did not block membership disable');
|
||||
|
||||
$disabledB = CloudAdminHostSchedulingService::saveMembership(1, 101, [
|
||||
'scheduler_state' => 'disabled', 'priority' => 20, 'weight' => 200,
|
||||
'expected_revision' => 1,
|
||||
]);
|
||||
$assert($membership($disabledB, 101)['revision'] === 2
|
||||
&& $membership($disabledB, 101)['scheduler_state'] === 'disabled',
|
||||
'Reservation in another pool incorrectly blocked disable.');
|
||||
$afterDelete = CloudAdminHostSchedulingService::deleteMembership(1, 101, 2);
|
||||
$assert(count($afterDelete['memberships']) === 1
|
||||
&& $membership($afterDelete, 100)['revision'] === 2,
|
||||
'Deleting one empty membership disturbed its sibling.');
|
||||
$expectError(static function (): void {
|
||||
CloudAdminHostSchedulingService::deleteMembership(1, 100, 2);
|
||||
}, 409, 'Exact-pool reservation did not block membership deletion');
|
||||
$expectError(static function (): void {
|
||||
CloudAdminHostSchedulingService::deleteMembership(1, 101, 2);
|
||||
}, 404, 'Deleting an absent membership did not return not found');
|
||||
|
||||
echo "Administrator host multi-membership behavior passed.\n";
|
||||
Loading…
Reference in New Issue