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

236 lines
8.6 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\CloudBackupSubscription;
use App\Models\CloudInstance;
use App\Models\CloudInstanceBackup;
use App\Services\Cloud\CloudAutomaticBackupService;
use App\Services\Cloud\CloudJobService;
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);
}
};
$db = new Capsule();
$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
], 'default');
$db->setAsGlobal();
$db->bootEloquent();
$schema = $db->schema('default');
$schema->create('cloud_instances', static function (Blueprint $t): void {
$t->increments('id');
$t->unsignedBigInteger('user_id');
$t->unsignedBigInteger('offer_id');
$t->unsignedBigInteger('host_id');
$t->string('uuid');
$t->string('status');
$t->string('service_status');
$t->unsignedBigInteger('expires_at')->nullable();
});
$schema->create('cloud_backup_subscriptions',
static function (Blueprint $t): void {
$t->increments('id');
$t->unsignedBigInteger('instance_id')->unique();
$t->unsignedBigInteger('user_id');
$t->unsignedBigInteger('offer_id');
$t->boolean('enabled');
$t->boolean('renew_next_period');
$t->string('frequency');
$t->unsignedTinyInteger('weekday');
$t->unsignedTinyInteger('retention_count');
$t->string('timezone');
$t->string('price');
$t->string('currency');
$t->unsignedInteger('price_version');
$t->unsignedInteger('state_revision')->default(1);
$t->unsignedBigInteger('next_run_at');
$t->unsignedBigInteger('last_scheduled_at')->nullable();
$t->unsignedBigInteger('last_backup_id')->nullable();
$t->unsignedBigInteger('created_at');
$t->unsignedBigInteger('updated_at');
});
$schema->create('cloud_backup_period_entitlements',
static function (Blueprint $t): void {
$t->increments('id');
$t->unsignedBigInteger('subscription_id');
$t->unsignedBigInteger('instance_id');
$t->unsignedBigInteger('user_id');
$t->unsignedBigInteger('starts_at');
$t->unsignedBigInteger('ends_at');
});
$schema->create('cloud_instance_migrations',
static function (Blueprint $t): void {
$t->increments('id');
$t->unsignedBigInteger('instance_id');
$t->string('state');
});
$schema->create('cloud_peer_migration_artifacts',
static function (Blueprint $t): void {
$t->increments('id');
});
$schema->create('cloud_jobs', static function (Blueprint $t): void {
$t->increments('id');
$t->unsignedBigInteger('user_id');
$t->string('request_idempotency_key');
$t->unsignedBigInteger('backup_id')->nullable();
});
$schema->create('cloud_instance_backups',
static function (Blueprint $t): void {
$t->increments('id');
$t->string('backup_key');
$t->unsignedBigInteger('storage_artifact_id')->nullable();
$t->unsignedBigInteger('create_request_actor_id')->nullable();
$t->string('create_request_idempotency_key')->nullable();
$t->string('create_request_fingerprint')->nullable();
$t->unsignedBigInteger('source_instance_id');
$t->unsignedBigInteger('source_host_id');
$t->unsignedBigInteger('user_id');
$t->string('source_uuid');
$t->string('name');
$t->string('purpose');
$t->string('source');
$t->unsignedBigInteger('subscription_id')->nullable();
$t->unsignedBigInteger('scheduled_for')->nullable();
$t->string('state');
$t->string('consistency');
$t->unsignedBigInteger('active_job_id')->nullable();
$t->string('last_operation_id')->nullable();
$t->string('source_portable_restore_contract_sha256');
$t->string('error_code')->default('');
$t->unsignedBigInteger('created_at');
$t->unsignedBigInteger('updated_at');
});
$now = time();
$scheduledFor = $now - 60;
$instance = new CloudInstance();
$instance->id = 41;
$instance->user_id = 77;
$instance->offer_id = 5;
$instance->host_id = 9;
$instance->uuid = '11111111-2222-4333-8444-555555555555';
$instance->status = 'active';
$instance->service_status = 'active';
$instance->expires_at = $now + 86400;
$instance->save();
$subscription = new CloudBackupSubscription();
$subscription->id = 13;
$subscription->instance_id = (int) $instance->id;
$subscription->user_id = (int) $instance->user_id;
$subscription->offer_id = (int) $instance->offer_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.00';
$subscription->currency = 'USD';
$subscription->price_version = 1;
$subscription->state_revision = 1;
$subscription->next_run_at = $scheduledFor;
$subscription->last_scheduled_at = null;
$subscription->last_backup_id = null;
$subscription->created_at = $subscription->updated_at = $now - 3600;
$subscription->save();
$db->table('cloud_backup_period_entitlements')->insert([
'subscription_id' => (int) $subscription->id,
'instance_id' => (int) $instance->id,
'user_id' => (int) $instance->user_id,
'starts_at' => $scheduledFor - 3600,
'ends_at' => $scheduledFor + 3600,
]);
$zone = new DateTimeZone(CloudAutomaticBackupService::TIMEZONE);
$name = 'autobackup-' . (new DateTimeImmutable('@' . $scheduledFor))
->setTimezone($zone)->format('Ymd-His');
$canonical = [
'instance_id' => (int) $instance->id,
'name' => $name,
'consistency_mode' => 'filesystem',
'source' => 'automatic',
'subscription_id' => (int) $subscription->id,
'scheduled_for' => $scheduledFor,
];
$fingerprint = CloudJobService::standaloneRequestFingerprint(
(int) $instance->host_id,
'backup_create',
'POST',
'/v1/vms/' . rawurlencode((string) $instance->uuid) . '/backups',
$canonical
);
$requestKey = 'automatic-backup-' . (int) $subscription->id
. '-' . $scheduledFor;
$intent = new CloudInstanceBackup();
foreach ([
'backup_key' => str_repeat('a', 32),
'storage_artifact_id' => 101,
'create_request_actor_id' => (int) $instance->user_id,
'create_request_idempotency_key' => $requestKey,
'create_request_fingerprint' => $fingerprint,
'source_instance_id' => (int) $instance->id,
'source_host_id' => (int) $instance->host_id,
'user_id' => (int) $instance->user_id,
'source_uuid' => (string) $instance->uuid,
'name' => $name,
'purpose' => 'portable',
'source' => 'automatic',
'subscription_id' => (int) $subscription->id,
'scheduled_for' => $scheduledFor,
'state' => 'creating',
'consistency' => 'filesystem',
'active_job_id' => null,
'last_operation_id' => null,
'source_portable_restore_contract_sha256' => str_repeat('b', 64),
'error_code' => '',
'created_at' => $now - 120,
'updated_at' => $now - 120,
] as $field => $value) {
$intent->{$field} = $value;
}
$intent->save();
// Publication preparation cannot proceed in this deliberately incomplete
// storage fixture. The already committed logical slot must nevertheless stay
// unchanged so the next pass reuses the same intent and idempotency key.
$queued = CloudAutomaticBackupService::scheduleDue(10, $now);
$subscription->refresh();
$assert($queued === 0
&& (int) $subscription->next_run_at === $scheduledFor
&& $subscription->last_scheduled_at === null
&& $subscription->last_backup_id === null
&& CloudInstanceBackup::where(
'subscription_id', (int) $subscription->id
)->count() === 1,
'A temporary preparation failure changed the slot or duplicated its durable intent.');
// Two stable, conflicting multipart observations move the same pre-Agent
// intent to operator attention (`uncertain`). That is still the original
// scheduled run, not permission to advance the subscription and create a new
// logical backup five minutes later.
$intent->refresh();
$intent->state = 'uncertain';
$intent->error_code = 'multipart_identity_conflict_attention';
$intent->updated_at = $now;
$intent->save();
$queued = CloudAutomaticBackupService::scheduleDue(10, $now + 1);
$subscription->refresh();
$assert($queued === 0
&& (int) $subscription->next_run_at === $scheduledFor
&& CloudInstanceBackup::where(
'subscription_id', (int) $subscription->id
)->count() === 1,
'Pre-Agent storage attention advanced the slot and allowed a duplicate intent.');
echo "Automatic backup durable-intent scheduling tests passed.\n";