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

172 lines
7.8 KiB
PHP

<?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');
$state = [
'name' => '"><script>window.__objectStorageXss=1</script>测试存储',
'driver' => 's3',
'endpoint' => 'https://minio.example.com:9000',
'region' => 'us-east-1',
'bucket' => 'test-bucket',
'prefix' => 'virt-cloud/backups',
'path_style' => true,
'enabled' => true,
'ready' => true,
'configured' => true,
'credentials_configured' => true,
'revision' => 7,
'tested_revision' => 7,
'last_test_status' => 'healthy',
'last_error_code' => '',
'last_error' => '',
'last_tested_at' => 1792876800,
'last_tested_at_text' => '2026-10-25 00:00:00',
'status_label' => '已启用',
'status_class' => 'success',
];
$cleanupDebt = [
'summary' => ['count' => 2, 'bytes' => 4096, 'attention_count' => 1],
'items' => [
[
'id' => '31',
'name' => 'automatic-31',
'source_instance_id' => '41',
'user_id' => '5',
'state' => 'delete_uncertain',
'object_size_bytes' => 1024,
'delete_generation' => 1,
'cycle_attempts' => 1,
'next_check_at' => 1792877100,
'attention_at' => null,
'error_code' => 'delete_effect_unknown',
'can_retry' => false,
],
[
'id' => '32',
'name' => 'automatic-32',
'source_instance_id' => '42',
'user_id' => '6',
'state' => 'delete_attention',
'object_size_bytes' => 3072,
'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' => 2,
'total_pages' => 1,
],
];
$render = static function (bool $canManage) use ($smarty, $state, $cleanupDebt): string {
$smarty->assign('admin_can_manage', $canManage);
$smarty->assign('object_storage', $state);
$smarty->assign('object_storage_error', '');
$smarty->assign('cleanup_debt', $cleanupDebt);
$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(concat(" ",normalize-space(@class)," ")," os-summary ")]/article')->length === 4,
'Object-storage operations overview or its four summary cards are missing.');
$assert($xpath->query('//*[@role="tab" and @data-os-tab]')->length === 2
&& $xpath->query('//*[@role="tabpanel" and @data-os-panel]')->length === 2
&& $one($xpath, '//*[@data-os-panel="cleanup"]')->hasAttribute('hidden'),
'Object-storage overview and cleanup workspaces are not accessible tabs.');
$assert($xpath->query('//*[@class="os-debt-list"]/*[@role="listitem"]')->length === 2
&& $xpath->query('//table')->length === 0
&& str_contains($html, '下次核对')
&& str_contains($html, '停止于'),
'Cleanup debt did not render its responsive state and scheduling list.');
$form = $one($xpath, '//form[@id="objectStorageForm"]');
$assert($form->getAttribute('action') === '/admin/object-storage/config'
&& $form->getAttribute('method') === 'post'
&& $form->getAttribute('data-can-manage') === '1'
&& $form->getAttribute('data-credentials-configured') === '1',
'Object-storage form method, endpoint, or permission marker changed.');
$assert($xpath->query('//*[@id="osConfigDrawer" and @hidden]//*[@role="dialog"]//form[@id="objectStorageForm"]')->length === 1
&& !$one($xpath, '//button[@id="osEditConfig"]')->hasAttribute('disabled'),
'Writable object-storage connection is not isolated in its edit drawer.');
$assert($one($xpath, '//input[@id="osName"]')->getAttribute('value') === $state['name']
&& $xpath->query('//script[contains(.,"__objectStorageXss")]')->length === 0,
'Object-storage application name escaped its value attribute.');
foreach (['osAccessKey', 'osSecretKey'] as $id) {
$secret = $one($xpath, '//input[@id="' . $id . '"]');
$assert($secret->getAttribute('type') === 'password'
&& $secret->getAttribute('value') === '',
'A stored object-storage credential can be rendered into the browser.');
}
$assert($one($xpath, '//*[@id="osCredentialFields"]')->hasAttribute('hidden')
&& !$one($xpath, '//input[@id="osAccessKey"]')->hasAttribute('required')
&& !$one($xpath, '//input[@id="osSecretKey"]')->hasAttribute('required'),
'Stored credentials must remain unchanged until replacement is explicitly enabled.');
$assert($one($xpath, '//input[@name="expected_revision"]')->getAttribute('value') === '7'
&& !$one($xpath, '//button[@id="osSave"]')->hasAttribute('disabled')
&& !$one($xpath, '//button[@id="osTest"]')->hasAttribute('disabled')
&& !$one($xpath, '//button[@id="osDisable"]')->hasAttribute('disabled'),
'Writable object-storage state did not render its current revision and actions.');
$readonly = $parse($readonlyHtml);
$assert($one($readonly, '//fieldset')->hasAttribute('disabled')
&& $one($readonly, '//button[@id="osEditConfig"]')->hasAttribute('disabled')
&& $one($readonly, '//button[@id="osSave"]')->hasAttribute('disabled')
&& $one($readonly, '//button[@id="osTest"]')->hasAttribute('disabled')
&& $one($readonly, '//button[@id="osDisable"]')->hasAttribute('disabled'),
'Ordinary administrator object-storage controls are not read-only.');
$assert(str_contains($html, 'window.ADMIN_CSRF_TOKEN ='),
'Shared administrator CSRF bootstrap is missing from object-storage page.');
$assert($xpath->query('//details[contains(concat(" ",normalize-space(@class)," ")," os-boundaries ")]')->length === 1,
'Object-storage runtime boundaries are not available as secondary detail.');
$footer = file_get_contents(dirname(__DIR__) . '/resources/views/venix/admin/static_footer.tpl');
$assert(is_string($footer)
&& str_contains($footer, "{include file='admin/components/danger_dialog.tpl'}")
&& str_contains($footer, '/cloud/admin-danger-dialog.js?v={$venix_asset_version}')
&& $xpath->query('//*[@id="osConfirmDialog"]')->length === 0,
'Object-storage destructive actions are not delegated to the shared administrator confirmation dialog.');
echo "Object-storage administrator template rendering passed.\n";