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

60 lines
2.9 KiB
PHP

<?php
$root = dirname(__DIR__, 2);
$read = static function (string $path) use ($root): string {
$contents = file_get_contents($root . '/' . $path);
if (!is_string($contents)) {
throw new RuntimeException('Unable to read ' . $path);
}
return $contents;
};
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$xcat = $read('web/app/Command/XCat.php');
$command = $read('web/app/Command/CloudStorageHealthWorker.php');
$service = $read('web/app/Services/Cloud/CloudObjectStorageHealthProbeService.php');
$heartbeat = $read('web/app/Services/Cloud/CloudRuntimeHeartbeat.php');
$runtime = $read('web/app/Services/Cloud/CloudRuntimeHealthService.php');
$unit = $read('deploy/virt-cloud-storage-health-worker.service');
$replicaUnit = $read('deploy/virt-cloud-storage-replica-worker.service');
$deployment = $read('docs/DEPLOYMENT.md');
$assert(str_contains($xcat, "'cloudStorageHealthWorker'")
&& str_contains($xcat, 'CloudStorageHealthWorker::run'),
'Health worker command is not reachable through xcat.');
$assert(str_contains($command, 'CloudObjectStorageHealthProbeService')
&& str_contains($command, "in_array('--once'")
&& str_contains($command, "'storage_health'")
&& str_contains($command, "busy('storage_health_probe')")
&& str_contains($command, "error('storage_health_failed')"),
'Health worker lost its independent loop, heartbeat, or once mode.');
$assert(str_contains($service, '->lockForUpdate()->first()')
&& str_contains($service, "where('next_probe_at', '<=', \$now)")
&& str_contains($service, "'claim_until' => \$claimUntil")
&& str_contains($service, "config_revision !== \$claim['revision']")
&& str_contains($service, "next_probe_at !== \$claim['claim_until']")
&& str_contains($service, "admin_state === 'active'")
&& str_contains($service, "admin_state = 'read_only'")
&& !str_contains($service, 'access_key_id')
&& !str_contains($service, 'secret_access_key'),
'Probe lost its one-row claim, frozen revision settlement, write fence, or credential boundary.');
$assert(str_contains($unit,
'ExecStart=/usr/bin/php /home/virt-cloud/web/xcat cloudStorageHealthWorker')
&& str_contains($unit, 'NoNewPrivileges=true')
&& str_contains($unit, 'ProtectSystem=strict')
&& str_contains($unit, 'LimitCORE=0')
&& !str_contains($replicaUnit, 'cloudStorageHealthWorker'),
'Health worker systemd isolation or hardening is incomplete.');
$assert(str_contains($heartbeat, "'storage_health'")
&& str_contains($heartbeat, "'storage_health_probe'")
&& str_contains($runtime, "'storage_health' => '对象存储健康探测'")
&& str_contains($deployment, 'virt-cloud-storage-health-worker.service')
&& str_contains($deployment, '事务外执行最长 15 秒'),
'Health worker deployment or runtime observability is incomplete.');
echo "Storage health worker contract tests passed.\n";