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/CloudObjectStorageViewTest.php

143 lines
6.0 KiB
PHTML

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
set_error_handler(static function (int $severity, string $message, string $file, int $line): bool {
if ((error_reporting() & $severity)
&& ($severity & (E_WARNING | E_USER_WARNING | E_DEPRECATED))) {
throw new ErrorException($message, 0, $severity, $file, $line);
}
return false;
});
$smarty = new Smarty();
$compileDir = sys_get_temp_dir() . '/object-storage-view-' . bin2hex(random_bytes(6));
mkdir($compileDir);
$smarty->setCompileDir($compileDir);
$smarty->setTemplateDir(dirname(__DIR__) . '/resources/views/venix');
$smarty->assign('config', ['appName' => 'Virt Test']);
$smarty->assign('cdn', '');
$smarty->assign('user', (object) ['isLogin' => false]);
$smarty->assign('admin_csrf_token', str_repeat('c', 64));
$smarty->assign('admin_display_user_name', 'admin');
$backend = [
'id' => 7,
'name' => '"><script>window.__storageXss=1</script>主存储',
'endpoint' => 'https://s3-a.example.com',
'region' => 'us-east-1',
'bucket' => 'virt-primary',
'prefix' => 'virt-cloud',
'path_style' => false,
'failure_domain' => 'provider-a',
'write_priority' => 10,
'admin_state' => 'active',
'health_state' => 'healthy',
'credentials_configured' => true,
'revision' => 4,
'tested_revision' => 4,
'last_tested_at' => 1792876800,
'last_tested_at_text' => '2026-10-25 00:00:00',
'last_error' => '',
'can_edit' => true,
'can_test' => true,
'can_activate' => false,
'can_drain' => true,
'can_resume' => false,
'can_disable' => true,
];
$cleanup = [
'summary' => ['count' => 1, 'bytes' => 4096, 'attention_count' => 1],
'items' => [[
'id' => 31, 'name' => 'automatic-31', 'source_instance_id' => 41,
'user_id' => 5, 'state' => 'delete_attention',
'object_size_bytes' => 4096, 'delete_generation' => 2,
'cycle_attempts' => 3, 'next_check_at' => 0,
'attention_at' => 1792877200,
'error_code' => 'reconciliation_limit_reached', 'can_retry' => true,
]],
'pagination' => ['page' => 1, 'per_page' => 20, 'total' => 1, 'total_pages' => 1],
];
$summary = [
'backend_count' => 1, 'write_ready_count' => 1,
'protected_artifact_count' => 0, 'degraded_artifact_count' => 0,
'replication_backlog_count' => 0,
];
$render = static function (bool $canManage) use (
$smarty, $backend, $cleanup, $summary
): string {
$smarty->assign('admin_can_manage', $canManage);
$smarty->assign('object_storage_backends', [$backend]);
$smarty->assign('object_storage_summary', $summary);
$smarty->assign('object_storage_error', '');
$smarty->assign('cleanup_debt', $cleanup);
$smarty->assign('cleanup_debt_error', '');
return $smarty->fetch('admin/object_storage.tpl');
};
$html = $render(true);
$readonlyHtml = $render(false);
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$parse = static function (string $document): DOMXPath {
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML('<?xml encoding="UTF-8">' . $document);
return new DOMXPath($dom);
};
$one = static function (DOMXPath $xpath, string $query) use ($assert): DOMElement {
$nodes = $xpath->query($query);
$assert($nodes->length === 1 && $nodes->item(0) instanceof DOMElement,
'Expected one element matching ' . $query);
return $nodes->item(0);
};
$xpath = $parse($html);
$assert($xpath->query('//main[@data-object-storage-page]')->length === 1
&& $xpath->query('//section[contains(@class,"os-summary")]/article')->length === 4,
'Object-storage summary is incomplete.');
$assert($xpath->query('//*[@role="tab" and @data-os-tab]')->length === 3
&& $xpath->query('//*[@role="tabpanel" and @data-os-panel]')->length === 3,
'Backends, protection and cleanup tabs are missing.');
$card = $one($xpath, '//*[@data-os-backend and @data-backend-id="7"]');
$assert($card->getAttribute('data-revision') === '4'
&& $card->getAttribute('data-credentials-configured') === '1',
'Backend card lost its non-secret edit metadata.');
$assert($xpath->query('//script[contains(.,"__storageXss")]')->length === 0,
'Backend name escaped into executable markup.');
$assert($xpath->query('//*[@data-os-action="test"]')->length === 1
&& $xpath->query('//*[@data-os-action="drain"]')->length === 1
&& $xpath->query('//*[@data-os-action="disable"]')->length === 1,
'Backend lifecycle controls are missing.');
$form = $one($xpath, '//form[@id="objectStorageForm"]');
$assert($form->getAttribute('method') === 'post'
&& $form->getAttribute('data-can-manage') === '1'
&& $one($xpath, '//input[@name="backend_id"]')->getAttribute('value') === '',
'Create/edit drawer lost its permission or identity boundary.');
foreach (['osAccessKey', 'osSecretKey'] as $id) {
$secret = $one($xpath, '//input[@id="' . $id . '"]');
$assert($secret->getAttribute('type') === 'password'
&& $secret->getAttribute('value') === '',
'A stored credential can be rendered into the browser.');
}
$assert($xpath->query('//*[@class="os-debt-list"]/*[@role="listitem"]')->length === 1
&& $xpath->query('//table')->length === 0
&& !str_contains($html, 'object_version_id'),
'Cleanup debt lost responsive rendering or exposed provider identity.');
$readonly = $parse($readonlyHtml);
$assert($one($readonly, '//fieldset')->hasAttribute('disabled')
&& $one($readonly, '//button[@id="osAddBackend"]')->hasAttribute('disabled')
&& $one($readonly, '//*[@data-os-edit]')->hasAttribute('disabled')
&& $one($readonly, '//*[@data-os-action="test"]')->hasAttribute('disabled')
&& $one($readonly, '//*[@data-os-action="drain"]')->hasAttribute('disabled')
&& $one($readonly, '//*[@data-os-action="disable"]')->hasAttribute('disabled'),
'Ordinary administrator controls are not read-only.');
$assert(str_contains($html, 'window.ADMIN_CSRF_TOKEN ='),
'Shared administrator CSRF bootstrap is missing.');
echo "Object-storage multi-backend administrator rendering passed.\n";