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

95 lines
4.7 KiB
PHTML

<?php
// The operation error catalog is a trust-boundary contract. Keep this test
// independent from the database and engine so it can run in a minimal CI job.
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudOperationErrorCatalog;
use App\Services\Cloud\CloudInstanceMetadata;
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
foreach (['errorMessage', 'stderrText', 'debugInfo', 'error-code', 'engine.error.detail'] as $key) {
$assert(CloudOperationErrorCatalog::isDiagnosticKey($key),
'Diagnostic spelling bypassed the normalized key catalog: ' . $key);
}
$raw = [
'status' => 'succeeded',
'phase' => 'Traceback: /var/lib/libvirt/private.xml',
'errorMessage' => 'libvirtError: source=/srv/secret-volume',
'safe_to_refund' => true,
'unknown_engine_field' => 'host-internal-value',
'vm' => [
'uuid' => '11111111-1111-4111-8111-111111111111',
'status' => 'succeeded',
'disks' => [['source' => '/srv/secret-volume', 'capacity_gib' => 40]],
'rescue_volume_name' => 'rescue-secret',
'console_port' => 5901,
],
];
$user = CloudOperationErrorCatalog::result($raw, false, 'action', 'failed');
$assert(!isset($user['errorMessage'], $user['diagnostic'], $user['safe_to_refund'],
$user['unknown_engine_field'])
&& !isset($user['vm']['disks'], $user['vm']['rescue_volume_name'], $user['vm']['console_port'])
&& !str_contains(json_encode($user, JSON_UNESCAPED_UNICODE), '/var/lib'),
'User operation result retained raw engine diagnostics or storage metadata.');
$admin = CloudOperationErrorCatalog::result($raw, true, 'action', 'failed');
$adminJson = json_encode($admin, JSON_UNESCAPED_UNICODE);
$assert(isset($admin['diagnostic'])
&& !str_contains($adminJson, '/var/lib') && !str_contains($adminJson, '/srv/secret'),
'Administrator operation result did not retain a redacted diagnostic summary.');
$durable = CloudOperationErrorCatalog::workerResult($raw);
$assert(!isset($durable['errorMessage'], $durable['diagnostic'], $durable['safe_to_refund'])
&& !isset($durable['vm']['disks'], $durable['vm']['rescue_volume_name'], $durable['vm']['console_port'])
&& !str_contains(json_encode($durable, JSON_UNESCAPED_UNICODE), 'host-internal-value'),
'Durable operation result retained a non-allowlisted engine field.');
$assert(CloudOperationErrorCatalog::userStatus('COMPLETED') === 'completed'
&& CloudOperationErrorCatalog::userStatus('host-internal-state') === null,
'Unknown operation status bypassed the user status allow-list.');
$phase = CloudOperationErrorCatalog::userPhase('宿主机 /var/lib/libvirt', 'running');
$assert($phase === '正在处理', 'Unknown operation phase was exposed to the user.');
$assert(CloudOperationErrorCatalog::userPhase('重装完成', 'uncertain') === '等待核对结果',
'An uncertain reinstall retained a stale successful phase.');
$assert(CloudOperationErrorCatalog::userPhase('在线快照创建完成', 'uncertain', 'snapshot_create')
=== '在线快照创建完成',
'An uncertain snapshot lost its reviewed phase label.');
$reinstall = CloudOperationErrorCatalog::workerResult([
'status' => 'uncertain', 'phase' => '重装完成',
'reinstall_committed' => true, 'cleanup_pending' => true,
'warning' => '旧存储卷 /srv/private.qcow2 删除失败',
]);
$assert(($reinstall['reinstall_committed'] ?? false) === true
&& ($reinstall['cleanup_pending'] ?? false) === true
&& !isset($reinstall['warning']),
'Reinstall cleanup marker was lost or raw warning was persisted.');
$assert(CloudOperationErrorCatalog::workerSummary('reinstall', 'uncertain', [
'error_code' => 'reinstall_cleanup_pending',
]) === 'reinstall_cleanup_pending'
&& str_contains((string) CloudOperationErrorCatalog::userMessage(
'reinstall', 'uncertain', 'reinstall_cleanup_pending'), '旧系统盘清理'),
'Reinstall cleanup uncertainty did not retain its reviewed error code.');
$publicSpec = CloudInstanceMetadata::publicSpec([
'plan_name' => 'Basic', 'public_ip' => '203.0.113.10',
'region' => str_repeat('x', 500), 'memory_mib' => 'not-a-number',
'host_private_path' => '/srv/libvirt/secret.qcow2',
'engine_debug' => 'libvirt traceback',
]);
$publicJson = json_encode($publicSpec, JSON_UNESCAPED_UNICODE);
$assert(isset($publicSpec['plan_name'], $publicSpec['public_ip'])
&& !isset($publicSpec['region'], $publicSpec['memory_mib'])
&& !isset($publicSpec['host_private_path'], $publicSpec['engine_debug'])
&& !str_contains($publicJson, '/srv/libvirt'),
'Public instance spec copied an unallowlisted historical field.');
echo "Cloud operation error catalog checks passed.\n";