mirror of
https://github.com/coollabsio/coolify.git
synced 2026-09-28 02:06:37 -04:00
feat(backups): add scheduled persistent volume backups
This commit is contained in:
@@ -72,6 +72,35 @@ beforeEach(function () {
|
||||
session(['currentTeam' => $this->team]);
|
||||
});
|
||||
|
||||
it('renders a highlighted enable backup button and a regular disable backup button', function () {
|
||||
$view = file_get_contents(resource_path('views/livewire/project/database/backup-edit.blade.php'));
|
||||
|
||||
expect($view)
|
||||
->toContain('wire:target="toggleEnabled" isHighlighted>Enable Backup</x-forms.button>')
|
||||
->toContain('wire:target="toggleEnabled">Disable Backup</x-forms.button>')
|
||||
->not->toContain('label="Backup Enabled"');
|
||||
});
|
||||
|
||||
it('enables and disables a scheduled database backup from the title action', function () {
|
||||
$backup = createBackupForEditValidationTest($this->team, [
|
||||
'enabled' => false,
|
||||
'save_s3' => false,
|
||||
]);
|
||||
|
||||
$component = Livewire::test(BackupEdit::class, ['backup' => $backup->fresh(), 'availableS3Storages' => $this->team->s3s])
|
||||
->assertSet('backupEnabled', false)
|
||||
->assertSee('Enable Backup')
|
||||
->call('toggleEnabled')
|
||||
->assertSet('backupEnabled', true)
|
||||
->assertSee('Disable Backup');
|
||||
|
||||
expect($backup->refresh()->enabled)->toBeTruthy();
|
||||
|
||||
$component->call('toggleEnabled')->assertSet('backupEnabled', false);
|
||||
|
||||
expect($backup->refresh()->enabled)->toBeFalsy();
|
||||
});
|
||||
|
||||
it('disables S3 backup when saved without a selected S3 storage', function () {
|
||||
$backup = createBackupForEditValidationTest($this->team);
|
||||
|
||||
@@ -177,6 +206,30 @@ it('shows disabled S3 storage dropdown when no storages are available', function
|
||||
->assertSee('No S3 storage available');
|
||||
});
|
||||
|
||||
it('allows S3 backups to be disabled when no usable storage remains', function () {
|
||||
$backup = createBackupForEditValidationTest($this->team, [
|
||||
'save_s3' => true,
|
||||
's3_storage_id' => null,
|
||||
]);
|
||||
|
||||
$component = Livewire::test(BackupEdit::class, [
|
||||
'backup' => $backup->fresh(),
|
||||
'availableS3Storages' => collect(),
|
||||
])->assertSet('saveS3', true);
|
||||
|
||||
preg_match('/<input\b(?=[^>]*wire:model=(?:"saveS3"|saveS3))[^>]*>/', $component->html(), $matches);
|
||||
expect($matches[0] ?? null)->not->toBeNull()
|
||||
->and(preg_match('/\sdisabled(?:\s|\/>)/', $matches[0]))->toBe(0);
|
||||
|
||||
$component->set('saveS3', false)->call('instantSave')->assertDispatched('success');
|
||||
|
||||
expect($backup->refresh()->save_s3)->toBeFalsy()
|
||||
->and($backup->s3_storage_id)->toBeNull();
|
||||
|
||||
preg_match('/<input\b(?=[^>]*wire:model=(?:"saveS3"|saveS3))[^>]*>/', $component->html(), $matches);
|
||||
expect(preg_match('/\sdisabled(?:\s|\/>)/', $matches[0]))->toBe(1);
|
||||
});
|
||||
|
||||
it('shows when S3 backups are currently disabled', function () {
|
||||
createS3StorageForBackupEditValidationTest($this->team);
|
||||
$backup = createBackupForEditValidationTest($this->team, [
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Application;
|
||||
use App\Models\Environment;
|
||||
use App\Models\InstanceSettings;
|
||||
use App\Models\LocalPersistentVolume;
|
||||
use App\Models\Project;
|
||||
use App\Models\ScheduledVolumeBackup;
|
||||
use App\Models\Server;
|
||||
use App\Models\StandaloneDocker;
|
||||
use App\Models\StandalonePostgresql;
|
||||
use App\Models\Team;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
InstanceSettings::forceCreate(['id' => 0]);
|
||||
|
||||
$this->team = Team::factory()->create();
|
||||
$this->user = User::factory()->create();
|
||||
$this->user->teams()->attach($this->team, ['role' => 'owner']);
|
||||
$this->actingAs($this->user);
|
||||
session(['currentTeam' => $this->team]);
|
||||
});
|
||||
|
||||
it('renders frontend-only application backup search data for volume names and frequencies', function () {
|
||||
$application = createBackupSearchApplication($this->team);
|
||||
$dailyVolume = createBackupSearchVolume($application, 'app-data');
|
||||
$weeklyVolume = createBackupSearchVolume($application, 'Cache-Data');
|
||||
|
||||
ScheduledVolumeBackup::create([
|
||||
'local_persistent_volume_id' => $dailyVolume->id,
|
||||
'team_id' => $this->team->id,
|
||||
'frequency' => 'daily',
|
||||
]);
|
||||
ScheduledVolumeBackup::create([
|
||||
'local_persistent_volume_id' => $weeklyVolume->id,
|
||||
'team_id' => $this->team->id,
|
||||
'frequency' => '0 4 * * 0',
|
||||
]);
|
||||
|
||||
$parameters = [
|
||||
'project_uuid' => $application->project()->uuid,
|
||||
'environment_uuid' => $application->environment->uuid,
|
||||
'application_uuid' => $application->uuid,
|
||||
];
|
||||
|
||||
$this->get(route('project.application.backup.index', [...$parameters, 'search' => 'cache']))
|
||||
->assertOk()
|
||||
->assertSee('Cache-Data')
|
||||
->assertSee('Volume: app-data')
|
||||
->assertSee('x-model="search"', false)
|
||||
->assertSee('x-show=', false)
|
||||
->assertSee('No scheduled backups match your search.');
|
||||
});
|
||||
|
||||
it('renders frontend-only database backup search data for database names and frequencies', function () {
|
||||
$server = Server::factory()->create(['team_id' => $this->team->id]);
|
||||
$destination = StandaloneDocker::where('server_id', $server->id)->firstOrFail();
|
||||
$project = Project::factory()->create(['team_id' => $this->team->id]);
|
||||
$environment = Environment::factory()->create(['project_id' => $project->id]);
|
||||
$database = StandalonePostgresql::create([
|
||||
'name' => 'Orders-Primary',
|
||||
'image' => 'postgres:16-alpine',
|
||||
'postgres_user' => 'postgres',
|
||||
'postgres_password' => 'password',
|
||||
'postgres_db' => 'postgres',
|
||||
'environment_id' => $environment->id,
|
||||
'destination_id' => $destination->id,
|
||||
'destination_type' => $destination->getMorphClass(),
|
||||
]);
|
||||
$database->scheduledBackups()->create([
|
||||
'team_id' => $this->team->id,
|
||||
'frequency' => 'daily',
|
||||
]);
|
||||
$database->scheduledBackups()->create([
|
||||
'team_id' => $this->team->id,
|
||||
'frequency' => '0 3 * * 1',
|
||||
]);
|
||||
|
||||
$parameters = [
|
||||
'project_uuid' => $project->uuid,
|
||||
'environment_uuid' => $environment->uuid,
|
||||
'database_uuid' => $database->uuid,
|
||||
];
|
||||
|
||||
$this->get(route('project.database.backup.index', [...$parameters, 'search' => '0 3']))
|
||||
->assertOk()
|
||||
->assertSee('<h3 class="font-semibold">0 3 * * 1</h3>', false)
|
||||
->assertSee('<h3 class="font-semibold">daily</h3>', false)
|
||||
->assertSee('x-model="search"', false)
|
||||
->assertSee('x-show=', false)
|
||||
->assertSee('No scheduled backups match your search.');
|
||||
});
|
||||
|
||||
function createBackupSearchApplication(Team $team): Application
|
||||
{
|
||||
$server = Server::factory()->create(['team_id' => $team->id]);
|
||||
$destination = StandaloneDocker::where('server_id', $server->id)->firstOrFail();
|
||||
$project = Project::factory()->create(['team_id' => $team->id]);
|
||||
$environment = Environment::factory()->create(['project_id' => $project->id]);
|
||||
$application = Application::factory()->create([
|
||||
'environment_id' => $environment->id,
|
||||
'destination_id' => $destination->id,
|
||||
'destination_type' => $destination->getMorphClass(),
|
||||
]);
|
||||
|
||||
return $application;
|
||||
}
|
||||
|
||||
function createBackupSearchVolume(Application $application, string $name): LocalPersistentVolume
|
||||
{
|
||||
return LocalPersistentVolume::create([
|
||||
'name' => $name,
|
||||
'mount_path' => '/'.str($name)->lower(),
|
||||
'resource_id' => $application->id,
|
||||
'resource_type' => $application->getMorphClass(),
|
||||
]);
|
||||
}
|
||||
@@ -131,6 +131,19 @@ function mobileActionsAreBeforeSelect(string $heading, string $actionsId, string
|
||||
return $actionsPosition !== false && $selectPosition !== false && $actionsPosition < $selectPosition;
|
||||
}
|
||||
|
||||
it('places database backups immediately after configuration in navigation menus', function () {
|
||||
$databaseHeading = file_get_contents(resource_path('views/livewire/project/database/heading.blade.php'));
|
||||
$mobileMenuItems = str($databaseHeading)->between('$databasePageItems = [', '$databaseConfigurationItems = [')->toString();
|
||||
$desktopMenuItems = str($databaseHeading)->between('class="scrollbar hidden', '</nav>')->toString();
|
||||
|
||||
foreach ([$mobileMenuItems, $desktopMenuItems] as $menuItems) {
|
||||
expect(strpos($menuItems, 'Configuration'))
|
||||
->toBeLessThan(strpos($menuItems, 'Backups'))
|
||||
->and(strpos($menuItems, 'Backups'))->toBeLessThan(strpos($menuItems, 'Logs'))
|
||||
->and(strpos($menuItems, 'Logs'))->toBeLessThan(strpos($menuItems, 'Terminal'));
|
||||
}
|
||||
});
|
||||
|
||||
it('keeps configuration sidebars hidden until desktop breakpoint', function () {
|
||||
expect(file_get_contents(resource_path('views/livewire/project/database/configuration.blade.php')))
|
||||
->toContain('sub-menu-wrapper hidden md:flex');
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user