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.
206 lines
7.1 KiB
PHP
206 lines
7.1 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Models\CloudInstance;
|
|
use App\Services\Cloud\CloudBackupEligibility;
|
|
use App\Services\Cloud\CloudBackupEligibilityDecision;
|
|
use App\Services\Cloud\CloudBackupReadinessProbe;
|
|
use App\Services\Cloud\CloudInstanceMigrationService;
|
|
use App\Services\Cloud\CloudJobService;
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
|
|
final class BackupReadinessProbeFake implements CloudBackupReadinessProbe
|
|
{
|
|
public function __construct(
|
|
private array $payload = [],
|
|
private ?Throwable $failure = null
|
|
) {
|
|
}
|
|
|
|
public function inspect(CloudInstance $instance, array $actor): array
|
|
{
|
|
if ($this->failure !== null) {
|
|
throw $this->failure;
|
|
}
|
|
return $this->payload;
|
|
}
|
|
}
|
|
|
|
$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('host_id');
|
|
$t->string('uuid');
|
|
});
|
|
$schema->create('cloud_instance_migrations',
|
|
static function (Blueprint $t): void {
|
|
$t->increments('id');
|
|
$t->unsignedBigInteger('instance_id');
|
|
$t->unsignedBigInteger('vpc_port_id')->nullable();
|
|
$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->string('operation_id')->unique();
|
|
$t->string('request_idempotency_key')->unique();
|
|
$t->string('request_fingerprint');
|
|
$t->unsignedBigInteger('parent_job_id')->nullable();
|
|
$t->unsignedBigInteger('order_id')->nullable();
|
|
$t->unsignedBigInteger('invoice_id')->nullable();
|
|
$t->unsignedBigInteger('instance_id')->nullable();
|
|
$t->unsignedBigInteger('snapshot_id')->nullable();
|
|
$t->unsignedBigInteger('backup_id')->nullable();
|
|
$t->unsignedBigInteger('backup_restore_id')->nullable();
|
|
$t->unsignedBigInteger('backup_recovery_id')->nullable();
|
|
$t->unsignedBigInteger('user_id');
|
|
$t->unsignedBigInteger('host_id')->nullable();
|
|
$t->string('kind');
|
|
$t->string('phase');
|
|
$t->string('outcome')->nullable();
|
|
$t->string('status');
|
|
$t->string('execution_mode')->nullable();
|
|
$t->text('sealed_payload');
|
|
$t->unsignedInteger('attempts')->default(1);
|
|
$t->unsignedInteger('dispatch_attempts')->nullable();
|
|
$t->unsignedBigInteger('dispatch_deadline_at')->nullable();
|
|
$t->unsignedBigInteger('dispatch_started_at')->nullable();
|
|
$t->unsignedBigInteger('agent_accepted_at')->nullable();
|
|
$t->unsignedBigInteger('receipt_deadline_at')->nullable();
|
|
$t->unsignedBigInteger('dispatched_at')->nullable();
|
|
$t->unsignedBigInteger('terminal_at')->nullable();
|
|
$t->boolean('effect_may_have_applied')->nullable();
|
|
$t->boolean('safe_to_resubmit')->nullable();
|
|
$t->string('effect_resolution');
|
|
$t->string('error_code')->nullable();
|
|
$t->boolean('finalization_incident')->default(false);
|
|
$t->string('finalization_error_code')->nullable();
|
|
$t->string('completion_event_id')->nullable();
|
|
$t->text('result_json')->nullable();
|
|
});
|
|
|
|
$instance = new CloudInstance();
|
|
$instance->id = 41;
|
|
$instance->user_id = 7;
|
|
$instance->host_id = 3;
|
|
$instance->uuid = '11111111-2222-4333-8444-555555555555';
|
|
$instance->save();
|
|
$actor = [
|
|
'user_id' => 7,
|
|
'is_admin' => true,
|
|
'is_super_admin' => true,
|
|
'can_manage' => true,
|
|
'session_id' => 'backup-test',
|
|
];
|
|
|
|
$assert(!CloudInstanceMigrationService::hasOpenMigration(41),
|
|
'Fresh instance unexpectedly has an open migration.');
|
|
$assert(!CloudJobService::blockingQueryForInstance(41)->exists(),
|
|
'Fresh instance unexpectedly has a blocking job.');
|
|
|
|
$assess = static function (array $payload) use ($instance, $actor) {
|
|
return (new CloudBackupEligibility(
|
|
new BackupReadinessProbeFake($payload)
|
|
))->assess($instance, $actor);
|
|
};
|
|
|
|
$ready = $assess([
|
|
'eligible' => true,
|
|
'retryable' => false,
|
|
]);
|
|
$assert($ready->allowed()
|
|
&& $ready->reasonCode === CloudBackupEligibilityDecision::READY,
|
|
'Ready Agent evidence was not admitted: ' . $ready->reasonCode);
|
|
|
|
foreach ([
|
|
CloudBackupEligibilityDecision::BACKUP_RESCUE_ACTIVE,
|
|
CloudBackupEligibilityDecision::BACKUP_DISK_TRANSITION_ACTIVE,
|
|
CloudBackupEligibilityDecision::DOMAIN_JOB_ACTIVE,
|
|
] as $reason) {
|
|
$decision = $assess([
|
|
'eligible' => false,
|
|
'retryable' => true,
|
|
'reason' => $reason,
|
|
]);
|
|
$assert(!$decision->allowed() && $decision->reasonCode === $reason
|
|
&& $decision->message !== '',
|
|
'Transient Agent blocker was not represented by a clear decision.');
|
|
}
|
|
|
|
$invalid = $assess([
|
|
'eligible' => false,
|
|
'retryable' => false,
|
|
'reason' => 'host-path:/secret',
|
|
]);
|
|
$assert($invalid->reasonCode
|
|
=== CloudBackupEligibilityDecision::READINESS_UNAVAILABLE,
|
|
'Malformed Agent readiness was accepted.');
|
|
|
|
$unavailable = (new CloudBackupEligibility(
|
|
new BackupReadinessProbeFake([], new RuntimeException('offline'))
|
|
))->assess($instance, $actor);
|
|
$assert($unavailable->reasonCode
|
|
=== CloudBackupEligibilityDecision::READINESS_UNAVAILABLE,
|
|
'Agent failure was not fail-closed.');
|
|
|
|
$db->table('cloud_instance_migrations')->insert([
|
|
'instance_id' => 41,
|
|
'state' => 'attention_required',
|
|
]);
|
|
$migration = $assess(['eligible' => true, 'retryable' => false]);
|
|
$assert($migration->reasonCode
|
|
=== CloudBackupEligibilityDecision::MIGRATION_ACTIVE,
|
|
'Open maintenance migration did not win admission ordering.');
|
|
$db->table('cloud_instance_migrations')->delete();
|
|
|
|
$db->table('cloud_jobs')->insert([
|
|
'operation_id' => str_repeat('a', 32),
|
|
'request_idempotency_key' => 'eligibility-running-job',
|
|
'request_fingerprint' => str_repeat('b', 64),
|
|
'instance_id' => 41,
|
|
'user_id' => 7,
|
|
'host_id' => 3,
|
|
'kind' => 'reinstall',
|
|
'phase' => 'running',
|
|
'status' => 'running',
|
|
'execution_mode' => 'agent',
|
|
'sealed_payload' => '{}',
|
|
'attempts' => 1,
|
|
'dispatch_attempts' => 1,
|
|
'dispatch_deadline_at' => time() + 300,
|
|
'effect_resolution' => 'not_applicable',
|
|
]);
|
|
$job = $assess(['eligible' => true, 'retryable' => false]);
|
|
$assert($job->reasonCode
|
|
=== CloudBackupEligibilityDecision::INSTANCE_MUTATION_ACTIVE,
|
|
'Blocking instance job did not prevent Agent admission.');
|
|
|
|
$source = (string) file_get_contents(
|
|
dirname(__DIR__) . '/app/Services/Cloud/CloudBackupService.php'
|
|
);
|
|
$assert(str_contains($source, '(new CloudBackupEligibility())->assess(')
|
|
&& strpos($source, '(new CloudBackupEligibility())->assess(')
|
|
< strpos($source, 'beginAgentPublication('),
|
|
'Manual backup does not run readiness before storage publication.');
|
|
|
|
echo "Cloud backup eligibility tests passed.\n";
|