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

125 lines
5.0 KiB
PHP

<?php
$root = dirname(__DIR__);
$mainSql = file_get_contents($root . '/sql/cloud.sql');
$logSql = file_get_contents($root . '/sql/cloud_log.sql');
$setup = file_get_contents($root . '/app/Command/CloudSetup.php');
$deployment = file_get_contents(dirname($root) . '/docs/DEPLOYMENT.md');
if (!is_string($mainSql) || !is_string($logSql)
|| !is_string($setup) || !is_string($deployment)) {
throw new RuntimeException('Unable to read cloud schema');
}
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$tables = [
'cloud_network_monitor_hosts',
'cloud_network_instance_ledger',
'cloud_network_daily_summaries',
'cloud_network_findings',
'cloud_network_alert_events',
'cloud_network_alert_deliveries',
];
foreach ($tables as $table) {
$assert(
preg_match('/CREATE TABLE ' . preg_quote($table, '/')
. ' \\((.*?)\\) ENGINE=/s', $mainSql, $matches) === 1,
'Missing compact abuse table: ' . $table
);
$definitions[$table] = $matches[1];
}
$ledger = $definitions['cloud_network_instance_ledger'];
$assert(str_contains($ledger, 'instance_id BIGINT UNSIGNED NOT NULL PRIMARY KEY')
&& str_contains($ledger, 'detector_state_json JSON NOT NULL')
&& str_contains($ledger, 'current_day_json JSON NOT NULL'),
'Instance ledger lost its single-row detector contract.');
$daily = $definitions['cloud_network_daily_summaries'];
$assert(str_contains($daily, 'PRIMARY KEY (instance_id, day_start)')
&& str_contains($daily, 'cloud_network_daily_retention')
&& str_contains($daily, 'sample_count INT UNSIGNED NOT NULL'),
'Daily abuse summaries lost their bounded instance/day identity.');
$finding = $definitions['cloud_network_findings'];
$event = $definitions['cloud_network_alert_events'];
$assert(str_contains($finding, 'id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT')
&& str_contains($finding, 'cloud_network_finding_scope')
&& str_contains($event, 'id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT')
&& str_contains($event, 'cloud_network_alert_finding_revision'),
'Sparse findings or transitions lost their numeric identity.');
foreach (array_keys($definitions) as $table) {
$definition = $definitions[$table];
foreach ([
'sample_key', 'placement_key', 'window_id', 'vm_uuid',
'policy_hash',
] as $legacyIdentity) {
$assert(!str_contains($definition, $legacyIdentity),
$table . ' retained high-rate legacy identity '
. $legacyIdentity . '.');
}
}
$assert(str_contains($definitions['cloud_network_alert_deliveries'],
"state IN ('pending','dispatching','sent','failed','unknown','skipped')")
&& str_contains($definitions['cloud_network_alert_deliveries'],
'content_hash CHAR(64)')
&& str_contains($definitions['cloud_network_alert_deliveries'],
'chk_cloud_network_alert_delivery_destination')
&& str_contains($definitions['cloud_network_alert_deliveries'],
'chk_cloud_network_alert_delivery_shape'),
'Telegram delivery lost its conservative unknown-effect fence.');
$legacyMainTables = [
'cloud_abuse_instance_states',
'cloud_abuse_findings',
'cloud_abuse_alert_events',
'cloud_abuse_alert_event_items',
'cloud_abuse_alert_deliveries',
'cloud_abuse_projection_receipts',
];
$legacyLogTables = [
'cloud_vm_network_observation_samples',
'cloud_vm_network_observation_rollups',
'cloud_vm_network_projection_outbox',
'cloud_vm_network_monitor_cursors',
'cloud_vm_network_baselines',
];
foreach ($legacyMainTables as $table) {
$assert(!str_contains($mainSql, 'CREATE TABLE ' . $table . ' ('),
'Fresh main schema retained legacy abuse table ' . $table . '.');
$assert(str_contains($setup, "'" . $table . "'"),
'CloudSetup no longer refuses retired main table ' . $table . '.');
}
foreach ($legacyLogTables as $table) {
$assert(!str_contains($logSql, 'CREATE TABLE ' . $table . ' ('),
'Fresh log schema retained legacy abuse table ' . $table . '.');
$assert(str_contains($setup, "'" . $table . "'"),
'CloudSetup no longer refuses retired log table ' . $table . '.');
}
foreach ([
'CloudAbuseProjectionService.php',
'CloudAbuseObservationRollupStore.php',
'CloudAbuseAlertEventCoordinator.php',
] as $legacyClass) {
$assert(!is_file($root . '/app/Services/Cloud/' . $legacyClass),
'Legacy abuse pipeline class still exists: ' . $legacyClass);
}
$assert(str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v57-compact-abuse-ledger'")
&& str_contains($setup, 'private const RETIRED_MAIN_TABLES')
&& str_contains($setup, 'private const RETIRED_LOG_TABLES')
&& str_contains($setup, "'cloud_network_alert_delivery_admin' => [")
&& str_contains($deployment, '不再保存逐分钟样本')
&& str_contains($deployment, '没有第二阶段投影 worker'),
'Compact clean-break schema gate or deployment contract is incomplete.');
echo "Compact abuse monitoring schema contract passed.\n";