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

68 lines
3.3 KiB
PHP

<?php
// Static regression checks for the instance read path. These protect the two
// performance properties that are also security boundaries: list inventory is
// fetched once per visible host and engine rows are intersected with the
// actor-scoped database UUID set; detail pages never refresh the full list.
$root = dirname(__DIR__);
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$section = static function (string $source, string $start, string $end): string {
$offset = strpos($source, $start);
if ($offset === false) {
return '';
}
$limit = strpos($source, $end, $offset + strlen($start));
return $limit === false ? substr($source, $offset)
: substr($source, $offset, $limit - $offset);
};
$controller = file_get_contents($root . '/app/Controllers/CloudApiController.php');
$runtime = file_get_contents($root . '/public/cloud/runtime.js');
$detail = file_get_contents($root . '/public/cloud/detail.js');
$contexts = $section($controller, 'private function vmListContexts(',
'private function vmData(');
$list = $section($controller, 'public function vms(', 'public function vm(');
$detailApi = $section($controller, 'public function vm(', 'public function metrics(');
$assert(str_contains($contexts,
"groupBy(fn (\$instance) => (int) \$instance->host_id)")
&& substr_count($contexts, "->host((int) \$hostId, 'GET', 'vms', \$actor)") === 1,
'Instance inventory is not requested once per visible host.');
$assert(str_contains($contexts, 'CloudHost::whereIn')
&& str_contains($contexts, 'CloudServicePeriod::whereIn')
&& str_contains($contexts, "->groupBy('instance_id')"),
'Instance host and service-period dependencies are no longer bulk loaded.');
$assert(str_contains($contexts,
"\$allowed[strtolower((string) \$instance->uuid)] = (int) \$instance->id")
&& str_contains($contexts,
"\$instanceId = \$allowed[strtolower(trim(\$vm['uuid']))] ?? null")
&& str_contains($contexts, 'if ($instanceId !== null)'),
'Engine inventory is not strictly intersected with actor-visible database UUIDs.');
$assert(str_contains($list, '$contexts = $this->vmListContexts($instances, $actor)')
&& str_contains($list, '$contexts[(int) $instance->id] ?? []'),
'The VM list bypasses its bulk contexts and may reintroduce per-row reads.');
$assert(str_contains($detailApi,
'$this->vmData(CloudAccess::instance($args[\'id\'], $actor), $actor)'),
'The single-instance API no longer starts from the owner-scoped access check.');
$loadVm = $section($runtime, 'async function loadVm(',
'function renderVmDetail(');
$selection = $section($runtime, 'async function selectVmByApiId(',
'function bindPageCleanup(');
$assert(str_contains($loadVm, "api(`/api/v1/vms/\${encodeURIComponent(id)}`)")
&& str_contains($loadVm, 'state.vms = [vm]'),
'The detail loader does not use the single-instance API.');
$assert(str_contains($selection, 'await loadVm(apiId, true)')
&& !str_contains($selection, 'loadVms('),
'Detail selection still fetches the full VM list.');
$assert(!str_contains($detail, 'loadVms')
&& substr_count($detail, 'loadVm(vmId)') >= 4,
'Detail refresh or polling still loads every instance.');
echo "Cloud instance bulk-list and single-detail read paths passed.\n";