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

212 lines
8.2 KiB
PHTML

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudLogIdentity;
use App\Services\Cloud\CloudTrafficCheckpointRetention;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$capsule = new Capsule();
$capsule->addConnection([
'driver' => 'sqlite', 'database' => ':memory:',
], 'default');
$capsule->addConnection([
'driver' => 'sqlite', 'database' => ':memory:',
], 'log');
$capsule->setAsGlobal();
$capsule->bootEloquent();
$main = $capsule->schema('default');
$main->create('cloud_instances', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('user_id');
$table->integer('lifetime_download_bytes')->default(0);
$table->integer('lifetime_upload_bytes')->default(0);
$table->integer('traffic_synced_at')->default(0);
});
$main->create('cloud_service_periods', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('instance_id');
$table->integer('user_id');
$table->integer('starts_at');
$table->integer('download_base_bytes')->nullable();
$table->integer('upload_base_bytes')->nullable();
$table->string('status');
});
$main->create('cloud_traffic_instance_cursors',
static function (Blueprint $table): void {
$table->integer('instance_id')->primary();
$table->integer('user_id');
$table->string('state');
});
$logSchema = $capsule->schema('log');
$logSchema->create('cloud_vm_traffic_checkpoints',
static function (Blueprint $table): void {
$table->integer('instance_id');
$table->integer('host_id');
$table->binary('vm_uuid');
$table->integer('checkpoint_hour');
$table->integer('user_id');
$table->integer('first_observed_at');
$table->integer('download_rx_bytes');
$table->integer('upload_tx_bytes');
$table->primary(['host_id', 'vm_uuid', 'checkpoint_hour']);
});
$logSchema->create('cloud_vm_traffic_daily_checkpoints',
static function (Blueprint $table): void {
$table->integer('instance_id');
$table->integer('user_id');
$table->integer('checkpoint_day');
$table->integer('first_observed_at');
$table->integer('first_download_rx_bytes');
$table->integer('first_upload_tx_bytes');
$table->integer('last_observed_at');
$table->integer('last_download_rx_bytes');
$table->integer('last_upload_tx_bytes');
$table->integer('sample_count');
$table->primary(['instance_id', 'checkpoint_day']);
});
$logSchema->create('cloud_vm_traffic_boundary_anchors',
static function (Blueprint $table): void {
$table->integer('service_period_id')->primary();
$table->integer('instance_id');
$table->integer('user_id');
$table->integer('boundary_at');
$table->integer('observed_at');
$table->string('source_kind');
$table->integer('download_rx_bytes');
$table->integer('upload_tx_bytes');
});
$now = 10 * 86400;
$cutoff = $now - CloudTrafficCheckpointRetention::RETENTION_SECONDS;
$uuid = CloudLogIdentity::uuid('11111111-1111-4111-8111-111111111111');
$log = $capsule->connection('log');
$insertInstance = static function (int $id, string $cursorState = 'ready')
use ($capsule, $now): void {
$capsule->connection('default')->table('cloud_instances')->insert([
'id' => $id,
'user_id' => 7,
'lifetime_download_bytes' => 1000,
'lifetime_upload_bytes' => 500,
'traffic_synced_at' => $now,
]);
$capsule->connection('default')
->table('cloud_traffic_instance_cursors')->insert([
'instance_id' => $id,
'user_id' => 7,
'state' => $cursorState,
]);
};
$insertCheckpoint = static function (int $instanceId, int $observedAt,
bool $withDaily = true) use ($log, $uuid): void {
$day = intdiv($observedAt, 86400) * 86400;
$log->table('cloud_vm_traffic_checkpoints')->insert([
'instance_id' => $instanceId,
'host_id' => $instanceId,
'vm_uuid' => $uuid,
'checkpoint_hour' => intdiv($observedAt, 3600) * 3600,
'user_id' => 7,
'first_observed_at' => $observedAt,
'download_rx_bytes' => 400,
'upload_tx_bytes' => 200,
]);
if ($withDaily) {
$log->table('cloud_vm_traffic_daily_checkpoints')->insert([
'instance_id' => $instanceId,
'user_id' => 7,
'checkpoint_day' => $day,
'first_observed_at' => $day + 60,
'first_download_rx_bytes' => 300,
'first_upload_tx_bytes' => 100,
'last_observed_at' => $day + 23 * 3600,
'last_download_rx_bytes' => 800,
'last_upload_tx_bytes' => 400,
'sample_count' => 10,
]);
}
};
// Fully covered rows are safely removable and repeated cleanup is idempotent.
$insertInstance(1);
$insertCheckpoint(1, $cutoff - 3600);
$capsule->connection('default')->table('cloud_service_periods')->insert([
'id' => 1, 'instance_id' => 1, 'user_id' => 7,
'starts_at' => $cutoff - 7200,
'download_base_bytes' => 300, 'upload_base_bytes' => 100,
'status' => 'active',
]);
$first = CloudTrafficCheckpointRetention::prune(20, $now);
$assert($first['deleted'] === 1 && $first['blocked'] === []
&& !$log->table('cloud_vm_traffic_checkpoints')
->where('instance_id', 1)->exists(),
'Verified hourly checkpoint was not pruned.');
$repeat = CloudTrafficCheckpointRetention::prune(20, $now);
$assert($repeat['deleted'] === 0,
'Repeated hourly checkpoint pruning was not idempotent.');
// Missing daily evidence is retained rather than guessed away.
$insertInstance(2);
$insertCheckpoint(2, $cutoff - 7200, false);
$missingDaily = CloudTrafficCheckpointRetention::prune(20, $now);
$assert($log->table('cloud_vm_traffic_checkpoints')
->where('instance_id', 2)->exists()
&& in_array('traffic_checkpoint_daily_evidence_missing',
array_column($missingDaily['blocked'], 'code'), true),
'Checkpoint without daily evidence was deleted or not quarantined.');
// An unresolved service boundary blocks deletion until its immutable anchor
// exists; the anchor then makes the old hourly row disposable.
$insertInstance(3);
$observedAt = $cutoff - 10_800;
$insertCheckpoint(3, $observedAt);
$capsule->connection('default')->table('cloud_service_periods')->insert([
'id' => 3, 'instance_id' => 3, 'user_id' => 7,
'starts_at' => $observedAt - 60,
'download_base_bytes' => null, 'upload_base_bytes' => null,
'status' => 'scheduled',
]);
$unfrozen = CloudTrafficCheckpointRetention::prune(20, $now);
$assert($log->table('cloud_vm_traffic_checkpoints')
->where('instance_id', 3)->exists()
&& in_array('traffic_checkpoint_period_boundary_unfrozen',
array_column($unfrozen['blocked'], 'code'), true),
'Unfrozen service-period boundary did not retain its hourly evidence.');
$log->table('cloud_vm_traffic_boundary_anchors')->insert([
'service_period_id' => 3,
'instance_id' => 3,
'user_id' => 7,
'boundary_at' => $observedAt - 60,
'observed_at' => $observedAt,
'source_kind' => 'hourly_checkpoint',
'download_rx_bytes' => 400,
'upload_tx_bytes' => 200,
]);
$anchored = CloudTrafficCheckpointRetention::prune(20, $now);
$assert($anchored['deleted'] === 1
&& !$log->table('cloud_vm_traffic_checkpoints')
->where('instance_id', 3)->exists(),
'Frozen service-period boundary did not release old hourly evidence.');
// Attention is a fail-closed accounting state, so no historical evidence is
// removed until the original cursor is reconciled.
$insertInstance(4, 'attention');
$insertCheckpoint(4, $cutoff - 14_400);
$attention = CloudTrafficCheckpointRetention::prune(20, $now);
$assert($log->table('cloud_vm_traffic_checkpoints')
->where('instance_id', 4)->exists()
&& in_array('traffic_checkpoint_cursor_attention',
array_column($attention['blocked'], 'code'), true),
'Attention cursor did not retain its hourly checkpoint evidence.');
echo "Traffic checkpoint retention contracts passed.\n";