|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
|
|
|
|
|
|
use App\Models\CloudServicePeriod;
|
|
|
|
|
use App\Services\Cloud\CloudTrafficPoolReadModel;
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
$assertNoInternalFields = static function (array $payload, string $message)
|
|
|
|
|
use (&$assertNoInternalFields): void {
|
|
|
|
|
foreach ($payload as $key => $value) {
|
|
|
|
|
if (is_string($key) && (str_contains($key, 'credential')
|
|
|
|
|
|| str_contains($key, 'operation')
|
|
|
|
|
|| in_array($key, [
|
|
|
|
|
'host', 'host_id', 'resource_pool', 'resource_pool_id',
|
|
|
|
|
'pool_id', 'user_id',
|
|
|
|
|
], true))) {
|
|
|
|
|
throw new RuntimeException($message . ': ' . $key);
|
|
|
|
|
}
|
|
|
|
|
if (is_array($value)) {
|
|
|
|
|
$assertNoInternalFields($value, $message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$capsule = new Capsule();
|
|
|
|
|
$capsule->addConnection([
|
|
|
|
|
'driver' => 'sqlite', 'database' => ':memory:',
|
|
|
|
|
], 'default');
|
|
|
|
|
$capsule->setAsGlobal();
|
|
|
|
|
$capsule->bootEloquent();
|
|
|
|
|
$schema = $capsule->schema('default');
|
|
|
|
|
|
|
|
|
|
$schema->create('cloud_traffic_pool_groups', static function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->string('code')->unique();
|
|
|
|
|
$table->string('name');
|
|
|
|
|
$table->string('mode');
|
|
|
|
|
$table->integer('revision')->default(1);
|
|
|
|
|
$table->integer('created_at');
|
|
|
|
|
$table->integer('updated_at');
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_regions', static function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->string('code')->unique();
|
|
|
|
|
$table->string('name');
|
|
|
|
|
$table->string('country_code');
|
|
|
|
|
$table->string('status');
|
|
|
|
|
$table->integer('sort')->default(0);
|
|
|
|
|
$table->integer('created_at');
|
|
|
|
|
$table->integer('updated_at');
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_network_lines', static function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->integer('region_id');
|
|
|
|
|
$table->integer('traffic_pool_group_id')->nullable();
|
|
|
|
|
$table->string('code');
|
|
|
|
|
$table->string('name');
|
|
|
|
|
$table->boolean('is_default')->default(false);
|
|
|
|
|
$table->string('status');
|
|
|
|
|
$table->integer('sort')->default(0);
|
|
|
|
|
$table->integer('created_at');
|
|
|
|
|
$table->integer('updated_at');
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_traffic_pools', static function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->integer('user_id');
|
|
|
|
|
$table->integer('traffic_pool_group_id');
|
|
|
|
|
$table->string('state');
|
|
|
|
|
$table->integer('available_bytes')->default(0);
|
|
|
|
|
$table->integer('overage_bytes')->default(0);
|
|
|
|
|
$table->integer('version')->default(1);
|
|
|
|
|
$table->integer('last_sequence')->default(0);
|
|
|
|
|
$table->integer('last_event_id')->nullable();
|
|
|
|
|
$table->integer('created_at');
|
|
|
|
|
$table->integer('updated_at');
|
|
|
|
|
$table->unique(['user_id', 'traffic_pool_group_id']);
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_instances', static function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->integer('user_id');
|
|
|
|
|
$table->integer('current_period_id')->nullable();
|
|
|
|
|
$table->string('name');
|
|
|
|
|
$table->string('status');
|
|
|
|
|
$table->string('service_status');
|
|
|
|
|
$table->string('network_status');
|
|
|
|
|
$table->integer('expires_at')->nullable();
|
|
|
|
|
$table->integer('traffic_download_bytes')->default(0);
|
|
|
|
|
$table->integer('traffic_upload_bytes')->default(0);
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_service_periods', static function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->integer('instance_id');
|
|
|
|
|
$table->integer('user_id');
|
|
|
|
|
$table->integer('traffic_pool_group_id')->nullable();
|
|
|
|
|
$table->string('traffic_quota_mode')->default('finite');
|
|
|
|
|
$table->integer('traffic_quota_bytes')->default(0);
|
|
|
|
|
$table->integer('starts_at');
|
|
|
|
|
$table->integer('ends_at');
|
|
|
|
|
$table->string('status');
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_traffic_entitlements', static function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->integer('pool_id');
|
|
|
|
|
$table->integer('user_id');
|
|
|
|
|
$table->integer('instance_id');
|
|
|
|
|
$table->integer('service_period_id')->unique();
|
|
|
|
|
$table->integer('starts_at');
|
|
|
|
|
$table->integer('ends_at');
|
|
|
|
|
$table->string('quota_mode');
|
|
|
|
|
$table->integer('granted_bytes')->default(0);
|
|
|
|
|
$table->integer('consumed_bytes')->default(0);
|
|
|
|
|
$table->integer('forfeited_bytes')->default(0);
|
|
|
|
|
$table->string('state');
|
|
|
|
|
$table->integer('activated_at')->nullable();
|
|
|
|
|
$table->integer('closed_at')->nullable();
|
|
|
|
|
$table->integer('version')->default(1);
|
|
|
|
|
$table->integer('created_at');
|
|
|
|
|
$table->integer('updated_at');
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_traffic_instance_cursors', static function (Blueprint $table): void {
|
|
|
|
|
$table->integer('instance_id')->primary();
|
|
|
|
|
$table->integer('user_id');
|
|
|
|
|
$table->integer('pool_id');
|
|
|
|
|
$table->integer('accounted_download_bytes')->default(0);
|
|
|
|
|
$table->integer('accounted_upload_bytes')->default(0);
|
|
|
|
|
$table->integer('last_observed_at')->default(0);
|
|
|
|
|
$table->integer('last_event_id')->nullable();
|
|
|
|
|
$table->string('state')->default('ready');
|
|
|
|
|
$table->string('attention_code')->default('');
|
|
|
|
|
$table->integer('version')->default(1);
|
|
|
|
|
$table->integer('updated_at');
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_instance_network_restrictions', static function (Blueprint $table): void {
|
|
|
|
|
$table->increments('id');
|
|
|
|
|
$table->integer('instance_id');
|
|
|
|
|
$table->integer('user_id');
|
|
|
|
|
$table->string('reason_code');
|
|
|
|
|
$table->boolean('desired_active');
|
|
|
|
|
$table->integer('revision')->default(1);
|
|
|
|
|
$table->integer('created_at');
|
|
|
|
|
$table->integer('updated_at');
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_instance_network_policy_states', static function (Blueprint $table): void {
|
|
|
|
|
$table->integer('instance_id')->primary();
|
|
|
|
|
$table->integer('user_id');
|
|
|
|
|
$table->boolean('desired_suspended');
|
|
|
|
|
$table->boolean('applied_suspended');
|
|
|
|
|
$table->string('state');
|
|
|
|
|
$table->integer('revision')->default(1);
|
|
|
|
|
$table->integer('created_at');
|
|
|
|
|
$table->integer('updated_at');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$now = 2_000_000;
|
|
|
|
|
$groups = [];
|
|
|
|
|
foreach ([
|
|
|
|
|
[1, 'line-a', '线路 A', 'enforced'],
|
|
|
|
|
[2, 'line-b', '线路 B', 'shadow'],
|
|
|
|
|
[3, 'line-c', '线路 C', 'enforced'],
|
|
|
|
|
[4, 'line-d', '线路 D', 'enforced'],
|
|
|
|
|
[5, 'line-e', '线路 E', 'enforced'],
|
|
|
|
|
[6, 'line-f', '线路 F', 'enforced'],
|
|
|
|
|
[7, 'line-g', '线路 G', 'enforced'],
|
|
|
|
|
] as [$id, $code, $name, $mode]) {
|
|
|
|
|
$groups[] = compact('id', 'code', 'name', 'mode') + [
|
|
|
|
|
'revision' => 1, 'created_at' => $now, 'updated_at' => $now,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
Capsule::table('cloud_traffic_pool_groups')->insert($groups);
|
|
|
|
|
Capsule::table('cloud_regions')->insert([
|
|
|
|
|
['id' => 10, 'code' => 'hkg', 'name' => '香港',
|
|
|
|
|
'country_code' => 'HK', 'status' => 'active', 'sort' => 10,
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now],
|
|
|
|
|
['id' => 20, 'code' => 'sgp', 'name' => '新加坡',
|
|
|
|
|
'country_code' => 'SG', 'status' => 'active', 'sort' => 10,
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now],
|
|
|
|
|
['id' => 30, 'code' => 'tyo', 'name' => '东京',
|
|
|
|
|
'country_code' => 'JP', 'status' => 'active', 'sort' => 30,
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now],
|
|
|
|
|
]);
|
|
|
|
|
$lines = [];
|
|
|
|
|
foreach ([
|
|
|
|
|
[1, 10, 1, 'hkg-a', '香港线路 A'],
|
|
|
|
|
[2, 10, 1, 'hkg-a2', '香港线路 A2'],
|
|
|
|
|
[3, 10, 2, 'hkg-shadow', '香港影子线路'],
|
|
|
|
|
[4, 20, 3, 'sgp-c', '新加坡线路 C'],
|
|
|
|
|
[5, 20, 5, 'sgp-e', '新加坡线路 E'],
|
|
|
|
|
[6, 10, 6, 'hkg-f', '香港跨区线路'],
|
|
|
|
|
[7, 20, 6, 'sgp-f', '新加坡跨区线路'],
|
|
|
|
|
[8, 20, 7, 'sgp-g', '新加坡线路 G'],
|
|
|
|
|
[9, 30, 3, 'tyo-c', '东京线路 C'],
|
|
|
|
|
] as [$id, $regionId, $groupId, $code, $name]) {
|
|
|
|
|
$lines[] = [
|
|
|
|
|
'id' => $id, 'region_id' => $regionId,
|
|
|
|
|
'traffic_pool_group_id' => $groupId,
|
|
|
|
|
'code' => $code, 'name' => $name, 'is_default' => 0,
|
|
|
|
|
'status' => 'active', 'sort' => $id,
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
Capsule::table('cloud_network_lines')->insert($lines);
|
|
|
|
|
Capsule::table('cloud_traffic_pools')->insert([
|
|
|
|
|
['id' => 11, 'user_id' => 1, 'traffic_pool_group_id' => 1,
|
|
|
|
|
'state' => 'available', 'available_bytes' => 1100,
|
|
|
|
|
'overage_bytes' => 0, 'version' => 3, 'last_sequence' => 0,
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now],
|
|
|
|
|
['id' => 12, 'user_id' => 1, 'traffic_pool_group_id' => 2,
|
|
|
|
|
'state' => 'available', 'available_bytes' => 200,
|
|
|
|
|
'overage_bytes' => 0, 'version' => 1, 'last_sequence' => 0,
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now],
|
|
|
|
|
['id' => 13, 'user_id' => 1, 'traffic_pool_group_id' => 3,
|
|
|
|
|
'state' => 'exhausted', 'available_bytes' => 0,
|
|
|
|
|
'overage_bytes' => 20, 'version' => 4, 'last_sequence' => 0,
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now],
|
|
|
|
|
['id' => 14, 'user_id' => 1, 'traffic_pool_group_id' => 4,
|
|
|
|
|
'state' => 'attention', 'available_bytes' => 300,
|
|
|
|
|
'overage_bytes' => 0, 'version' => 1, 'last_sequence' => 0,
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now],
|
|
|
|
|
['id' => 21, 'user_id' => 2, 'traffic_pool_group_id' => 1,
|
|
|
|
|
'state' => 'available', 'available_bytes' => 9999,
|
|
|
|
|
'overage_bytes' => 0, 'version' => 1, 'last_sequence' => 0,
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now],
|
|
|
|
|
['id' => 16, 'user_id' => 1, 'traffic_pool_group_id' => 6,
|
|
|
|
|
'state' => 'available', 'available_bytes' => 500,
|
|
|
|
|
'overage_bytes' => 0, 'version' => 2, 'last_sequence' => 0,
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now],
|
|
|
|
|
['id' => 17, 'user_id' => 1, 'traffic_pool_group_id' => 7,
|
|
|
|
|
'state' => 'available', 'available_bytes' => 0,
|
|
|
|
|
'overage_bytes' => 0, 'version' => 2, 'last_sequence' => 0,
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now],
|
|
|
|
|
['id' => 31, 'user_id' => 3, 'traffic_pool_group_id' => 1,
|
|
|
|
|
'state' => 'available', 'available_bytes' => 100,
|
|
|
|
|
'overage_bytes' => 0, 'version' => 1, 'last_sequence' => 0,
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now],
|
|
|
|
|
['id' => 33, 'user_id' => 3, 'traffic_pool_group_id' => 5,
|
|
|
|
|
'state' => 'available', 'available_bytes' => 100,
|
|
|
|
|
'overage_bytes' => 0, 'version' => 1, 'last_sequence' => 0,
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$instances = [
|
|
|
|
|
[1, 1, 101, 'owner-a', 'active', 'active', 'active', 100, 50],
|
|
|
|
|
[2, 1, 102, 'shadow', 'active', 'active', 'active', 30, 20],
|
|
|
|
|
[3, 2, 103, 'foreign', 'active', 'active', 'active', 8000, 1000],
|
|
|
|
|
[4, 1, 104, 'unlimited', 'active', 'active', 'active', 9000, 9000],
|
|
|
|
|
[5, 1, 105, 'owner-c', 'active', 'active', 'blocked_traffic', 140, 80],
|
|
|
|
|
[6, 1, 106, 'preparing', 'active', 'active', 'restoring', 0, 0],
|
|
|
|
|
[7, 1, 107, 'existing-pool-pending', 'active', 'active', 'active', 0, 0],
|
|
|
|
|
];
|
|
|
|
|
foreach ($instances as [$id, $userId, $periodId, $name, $status,
|
|
|
|
|
$serviceStatus, $networkStatus, $download, $upload]) {
|
|
|
|
|
Capsule::table('cloud_instances')->insert([
|
|
|
|
|
'id' => $id, 'user_id' => $userId,
|
|
|
|
|
'current_period_id' => $periodId, 'name' => $name,
|
|
|
|
|
'status' => $status, 'service_status' => $serviceStatus,
|
|
|
|
|
'network_status' => $networkStatus, 'expires_at' => $now + 900,
|
|
|
|
|
'traffic_download_bytes' => $download,
|
|
|
|
|
'traffic_upload_bytes' => $upload,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
$periods = [
|
|
|
|
|
[101, 1, 1, 1, 'finite', 1000],
|
|
|
|
|
[102, 2, 1, 2, 'finite', 200],
|
|
|
|
|
[103, 3, 2, 1, 'finite', 9999],
|
|
|
|
|
[104, 4, 1, 1, 'unlimited', 0],
|
|
|
|
|
[105, 5, 1, 3, 'finite', 200],
|
|
|
|
|
[106, 6, 1, 5, 'finite', 700],
|
|
|
|
|
[107, 7, 1, 6, 'finite', 900],
|
|
|
|
|
];
|
|
|
|
|
foreach ($periods as [$id, $instanceId, $userId, $groupId, $mode, $quota]) {
|
|
|
|
|
Capsule::table('cloud_service_periods')->insert([
|
|
|
|
|
'id' => $id, 'instance_id' => $instanceId, 'user_id' => $userId,
|
|
|
|
|
'traffic_pool_group_id' => $groupId, 'traffic_quota_mode' => $mode,
|
|
|
|
|
'traffic_quota_bytes' => $quota, 'starts_at' => $now - 100,
|
|
|
|
|
'ends_at' => $now + 900, 'status' => 'active',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$entitlements = [
|
|
|
|
|
[1, 11, 1, 1, 101, 1000, 300, $now + 900, 'active'],
|
|
|
|
|
[2, 11, 1, 99, 199, 500, 100, $now + 300, 'active'],
|
|
|
|
|
[3, 12, 1, 2, 102, 200, 0, $now + 900, 'active'],
|
|
|
|
|
[4, 13, 1, 5, 105, 200, 200, $now + 900, 'exhausted'],
|
|
|
|
|
[5, 14, 1, 98, 198, 300, 0, $now + 700, 'active'],
|
|
|
|
|
[6, 21, 2, 3, 103, 9999, 0, $now + 900, 'active'],
|
|
|
|
|
[7, 16, 1, 97, 197, 500, 0, $now + 600, 'active'],
|
|
|
|
|
[8, 17, 1, 96, 196, 400, 100, $now - 1, 'expired'],
|
|
|
|
|
[9, 31, 3, 301, 301, 100, 0, $now + 900, 'active'],
|
|
|
|
|
[10, 33, 3, 303, 303, 100, 0, $now + 900, 'active'],
|
|
|
|
|
];
|
|
|
|
|
foreach ($entitlements as [$id, $poolId, $userId, $instanceId, $periodId,
|
|
|
|
|
$granted, $consumed, $endsAt, $state]) {
|
|
|
|
|
Capsule::table('cloud_traffic_entitlements')->insert([
|
|
|
|
|
'id' => $id, 'pool_id' => $poolId, 'user_id' => $userId,
|
|
|
|
|
'instance_id' => $instanceId, 'service_period_id' => $periodId,
|
|
|
|
|
'starts_at' => $now - 100, 'ends_at' => $endsAt,
|
|
|
|
|
'quota_mode' => 'finite', 'granted_bytes' => $granted,
|
|
|
|
|
'consumed_bytes' => $consumed, 'forfeited_bytes' => 0,
|
|
|
|
|
'state' => $state, 'version' => 1,
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
Capsule::table('cloud_traffic_instance_cursors')->insert([
|
|
|
|
|
['instance_id' => 1, 'user_id' => 1, 'pool_id' => 11,
|
|
|
|
|
'last_observed_at' => $now - 5, 'updated_at' => $now],
|
|
|
|
|
['instance_id' => 5, 'user_id' => 1, 'pool_id' => 13,
|
|
|
|
|
'last_observed_at' => $now - 4, 'updated_at' => $now],
|
|
|
|
|
]);
|
|
|
|
|
Capsule::table('cloud_instance_network_restrictions')->insert([
|
|
|
|
|
['instance_id' => 1, 'user_id' => 1,
|
|
|
|
|
'reason_code' => 'traffic_pool_exhausted', 'desired_active' => 0,
|
|
|
|
|
'revision' => 2, 'created_at' => $now, 'updated_at' => $now],
|
|
|
|
|
['instance_id' => 1, 'user_id' => 1,
|
|
|
|
|
'reason_code' => 'admin_hold', 'desired_active' => 1,
|
|
|
|
|
'revision' => 1, 'created_at' => $now, 'updated_at' => $now],
|
|
|
|
|
['instance_id' => 5, 'user_id' => 1,
|
|
|
|
|
'reason_code' => 'traffic_pool_exhausted', 'desired_active' => 1,
|
|
|
|
|
'revision' => 1, 'created_at' => $now, 'updated_at' => $now],
|
|
|
|
|
['instance_id' => 6, 'user_id' => 1,
|
|
|
|
|
'reason_code' => 'traffic_pool_exhausted', 'desired_active' => 0,
|
|
|
|
|
'revision' => 2, 'created_at' => $now, 'updated_at' => $now],
|
|
|
|
|
]);
|
|
|
|
|
Capsule::table('cloud_instance_network_policy_states')->insert([
|
|
|
|
|
['instance_id' => 1, 'user_id' => 1,
|
|
|
|
|
'desired_suspended' => 1, 'applied_suspended' => 0,
|
|
|
|
|
'state' => 'failed', 'revision' => 3,
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now],
|
|
|
|
|
['instance_id' => 5, 'user_id' => 1,
|
|
|
|
|
'desired_suspended' => 1, 'applied_suspended' => 1,
|
|
|
|
|
'state' => 'applied', 'revision' => 1,
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now],
|
|
|
|
|
['instance_id' => 6, 'user_id' => 1,
|
|
|
|
|
'desired_suspended' => 0, 'applied_suspended' => 1,
|
|
|
|
|
'state' => 'applying', 'revision' => 2,
|
|
|
|
|
'created_at' => $now, 'updated_at' => $now],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$payload = CloudTrafficPoolReadModel::forUser(1, $now);
|
|
|
|
|
$assert(array_column($payload['items'], 'group_id') === [1, 3, 4, 5, 6],
|
|
|
|
|
'User projection exposed shadow or foreign pools, or lost an entitlement-only pool.');
|
|
|
|
|
$regionGroups = $payload['region_groups'];
|
|
|
|
|
$assert(array_column($regionGroups, 'key') === [
|
|
|
|
|
'region:hkg', 'region:sgp', 'region:cross', 'region:unassigned',
|
|
|
|
|
] && $payload['region_count'] === 4 && $payload['pool_count'] === 5,
|
|
|
|
|
'Traffic pools were not grouped in stable region order.');
|
|
|
|
|
$regionPoolIds = [];
|
|
|
|
|
foreach ($regionGroups as $regionGroup) {
|
|
|
|
|
foreach ($regionGroup['items'] as $item) {
|
|
|
|
|
$regionPoolIds[] = $item['group_id'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sort($regionPoolIds, SORT_NUMERIC);
|
|
|
|
|
$assert($regionPoolIds === [1, 3, 4, 5, 6],
|
|
|
|
|
'A region scope duplicated or lost one canonical traffic pool.');
|
|
|
|
|
$assert(array_column($regionGroups[0]['items'], 'group_id') === [1]
|
|
|
|
|
&& $regionGroups[0]['pool_count'] === 1
|
|
|
|
|
&& $regionGroups[0]['instance_count'] === 1,
|
|
|
|
|
'Same-region pools or instance counts were projected incorrectly.');
|
|
|
|
|
$assert(array_column($regionGroups[1]['items'], 'group_id') === [5]
|
|
|
|
|
&& $regionGroups[1]['instance_count'] === 1,
|
|
|
|
|
'The second region lost its pool or instance summary.');
|
|
|
|
|
$assert(array_column($regionGroups[2]['regions'], 'name') === [
|
|
|
|
|
'香港', '新加坡', '东京',
|
|
|
|
|
] && array_column($regionGroups[2]['items'], 'group_id') === [3, 6],
|
|
|
|
|
'A cross-region pool was duplicated or its ordered region context was lost.');
|
|
|
|
|
$assert($regionGroups[3]['kind'] === 'unassigned'
|
|
|
|
|
&& array_column($regionGroups[3]['items'], 'group_id') === [4]
|
|
|
|
|
&& $regionGroups[3]['summary_state'] === 'attention',
|
|
|
|
|
'A pool without a valid region was hidden instead of marked for attention.');
|
|
|
|
|
$assert(count(array_filter($regionGroups,
|
|
|
|
|
static fn (array $group): bool => $group['default_open'] === true)) === 3,
|
|
|
|
|
'Attention, restricted or preparing regions were not opened by default.');
|
|
|
|
|
$singleRegion = CloudTrafficPoolReadModel::forUser(2, $now);
|
|
|
|
|
$assert($singleRegion['region_count'] === 1
|
|
|
|
|
&& $singleRegion['region_groups'][0]['key'] === 'region:hkg'
|
|
|
|
|
&& $singleRegion['region_groups'][0]['default_open'] === true,
|
|
|
|
|
'A single healthy region should be open by default.');
|
|
|
|
|
Capsule::table('cloud_traffic_pools')->where('id', 21)->update([
|
|
|
|
|
'state' => 'unexpected_state',
|
|
|
|
|
]);
|
|
|
|
|
$unknownState = CloudTrafficPoolReadModel::forUser(2, $now);
|
|
|
|
|
$assert($unknownState['region_groups'][0]['summary_state'] === 'attention'
|
|
|
|
|
&& $unknownState['region_groups'][0]['default_open'] === true,
|
|
|
|
|
'An unknown pool state was presented as a healthy collapsed region.');
|
|
|
|
|
Capsule::table('cloud_traffic_pools')->where('id', 21)->update([
|
|
|
|
|
'state' => 'available',
|
|
|
|
|
]);
|
|
|
|
|
$healthyRegions = CloudTrafficPoolReadModel::forUser(3, $now);
|
|
|
|
|
$assert(array_column($healthyRegions['region_groups'], 'key') === [
|
|
|
|
|
'region:hkg', 'region:sgp',
|
|
|
|
|
] && array_column(
|
|
|
|
|
$healthyRegions['region_groups'], 'default_open'
|
|
|
|
|
) === [true, false],
|
|
|
|
|
'Multiple healthy regions should open only the first region by default.');
|
|
|
|
|
$assertNoInternalFields($payload,
|
|
|
|
|
'User traffic-pool DTO exposed an internal identifier');
|
|
|
|
|
|
|
|
|
|
$lineA = $payload['items'][0];
|
|
|
|
|
$assert($lineA['active_entitlement_bytes'] === 1500
|
|
|
|
|
&& $lineA['active_consumed_bytes'] === 400
|
|
|
|
|
&& $lineA['available_bytes'] === 1100
|
|
|
|
|
&& $lineA['next_expiry_at'] === $now + 300
|
|
|
|
|
&& $lineA['next_expiry_bytes'] === 400
|
|
|
|
|
&& $lineA['last_observed_at'] === $now - 5,
|
|
|
|
|
'User projection did not preserve the canonical FEFO snapshot.');
|
|
|
|
|
$assert(count($lineA['instances']) === 1
|
|
|
|
|
&& $lineA['instances'][0]['id'] === '1'
|
|
|
|
|
&& $lineA['instances'][0]['traffic_used_bytes'] === 150
|
|
|
|
|
&& $lineA['instances'][0]['period_traffic_quota_bytes'] === 1000
|
|
|
|
|
&& $lineA['network_restriction_state'] === 'clear',
|
|
|
|
|
'Current-instance observations crossed user, group, deletion or unlimited boundaries.');
|
|
|
|
|
$assert($payload['items'][1]['network_restriction_state'] === 'restricted',
|
|
|
|
|
'Applied aggregate traffic restriction was not projected to the user.');
|
|
|
|
|
$assert($payload['items'][2]['instances'] === []
|
|
|
|
|
&& $payload['items'][2]['available_bytes'] === 300
|
|
|
|
|
&& $payload['items'][2]['network_restriction_state'] === 'attention',
|
|
|
|
|
'Deleting an instance incorrectly removed its unexpired pool entitlement.');
|
|
|
|
|
$assert($payload['items'][3]['state'] === 'preparing'
|
|
|
|
|
&& $payload['items'][3]['projection_ready'] === false
|
|
|
|
|
&& $payload['items'][3]['network_restriction_state'] === 'applying'
|
|
|
|
|
&& $payload['items'][3]['instances'][0]['id'] === '6',
|
|
|
|
|
'A newly active enforced period disappeared before its pool projection was materialized.');
|
|
|
|
|
$assert($payload['items'][4]['state'] === 'preparing'
|
|
|
|
|
&& $payload['items'][4]['projection_ready'] === false
|
|
|
|
|
&& $payload['items'][4]['active_entitlement_bytes'] === 500,
|
|
|
|
|
'An existing pool concealed a newly active period whose entitlement was not materialized.');
|
|
|
|
|
|
|
|
|
|
$batchPeriods = [
|
|
|
|
|
CloudServicePeriod::find(101),
|
|
|
|
|
CloudServicePeriod::find(103),
|
|
|
|
|
CloudServicePeriod::find(105),
|
|
|
|
|
CloudServicePeriod::find(107),
|
|
|
|
|
];
|
|
|
|
|
Capsule::connection('default')->flushQueryLog();
|
|
|
|
|
Capsule::connection('default')->enableQueryLog();
|
|
|
|
|
$batch = CloudTrafficPoolReadModel::forPeriods($batchPeriods, $now);
|
|
|
|
|
$batchQueries = count(Capsule::connection('default')->getQueryLog());
|
|
|
|
|
Capsule::connection('default')->disableQueryLog();
|
|
|
|
|
$assert($batchQueries <= 5 && count($batch) === 4,
|
|
|
|
|
'Multi-user period projection regressed to a query loop per pool.');
|
|
|
|
|
|
|
|
|
|
$pooled = CloudTrafficPoolReadModel::forPeriod(
|
|
|
|
|
1, CloudServicePeriod::find(101), $now
|
|
|
|
|
);
|
|
|
|
|
$shadow = CloudTrafficPoolReadModel::forPeriod(
|
|
|
|
|
1, CloudServicePeriod::find(102), $now
|
|
|
|
|
);
|
|
|
|
|
$unlimited = CloudTrafficPoolReadModel::forPeriod(
|
|
|
|
|
1, CloudServicePeriod::find(104), $now
|
|
|
|
|
);
|
|
|
|
|
$foreign = CloudTrafficPoolReadModel::forPeriod(
|
|
|
|
|
1, CloudServicePeriod::find(103), $now
|
|
|
|
|
);
|
|
|
|
|
$preparing = CloudTrafficPoolReadModel::forPeriod(
|
|
|
|
|
1, CloudServicePeriod::find(106), $now
|
|
|
|
|
);
|
|
|
|
|
$existingPoolPending = CloudTrafficPoolReadModel::forPeriod(
|
|
|
|
|
1, CloudServicePeriod::find(107), $now
|
|
|
|
|
);
|
|
|
|
|
$assert($pooled['traffic_accounting_mode'] === 'pooled'
|
|
|
|
|
&& $pooled['traffic_pool']['group_id'] === 1
|
|
|
|
|
&& $pooled['traffic_pool']['instances'] === [],
|
|
|
|
|
'Enforced finite period did not resolve through the canonical pool model.');
|
|
|
|
|
$assert($shadow['traffic_accounting_mode'] === 'legacy'
|
|
|
|
|
&& $shadow['traffic_pool'] === null,
|
|
|
|
|
'Shadow accounting leaked into the user-facing shared-pool vocabulary.');
|
|
|
|
|
$assert($unlimited['traffic_accounting_mode'] === 'unlimited'
|
|
|
|
|
&& $unlimited['traffic_pool'] === null,
|
|
|
|
|
'Unlimited traffic was not kept outside the finite shared pool.');
|
|
|
|
|
$assert($foreign['traffic_accounting_mode'] === 'legacy',
|
|
|
|
|
'A foreign service period crossed the user boundary.');
|
|
|
|
|
$assert($preparing['traffic_accounting_mode'] === 'pooled'
|
|
|
|
|
&& $preparing['traffic_pool']['projection_ready'] === false
|
|
|
|
|
&& $preparing['traffic_pool']['state'] === 'preparing',
|
|
|
|
|
'An enforced period without a materialized pool was misreported as zero quota.');
|
|
|
|
|
$assert($existingPoolPending['traffic_accounting_mode'] === 'pooled'
|
|
|
|
|
&& $existingPoolPending['traffic_pool']['projection_ready'] === false
|
|
|
|
|
&& $existingPoolPending['traffic_pool']['state'] === 'preparing',
|
|
|
|
|
'An existing pool misreported a missing current entitlement as ready.');
|
|
|
|
|
|
|
|
|
|
Capsule::table('cloud_service_periods')->where('id', 101)->update([
|
|
|
|
|
'traffic_quota_bytes' => 1200,
|
|
|
|
|
]);
|
|
|
|
|
$upgradePending = CloudTrafficPoolReadModel::forPeriod(
|
|
|
|
|
1, CloudServicePeriod::find(101), $now
|
|
|
|
|
);
|
|
|
|
|
$assert($upgradePending['traffic_accounting_mode'] === 'pooled'
|
|
|
|
|
&& $upgradePending['traffic_pool']['projection_ready'] === false
|
|
|
|
|
&& $upgradePending['traffic_pool']['state'] === 'preparing'
|
|
|
|
|
&& $upgradePending['traffic_pool']['active_entitlement_bytes'] === 1500,
|
|
|
|
|
'A paid quota increase exposed the stale entitlement total as ready.');
|
|
|
|
|
|
|
|
|
|
echo "Cloud traffic-pool user read-model tests passed.\n";
|