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

211 lines
7.7 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudPlacementService;
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->setAsGlobal();
$capsule->bootEloquent();
$schema = $capsule->schema('default');
$schema->create('cloud_hosts', static function (Blueprint $table): void {
$table->increments('id');
$table->string('name');
$table->boolean('enabled');
$table->string('default_network');
});
$schema->create('cloud_regions', static function (Blueprint $table): void {
$table->increments('id');
$table->string('name');
$table->string('status');
});
$schema->create('cloud_network_lines', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('region_id');
$table->string('status');
});
$schema->create('cloud_resource_pools', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('line_id');
$table->string('name');
$table->string('status');
});
$schema->create('cloud_host_resource_pool_memberships',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('resource_pool_id');
$table->string('scheduler_state');
});
$schema->create('cloud_host_capacity_policies',
static function (Blueprint $table): void {
$table->integer('host_id')->primary();
$table->integer('configured_cpu_cores');
$table->integer('configured_memory_mib');
$table->integer('configured_disk_gib');
$table->integer('safety_cpu_cores');
$table->integer('safety_memory_mib');
$table->integer('safety_disk_gib');
$table->integer('max_instances');
});
$schema->create('cloud_host_agents', static function (Blueprint $table): void {
$table->integer('host_id')->primary();
$table->boolean('enabled');
$table->integer('revision');
$table->integer('tested_revision');
$table->string('last_test_status');
});
$schema->create('cloud_host_storage_configurations',
static function (Blueprint $table): void {
$table->integer('host_id')->primary();
$table->string('state');
});
$schema->create('cloud_host_network_attachments',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('line_id');
$table->string('network_selector');
$table->string('state');
$table->string('sync_state');
$table->integer('observed_revision');
$table->integer('desired_revision');
});
$schema->create('cloud_public_ip_pools',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('network_attachment_id');
$table->string('state');
$table->string('sync_state');
$table->integer('observed_revision');
$table->integer('desired_revision');
$table->integer('guest_prefix_length');
});
$schema->create('cloud_public_ips', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('public_ip_pool_id');
$table->string('address');
$table->integer('prefix_length');
$table->string('state');
$table->integer('reservation_id')->nullable();
$table->integer('instance_id')->nullable();
$table->string('operation_id')->nullable();
});
$schema->create('cloud_resource_reservations',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->string('state');
$table->integer('instance_id')->nullable();
$table->integer('cpu_cores');
$table->integer('memory_mib');
$table->integer('disk_gib');
});
$schema->create('cloud_instances', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->string('status');
$table->text('spec_json');
});
$capsule->table('cloud_regions')->insert([
'id' => 1, 'name' => 'Hong Kong', 'status' => 'active',
]);
$capsule->table('cloud_network_lines')->insert([
'id' => 1, 'region_id' => 1, 'status' => 'active',
]);
$capsule->table('cloud_resource_pools')->insert([
'id' => 1, 'line_id' => 1, 'name' => 'HKG', 'status' => 'active',
]);
foreach ([1 => 'source-host', 2 => 'other-host'] as $hostId => $name) {
$capsule->table('cloud_hosts')->insert([
'id' => $hostId, 'name' => $name, 'enabled' => 1,
'default_network' => 'bridge:br0',
]);
$capsule->table('cloud_host_resource_pool_memberships')->insert([
'host_id' => $hostId, 'resource_pool_id' => 1,
'scheduler_state' => 'active',
]);
$capsule->table('cloud_host_capacity_policies')->insert([
'host_id' => $hostId,
'configured_cpu_cores' => 16,
'configured_memory_mib' => 32768,
'configured_disk_gib' => 1000,
'safety_cpu_cores' => 0,
'safety_memory_mib' => 0,
'safety_disk_gib' => 0,
'max_instances' => 20,
]);
$capsule->table('cloud_host_agents')->insert([
'host_id' => $hostId, 'enabled' => 1, 'revision' => 1,
'tested_revision' => 1, 'last_test_status' => 'healthy',
]);
$capsule->table('cloud_host_storage_configurations')->insert([
'host_id' => $hostId, 'state' => 'applied',
]);
$attachmentId = $hostId;
$poolId = $hostId;
$capsule->table('cloud_host_network_attachments')->insert([
'id' => $attachmentId, 'host_id' => $hostId, 'line_id' => 1,
'network_selector' => 'bridge:br0', 'state' => 'active',
'sync_state' => 'applied', 'observed_revision' => 1,
'desired_revision' => 1,
]);
$capsule->table('cloud_public_ip_pools')->insert([
'id' => $poolId, 'host_id' => $hostId,
'network_attachment_id' => $attachmentId, 'state' => 'active',
'sync_state' => 'applied', 'observed_revision' => 1,
'desired_revision' => 1, 'guest_prefix_length' => 24,
]);
$capsule->table('cloud_public_ips')->insert([
'host_id' => $hostId, 'public_ip_pool_id' => $poolId,
'address' => '203.0.' . $hostId . '.20', 'prefix_length' => 24,
'state' => 'available', 'reservation_id' => null,
'instance_id' => null, 'operation_id' => null,
]);
}
// The source VM consumes capacity on its current host. A same-host restore
// must still reserve a second, independent resource and IP allocation.
$capsule->table('cloud_instances')->insert([
'id' => 1, 'host_id' => 1, 'status' => 'active',
'spec_json' => json_encode([
'vcpu' => 2, 'memory_mib' => 2048, 'disk_gib' => 20,
], JSON_THROW_ON_ERROR),
]);
$targets = CloudPlacementService::backupRestoreTargets(1, [
'cpu_cores' => 2, 'memory_mib' => 2048, 'disk_gib' => 20,
]);
$byId = [];
foreach ($targets as $target) {
$byId[(int) $target['id']] = $target;
}
$assert(isset($byId[1]),
'The original host was excluded from eligible manual restore targets.');
$assert(($byId[1]['is_source_host'] ?? null) === true,
'The original host target was not explicitly identified.');
$assert(isset($byId[2])
&& ($byId[2]['is_source_host'] ?? null) === false,
'A different eligible host was lost or misidentified.');
$assert((int) $byId[1]['available_ip_count'] === 1,
'The original host did not retain its independent IP capacity check.');
echo "Portable backup same-host restore target behavior passed.\n";