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

193 lines
6.1 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\CloudBackupSubscription;
use App\Models\CloudInstance;
use App\Models\CloudJob;
use App\Services\Cloud\CloudAutomaticBackupService;
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_instances', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('user_id');
$table->integer('offer_id');
$table->string('status');
$table->string('service_status');
$table->integer('expires_at')->nullable();
});
$schema->create('cloud_backup_subscriptions',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('instance_id')->unique();
$table->integer('user_id');
$table->integer('offer_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('state_revision')->default(1);
$table->integer('next_run_at');
$table->integer('deferred_until')->default(0);
$table->string('defer_reason')->default('');
$table->integer('deferred_at')->nullable();
$table->integer('defer_count')->default(0);
$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 = 17;
$instance->user_id = 9;
$instance->offer_id = 3;
$instance->status = 'active';
$instance->service_status = 'active';
$instance->expires_at = $now + 86400;
$instance->save();
$subscription = new CloudBackupSubscription();
$subscription->instance_id = 17;
$subscription->user_id = 9;
$subscription->offer_id = 3;
$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->state_revision = 1;
$subscription->next_run_at = $now - 60;
$subscription->created_at = $subscription->updated_at = $now - 3600;
$subscription->save();
$defer = static function (string $reason) use ($subscription, $now): void {
$subscription->refresh();
$subscription->deferred_until = $now + 300;
$subscription->defer_reason = $reason;
$subscription->deferred_at = $now;
$subscription->defer_count = 2;
$subscription->save();
};
$job = static function (string $kind, ?int $instanceId = 17): CloudJob {
$job = new CloudJob();
$job->kind = $kind;
$job->instance_id = $instanceId;
return $job;
};
$defer('backup_rescue_active');
$woke = CloudAutomaticBackupService::wakeAfterSuccessfulMutation(
$job('rescue'),
['request_target' => '/v1/vms/example/rescue/exit'],
$now
);
$subscription->refresh();
$assert($woke
&& (int) $subscription->next_run_at === $now - 60
&& (int) $subscription->deferred_until === 0
&& (string) $subscription->defer_reason === ''
&& $subscription->deferred_at === null
&& (int) $subscription->defer_count === 0,
'Confirmed rescue exit did not wake the matching logical slot.');
$defer('backup_rescue_active');
$woke = CloudAutomaticBackupService::wakeAfterSuccessfulMutation(
$job('rescue'),
['request_target' => '/v1/vms/example/rescue/enter'],
$now
);
$subscription->refresh();
$assert(!$woke && (int) $subscription->deferred_until === $now + 300,
'Entering rescue incorrectly woke a deferred backup.');
foreach (['reinstall', 'snapshot_restore', 'backup_recover'] as $kind) {
$defer('instance_mutation_active');
$woke = CloudAutomaticBackupService::wakeAfterSuccessfulMutation(
$job($kind),
[],
$now
);
$subscription->refresh();
$assert($woke && (int) $subscription->deferred_until === 0,
$kind . ' did not wake its matching deferred backup.');
}
$defer('migration_active');
$woke = CloudAutomaticBackupService::wakeAfterSuccessfulMutation(
$job('reinstall'),
[],
$now
);
$subscription->refresh();
$assert(!$woke && (string) $subscription->defer_reason === 'migration_active',
'A successful disk mutation cleared an unrelated defer reason.');
$woke = CloudAutomaticBackupService::wakeAfterSuccessfulMutation(
$job('reinstall', null),
[],
$now
);
$assert(!$woke, 'A job without an instance unexpectedly woke a subscription.');
$instance->expires_at = $now;
$instance->save();
$defer('backup_disk_transition_active');
$woke = CloudAutomaticBackupService::wakeAfterSuccessfulMutation(
$job('reinstall'),
[],
$now
);
$subscription->refresh();
$assert(!$woke
&& (string) $subscription->defer_reason
=== 'backup_disk_transition_active',
'An expired instance was incorrectly woken for automatic backup.');
$worker = file_get_contents(
dirname(__DIR__) . '/app/Services/Cloud/CloudWorkerService.php'
);
$wakeCall = strpos(
$worker,
'CloudAutomaticBackupService::wakeAfterSuccessfulMutation('
);
$postCommitTrigger = strpos(
$worker,
'$wakeAutomaticBackup = $status === \'succeeded\';'
);
$storageTrigger = strpos(
$worker,
"if ((string) \$claimed->kind === 'host_storage_configure'"
);
$assert($wakeCall !== false
&& $postCommitTrigger !== false
&& $storageTrigger !== false
&& $postCommitTrigger < $wakeCall
&& $wakeCall < $storageTrigger,
'Worker does not gate deferred wake on a committed success.');
echo "Automatic backup deferred wake tests passed.\n";