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/AdminOverviewServiceTest.php

218 lines
11 KiB
PHTML

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudAdminOverviewService;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
date_default_timezone_set('Asia/Shanghai');
$System_Config = ['timeZone' => 'Asia/Shanghai'];
$capsule = new Capsule();
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default');
$capsule->setAsGlobal();
$capsule->bootEloquent();
$schema = $capsule->getConnection('default')->getSchemaBuilder();
$schema->create('cloud_hosts', static function (Blueprint $table): void {
$table->increments('id');
$table->boolean('enabled');
});
$schema->create('cloud_instances', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('user_id');
$table->string('status');
$table->string('service_status');
$table->string('network_status');
$table->integer('expires_at')->nullable();
});
$schema->create('cloud_jobs', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('order_id')->nullable();
$table->string('kind');
$table->string('execution_mode')->default('agent');
$table->string('phase');
$table->string('outcome')->nullable();
$table->string('effect_resolution')->default('not_applicable');
$table->integer('terminal_at')->nullable();
$table->string('status');
$table->boolean('finalization_incident')->default(0);
$table->string('finalization_error_code')->nullable();
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_invoices', static function (Blueprint $table): void {
$table->increments('id');
$table->string('purpose');
$table->integer('instance_id')->nullable();
$table->integer('user_id');
$table->integer('plan_id')->nullable();
$table->integer('due_at');
$table->string('amount');
$table->string('status');
$table->integer('paid_at')->nullable();
});
$schema->create('user', static function (Blueprint $table): void {
$table->increments('id');
$table->boolean('is_admin');
$table->boolean('enable');
$table->dateTime('reg_date');
});
$schema->create('ticket', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('rootid');
$table->integer('status');
});
$schema->create('shop', static function (Blueprint $table): void {
$table->increments('id');
$table->string('name');
});
$now = time();
$today = strtotime('today', $now);
$month = strtotime(date('Y-m-01 00:00:00', $now));
$yesterday = $today - 86400;
Capsule::table('cloud_hosts')->insert([
['id' => 1, 'enabled' => 1],
['id' => 2, 'enabled' => 0],
]);
Capsule::table('cloud_instances')->insert([
['id' => 1, 'user_id' => 1, 'status' => 'active', 'service_status' => 'active',
'network_status' => 'active', 'expires_at' => $now + 3 * 86400],
['id' => 2, 'user_id' => 2, 'status' => 'active', 'service_status' => 'exempt',
'network_status' => 'active', 'expires_at' => null],
['id' => 3, 'user_id' => 1, 'status' => 'active', 'service_status' => 'active',
'network_status' => 'blocked_traffic', 'expires_at' => $now - 3600],
['id' => 4, 'user_id' => 2, 'status' => 'deleted', 'service_status' => 'active',
'network_status' => 'active', 'expires_at' => $now + 86400],
['id' => 5, 'user_id' => 2, 'status' => 'delete_failed', 'service_status' => 'purge_failed',
'network_status' => 'active', 'expires_at' => $now - 3600],
]);
Capsule::table('cloud_jobs')->insert([
['order_id' => 1, 'kind' => 'create', 'phase' => 'accepted',
'outcome' => null, 'effect_resolution' => 'not_applicable',
'terminal_at' => null, 'status' => 'queued',
'created_at' => $today + 10, 'updated_at' => $now],
['order_id' => null, 'kind' => 'reinstall', 'phase' => 'dispatching',
'outcome' => null, 'effect_resolution' => 'not_applicable',
'terminal_at' => null, 'status' => 'running',
'created_at' => $today + 20, 'updated_at' => $now],
['order_id' => null, 'kind' => 'rescue', 'phase' => 'terminal',
'outcome' => 'failed', 'effect_resolution' => 'unknown',
'terminal_at' => $now - 25, 'status' => 'failed',
'created_at' => $today + 25, 'updated_at' => $now],
['order_id' => 2, 'kind' => 'create', 'phase' => 'terminal',
'outcome' => 'failed', 'effect_resolution' => 'unknown',
'terminal_at' => $now - 30, 'status' => 'failed',
'created_at' => $today + 30, 'updated_at' => $now],
['order_id' => 3, 'kind' => 'create', 'phase' => 'terminal',
'outcome' => 'failed', 'effect_resolution' => 'not_applicable',
'terminal_at' => $now - 20, 'status' => 'failed',
'created_at' => $today + 40, 'updated_at' => $now],
['order_id' => 4, 'kind' => 'create', 'phase' => 'terminal',
'outcome' => 'succeeded', 'effect_resolution' => 'not_applicable',
'terminal_at' => $now - 10, 'status' => 'succeeded',
'created_at' => $today + 50, 'updated_at' => $now],
['order_id' => 7, 'kind' => 'create', 'phase' => 'terminal',
'outcome' => 'failed', 'effect_resolution' => 'unknown',
'terminal_at' => $now - 5, 'status' => 'failed',
'created_at' => $today + 60, 'updated_at' => $now],
['order_id' => 5, 'kind' => 'create', 'phase' => 'terminal',
'outcome' => 'succeeded', 'effect_resolution' => 'not_applicable',
'terminal_at' => $today + 70, 'status' => 'succeeded', 'created_at' => $today - 3600,
'updated_at' => $now],
['order_id' => 6, 'kind' => 'create', 'phase' => 'terminal',
'outcome' => 'failed', 'effect_resolution' => 'not_applicable',
'terminal_at' => $now - 90000, 'status' => 'failed', 'created_at' => $today - 172800,
'updated_at' => $now - 90000],
]);
Capsule::table('shop')->insert(['id' => 1, 'name' => '2c4g']);
Capsule::table('cloud_invoices')->insert([
['purpose' => 'provision', 'instance_id' => 1, 'user_id' => 1, 'plan_id' => 1,
'due_at' => $today, 'amount' => '10.00', 'status' => 'paid', 'paid_at' => $today + 100],
['purpose' => 'renewal', 'instance_id' => 1, 'user_id' => 1, 'plan_id' => 1,
'due_at' => $yesterday, 'amount' => '20.00', 'status' => 'paid', 'paid_at' => $yesterday + 100],
['purpose' => 'renewal', 'instance_id' => 1, 'user_id' => 1, 'plan_id' => 1,
'due_at' => $month, 'amount' => '30.00', 'status' => 'paid', 'paid_at' => $month + 100],
['purpose' => 'provision', 'instance_id' => 1, 'user_id' => 1, 'plan_id' => 1,
'due_at' => $today, 'amount' => '100.00', 'status' => 'refunded', 'paid_at' => $today + 200],
['purpose' => 'renewal', 'instance_id' => 1, 'user_id' => 1, 'plan_id' => 1,
'due_at' => $now + 1000, 'amount' => '5.00', 'status' => 'open', 'paid_at' => null],
['purpose' => 'renewal', 'instance_id' => 3, 'user_id' => 1, 'plan_id' => 1,
'due_at' => $now - 1000, 'amount' => '6.00', 'status' => 'open', 'paid_at' => null],
['purpose' => 'renewal', 'instance_id' => 4, 'user_id' => 2, 'plan_id' => 1,
'due_at' => $now - 1000, 'amount' => '9.00', 'status' => 'overdue', 'paid_at' => null],
['purpose' => 'provision', 'instance_id' => null, 'user_id' => 2, 'plan_id' => 1,
'due_at' => $now + 1000, 'amount' => '7.00', 'status' => 'open', 'paid_at' => null],
]);
Capsule::table('user')->insert([
['id' => 1, 'is_admin' => 0, 'enable' => 1,
'reg_date' => date('Y-m-d H:i:s', $today + 100)],
['id' => 2, 'is_admin' => 0, 'enable' => 0,
'reg_date' => date('Y-m-d H:i:s', $today - 86400)],
['id' => 3, 'is_admin' => 1, 'enable' => 1,
'reg_date' => date('Y-m-d H:i:s', $today + 200)],
]);
Capsule::table('ticket')->insert([
['id' => 1, 'rootid' => 0, 'status' => 1],
['id' => 2, 'rootid' => 0, 'status' => 0],
['id' => 3, 'rootid' => 1, 'status' => 1],
]);
Capsule::connection('default')->enableQueryLog();
$overview = CloudAdminOverviewService::snapshot();
$queries = count(Capsule::connection('default')->getQueryLog());
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$assert($overview['hosts'] === ['total' => 2, 'enabled' => 1],
'Host enabled/configured aggregation is incorrect.');
$assert($overview['instances']['managed'] === 4
&& $overview['instances']['billable'] === 3
&& $overview['instances']['service_normal'] === 2
&& $overview['instances']['network_normal'] === 2
&& $overview['instances']['expiring_7d'] === 1
&& $overview['instances']['grace'] === 1,
'Instance lifecycle aggregation is incorrect.');
$assert($overview['jobs']['active'] === 2
&& $overview['jobs']['unresolved_effect'] === 3
&& $overview['jobs']['failed_24h'] === 4
&& $overview['jobs']['today_total'] === 5
&& $overview['jobs']['today_succeeded'] === 2
&& $overview['jobs']['today_failed'] === 3,
'Global job aggregation must include non-provisioning work while today totals remain provisioning-only.');
$assert($overview['attention_total'] === $overview['jobs']['unresolved_effect']
+ $overview['jobs']['failed_24h']
+ $overview['instances']['network_attention']
+ $overview['receivables']['overdue_count'] + $overview['tickets']['open'],
'Terminal failures and unresolved effects must remain visible in the administrator attention total.');
$assert($overview['instances']['service_normal'] + $overview['instances']['grace'] <= 4,
'Expired instances must not be counted as both service-normal and in grace.');
$assert($overview['revenue']['today'] === '10.00'
&& $overview['revenue']['yesterday'] === '20.00'
&& $overview['revenue']['today_count'] === 1,
'Revenue must use paid_at and exclude refunded invoices.');
$assert($overview['receivables']['count'] === 3
&& $overview['receivables']['amount'] === '18.00'
&& $overview['receivables']['overdue_count'] === 1
&& $overview['receivables']['overdue_amount'] === '6.00',
'Receivables must exclude orphan renewal invoices for deleted instances.');
$assert($overview['users'] === ['total' => 2, 'enabled' => 1, 'new_today' => 1]
&& $overview['tickets']['open'] === 1,
'Customer or open-ticket aggregation is incorrect.');
$assert(count($overview['top_plans']) === 1
&& $overview['top_plans'][0]['name'] === '2c4g'
&& $overview['top_plans'][0]['count'] === 1
&& $overview['top_plans'][0]['amount'] === '10.00',
'Top plans must use current paid provisioning invoices only.');
$assert($queries <= 9,
'Overview introduced an unbounded query pattern: ' . $queries . ' queries.');
echo 'Administrator overview database behavior passed with ' . $queries . " queries.\n";