isFile() || $entry->isLink()) { unlink($entry->getPathname()); } } rmdir($directory); }; try { $now = 2_000_000_000; $ingest = new class { public int $ticks = 0; public function tick(): bool { ++$this->ticks; return true; } public function observationErrors(): array { return []; } }; $retention = new class { public int $runs = 0; public function prune(): array { ++$this->runs; return ['daily_summaries' => 1, 'resolved_findings' => 0, 'alert_events' => 0, 'deliveries' => 0]; } }; $heartbeat = CloudRuntimeHeartbeat::start( 'abuse_monitor', true, $temporary, static fn (): int => $now ); $status = CloudAbuseMonitor::run( ['cloudAbuseMonitor', '--once'], $heartbeat, $ingest, $retention, static fn (): int => $now, static function (int $_microseconds): void { throw new RuntimeException('Once mode attempted to sleep.'); } ); $records = iterator_to_array(new FilesystemIterator($temporary)); $record = json_decode( file_get_contents(reset($records)->getPathname()), true, 16, JSON_THROW_ON_ERROR ); $assert($status === 0 && $ingest->ticks === 1 && $retention->runs === 1 && $record['kind'] === 'abuse_monitor' && $record['state'] === 'stopped' && $record['last_success'] === $now && $record['unresolved_error'] === false, 'Direct observe-only worker did not complete cleanly.'); $failureDirectory = $temporary . '-failure'; mkdir($failureDirectory, 0700, true); $failedIngest = new class { public function tick(): bool { return true; } public function observationErrors(): array { return ['network_observation_collection_failed']; } }; $idleRetention = new class { public function prune(): array { return ['daily_summaries' => 0, 'resolved_findings' => 0, 'alert_events' => 0, 'deliveries' => 0]; } }; $failedHeartbeat = CloudRuntimeHeartbeat::start( 'abuse_monitor', true, $failureDirectory, static fn (): int => $now ); $failedStatus = CloudAbuseMonitor::run( ['cloudAbuseMonitor', '--once'], $failedHeartbeat, $failedIngest, $idleRetention, static fn (): int => $now ); $failedFiles = iterator_to_array( new FilesystemIterator($failureDirectory) ); $failedRecord = json_decode( file_get_contents(reset($failedFiles)->getPathname()), true, 16, JSON_THROW_ON_ERROR ); $assert($failedStatus === 1 && $failedRecord['state'] === 'stopped' && $failedRecord['last_error'] === 'abuse_monitor_collection_failed' && $failedRecord['unresolved_error'] === true, 'A caught collection failure was falsely reported healthy.'); $cleanup($failureDirectory); // A successful zero-work retry must clear a prior retention warning. $recoveryDirectory = $temporary . '-retention-recovery'; mkdir($recoveryDirectory, 0700, true); $idleIngest = new class { public function tick(): bool { return false; } public function observationErrors(): array { return []; } }; $recoveringRetention = new class { public int $runs = 0; public function prune(): array { if (++$this->runs === 1) { throw new RuntimeException('simulated retention outage'); } return ['daily_summaries' => 0, 'resolved_findings' => 0, 'alert_events' => 0, 'deliveries' => 0]; } }; $recoveryNow = $now; $sleepCalls = 0; $recoveryHeartbeat = CloudRuntimeHeartbeat::start( 'abuse_monitor', false, $recoveryDirectory, static function () use (&$recoveryNow): int { return $recoveryNow; } ); try { CloudAbuseMonitor::run( ['cloudAbuseMonitor'], $recoveryHeartbeat, $idleIngest, $recoveringRetention, static function () use (&$recoveryNow): int { return $recoveryNow; }, static function (int $_microseconds) use ( &$sleepCalls, &$recoveryNow, $recoveryDirectory, $assert ): void { ++$sleepCalls; $recoveryNow += 301; if ($sleepCalls < 2) { return; } $files = iterator_to_array( new FilesystemIterator($recoveryDirectory) ); $row = json_decode( file_get_contents(reset($files)->getPathname()), true, 16, JSON_THROW_ON_ERROR ); $assert($row['state'] === 'idle' && $row['unresolved_error'] === false && $row['last_success'] === $recoveryNow - 301, 'Successful zero-work retention did not clear warning.'); throw new RuntimeException('stop daemon fixture'); } ); throw new RuntimeException('Daemon fixture did not stop.'); } catch (RuntimeException $error) { if ($error->getMessage() !== 'stop daemon fixture') { throw $error; } } $cleanup($recoveryDirectory); $command = $read('web/app/Command/CloudAbuseMonitor.php'); $xcat = $read('web/app/Command/XCat.php'); $heartbeatSource = $read( 'web/app/Services/Cloud/CloudRuntimeHeartbeat.php' ); $runtime = $read( 'web/app/Services/Cloud/CloudRuntimeHealthService.php' ); $unit = $read('deploy/virt-cloud-abuse-monitor.service'); $deployment = $read('docs/DEPLOYMENT.md'); $assert(str_contains($xcat, "'cloudAbuseMonitor'") && str_contains($xcat, 'CloudAbuseMonitor::run') && str_contains($command, 'CloudAbuseObservationIngestService') && str_contains($command, 'CloudAbuseRetentionService') && !str_contains($command, 'CloudAbuseProjectionService') && !str_contains($command, 'PROJECTION_BURST') && !str_contains($command, "table('cloud_jobs')") && !str_contains($command, 'CloudJob'), 'Monitor command retained projection or job-queue coupling.'); $assert(str_contains($heartbeatSource, "'abuse_monitor'") && str_contains($heartbeatSource, "'abuse_monitor_ingest'") && !str_contains($heartbeatSource, 'abuse_monitor_projection') && str_contains($runtime, "'abuse_monitor' => '网络滥用监测'"), 'Runtime observation retained a projection phase.'); $assert(str_contains($unit, 'ExecStart=/usr/bin/php /home/virt-cloud/web/xcat cloudAbuseMonitor') && str_contains($unit, 'NoNewPrivileges=true') && str_contains($unit, 'ProtectSystem=strict') && str_contains($unit, 'LimitCORE=0') && str_contains($deployment, 'virt-cloud-abuse-monitor.service'), 'Monitor systemd isolation or deployment docs are incomplete.'); } finally { $cleanup($temporary); } echo "Direct abuse monitor worker contract tests passed.\n";