addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default'); $capsule->setAsGlobal(); $capsule->bootEloquent(); $schema = $capsule->schema('default'); $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('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('service_period_id')->unique(); $table->integer('instance_id'); $table->integer('user_id'); $table->integer('offer_id'); $table->integer('invoice_id'); $table->integer('starts_at'); $table->integer('ends_at'); $table->string('amount'); $table->string('full_period_price'); $table->string('currency'); $table->integer('price_version'); $table->string('source'); $table->integer('paid_at'); $table->integer('created_at'); $table->integer('updated_at'); }); $schema->create('cloud_instance_backups', static function (Blueprint $table): void { $table->increments('id'); $table->string('backup_key')->unique(); $table->integer('storage_artifact_id')->nullable(); $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->string('object_key')->unique(); $table->integer('object_size_bytes'); $table->integer('virtual_size_bytes'); $table->string('sha256'); $table->string('object_etag')->default(''); $table->string('object_version_id')->default(''); $table->text('spec_json'); $table->text('sealed_credential')->nullable(); $table->integer('storage_revision'); $table->integer('active_job_id')->nullable(); $table->string('last_operation_id')->nullable(); $table->string('delete_operation_id')->nullable(); $table->integer('delete_generation')->default(0); $table->integer('delete_cycle_attempts')->default(0); $table->integer('delete_presence_confirmations')->default(0); $table->integer('delete_requested_at')->nullable(); $table->integer('delete_last_checked_at')->nullable(); $table->integer('delete_next_check_at')->default(0); $table->integer('delete_attention_at')->nullable(); $table->string('error_code'); $table->integer('scheduled_for')->nullable(); $table->integer('ready_at')->nullable(); $table->integer('created_at'); $table->integer('updated_at'); }); $schema->create('cloud_storage_artifacts', static function (Blueprint $t): void { $t->increments('id'); $t->string('artifact_key')->unique(); $t->string('kind'); $t->integer('maximum_size_bytes'); $t->integer('size_bytes'); $t->string('sha256'); $t->string('durability_policy'); $t->integer('desired_copies'); $t->integer('required_copies'); $t->string('state'); $t->string('last_operation_id')->nullable(); $t->integer('verified_at'); $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_storage_backends', static function (Blueprint $t): void { $t->increments('id'); $t->string('backend_key'); $t->string('identity_sha256'); $t->string('name'); $t->string('driver'); $t->string('endpoint'); $t->string('region'); $t->string('bucket'); $t->string('prefix'); $t->boolean('path_style'); $t->string('failure_domain'); $t->text('sealed_credentials'); $t->integer('config_revision'); $t->integer('tested_revision'); $t->string('admin_state'); $t->string('health_state'); $t->integer('write_priority'); $t->integer('consecutive_failures')->default(0); $t->integer('consecutive_successes')->default(0); $t->string('last_error_code')->default(''); $t->integer('last_tested_at')->default(0); $t->integer('last_healthy_at')->default(0); $t->integer('next_probe_at')->default(0); $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_storage_replicas', static function (Blueprint $t): void { $t->increments('id'); $t->string('replica_key')->unique(); $t->integer('artifact_id'); $t->integer('backend_id'); $t->string('role'); $t->string('object_key'); $t->string('object_version_id')->default(''); $t->string('object_etag')->default(''); $t->integer('size_bytes'); $t->string('sha256'); $t->string('state'); $t->string('publish_operation_id')->nullable(); $t->string('delete_operation_id')->nullable(); $t->integer('delete_generation')->default(0); $t->integer('delete_cycle_attempts')->default(0); $t->integer('delete_presence_confirmations')->default(0); $t->integer('delete_last_checked_at')->default(0); $t->integer('delete_next_check_at')->default(0); $t->integer('delete_attention_at')->nullable(); $t->string('lease_token')->nullable(); $t->integer('lease_until')->default(0); $t->string('error_code')->default(''); $t->integer('verified_at')->default(0); $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_storage_operations', static function (Blueprint $t): void { $t->increments('id'); $t->string('operation_id')->unique(); $t->string('request_idempotency_key')->unique(); $t->string('request_fingerprint'); $t->integer('artifact_id'); $t->integer('replica_id')->nullable(); $t->integer('source_backend_id')->nullable(); $t->integer('target_backend_id')->nullable(); $t->string('kind'); $t->string('phase'); $t->string('outcome')->nullable(); $t->string('effect_resolution'); $t->integer('effect_may_have_applied')->nullable(); $t->integer('safe_to_resubmit')->nullable(); $t->integer('attempts'); $t->string('lease_token')->nullable(); $t->integer('lease_until'); $t->integer('available_at'); $t->integer('next_check_at'); $t->string('error_code'); $t->text('result_json')->nullable(); $t->integer('terminal_at')->nullable(); $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_storage_publications', static function (Blueprint $t): void { $t->increments('id'); $t->string('publication_key')->unique(); $t->integer('artifact_id')->unique(); $t->integer('backup_id')->nullable(); $t->string('template_import_intent_id')->nullable(); $t->string('rescue_template_id')->nullable(); $t->string('state'); $t->integer('attempts')->default(0); $t->integer('available_at')->default(0); $t->string('lease_token')->nullable(); $t->integer('lease_until')->default(0); $t->string('last_error_code')->default(''); $t->integer('coverage_ready_at')->nullable(); $t->integer('finalized_at')->nullable(); $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_deployment_snapshots', static function ( Blueprint $table ): void { $table->increments('id'); $table->integer('backup_id')->unique(); $table->string('state'); $table->string('error_code')->default(''); $table->integer('deleted_at')->nullable(); $table->integer('updated_at')->default(0); }); foreach (['cloud_backup_restores', 'cloud_backup_recoveries'] as $tableName) { $schema->create($tableName, static function (Blueprint $table): void { $table->increments('id'); $table->integer('backup_id'); $table->string('state'); }); } $schema->create('cloud_jobs', static function (Blueprint $table): void { $table->increments('id'); $table->integer('backup_id')->nullable(); $table->string('phase'); $table->string('effect_resolution'); $table->boolean('finalization_incident')->default(false); }); Config::$dataKey = base64_encode(str_repeat('r', 32)); $now = time(); $capsule->table('cloud_storage_backends')->insert([ 'id' => 1, 'backend_key' => str_repeat('b', 32), 'identity_sha256' => str_repeat('c', 64), 'name' => 'retention-primary', 'driver' => 's3', 'endpoint' => 'https://objects.example.com', 'region' => 'us-east-1', 'bucket' => 'backup-bucket', 'prefix' => 'virt', 'path_style' => 1, 'failure_domain' => 'retention-primary', 'sealed_credentials' => CloudSecrets::encrypt([ 'access_key_id' => 'RETENTIONACCESS', 'secret_access_key' => 'retention-secret-value', ], 'object-storage-backend-credentials'), 'config_revision' => 1, 'tested_revision' => 1, 'admin_state' => 'active', 'health_state' => 'healthy', 'write_priority' => 1, 'consecutive_successes' => 1, 'last_tested_at' => $now, 'last_healthy_at' => $now, 'next_probe_at' => $now + 300, 'created_at' => $now, 'updated_at' => $now, ]); $newSubscription = static function (int $instanceId, array $endsAt) use ($now): CloudBackupSubscription { $subscription = new CloudBackupSubscription(); foreach ([ 'instance_id' => $instanceId, 'user_id' => 9, 'offer_id' => 3, 'enabled' => false, 'renew_next_period' => false, 'frequency' => 'daily', 'weekday' => 0, 'retention_count' => 2, 'timezone' => 'UTC', 'price' => '2.50', 'currency' => 'USD', 'price_version' => 1, 'next_run_at' => 0, 'created_at' => $now - 1000, 'updated_at' => $now - 1000, ] as $field => $value) { $subscription->{$field} = $value; } $subscription->save(); foreach ($endsAt as $offset => $end) { $entitlement = new CloudBackupPeriodEntitlement(); foreach ([ 'subscription_id' => (int) $subscription->id, 'service_period_id' => $instanceId * 10 + $offset, 'instance_id' => $instanceId, 'user_id' => 9, 'offer_id' => 3, 'invoice_id' => $instanceId, 'starts_at' => $end - 30 * 86400, 'ends_at' => $end, 'amount' => '2.50', 'full_period_price' => '2.50', 'currency' => 'USD', 'price_version' => 1, 'source' => 'renewal', 'paid_at' => $now - 1000, 'created_at' => $now - 1000, 'updated_at' => $now - 1000, ] as $field => $value) { $entitlement->{$field} = $value; } $entitlement->save(); } return $subscription; }; $newBackup = static function (CloudBackupSubscription $subscription, string $suffix) use ($capsule, $now): CloudInstanceBackup { $backup = new CloudInstanceBackup(); foreach ([ 'backup_key' => substr(hash('sha256', $suffix), 0, 32), 'source_instance_id' => (int) $subscription->instance_id, 'source_host_id' => 2, 'user_id' => 9, 'subscription_id' => (int) $subscription->id, 'source_uuid' => '11111111-1111-4111-8111-111111111111', 'name' => 'autobackup-' . $suffix, 'purpose' => 'portable', 'source' => 'automatic', 'state' => 'ready', 'consistency' => 'crash', 'object_key' => 'virt/vm-backups/' . $suffix . '/system.qcow2', 'object_size_bytes' => 1024, 'virtual_size_bytes' => 2048, 'sha256' => str_repeat('a', 64), 'object_etag' => '', 'object_version_id' => '', 'spec_json' => json_encode([ 'managed' => true, 'os_source' => 'cloud_template', 'os_name' => 'Debian 13', 'login_name' => 'root', 'debian_12_network_fix' => false, ], JSON_THROW_ON_ERROR), 'sealed_credential' => 'sealed-credential', 'storage_revision' => 0, 'delete_generation' => 0, 'delete_cycle_attempts' => 0, 'delete_presence_confirmations' => 0, 'delete_next_check_at' => 0, 'error_code' => '', 'scheduled_for' => $now - 1000, 'ready_at' => $now - 900, 'created_at' => $now - 1000, 'updated_at' => $now - 1000, ] as $field => $value) { $backup->{$field} = $value; } $backup->save(); $artifactId = 1000 + (int) $backup->id; $capsule->table('cloud_storage_artifacts')->insert([ 'id' => $artifactId, 'artifact_key' => str_pad(dechex($artifactId), 32, '0', STR_PAD_LEFT), 'kind' => 'backup', 'maximum_size_bytes' => 2048, 'size_bytes' => 1024, 'sha256' => str_repeat('a', 64), 'durability_policy' => 'single', 'desired_copies' => 1, 'required_copies' => 1, 'state' => 'available', 'last_operation_id' => null, 'verified_at' => $now, 'created_at' => $now, 'updated_at' => $now, ]); $capsule->table('cloud_storage_publications')->insert([ 'publication_key' => str_pad( dechex(2000 + (int) $backup->id), 32, '0', STR_PAD_LEFT ), 'artifact_id' => $artifactId, 'backup_id' => (int) $backup->id, 'state' => 'completed', 'coverage_ready_at' => $now, 'finalized_at' => $now, 'created_at' => $now, 'updated_at' => $now, ]); $capsule->table('cloud_storage_replicas')->insert([ 'replica_key' => str_pad( dechex(3000 + (int) $backup->id), 32, '0', STR_PAD_LEFT ), 'artifact_id' => $artifactId, 'backend_id' => 1, 'role' => 'primary', 'object_key' => 'virt/vm-backups/' . $suffix . '/system.qcow2', 'object_version_id' => 'version-' . $backup->id, 'object_etag' => '"etag-' . $backup->id . '"', 'size_bytes' => 1024, 'sha256' => str_repeat('a', 64), 'state' => 'ready', 'verified_at' => $now, 'created_at' => $now, 'updated_at' => $now, ]); $backup->storage_artifact_id = $artifactId; $backup->save(); return $backup; }; $paid = $newBackup($newSubscription(11, [$now + 3600]), 'paid'); $insideGrace = $newBackup($newSubscription(12, [$now - 3 * 86400 + 1]), 'inside-grace'); $expired = $newBackup($newSubscription(13, [$now - 3 * 86400]), 'expired'); $renewed = $newBackup($newSubscription(14, [$now - 4 * 86400, $now + 20 * 86400]), 'renewed'); $ordinaryExpired = $newBackup( $newSubscription(15, [$now - 3 * 86400]), 'ordinary-expired' ); $debt = $newBackup( $newSubscription(16, [$now - 3 * 86400]), 'cleanup-debt' ); $debt->state = 'delete_uncertain'; $debt->object_etag = '"debt-etag"'; $debt->object_version_id = 'debt-version'; $debt->delete_generation = 1; $debt->delete_cycle_attempts = 1; $debt->delete_next_check_at = 0; $debt->save(); $capsule->table('cloud_storage_artifacts') ->where('id', (int) $debt->storage_artifact_id) ->update(['state' => 'deleted']); $capsule->table('cloud_storage_replicas') ->where('artifact_id', (int) $debt->storage_artifact_id) ->update(['state' => 'deleted']); $capsule->table('cloud_storage_publications') ->where('artifact_id', (int) $debt->storage_artifact_id) ->update(['state' => 'retired']); $capsule->table('cloud_deployment_snapshots')->insert([ 'backup_id' => (int) $expired->id, 'state' => 'ready', ]); $assert(CloudAutomaticBackupService::withinRecoveryGrace($paid, $now) && CloudAutomaticBackupService::withinRecoveryGrace($insideGrace, $now) && !CloudAutomaticBackupService::withinRecoveryGrace($expired, $now) && CloudAutomaticBackupService::withinRecoveryGrace($renewed, $now), 'Recovery eligibility did not follow the paid period plus three-day grace.'); $history = []; $stack = HandlerStack::create(new MockHandler([ new Response(200, [ 'Content-Length' => '1024', 'ETag' => '"etag-5"', 'x-amz-version-id' => 'version-5', ]), new Response(204), ])); $stack->push(Middleware::history($history)); $client = new CloudObjectStorageS3Client(new Client(['handler' => $stack])); $removed = CloudAutomaticBackupService::pruneRetention(1, $now, $client); foreach ([$paid, $insideGrace, $expired, $renewed, $ordinaryExpired, $debt] as $backup) { $backup->refresh(); } $assert($removed === 2 && (string) $paid->state === 'ready' && (string) $insideGrace->state === 'ready' && (string) $expired->state === 'ready' && (string) $renewed->state === 'ready' && (string) $ordinaryExpired->state === 'deleted' && (string) $debt->state === 'deleted' && count($history) === 2 && array_map(static fn ($item): string => $item['request']->getMethod(), $history) === ['HEAD', 'DELETE'], 'Cleanup debt starved fresh retention work or entitlement grace was not preserved.'); echo "Automatic backup entitlement grace retention tests passed.\n"; }