migrating tests (Backups)

This commit is contained in:
Saeed Vaziry 2024-10-08 23:00:16 +02:00
parent 0da21f40bd
commit eaecafc9b0
5 changed files with 43 additions and 25 deletions

View File

@ -14,6 +14,8 @@ class Index extends Page
protected static ?string $title = 'Headless Console'; protected static ?string $title = 'Headless Console';
protected static ?string $navigationLabel = 'Console';
public function mount(): void public function mount(): void
{ {
$this->authorize('manage', $this->server); $this->authorize('manage', $this->server);

View File

@ -31,7 +31,7 @@ public function mount(): void
protected function getHeaderActions(): array protected function getHeaderActions(): array
{ {
return [ return [
Action::make('Create backup') Action::make('create')
->icon('heroicon-o-plus') ->icon('heroicon-o-plus')
->modalWidth(MaxWidth::Large) ->modalWidth(MaxWidth::Large)
->authorize(fn () => auth()->user()?->can('create', [Backup::class, $this->server])) ->authorize(fn () => auth()->user()?->can('create', [Backup::class, $this->server]))

View File

@ -31,7 +31,7 @@ class MetricDetails extends Widget implements HasForms, HasInfolists
public function infolist(Infolist $infolist): Infolist public function infolist(Infolist $infolist): Infolist
{ {
return $infolist return $infolist
->record($this->server->metrics()->latest()->first()) ->record($this->server->metrics()->latest()->first() ?? new Metric)
->schema([ ->schema([
Grid::make() Grid::make()
->schema([ ->schema([

View File

@ -37,10 +37,10 @@ protected function getStats(): array
Stat::make('CPU Load', $lastMetric?->load ?? 0) Stat::make('CPU Load', $lastMetric?->load ?? 0)
->color('success') ->color('success')
->chart($metrics->pluck('load')->toArray()), ->chart($metrics->pluck('load')->toArray()),
Stat::make('Memory Usage', Number::fileSize($lastMetric->memory_used_in_bytes, 2)) Stat::make('Memory Usage', Number::fileSize($lastMetric?->memory_used_in_bytes || 0, 2))
->color('warning') ->color('warning')
->chart($metrics->pluck('memory_used')->toArray()), ->chart($metrics->pluck('memory_used')->toArray()),
Stat::make('Disk Usage', Number::fileSize($lastMetric->disk_used_in_bytes, 2)) Stat::make('Disk Usage', Number::fileSize($lastMetric?->disk_used_in_bytes || 0, 2))
->color('primary') ->color('primary')
->chart($metrics->pluck('disk_used')->toArray()), ->chart($metrics->pluck('disk_used')->toArray()),
]; ];

View File

@ -9,9 +9,13 @@
use App\Models\Backup; use App\Models\Backup;
use App\Models\Database; use App\Models\Database;
use App\Models\StorageProvider; use App\Models\StorageProvider;
use App\Web\Pages\Servers\Databases\Backups;
use App\Web\Pages\Servers\Databases\Widgets\BackupFilesList;
use App\Web\Pages\Servers\Databases\Widgets\BackupsList;
use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Bus; use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
use Livewire\Livewire;
use Tests\TestCase; use Tests\TestCase;
class DatabaseBackupTest extends TestCase class DatabaseBackupTest extends TestCase
@ -34,12 +38,14 @@ public function test_create_backup(): void
'provider' => \App\Enums\StorageProvider::DROPBOX, 'provider' => \App\Enums\StorageProvider::DROPBOX,
]); ]);
$this->post(route('servers.databases.backups.store', $this->server), [ Livewire::test(Backups::class, ['server' => $this->server])
'backup_database' => $database->id, ->callAction('create', [
'backup_storage' => $storage->id, 'database' => $database->id,
'backup_interval' => '0 * * * *', 'storage' => $storage->id,
'backup_keep' => '10', 'interval' => '0 * * * *',
])->assertSessionDoesntHaveErrors(); 'keep' => '10',
])
->assertSuccessful();
$this->assertDatabaseHas('backups', [ $this->assertDatabaseHas('backups', [
'status' => BackupStatus::RUNNING, 'status' => BackupStatus::RUNNING,
@ -65,13 +71,15 @@ public function test_create_custom_interval_backup(): void
'provider' => \App\Enums\StorageProvider::DROPBOX, 'provider' => \App\Enums\StorageProvider::DROPBOX,
]); ]);
$this->post(route('servers.databases.backups.store', $this->server), [ Livewire::test(Backups::class, ['server' => $this->server])
'backup_database' => $database->id, ->callAction('create', [
'backup_storage' => $storage->id, 'database' => $database->id,
'backup_interval' => 'custom', 'storage' => $storage->id,
'backup_custom' => '* * * * *', 'interval' => 'custom',
'backup_keep' => '10', 'custom_interval' => '* * * * *',
])->assertSessionDoesntHaveErrors(); 'keep' => '10',
])
->assertSuccessful();
$this->assertDatabaseHas('backups', [ $this->assertDatabaseHas('backups', [
'status' => BackupStatus::RUNNING, 'status' => BackupStatus::RUNNING,
@ -97,7 +105,12 @@ public function test_see_backups_list(): void
'storage_id' => $storage->id, 'storage_id' => $storage->id,
]); ]);
$this->get(route('servers.databases.backups', [$this->server, $backup])) $this->get(
Backups::getUrl([
'server' => $this->server,
'backup' => $backup,
])
)
->assertSuccessful() ->assertSuccessful()
->assertSee($backup->database->name); ->assertSee($backup->database->name);
} }
@ -121,8 +134,11 @@ public function test_delete_backup(): void
'storage_id' => $storage->id, 'storage_id' => $storage->id,
]); ]);
$this->delete(route('servers.databases.backups.destroy', [$this->server, $backup])) Livewire::test(BackupsList::class, [
->assertSessionDoesntHaveErrors(); 'server' => $this->server,
])
->callTableAction('delete', $backup->id)
->assertSuccessful();
$this->assertDatabaseMissing('backups', [ $this->assertDatabaseMissing('backups', [
'id' => $backup->id, 'id' => $backup->id,
@ -153,14 +169,14 @@ public function test_restore_backup(): void
$backupFile = app(RunBackup::class)->run($backup); $backupFile = app(RunBackup::class)->run($backup);
$this->post(route('servers.databases.backups.files.restore', [ Livewire::test(BackupFilesList::class, [
'server' => $this->server, 'server' => $this->server,
'backup' => $backup, 'backup' => $backup,
'backupFile' => $backupFile,
]), [
'database' => $database->id,
]) ])
->assertSessionDoesntHaveErrors(); ->callTableAction('restore', $backupFile->id, [
'database' => $database->id,
])
->assertSuccessful();
$this->assertDatabaseHas('backup_files', [ $this->assertDatabaseHas('backup_files', [
'id' => $backupFile->id, 'id' => $backupFile->id,