137 lines
4.5 KiB
PHP
137 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Console;
|
|
|
|
use App\Models\Event;
|
|
use App\Models\EventMediaAsset;
|
|
use App\Models\MediaStorageTarget;
|
|
use App\Services\Storage\StorageHealthService;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Mockery;
|
|
use Tests\TestCase;
|
|
|
|
class MonitorStorageCommandTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_monitor_command_caches_capacity_snapshot(): void
|
|
{
|
|
$target = MediaStorageTarget::create([
|
|
'key' => 'local-ssd',
|
|
'name' => 'Local SSD',
|
|
'driver' => 'local',
|
|
'config' => ['monitor_path' => storage_path('app')],
|
|
'is_hot' => true,
|
|
'is_default' => true,
|
|
'is_active' => true,
|
|
'priority' => 100,
|
|
]);
|
|
|
|
$event = Event::factory()->create();
|
|
|
|
EventMediaAsset::create([
|
|
'event_id' => $event->id,
|
|
'media_storage_target_id' => $target->id,
|
|
'photo_id' => null,
|
|
'variant' => 'original',
|
|
'disk' => 'local-ssd',
|
|
'path' => 'events/'.$event->id.'/photo.jpg',
|
|
'size_bytes' => 2048,
|
|
'status' => 'hot',
|
|
]);
|
|
|
|
EventMediaAsset::create([
|
|
'event_id' => $event->id,
|
|
'media_storage_target_id' => $target->id,
|
|
'photo_id' => null,
|
|
'variant' => 'thumbnail',
|
|
'disk' => 'local-ssd',
|
|
'path' => 'events/'.$event->id.'/thumb.jpg',
|
|
'size_bytes' => 512,
|
|
'status' => 'failed',
|
|
]);
|
|
|
|
$health = Mockery::mock(StorageHealthService::class);
|
|
$health->shouldReceive('getCapacity')->andReturn([
|
|
'status' => 'ok',
|
|
'total' => 100,
|
|
'free' => 10,
|
|
'used' => 90,
|
|
'percentage' => 90,
|
|
'path' => storage_path('app'),
|
|
]);
|
|
$this->app->instance(StorageHealthService::class, $health);
|
|
|
|
$this->artisan('storage:monitor')
|
|
->expectsOutput('Storage monitor finished: 1 targets, 2 alerts.')
|
|
->assertExitCode(0);
|
|
|
|
$snapshot = Cache::get('storage:monitor:last');
|
|
|
|
$this->assertNotNull($snapshot);
|
|
$this->assertCount(1, $snapshot['targets']);
|
|
$this->assertSame('local-ssd', $snapshot['targets'][0]['key']);
|
|
$this->assertSame(2, $snapshot['targets'][0]['assets']['total']);
|
|
$this->assertGreaterThanOrEqual(1, count($snapshot['alerts']));
|
|
}
|
|
|
|
public function test_monitor_command_flags_checksum_mismatches(): void
|
|
{
|
|
config([
|
|
'storage-monitor.checksum_validation.enabled' => true,
|
|
'storage-monitor.checksum_validation.alert_window_minutes' => 60,
|
|
'storage-monitor.checksum_validation.thresholds.warning' => 1,
|
|
'storage-monitor.checksum_validation.thresholds.critical' => 5,
|
|
]);
|
|
|
|
$target = MediaStorageTarget::create([
|
|
'key' => 'local-ssd',
|
|
'name' => 'Local SSD',
|
|
'driver' => 'local',
|
|
'config' => ['monitor_path' => storage_path('app')],
|
|
'is_hot' => true,
|
|
'is_default' => true,
|
|
'is_active' => true,
|
|
'priority' => 100,
|
|
]);
|
|
|
|
$event = Event::factory()->create();
|
|
|
|
EventMediaAsset::create([
|
|
'event_id' => $event->id,
|
|
'media_storage_target_id' => $target->id,
|
|
'photo_id' => null,
|
|
'variant' => 'original',
|
|
'disk' => 'local-ssd',
|
|
'path' => 'events/'.$event->id.'/photo.jpg',
|
|
'size_bytes' => 2048,
|
|
'status' => 'failed',
|
|
'meta' => [
|
|
'checksum_status' => 'mismatch',
|
|
'checksum_verified_at' => now()->toIso8601String(),
|
|
],
|
|
]);
|
|
|
|
$health = Mockery::mock(StorageHealthService::class);
|
|
$health->shouldReceive('getCapacity')->andReturn([
|
|
'status' => 'ok',
|
|
'total' => 100,
|
|
'free' => 90,
|
|
'used' => 10,
|
|
'percentage' => 10,
|
|
'path' => storage_path('app'),
|
|
]);
|
|
$this->app->instance(StorageHealthService::class, $health);
|
|
|
|
$this->artisan('storage:monitor')
|
|
->assertExitCode(0);
|
|
|
|
$snapshot = Cache::get('storage:monitor:last');
|
|
|
|
$this->assertNotNull($snapshot);
|
|
$alerts = collect($snapshot['alerts'] ?? []);
|
|
$this->assertTrue($alerts->contains(fn ($alert) => ($alert['type'] ?? null) === 'checksum_mismatch'));
|
|
}
|
|
}
|