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.
238 lines
8.5 KiB
PHP
238 lines
8.5 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Models\CloudBackupPeriodEntitlement;
|
|
use App\Models\CloudBackupSubscription;
|
|
use App\Models\CloudInstance;
|
|
use App\Models\CloudInstanceBackup;
|
|
use App\Services\Cloud\CloudAutomaticBackupService;
|
|
use App\Services\Cloud\CloudError;
|
|
use App\Services\Cloud\CloudOriginalBackupRestorePolicy;
|
|
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->string('uuid');
|
|
$table->integer('user_id');
|
|
$table->string('name');
|
|
$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');
|
|
});
|
|
$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('ends_at');
|
|
});
|
|
$schema->create('cloud_instance_backups',
|
|
static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->integer('source_instance_id');
|
|
$table->integer('source_host_id');
|
|
$table->integer('user_id');
|
|
$table->integer('subscription_id')->nullable();
|
|
$table->string('source_uuid');
|
|
$table->string('name');
|
|
$table->string('purpose');
|
|
$table->string('source');
|
|
$table->string('state');
|
|
$table->string('consistency');
|
|
$table->integer('object_size_bytes');
|
|
$table->integer('virtual_size_bytes');
|
|
$table->string('sha256');
|
|
$table->integer('scheduled_for')->nullable();
|
|
$table->integer('ready_at')->nullable();
|
|
$table->integer('created_at');
|
|
});
|
|
|
|
$now = time();
|
|
$currentUuid = '22222222-2222-4222-8222-222222222222';
|
|
$sourceUuid = '11111111-1111-4111-8111-111111111111';
|
|
$instance = new CloudInstance();
|
|
$instance->id = 41;
|
|
$instance->uuid = $currentUuid;
|
|
$instance->user_id = 9;
|
|
$instance->name = 'migrated-vm';
|
|
$instance->status = 'active';
|
|
$instance->service_status = 'active';
|
|
$instance->expires_at = $now + 86400;
|
|
$instance->save();
|
|
|
|
$subscription = new CloudBackupSubscription();
|
|
$subscription->id = 7;
|
|
$subscription->instance_id = (int) $instance->id;
|
|
$subscription->user_id = (int) $instance->user_id;
|
|
$subscription->save();
|
|
|
|
$entitlement = new CloudBackupPeriodEntitlement();
|
|
$entitlement->subscription_id = (int) $subscription->id;
|
|
$entitlement->instance_id = (int) $instance->id;
|
|
$entitlement->user_id = (int) $instance->user_id;
|
|
$entitlement->ends_at = $now + 86400;
|
|
$entitlement->save();
|
|
|
|
$backup = new CloudInstanceBackup();
|
|
$backup->source_instance_id = (int) $instance->id;
|
|
$backup->source_host_id = 3;
|
|
$backup->user_id = (int) $instance->user_id;
|
|
$backup->subscription_id = (int) $subscription->id;
|
|
$backup->source_uuid = $sourceUuid;
|
|
$backup->name = 'autobackup-before-migration';
|
|
$backup->purpose = 'portable';
|
|
$backup->source = 'automatic';
|
|
$backup->state = 'ready';
|
|
$backup->consistency = 'filesystem';
|
|
$backup->object_size_bytes = 1024;
|
|
$backup->virtual_size_bytes = 2048;
|
|
$backup->sha256 = str_repeat('a', 64);
|
|
$backup->scheduled_for = $now - 300;
|
|
$backup->ready_at = $now - 240;
|
|
$backup->created_at = $now - 300;
|
|
$backup->save();
|
|
|
|
$decision = CloudOriginalBackupRestorePolicy::evaluate(
|
|
$instance, $backup, $now
|
|
);
|
|
$check(($decision['eligible'] ?? false) === true
|
|
&& ($decision['reason'] ?? '') === 'eligible',
|
|
'A backup from the same logical instance was rejected after migration changed its UUID.');
|
|
|
|
try {
|
|
CloudOriginalBackupRestorePolicy::assertEligible($instance, $backup, $now);
|
|
} catch (CloudError $error) {
|
|
$failures[] = 'The authoritative restore guard rejected the migrated instance: '
|
|
. $error->getMessage();
|
|
}
|
|
|
|
$display = new ReflectionMethod(
|
|
CloudAutomaticBackupService::class, 'displayBackup'
|
|
);
|
|
$presented = $display->invoke(null, $backup, $instance);
|
|
$check(($presented['can_restore_original'] ?? false) === true
|
|
&& ($presented['restore_original_block_reason'] ?? '') === '',
|
|
'The user catalog disagreed with the authoritative restore policy.');
|
|
|
|
$instance->expires_at = $now - 1;
|
|
$instance->save();
|
|
$expiredDecision = CloudOriginalBackupRestorePolicy::evaluate(
|
|
$instance, $backup, $now
|
|
);
|
|
$expiredPresented = $display->invoke(null, $backup, $instance);
|
|
$expiredRejected = false;
|
|
try {
|
|
CloudOriginalBackupRestorePolicy::assertEligible(
|
|
$instance, $backup, $now
|
|
);
|
|
} catch (CloudError $error) {
|
|
$expiredRejected = $error->httpStatus === 409;
|
|
}
|
|
$check(($expiredDecision['eligible'] ?? true) === false
|
|
&& ($expiredDecision['reason'] ?? '') === 'instance_service_expired'
|
|
&& ($expiredPresented['can_restore_original'] ?? true) === false
|
|
&& ($expiredPresented['restore_original_block_reason'] ?? '') !== ''
|
|
&& $expiredRejected,
|
|
'An expired instance remained restorable during backup retention grace.');
|
|
|
|
$instance->service_status = 'expired_grace';
|
|
$instance->expires_at = $now + 86400;
|
|
$instance->save();
|
|
$graceDecision = CloudOriginalBackupRestorePolicy::evaluate(
|
|
$instance, $backup, $now
|
|
);
|
|
$gracePresented = $display->invoke(null, $backup, $instance);
|
|
$check(($graceDecision['eligible'] ?? true) === false
|
|
&& ($graceDecision['reason'] ?? '') === 'instance_service_expired'
|
|
&& ($gracePresented['can_restore_original'] ?? true) === false,
|
|
'A grace-period service state remained restorable before renewal.');
|
|
|
|
$instance->service_status = 'active';
|
|
$instance->expires_at = $now + 86400;
|
|
$instance->save();
|
|
$renewedDecision = CloudOriginalBackupRestorePolicy::evaluate(
|
|
$instance, $backup, $now
|
|
);
|
|
$renewedPresented = $display->invoke(null, $backup, $instance);
|
|
$check(($renewedDecision['eligible'] ?? false) === true
|
|
&& ($renewedPresented['can_restore_original'] ?? false) === true,
|
|
'Renewal did not restore automatic-backup recovery eligibility.');
|
|
|
|
$backup->refresh();
|
|
$check((string) $backup->source_uuid === $sourceUuid,
|
|
'Restore eligibility rewrote the immutable source UUID audit field.');
|
|
|
|
$other = new CloudInstance();
|
|
$other->id = 42;
|
|
$other->uuid = $sourceUuid;
|
|
$other->user_id = (int) $instance->user_id;
|
|
$other->name = 'different-logical-vm';
|
|
$other->status = 'active';
|
|
$other->service_status = 'active';
|
|
$other->expires_at = $now + 86400;
|
|
$other->save();
|
|
$rejected = CloudOriginalBackupRestorePolicy::evaluate($other, $backup, $now);
|
|
$check(($rejected['eligible'] ?? true) === false
|
|
&& ($rejected['reason'] ?? '') === 'backup_instance_mismatch'
|
|
&& ($rejected['http_status'] ?? 0) === 404,
|
|
'A backup was allowed to cross the stable logical-instance boundary.');
|
|
|
|
$threw = false;
|
|
try {
|
|
CloudOriginalBackupRestorePolicy::assertEligible($other, $backup, $now);
|
|
} catch (CloudError $error) {
|
|
$threw = $error->httpStatus === 404;
|
|
}
|
|
$check($threw,
|
|
'The authoritative guard did not reject a different logical instance.');
|
|
|
|
$automaticSource = (string) file_get_contents(
|
|
dirname(__DIR__) . '/app/Services/Cloud/CloudAutomaticBackupService.php'
|
|
);
|
|
$backupSource = (string) file_get_contents(
|
|
dirname(__DIR__) . '/app/Services/Cloud/CloudBackupService.php'
|
|
);
|
|
$policySource = (string) file_get_contents(
|
|
dirname(__DIR__) . '/app/Services/Cloud/CloudOriginalBackupRestorePolicy.php'
|
|
);
|
|
$check(str_contains($automaticSource,
|
|
'CloudOriginalBackupRestorePolicy::evaluate(')
|
|
&& str_contains($automaticSource,
|
|
"'service_status', 'expires_at'")
|
|
&& str_contains($policySource,
|
|
'CloudAutomaticBackupService::shouldPauseForInstanceExpiry(')
|
|
&& substr_count($backupSource,
|
|
'CloudOriginalBackupRestorePolicy::assertEligible(') >= 2
|
|
&& !str_contains($backupSource,
|
|
'function assertRecoverableAutomaticBackup('),
|
|
'User presentation and execution did not converge on the central restore policy.');
|
|
|
|
if ($failures !== []) {
|
|
throw new RuntimeException(implode("\n", $failures));
|
|
}
|
|
|
|
echo "Original automatic-backup restore policy tests passed.\n";
|