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.
136 lines
4.9 KiB
PHP
136 lines
4.9 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Models\CloudBackupSubscription;
|
|
use App\Models\CloudInstance;
|
|
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');
|
|
});
|
|
$schema->create('cloud_backup_period_entitlements',
|
|
static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->integer('subscription_id');
|
|
$table->integer('instance_id');
|
|
$table->integer('user_id');
|
|
$table->integer('starts_at');
|
|
$table->integer('ends_at');
|
|
});
|
|
|
|
$now = time();
|
|
$newDue = static function (int $id, string $serviceStatus,
|
|
int $expiresAt) use ($now): CloudBackupSubscription {
|
|
$instance = new CloudInstance();
|
|
$instance->id = $id;
|
|
$instance->user_id = 9;
|
|
$instance->offer_id = 3;
|
|
$instance->status = 'active';
|
|
$instance->service_status = $serviceStatus;
|
|
$instance->expires_at = $expiresAt;
|
|
$instance->save();
|
|
|
|
$subscription = new CloudBackupSubscription();
|
|
$subscription->instance_id = $id;
|
|
$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->next_run_at = $now - 60;
|
|
$subscription->created_at = $subscription->updated_at = $now - 3600;
|
|
$subscription->save();
|
|
return $subscription;
|
|
};
|
|
|
|
$deferred = $newDue(10, 'expired_grace', $now + 3600);
|
|
$deferred->deferred_until = $now + 300;
|
|
$deferred->defer_reason = 'migration_active';
|
|
$deferred->deferred_at = $now;
|
|
$deferred->defer_count = 1;
|
|
$deferred->save();
|
|
$grace = $newDue(11, 'expired_grace', $now + 3600);
|
|
$expiredBeforeLifecycleTick = $newDue(12, 'active', $now - 1);
|
|
$missingEntitlement = $newDue(13, 'active', $now + 3600);
|
|
$queued = CloudAutomaticBackupService::scheduleDue(1, $now);
|
|
$deferred->refresh();
|
|
$grace->refresh();
|
|
$assert($queued === 0 && (bool) $deferred->enabled
|
|
&& (int) $deferred->next_run_at === $now - 60
|
|
&& (int) $deferred->deferred_until === $now + 300
|
|
&& !(bool) $grace->enabled,
|
|
'A future deferred row starved the next runnable subscription.');
|
|
|
|
$queued = CloudAutomaticBackupService::scheduleDue(10, $now);
|
|
$grace->refresh();
|
|
$expiredBeforeLifecycleTick->refresh();
|
|
$missingEntitlement->refresh();
|
|
|
|
$assert($queued === 0
|
|
&& !(bool) $grace->enabled
|
|
&& (bool) $grace->renew_next_period
|
|
&& (int) $grace->next_run_at === 0
|
|
&& (int) $grace->state_revision === 2
|
|
&& !(bool) $expiredBeforeLifecycleTick->enabled
|
|
&& (bool) $expiredBeforeLifecycleTick->renew_next_period
|
|
&& (int) $expiredBeforeLifecycleTick->next_run_at === 0
|
|
&& (int) $expiredBeforeLifecycleTick->state_revision === 2
|
|
&& !(bool) $missingEntitlement->enabled
|
|
&& (int) $missingEntitlement->next_run_at === 0
|
|
&& (int) $missingEntitlement->deferred_until === 0,
|
|
'Expired or unentitled instances were not parked and would retry forever.');
|
|
|
|
echo "Automatic backup expiry pause lifecycle tests passed.\n";
|