feat: VM metrics via portal-monitor integration
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
0b08689ea4
commit
9ed77a85fd
@@ -0,0 +1,162 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature\Api\Internal;
|
||||
|
||||
use App\Http\Facade\SignRSA;
|
||||
use App\Models\vCenter;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PortalMonitorVCenterTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_rejects_requests_without_service_key(): void
|
||||
{
|
||||
config()->set('portal-monitor.config_api_key', 'service-key');
|
||||
|
||||
$this->getJson('/api/v1/internal/portal-monitor/vcenter')
|
||||
->assertUnauthorized()
|
||||
->assertJsonMissingPath('data');
|
||||
}
|
||||
|
||||
public function test_returns_active_vcenter_credentials_from_module_vcenter(): void
|
||||
{
|
||||
config()->set('portal-monitor.config_api_key', 'service-key');
|
||||
|
||||
vCenter::query()->create([
|
||||
'host' => '10.0.0.20',
|
||||
'user' => 'monitor@example.local',
|
||||
'password' => SignRSA::EncryptFunc('secret-value'),
|
||||
'status' => 1,
|
||||
]);
|
||||
|
||||
$this->withHeader('X-Portal-Monitor-Key', 'service-key')
|
||||
->getJson('/api/v1/internal/portal-monitor/vcenter')
|
||||
->assertOk()
|
||||
->assertHeader('Cache-Control', 'no-store, private')
|
||||
->assertJsonPath('data.host', '10.0.0.20')
|
||||
->assertJsonPath('data.port', 443)
|
||||
->assertJsonPath('data.username', 'monitor@example.local')
|
||||
->assertJsonPath('data.password', 'secret-value');
|
||||
}
|
||||
|
||||
public function test_returns_sanitized_error_when_no_active_vcenter_exists(): void
|
||||
{
|
||||
config()->set('portal-monitor.config_api_key', 'service-key');
|
||||
|
||||
$this->withHeader('X-Portal-Monitor-Key', 'service-key')
|
||||
->getJson('/api/v1/internal/portal-monitor/vcenter')
|
||||
->assertStatus(503)
|
||||
->assertJson([
|
||||
'error' => true,
|
||||
'message' => 'Active vCenter configuration is unavailable',
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature\Api\Service;
|
||||
|
||||
use App\Models\Customer;
|
||||
use App\Models\ModuleVmware;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Tests\TestCase;
|
||||
|
||||
class VmMetricsTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_customer_can_read_metrics_for_owned_vm(): void
|
||||
{
|
||||
$customer = Customer::factory()->create(['status' => 1]);
|
||||
$vm = ModuleVmware::create([
|
||||
'customer_id' => $customer->id,
|
||||
'vmware_id' => 'vm-217208',
|
||||
'ram_mb' => 2048,
|
||||
'status' => 1,
|
||||
]);
|
||||
|
||||
config()->set('portal-monitor.url', 'http://portal-monitor-api:8080');
|
||||
config()->set('portal-monitor.api_key', 'test-api-key');
|
||||
Http::fake([
|
||||
'*/api/v1/vsphere/vms/vm-217208/metrics' => Http::response([
|
||||
'success' => true,
|
||||
'data' => ['cpu_usage_percent' => 15.5, 'timestamp' => '2026-10-17T04:00:00Z'],
|
||||
]),
|
||||
'*/api/v1/vsphere/vms/vm-217208/storage' => Http::response([
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'disks' => [['capacity_bytes' => 10737418240]],
|
||||
'summary' => [
|
||||
'total_bytes' => 10420744192,
|
||||
'used_bytes' => 5787086848,
|
||||
'usage_percent' => 55.5343,
|
||||
],
|
||||
],
|
||||
]),
|
||||
]);
|
||||
|
||||
$this->actingAs($customer, 'client')
|
||||
->withHeaders(['x-access-token' => $this->buildClientAccessToken()])
|
||||
->getJson('/v1/system/vm-metrics?product=' . $vm->vmware_id)
|
||||
->assertOk()
|
||||
->assertJsonPath('success', true)
|
||||
->assertJsonPath('data.cpu_usage_percent', 15.5)
|
||||
->assertJsonPath('data.mem_total_mb', 2048)
|
||||
->assertJsonPath('data.disk_used_bytes', 5787086848)
|
||||
->assertJsonPath('data.disk_total_bytes', 10420744192)
|
||||
->assertJsonPath('data.disk_usage_percent', 55.5343);
|
||||
}
|
||||
|
||||
public function test_customer_cannot_read_another_customers_vm_metrics(): void
|
||||
{
|
||||
$owner = Customer::factory()->create(['status' => 1]);
|
||||
$attacker = Customer::factory()->create(['status' => 1]);
|
||||
$vm = ModuleVmware::create([
|
||||
'customer_id' => $owner->id,
|
||||
'vmware_id' => 'vm-217209',
|
||||
'status' => 1,
|
||||
]);
|
||||
Http::fake();
|
||||
|
||||
$this->actingAs($attacker, 'client')
|
||||
->withHeaders(['x-access-token' => $this->buildClientAccessToken()])
|
||||
->getJson('/v1/system/vm-metrics?product=' . $vm->vmware_id)
|
||||
->assertNotFound();
|
||||
|
||||
Http::assertNothingSent();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user