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

202 lines
7.4 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\CloudBackupSubscription;
use App\Models\CloudInstance;
use App\Services\Cloud\CloudAutomaticBackupService;
use App\Services\Cloud\CloudBackupService;
use App\Services\Cloud\CloudError;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
$failures = [];
$check = static function (bool $condition, string $message) use (&$failures): void {
if (!$condition) {
$failures[] = $message;
}
};
$capsule = new Capsule();
$capsule->addConnection([
'driver' => 'sqlite', 'database' => ':memory:',
], 'default');
$capsule->setAsGlobal();
$capsule->bootEloquent();
$schema = $capsule->schema('default');
$schema->create('cloud_instances', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('user_id');
$table->string('status');
$table->string('service_status');
$table->integer('expires_at')->nullable();
});
$schema->create('cloud_jobs', static function (Blueprint $table): void {
$table->increments('id');
$table->string('operation_id')->unique();
$table->string('request_idempotency_key')->unique();
$table->string('request_fingerprint');
$table->integer('parent_job_id')->nullable();
$table->integer('order_id')->nullable();
$table->integer('invoice_id')->nullable();
$table->integer('instance_id')->nullable();
$table->integer('snapshot_id')->nullable();
$table->integer('backup_id')->nullable();
$table->integer('backup_restore_id')->nullable();
$table->integer('backup_recovery_id')->nullable();
$table->integer('user_id');
$table->integer('host_id')->nullable();
$table->string('phase');
$table->string('outcome')->nullable();
$table->string('status');
$table->string('kind');
$table->string('execution_mode');
$table->text('sealed_payload');
$table->integer('attempts')->default(1);
$table->integer('dispatch_attempts')->default(1);
$table->integer('dispatch_deadline_at')->nullable();
$table->integer('dispatch_started_at')->nullable();
$table->integer('agent_accepted_at')->nullable();
$table->integer('receipt_deadline_at')->nullable();
$table->integer('dispatched_at')->nullable();
$table->integer('terminal_at')->nullable();
$table->string('error_code')->nullable();
$table->string('effect_resolution');
$table->boolean('effect_may_have_applied')->nullable();
$table->boolean('safe_to_resubmit')->nullable();
$table->boolean('finalization_incident')->default(false);
$table->string('finalization_error_code')->nullable();
$table->string('completion_event_id')->nullable();
$table->text('result_json')->nullable();
});
$schema->create('cloud_instance_migrations',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('instance_id');
$table->integer('vpc_port_id')->nullable();
$table->string('state');
});
$schema->create('cloud_peer_migration_artifacts',
static function (Blueprint $table): void {
$table->increments('id');
});
$schema->create('cloud_backup_subscriptions',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('instance_id')->unique();
$table->integer('user_id');
$table->boolean('enabled');
$table->boolean('renew_next_period');
$table->string('frequency');
$table->integer('weekday');
$table->integer('retention_count');
$table->string('timezone');
$table->string('price');
$table->string('currency');
$table->integer('price_version');
$table->integer('next_run_at');
$table->integer('last_scheduled_at')->nullable();
$table->integer('last_backup_id')->nullable();
$table->integer('created_at');
$table->integer('updated_at');
});
$now = time();
$instance = new CloudInstance();
$instance->id = 1;
$instance->user_id = 9;
$instance->status = 'active';
$instance->service_status = 'active';
$instance->expires_at = $now + 86400;
$instance->save();
$capsule->table('cloud_instance_migrations')->insert([
'id' => 1,
'instance_id' => (int) $instance->id,
'state' => 'attention_required',
]);
$guard = new ReflectionMethod(CloudBackupService::class, 'assertNoActiveJob');
$blocked = false;
try {
$guard->invoke(null, $instance);
} catch (CloudError $error) {
$blocked = $error->httpStatus === 409
&& str_contains($error->getMessage(), '维护迁移');
}
$check($blocked,
'An open maintenance migration without an active job bypassed the backup mutation fence.');
$source = (string) file_get_contents(
dirname(__DIR__) . '/app/Services/Cloud/CloudBackupService.php'
);
$section = static function (string $source, string $start, string $end): string {
$offset = strpos($source, $start);
$finish = $offset === false ? false : strpos($source, $end, $offset + 1);
return $offset === false || $finish === false
? '' : substr($source, $offset, $finish - $offset);
};
$createPath = $section($source, 'private static function createInternal(',
'public static function restore(');
$recoverPath = $section($source, 'public static function recover(',
'public static function markJobUncertain(');
$check(str_contains($createPath, 'self::assertNoActiveJob($locked);')
&& str_contains($recoverPath,
'self::assertNoActiveJob($lockedInstance);'),
'Backup creation and original-instance recovery no longer share the authoritative fence.');
$capsule->table('cloud_instance_migrations')->where('id', 1)->update([
'state' => 'completed',
]);
$completedAllowed = true;
try {
$guard->invoke(null, $instance);
} catch (CloudError $error) {
$completedAllowed = false;
}
$check($completedAllowed,
'A completed maintenance migration continued to block backup mutations.');
$capsule->table('cloud_instance_migrations')->where('id', 1)->update([
'state' => 'attention_required',
]);
$subscription = new CloudBackupSubscription();
$subscription->instance_id = (int) $instance->id;
$subscription->user_id = (int) $instance->user_id;
$subscription->enabled = true;
$subscription->renew_next_period = true;
$subscription->frequency = 'daily';
$subscription->weekday = 0;
$subscription->retention_count = 2;
$subscription->timezone = 'UTC';
$subscription->price = '2.50';
$subscription->currency = 'USD';
$subscription->price_version = 1;
$subscription->next_run_at = $now - 60;
$subscription->created_at = $subscription->updated_at = $now - 3600;
$subscription->save();
$logPath = tempnam(sys_get_temp_dir(), 'virt-backup-migration-fence-');
$previousLog = ini_set('error_log', $logPath);
try {
$queued = CloudAutomaticBackupService::scheduleDue(10, $now);
$subscription->refresh();
$log = (string) file_get_contents($logPath);
} finally {
if (is_string($previousLog)) {
ini_set('error_log', $previousLog);
}
@unlink($logPath);
}
$check($queued === 0
&& (bool) $subscription->enabled
&& (int) $subscription->next_run_at === $now + 300,
'Automatic backup was not silently deferred for the open migration.');
$check(!str_contains($log, 'cloud automatic backup schedule unavailable'),
'Expected migration deferral was recorded as an automatic-backup failure.');
if ($failures !== []) {
throw new RuntimeException(implode("\n", $failures));
}
echo "Cloud backup maintenance-migration fence tests passed.\n";