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.
141 lines
5.0 KiB
PHP
141 lines
5.0 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Services\Cloud\CloudInstanceMetadataRepairService;
|
|
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_templates', static function (Blueprint $table): void {
|
|
$table->string('id')->primary();
|
|
$table->string('name');
|
|
$table->string('state');
|
|
});
|
|
$schema->create('cloud_orders', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->integer('user_id');
|
|
$table->integer('host_id')->nullable();
|
|
$table->integer('instance_id')->nullable();
|
|
$table->string('status');
|
|
$table->text('spec_json');
|
|
});
|
|
$schema->create('cloud_instances', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->integer('host_id');
|
|
$table->integer('user_id');
|
|
$table->integer('order_id')->nullable();
|
|
$table->string('status');
|
|
$table->text('spec_json');
|
|
$table->integer('updated_at');
|
|
});
|
|
|
|
Capsule::table('cloud_templates')->insert([
|
|
'id' => 'debian-13-template',
|
|
'name' => 'Debian 13',
|
|
'state' => 'active',
|
|
]);
|
|
$baseSpec = [
|
|
'source' => 'cloud',
|
|
'template_id' => 'debian-13-template',
|
|
'public_ip_cidr' => '203.0.113.18/28',
|
|
'agent_metadata_version' => 1,
|
|
];
|
|
foreach ([
|
|
1 => $baseSpec,
|
|
2 => array_replace($baseSpec, ['os_source' => 'iso']),
|
|
3 => $baseSpec,
|
|
4 => $baseSpec,
|
|
] as $id => $spec) {
|
|
$orderSpec = $id === 4 ? array_replace($baseSpec, [
|
|
'os_source' => 'cloud_template',
|
|
'os_name' => 'Debian 12 Snapshot',
|
|
'login_name' => 'root',
|
|
]) : $baseSpec;
|
|
Capsule::table('cloud_orders')->insert([
|
|
'id' => $id,
|
|
'user_id' => 10,
|
|
'host_id' => 7,
|
|
'instance_id' => $id,
|
|
'status' => 'succeeded',
|
|
'spec_json' => json_encode($orderSpec, JSON_THROW_ON_ERROR),
|
|
]);
|
|
Capsule::table('cloud_instances')->insert([
|
|
'id' => $id,
|
|
'host_id' => 7,
|
|
'user_id' => $id === 3 ? 99 : 10,
|
|
'order_id' => $id,
|
|
'status' => 'active',
|
|
'spec_json' => json_encode($spec, JSON_THROW_ON_ERROR),
|
|
'updated_at' => 1,
|
|
]);
|
|
}
|
|
|
|
$firstPage = CloudInstanceMetadataRepairService::run(false, 1);
|
|
$assert($firstPage['scanned'] === 1 && $firstPage['would_repair'] === 1
|
|
&& $firstPage['has_more'] === true
|
|
&& $firstPage['next_after_id'] === 1,
|
|
'Repair scan did not expose a stable next-page cursor.');
|
|
$lastPage = CloudInstanceMetadataRepairService::run(false, 1, 3);
|
|
$assert($lastPage['after_id'] === 3 && $lastPage['scanned'] === 1
|
|
&& $lastPage['would_repair'] === 1
|
|
&& $lastPage['has_more'] === false
|
|
&& $lastPage['next_after_id'] === null,
|
|
'Repair scan did not honor the after-id cursor.');
|
|
|
|
$before = (string) Capsule::table('cloud_instances')->where('id', 1)
|
|
->value('spec_json');
|
|
$dryRun = CloudInstanceMetadataRepairService::run(false, 100);
|
|
$assert($dryRun === [
|
|
'mode' => 'dry-run',
|
|
'after_id' => 0,
|
|
'next_after_id' => null,
|
|
'has_more' => false,
|
|
'scanned' => 4,
|
|
'would_repair' => 2,
|
|
'repaired' => 0,
|
|
'unchanged' => 0,
|
|
'skipped' => 2,
|
|
] && Capsule::table('cloud_instances')->where('id', 1)
|
|
->value('spec_json') === $before,
|
|
'Dry-run changed data or did not report the one proven repair.');
|
|
|
|
$applied = CloudInstanceMetadataRepairService::run(true, 100);
|
|
$repaired = json_decode((string) Capsule::table('cloud_instances')
|
|
->where('id', 1)->value('spec_json'), true, flags: JSON_THROW_ON_ERROR);
|
|
$conflict = json_decode((string) Capsule::table('cloud_instances')
|
|
->where('id', 2)->value('spec_json'), true, flags: JSON_THROW_ON_ERROR);
|
|
$snapshot = json_decode((string) Capsule::table('cloud_instances')
|
|
->where('id', 4)->value('spec_json'), true, flags: JSON_THROW_ON_ERROR);
|
|
$assert($applied['repaired'] === 2 && $applied['skipped'] === 2
|
|
&& ($repaired['os_source'] ?? null) === 'cloud_template'
|
|
&& ($repaired['os_name'] ?? null) === 'Debian 13'
|
|
&& ($repaired['login_name'] ?? null) === 'root'
|
|
&& ($repaired['managed'] ?? null) === true
|
|
&& ($repaired['public_ip_cidr'] ?? null) === '203.0.113.18/28'
|
|
&& ($repaired['agent_metadata_version'] ?? null) === 1
|
|
&& ($conflict['os_source'] ?? null) === 'iso',
|
|
'Apply did not repair only the proven panel instance.');
|
|
$assert(($snapshot['os_name'] ?? null) === 'Debian 12 Snapshot'
|
|
&& ($snapshot['managed'] ?? null) === true,
|
|
'Repair did not prefer the immutable paid-order metadata snapshot.');
|
|
|
|
$repeat = CloudInstanceMetadataRepairService::run(true, 100);
|
|
$assert($repeat['repaired'] === 0 && $repeat['unchanged'] === 2
|
|
&& $repeat['skipped'] === 2,
|
|
'Metadata repair was not idempotent.');
|
|
|
|
echo "Cloud instance metadata repair tests passed.\n";
|