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.
211 lines
7.9 KiB
PHP
211 lines
7.9 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Models\CloudInstance;
|
|
use App\Services\Cloud\CloudDailyTrafficLedger;
|
|
use App\Services\Cloud\CloudError;
|
|
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);
|
|
}
|
|
};
|
|
$expect = static function (callable $callable, int $status,
|
|
string $message) use ($assert): void {
|
|
try {
|
|
$callable();
|
|
} catch (CloudError $error) {
|
|
$assert($error->httpStatus === $status,
|
|
$message . ' Unexpected status: ' . $error->httpStatus);
|
|
return;
|
|
}
|
|
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();
|
|
|
|
$capsule->schema('default')->create('cloud_instances',
|
|
static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->integer('host_id');
|
|
$table->integer('user_id');
|
|
$table->string('uuid');
|
|
$table->integer('lifetime_download_bytes')->default(0);
|
|
$table->integer('lifetime_upload_bytes')->default(0);
|
|
$table->integer('traffic_synced_at')->default(0);
|
|
$table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
});
|
|
$logSchema = $capsule->schema('log');
|
|
$logSchema->create('cloud_vm_daily_traffic_cursors',
|
|
static function (Blueprint $table): void {
|
|
$table->integer('instance_id')->primary();
|
|
$table->integer('user_id');
|
|
$table->integer('last_observed_at');
|
|
$table->integer('download_total_bytes');
|
|
$table->integer('upload_total_bytes');
|
|
$table->integer('updated_at');
|
|
});
|
|
$logSchema->create('cloud_vm_daily_traffic',
|
|
static function (Blueprint $table): void {
|
|
$table->integer('instance_id');
|
|
$table->integer('user_id');
|
|
$table->integer('day_start');
|
|
$table->integer('download_bytes')->default(0);
|
|
$table->integer('upload_bytes')->default(0);
|
|
$table->integer('sample_count')->default(0);
|
|
$table->integer('first_observed_at');
|
|
$table->integer('last_observed_at');
|
|
$table->integer('created_at');
|
|
$table->integer('updated_at');
|
|
$table->primary(['instance_id', 'day_start']);
|
|
});
|
|
|
|
$today = intdiv(time(), 86400) * 86400;
|
|
$firstAt = $today - 120;
|
|
$secondAt = $today + 60;
|
|
$instance = new CloudInstance();
|
|
$instance->host_id = 1;
|
|
$instance->user_id = 17;
|
|
$instance->uuid = '11111111-1111-4111-8111-111111111111';
|
|
$instance->lifetime_download_bytes = 0;
|
|
$instance->lifetime_upload_bytes = 0;
|
|
$instance->traffic_synced_at = 0;
|
|
$instance->created_at = $instance->updated_at = time();
|
|
$instance->save();
|
|
$record = static function (CloudInstance $instance, int $observedAt,
|
|
int $download, int $upload): bool {
|
|
$current = CloudInstance::findOrFail($instance->id);
|
|
$syncedAt = max((int) $current->traffic_synced_at, $observedAt);
|
|
CloudInstance::where('id', $instance->id)->update([
|
|
'host_id' => (int) $instance->host_id,
|
|
'user_id' => (int) $instance->user_id,
|
|
'uuid' => (string) $instance->uuid,
|
|
'lifetime_download_bytes' => $download,
|
|
'lifetime_upload_bytes' => $upload,
|
|
'traffic_synced_at' => $syncedAt,
|
|
]);
|
|
$instance->lifetime_download_bytes = $download;
|
|
$instance->lifetime_upload_bytes = $upload;
|
|
$instance->traffic_synced_at = $syncedAt;
|
|
return CloudDailyTrafficLedger::record(
|
|
$instance, $observedAt, $download, $upload
|
|
);
|
|
};
|
|
|
|
$assert($record(
|
|
$instance, $firstAt, 1_000, 500
|
|
), 'First daily observation did not establish the clean baseline.');
|
|
$log = $capsule->connection('log');
|
|
$firstDay = $log->table('cloud_vm_daily_traffic')->where(
|
|
'day_start', $today - 86400
|
|
)->first();
|
|
$assert($firstDay !== null
|
|
&& (int) $firstDay->download_bytes === 0
|
|
&& (int) $firstDay->upload_bytes === 0
|
|
&& (int) $firstDay->sample_count === 1,
|
|
'First daily observation backfilled traffic that predates deployment.');
|
|
|
|
$assert($record(
|
|
$instance, $secondAt, 1_300, 600
|
|
), 'Second daily observation was not recorded.');
|
|
$assert(!$record(
|
|
$instance, $secondAt, 1_300, 600
|
|
), 'An exact daily observation replay was counted twice.');
|
|
$assert($record(
|
|
$instance, $secondAt - 30, 1_400, 650
|
|
), 'A clock rollback froze an increasing cumulative total.');
|
|
|
|
// Maintenance migration changes host/UUID but retains the logical instance id.
|
|
$staleSource = clone $instance;
|
|
$instance->host_id = 2;
|
|
$instance->uuid = '22222222-2222-4222-8222-222222222222';
|
|
CloudInstance::where('id', $instance->id)->update([
|
|
'host_id' => 2,
|
|
'uuid' => $instance->uuid,
|
|
'traffic_synced_at' => $secondAt + 30,
|
|
]);
|
|
$instance->traffic_synced_at = $secondAt + 30;
|
|
$assert(!CloudDailyTrafficLedger::record(
|
|
$staleSource, $secondAt + 45, 1_500, 675
|
|
), 'A stale source-host sample advanced the post-cutover daily cursor.');
|
|
$assert($record(
|
|
$instance, $secondAt + 60, 1_600, 700
|
|
), 'A maintenance migration reset the logical-instance daily cursor.');
|
|
$assert($record(
|
|
$instance, $secondAt + 120, 1_600, 700
|
|
), 'A real zero-traffic observation was discarded.');
|
|
$assert(!$record(
|
|
$instance, $secondAt + 120, 1_600, 700
|
|
), 'A repeated zero-traffic observation inflated the sample count.');
|
|
|
|
$todayRow = $log->table('cloud_vm_daily_traffic')
|
|
->where('instance_id', $instance->id)
|
|
->where('day_start', $today)->first();
|
|
$assert($todayRow !== null
|
|
&& (int) $todayRow->download_bytes === 600
|
|
&& (int) $todayRow->upload_bytes === 200
|
|
&& (int) $todayRow->sample_count === 4,
|
|
'Daily deltas did not survive UTC rollover, replay or migration.');
|
|
|
|
$assert(!CloudDailyTrafficLedger::record(
|
|
$instance, $secondAt + 180, 1_599, 700
|
|
), 'A stale smaller cumulative total passed the main-instance fence.');
|
|
$todayAfterRegression = $log->table('cloud_vm_daily_traffic')
|
|
->where('instance_id', $instance->id)
|
|
->where('day_start', $today)->first();
|
|
$assert((int) $todayAfterRegression->download_bytes === 600
|
|
&& (int) $todayAfterRegression->sample_count === 4,
|
|
'A rejected cumulative regression changed the daily ledger.');
|
|
|
|
$window = CloudDailyTrafficLedger::read($instance, 3);
|
|
$assert(count($window['points']) === 3
|
|
&& $window['from'] === $today - 2 * 86400
|
|
&& $window['until'] === $today + 86400
|
|
&& $window['points'][0]['download_bytes'] === null
|
|
&& $window['points'][0]['upload_bytes'] === null
|
|
&& $window['points'][1]['download_bytes'] === 0
|
|
&& $window['points'][1]['upload_bytes'] === 0
|
|
&& $window['points'][2]['download_bytes'] === 600
|
|
&& $window['points'][2]['upload_bytes'] === 200,
|
|
'Daily read fabricated missing samples or lost a genuine zero sample.');
|
|
|
|
$log->table('cloud_vm_daily_traffic')->insert([
|
|
'instance_id' => 999,
|
|
'user_id' => 17,
|
|
'day_start' => $today - 30 * 86400,
|
|
'download_bytes' => 1,
|
|
'upload_bytes' => 1,
|
|
'sample_count' => 1,
|
|
'first_observed_at' => $today - 30 * 86400,
|
|
'last_observed_at' => $today - 30 * 86400,
|
|
'created_at' => time(),
|
|
'updated_at' => time(),
|
|
]);
|
|
$assert(CloudDailyTrafficLedger::prune($today + 3600) === 1
|
|
&& $log->table('cloud_vm_daily_traffic_cursors')
|
|
->where('instance_id', $instance->id)->exists(),
|
|
'Thirty-day pruning removed the continuity cursor or retained expired data.');
|
|
|
|
$log->table('cloud_vm_daily_traffic_cursors')
|
|
->where('instance_id', $instance->id)->update(['user_id' => 18]);
|
|
CloudInstance::where('id', $instance->id)->update([
|
|
'traffic_synced_at' => $secondAt + 240,
|
|
]);
|
|
$expect(static fn () => CloudDailyTrafficLedger::record(
|
|
$instance, $secondAt + 240, 1_600, 700
|
|
), 409, 'A cursor bound to another owner was accepted.');
|
|
|
|
echo "Logical-instance daily traffic ledger contracts passed.\n";
|