Files
portal-api/tests/Feature/Api/Admin/VCenterManagerVmStatsTest.php
T
2026-10-17 13:04:35 +07:00

163 lines
5.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\Api\Admin;
use App\Models\ModuleDatacenter;
use App\Models\User;
use App\Models\VirtualMachines;
use App\Models\vCenter;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use Spatie\Permission\Models\Role;
use Tests\TestCase;
class VCenterManagerVmStatsTest extends TestCase
{
use RefreshDatabase;
private User $admin;
protected function setUp(): void
{
parent::setUp();
Role::firstOrCreate(['name' => 'Full Administrator', 'guard_name' => 'admin']);
$this->admin = User::factory()->create();
$this->admin->assignRole('Full Administrator');
config()->set('portal-monitor.url', 'http://portal-monitor-api:8080');
config()->set('portal-monitor.api_key', 'test-api-key');
config()->set('portal-monitor.timeout', 3);
}
private function datacenterId(): int
{
$vcenter = vCenter::create(['host' => 'vcenter.test']);
return ModuleDatacenter::create([
'name' => 'DC-01',
'datacenter_id' => 'datacenter-1',
'vcenter_id' => $vcenter->id,
])->id;
}
public function test_stats_resolves_database_id_and_returns_portal_monitor_metrics(): void
{
$vm = VirtualMachines::create([
'uuid' => '8d804daf-c856-4a99-b749-fb97060b84f0',
'vm_id' => 'vm-217209',
'name' => 'web-01',
'power_state' => 'POWERED_ON',
'ram_mb' => 8192,
'datacenter_id' => $this->datacenterId(),
]);
Http::fake([
'http://portal-monitor-api:8080/api/v1/vsphere/vms/vm-217209/metrics' => Http::response([
'success' => true,
'data' => [
'cpu_usage_percent' => 12.5,
'cpu_mhz' => 420.25,
'cpu_ready_ms' => 1.5,
'memory_active_mb' => 2048.5,
'memory_consumed_mb' => 3072.5,
'guest_memory_usage_percent' => 25.1,
'disk_read_bps' => 1024,
'disk_write_bps' => 2048,
'disk_latency_ms' => 4.2,
'network_rx_bps' => 4096,
'network_tx_bps' => 8192,
'timestamp' => '2026-10-17T08:00:00Z',
'stale' => true,
],
]),
'http://portal-monitor-api:8080/api/v1/vsphere/vms/vm-217209/storage' => Http::response([
'success' => true,
'data' => [
'summary' => [
'total_bytes' => 161061273600,
'used_bytes' => 107374182400,
'free_bytes' => 53687091200,
'usage_percent' => 66.6667,
],
'disks' => [
['capacity_bytes' => 53687091200],
['capacity_bytes' => 107374182400],
],
],
]),
]);
$this->actingAs($this->admin, 'admin')
->getJson('/api/v1/administrator/vcenter-manager/vm/stats?vm_id=' . $vm->id)
->assertOk()
->assertJsonPath('data.cpu_usage_percent', 12.5)
->assertJsonPath('data.mem_usage_mb', 2048.5)
->assertJsonPath('data.mem_consumed_mb', 3072.5)
->assertJsonPath('data.mem_total_mb', 8192)
->assertJsonPath('data.disk_read_bps', 1024)
->assertJsonPath('data.disk_total_bytes', 161061273600)
->assertJsonPath('data.disk_used_bytes', 107374182400)
->assertJsonPath('data.disk_usage_percent', 66.6667)
->assertJsonPath('data.timestamp', '2026-10-17T08:00:00Z')
->assertJsonPath('data.stale', true);
Http::assertSent(fn (Request $request) =>
$request->url() === 'http://portal-monitor-api:8080/api/v1/vsphere/vms/vm-217209/metrics'
&& $request->hasHeader('X-API-Key', 'test-api-key')
);
Http::assertSent(fn (Request $request) =>
$request->url() === 'http://portal-monitor-api:8080/api/v1/vsphere/vms/vm-217209/storage'
&& $request->hasHeader('X-API-Key', 'test-api-key')
);
}
public function test_stats_returns_sanitized_gateway_error(): void
{
$vm = VirtualMachines::create([
'uuid' => '796ae79b-b8b9-4638-9be2-d1b3b39325f2',
'vm_id' => 'vm-217210',
'name' => 'web-02',
'power_state' => 'POWERED_ON',
'datacenter_id' => $this->datacenterId(),
]);
Http::fake([
'*' => Http::response([
'error' => ['message' => 'upstream-secret-detail'],
], 500),
]);
$this->actingAs($this->admin, 'admin')
->getJson('/api/v1/administrator/vcenter-manager/vm/stats?vm_id=' . $vm->id)
->assertStatus(502)
->assertExactJson([
'error' => true,
'message' => 'Unable to fetch VM metrics',
])
->assertDontSee('upstream-secret-detail');
}
public function test_stats_does_not_call_monitor_for_powered_off_vm(): void
{
$vm = VirtualMachines::create([
'uuid' => 'f33f3f6c-dd0a-45f7-82f7-4c61a0d4e750',
'vm_id' => 'vm-217211',
'name' => 'web-03',
'power_state' => 'POWERED_OFF',
'datacenter_id' => $this->datacenterId(),
]);
Http::fake();
$this->actingAs($this->admin, 'admin')
->getJson('/api/v1/administrator/vcenter-manager/vm/stats?vm_id=' . $vm->id)
->assertStatus(400);
Http::assertNothingSent();
}
}