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

330 lines
13 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\CloudBackupRestore;
use App\Services\Cloud\CloudError;
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('country_code');
$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');
$table->string('pool_key');
$table->string('cidr');
$table->string('gateway');
$table->text('dns_servers_json');
$table->text('reserved_ips_json');
});
$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();
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_resource_reservations',
static function (Blueprint $table): void {
$table->increments('id');
$table->string('reservation_key');
$table->string('operation_id');
$table->integer('order_id')->nullable();
$table->integer('backup_restore_id')->nullable();
$table->integer('offer_id')->nullable();
$table->integer('resource_pool_id');
$table->integer('host_id');
$table->integer('user_id');
$table->string('state');
$table->integer('instance_id')->nullable();
$table->integer('network_attachment_id')->nullable();
$table->integer('public_ip_pool_id')->nullable();
$table->integer('public_ip_id')->nullable();
$table->integer('cpu_cores');
$table->integer('memory_mib');
$table->integer('disk_gib');
$table->integer('expires_at')->nullable();
$table->integer('released_at')->nullable();
$table->string('release_reason');
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_backup_restores',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('target_host_id');
$table->integer('reservation_id')->nullable();
$table->string('operation_id');
});
$schema->create('cloud_instances', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->string('status');
$table->text('spec_json');
});
$schema->create('cloud_instance_snapshots',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('instance_id');
$table->integer('host_id');
$table->string('state');
$table->integer('size_bytes')->nullable();
});
$capsule->table('cloud_regions')->insert([
'id' => 1, 'name' => 'Hong Kong', 'country_code' => 'HK',
'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,
'pool_key' => str_repeat((string) $hostId, 32),
'cidr' => '203.0.' . $hostId . '.0/24',
'gateway' => '203.0.' . $hostId . '.1',
'dns_servers_json' => '["1.1.1.1","8.8.8.8"]',
'reserved_ips_json' => '[]',
]);
$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,
'created_at' => 1700000000, 'updated_at' => 1700000000,
]);
}
// 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.');
$restore = new CloudBackupRestore();
$restore->target_host_id = 1;
$restore->reservation_id = null;
$restore->operation_id = str_repeat('a', 32);
$restore->save();
$placement = CloudPlacementService::reserveBackupRestore($restore, 77, [
'cpu_cores' => 2, 'memory_mib' => 2048, 'disk_gib' => 20,
]);
$reservation = $placement['reservation'];
$publicIp = $placement['public_ip'];
$assert($placement['network']['dns_servers'] === ['1.1.1.1', '8.8.8.8'],
'A valid pool DNS list could not reach the restore placement payload.');
$assert((int) $reservation->backup_restore_id === (int) $restore->id
&& (int) $reservation->public_ip_id === (int) $publicIp->id
&& (string) $reservation->state === 'held'
&& (string) $publicIp->state === 'reserved'
&& (int) $publicIp->reservation_id === (int) $reservation->id,
'Restore placement did not atomically reserve capacity and one public IP.');
// Maintenance migration consumes the exact pre-reserved network rather than
// scheduling another address after the source VM has already stopped.
$restore->reservation_id = (int) $reservation->id;
$restore->save();
$reservation->instance_id = 99;
$reservation->save();
$prepared = CloudPlacementService::preparedBackupRestorePlacement(
$restore->fresh(),
99
);
$assert($prepared['network']['dns_servers'] === ['1.1.1.1', '8.8.8.8']
&& (int) $prepared['reservation']->id === (int) $reservation->id,
'Maintenance migration did not preserve the pre-reserved pool DNS data.');
$capsule->table('cloud_public_ip_pools')->where('id', 1)->update([
'dns_servers_json' => '["not-an-ip"]',
]);
try {
CloudPlacementService::preparedBackupRestorePlacement(
$restore->fresh(),
99
);
throw new RuntimeException('A malformed prepared DNS list was accepted.');
} catch (CloudError $error) {
$assert($error->httpStatus === 409
&& $error->getMessage() === '公网地址池 DNS 配置损坏',
'Malformed prepared DNS did not return the bounded placement error.');
}
$capsule->table('cloud_public_ip_pools')->where('id', 2)->update([
'dns_servers_json' => '["not-an-ip"]',
]);
$invalidRestore = new CloudBackupRestore();
$invalidRestore->target_host_id = 2;
$invalidRestore->reservation_id = null;
$invalidRestore->operation_id = str_repeat('b', 32);
$invalidRestore->save();
try {
CloudPlacementService::reserveBackupRestore($invalidRestore, 77, [
'cpu_cores' => 2, 'memory_mib' => 2048, 'disk_gib' => 20,
]);
throw new RuntimeException('A malformed pool DNS list was accepted.');
} catch (CloudError $error) {
$assert($error->httpStatus === 409
&& $error->getMessage() === '目标宿主机没有可分配的公网 IP',
'Malformed pool DNS was not excluded from target allocation.');
}
$assert($capsule->table('cloud_resource_reservations')->count() === 1
&& $capsule->table('cloud_public_ips')->where('host_id', 2)
->value('state') === 'available',
'Malformed pool DNS consumed restore capacity or a public IP.');
echo "Portable backup same-host restore target behavior passed.\n";