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

196 lines
9.3 KiB
PHTML

<?php
namespace App\Services {
final class Config
{
public static function get($name, $default = null)
{
return $name === 'cloud_max_renewal_periods_ahead' ? 24 : $default;
}
}
}
namespace {
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudInstancePageService;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$capsule = new Capsule();
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default');
$capsule->setAsGlobal();
$capsule->bootEloquent();
$schema = $capsule->schema('default');
$schema->create('cloud_hosts', static function (Blueprint $table): void {
$table->increments('id');
$table->string('region')->default('');
$table->string('country_code')->default('');
});
$schema->create('cloud_instances', static function (Blueprint $table): void {
$table->increments('id');
$table->string('uuid');
$table->integer('host_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->string('renewal_price')->nullable();
$table->integer('renewal_days')->default(0);
$table->integer('renewal_traffic_bytes')->default(0);
$table->integer('traffic_quota_bytes')->default(0);
$table->integer('traffic_download_bytes')->default(0);
$table->integer('traffic_upload_bytes')->default(0);
$table->text('spec_json');
$table->text('credential_cipher')->nullable();
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_service_periods', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('instance_id');
$table->integer('starts_at');
$table->integer('ends_at');
$table->string('status');
});
Capsule::table('cloud_hosts')->insert([
'id' => 7, 'region' => 'HongKong', 'country_code' => 'HK',
]);
$now = time();
$common = [
'host_id' => 7, 'current_period_id' => null, 'status' => 'active',
'service_status' => 'active', 'network_status' => 'active',
'expires_at' => $now + 86400, 'renewal_price' => '10.00',
'renewal_days' => 30, 'renewal_traffic_bytes' => 2000,
'traffic_quota_bytes' => 2000, 'traffic_download_bytes' => 300,
'traffic_upload_bytes' => 200,
'spec_json' => json_encode([
'vcpu' => 2,
'memory_mib' => 2048,
'state' => 'running',
'console_port' => 5900,
'api_token' => 'must-not-reach-ssr',
'disk_gib' => 40,
'disks' => [[
'capacity_gib' => 40,
'source' => '/home/kvm/private.qcow2',
'target' => 'vda',
'format' => 'qcow2',
]],
'os_source' => 'cloud_template',
'os_name' => 'AlmaLinux 10',
'login_name' => 'root',
'engine_metadata_version' => 1,
]),
'credential_cipher' => 'test-cipher', 'created_at' => $now - 60,
'updated_at' => $now,
];
Capsule::table('cloud_instances')->insert($common + [
'id' => 1, 'uuid' => '11111111-1111-4111-8111-111111111111',
'user_id' => 1, 'name' => 'owner-vm',
]);
Capsule::table('cloud_instances')->insert($common + [
'id' => 2, 'uuid' => '22222222-2222-4222-8222-222222222222',
'user_id' => 2, 'name' => 'foreign-vm',
]);
$owner = ['user_id' => 1, 'is_admin' => false, 'is_super_admin' => false,
'can_manage' => false];
$admin = ['user_id' => 1, 'is_admin' => true, 'is_super_admin' => false,
'can_manage' => false];
$global = ['user_id' => 1, 'is_admin' => true, 'is_super_admin' => true,
'can_manage' => true];
Capsule::connection('default')->flushQueryLog();
Capsule::connection('default')->enableQueryLog();
$page = CloudInstancePageService::forActor($owner, 1, 20);
$pageQueryCount = count(Capsule::connection('default')->getQueryLog());
Capsule::connection('default')->disableQueryLog();
$assert($page['stats']['total'] === 1 && count($page['vms']) === 1
&& $page['pagination']['page_count'] === 1
&& $page['vms'][0]['name'] === 'owner-vm',
'Ordinary user instance first page crossed the owner boundary.');
$assert($pageQueryCount <= 3,
'Instance first page regressed beyond count, rows and host batch queries.');
$assert($page['vms'][0]['state'] === 'unknown',
'Persisted specification power state was trusted before a live UUID check.');
$assert(count($page['vms'][0]['disks']) === 1
&& array_keys($page['vms'][0]['disks'][0]) === ['capacity_gib']
&& (float) $page['vms'][0]['disks'][0]['capacity_gib'] === 40.0
&& $page['vms'][0]['login_name'] === 'root'
&& $page['vms'][0]['engine_metadata_version'] === 1
&& !array_key_exists('console_port', $page['vms'][0])
&& !array_key_exists('api_token', $page['vms'][0]),
'Instance list SSR exposed an unsafe field from the persisted engine snapshot.');
$assert(!array_key_exists('traffic_quota_bytes', $page['vms'][0])
&& !array_key_exists('password_available', $page['vms'][0]),
'Paginated instance rows leaked detail-only billing or credential metadata.');
$ownedDetail = CloudInstancePageService::detailForActor($owner, 1);
$assert($ownedDetail['can_operate'] && $ownedDetail['can_delete']
&& $ownedDetail['password_available'] && $ownedDetail['can_renew'],
'Owner lost expected instance capabilities.');
$assert(count($ownedDetail['disks']) === 1
&& array_keys($ownedDetail['disks'][0]) === ['capacity_gib']
&& (float) $ownedDetail['disks'][0]['capacity_gib'] === 40.0
&& $ownedDetail['login_name'] === 'root'
&& $ownedDetail['engine_metadata_version'] === 1
&& !array_key_exists('console_port', $ownedDetail)
&& !array_key_exists('api_token', $ownedDetail),
'Instance detail SSR exposed an unsafe field from the persisted engine snapshot.');
$assert(count(CloudInstancePageService::forActorIds($owner, [1, 2])) === 1,
'Ordinary user batch status scope disclosed a foreign instance.');
$adminPage = CloudInstancePageService::forActor($admin, 1, 20);
$assert($adminPage['stats']['total'] === 2,
'Read-only administrator lost the historical all-instance view.');
$foreign = CloudInstancePageService::detailForActor($admin, 2);
$assert(!$foreign['is_owner'] && !$foreign['can_operate']
&& !$foreign['can_delete'] && !$foreign['can_view_credential']
&& !$foreign['password_available'] && !$foreign['can_renew'],
'Read-only administrator inherited foreign instance mutation or credential access.');
$managed = CloudInstancePageService::detailForActor($global, 2);
$assert(!$managed['is_owner'] && $managed['can_operate']
&& $managed['can_delete'] && $managed['can_view_credential']
&& $managed['password_available'] && !$managed['can_renew'],
'Super administrator capabilities no longer match the legacy VM boundary.');
Capsule::connection('default')->flushQueryLog();
Capsule::connection('default')->enableQueryLog();
$mutable = CloudInstancePageService::statusForActor($global, 2);
$statusQueryCount = count(Capsule::connection('default')->getQueryLog());
Capsule::connection('default')->disableQueryLog();
$assert($statusQueryCount <= 1 && $mutable['host_id'] === 7
&& !array_key_exists('credential_cipher', $mutable)
&& !array_key_exists('renewal_price', $mutable),
'Detail status poll loaded static host, credential or renewal metadata.');
$status = CloudInstancePageService::status($managed,
['uuid' => $managed['uuid'], 'name' => 'vc-2222222222224222', 'state' => 'running']);
$assert($status['state'] === 'running' && $status['traffic_used_bytes'] === 500
&& $status['traffic_remaining_bytes'] === 1500
&& $status['name'] === 'foreign-vm'
&& $status['live_state_available'] === true,
'Lightweight status did not merge live state while preserving the database display name.');
$unavailable = CloudInstancePageService::status($managed);
$assert($unavailable['state'] === 'unknown'
&& $unavailable['live_state_available'] === false,
'Unavailable hosts were presented as confirmed live power state.');
$power = CloudInstancePageService::powerStatus($managed,
['uuid' => $managed['uuid'], 'name' => 'vc-2222222222224222', 'state' => 'running']);
$assert($power['state'] === 'running' && $power['live_state_available'] === true
&& $power['name'] === 'foreign-vm'
&& !array_key_exists('traffic_quota_bytes', $power)
&& !array_key_exists('can_delete', $power),
'List polling returned detail-only billing or permission fields.');
echo "Cloud instance page service ownership and administrator permissions passed.\n";
}