Files
Heimdall/tests/Heimdall.Domain.Tests/Entities/AgentInventoryTests.cs
T
Arnaud Dartois 4792b69e46 feat(agent): télémétrie inventaire matériel — CPU/mémoire/disque/GPU/OS/réseau (Fixes #82 #83 #84 #85) (#137)
## Contexte

Implémentation des tickets #82, #83, #84, #85 — télémétrie inventaire matériel agent. L'objectif est de fournir au central un maximum d'informations sur les machines des agents (CPU, mémoire, disques, GPU, OS, réseau, conteneur) sans SSH.

## Changements

### Nouveaux collecteurs (src/Heimdall.Agent/Services/)
- **CpuInfoProvider** : parse `/proc/cpuinfo` (vendor, modèle, cœurs physiques/logiques, fréquence, flags AVX/AVX2/FMA/AVX512, cache), topologie, détection cgroup CPU (`/sys/fs/cgroup/cpu.max` v2)
- **DiskInventoryProvider** : énumère les montages (`/proc/mounts`), capacité/libre/type SSD/HDD via `/sys/block/*/queue/rotation`
- **GpuInventoryProvider** : nvidia-smi, sysfs DRM, vainfo, rocm-smi avec fallback silencieux, multi-GPU
- **HostInfoProvider** : os-release, uname, `/proc/uptime` (uptime machine ≠ process), températures sysfs
- **ContainerInfoProvider** : détection Docker (/.dockerenv, cgroup), ID conteneur, limites CPU/mémoire

### Refonte SystemMetricsProvider
- Séparation host vs process (RAM machine ≠ RAM conteneur ≠ WorkingSet64)
- Uptime machine (`/proc/uptime`) distinct de l'uptime process
- Mémoire swap

### Entité Agent + Migration
- Champs structurés CPU/mémoire/disque/GPU/OS/réseau (tous nullable, rétrocompatible)
- Migration EF `AddAgentTelemetryInventory`

### Transport heartbeat
- Inventaire au register + refresh périodique (5 min, pas à chaque tick)
- Champs JSON pour listes (disques, GPU, interfaces réseau, températures)

### UI Admin (AgentDetail.razor)
- Sections CPU, Mémoire/Disques, GPU, Machine/Conteneur avec barres de progression et badges

### Tests
- 40+ tests unitaires : providers, handlers, entité Agent
- Total : 999 tests verts (Domain 149, Application 397, Agent 210, Web 243)

## Tickets liés
- Fixes #82 — CPU inventory
- Fixes #83 — Mémoire & disque
- Fixes #84 — GPU inventory
- Fixes #85 — OS, conteneur, réseau

## Résultats des tests
- `dotnet build` : 0 erreur, 0 warning
- Domain.Tests : 149 passed
- Application.Tests : 397 passed
- Agent.Tests : 210 passed
- Web.Tests : 243 passed
- Total : 999 tests ✅

## Checklist
- [x] `dotnet build` vert (TreatWarningsAsErrors)
- [x] Tests unitaires verts
- [x] Migration EF créée
- [x] Champs nullable (rétrocompatible)
- [x] Pas de référence Infrastructure dans les contrôleurs
- [x] Collecteurs avec fallback silencieux (pas de crash sans GPU/Docker/sysfs)

Reviewed-on: https://forgejo.tordais.cc/nonobis/Heimdall/pulls/137
2026-08-20 14:13:06 +02:00

168 lines
5.7 KiB
C#

using Heimdall.Domain.Entities;
using Heimdall.Domain.Enums;
using Shouldly;
namespace Heimdall.Domain.Tests.Entities;
public class AgentInventoryTests
{
[Fact]
public void UpdateCpuInventory_should_set_all_fields()
{
var agent = CreateAgent();
agent.UpdateCpuInventory(new CpuInventoryData(
Vendor: "GenuineIntel",
Model: "Intel(R) Core(TM) i7-12700K",
Architecture: "x86_64",
PhysicalCores: 12,
LogicalCores: 20,
ThreadsPerCore: 2,
FrequencyMhz: 3600.0,
Flags: "avx,avx2,fma,aes",
CacheBytes: 25_165_824,
ContainerCpuLimit: 200));
agent.CpuVendor.ShouldBe("GenuineIntel");
agent.CpuModel.ShouldBe("Intel(R) Core(TM) i7-12700K");
agent.CpuArchitecture.ShouldBe("x86_64");
agent.PhysicalCores.ShouldBe(12);
agent.LogicalCores.ShouldBe(20);
agent.ThreadsPerCore.ShouldBe(2);
agent.CpuFrequencyMhz.ShouldBe(3600.0);
agent.CpuFlags.ShouldBe("avx,avx2,fma,aes");
agent.CpuCacheBytes.ShouldBe(25_165_824);
agent.ContainerCpuLimit.ShouldBe(200);
}
[Fact]
public void UpdateCpuInventory_should_accept_nulls()
{
var agent = CreateAgent();
agent.UpdateCpuInventory(new CpuInventoryData(null, null, null, null, null, null, null, null, null, null));
agent.CpuVendor.ShouldBeNull();
agent.CpuModel.ShouldBeNull();
agent.PhysicalCores.ShouldBeNull();
}
[Fact]
public void UpdateMemoryDiskInventory_should_set_all_fields()
{
var agent = CreateAgent();
var diskJson = """[{"MountPoint":"/","FileSystem":"ext4","TotalBytes":107374182400,"UsedBytes":53687091200,"Type":"SSD"}]""";
agent.UpdateMemoryDiskInventory(new MemoryDiskInventoryData(
MemTotalBytes: 17_179_869_184,
MemAvailableBytes: 8_589_934_592,
SwapTotalBytes: 2_147_483_648,
SwapFreeBytes: 1_073_741_824,
ContainerMemoryLimitBytes: 4_294_967_296,
DiskInventoryJson: diskJson));
agent.MemTotalBytes.ShouldBe(17_179_869_184);
agent.MemAvailableBytes.ShouldBe(8_589_934_592);
agent.SwapTotalBytes.ShouldBe(2_147_483_648);
agent.SwapFreeBytes.ShouldBe(1_073_741_824);
agent.ContainerMemoryLimitBytes.ShouldBe(4_294_967_296);
agent.DiskInventoryJson.ShouldBe(diskJson);
}
[Fact]
public void UpdateGpuInventory_should_set_json()
{
var agent = CreateAgent();
var gpuJson = """[{"Vendor":"Nvidia","Model":"RTX 3090","VramBytes":25769803776}]""";
agent.UpdateGpuInventory(gpuJson);
agent.GpuInventoryJson.ShouldBe(gpuJson);
}
[Fact]
public void UpdateHostInventory_should_set_all_fields()
{
var agent = CreateAgent();
var netJson = """[{"Name":"eth0","IpAddress":"192.168.1.100","MacAddress":"aa:bb:cc:dd:ee:ff"}]""";
var tempJson = """[{"Zone":"coretemp","TemperatureCelsius":45.0}]""";
agent.UpdateHostInventory(new HostInventoryData(
OsName: "Ubuntu 22.04 LTS",
KernelVersion: "5.15.0-76-generic",
Arch: "x86_64",
UptimeMachineSeconds: 86400,
ContainerId: "abc123def456",
ContainerImage: "heimdall/agent:latest",
NetworkInterfacesJson: netJson,
TemperatureJson: tempJson));
agent.OsName.ShouldBe("Ubuntu 22.04 LTS");
agent.KernelVersion.ShouldBe("5.15.0-76-generic");
agent.Arch.ShouldBe("x86_64");
agent.UptimeMachineSeconds.ShouldBe(86400);
agent.ContainerId.ShouldBe("abc123def456");
agent.ContainerImage.ShouldBe("heimdall/agent:latest");
agent.NetworkInterfacesJson.ShouldBe(netJson);
agent.TemperatureJson.ShouldBe(tempJson);
}
[Fact]
public void UpdateHostInventory_should_accept_nulls()
{
var agent = CreateAgent();
agent.UpdateHostInventory(new HostInventoryData(null, null, null, null, null, null, null, null));
agent.OsName.ShouldBeNull();
agent.KernelVersion.ShouldBeNull();
agent.ContainerId.ShouldBeNull();
}
[Fact]
public void All_new_fields_should_be_null_by_default()
{
var agent = CreateAgent();
// CPU
agent.CpuVendor.ShouldBeNull();
agent.CpuModel.ShouldBeNull();
agent.CpuArchitecture.ShouldBeNull();
agent.PhysicalCores.ShouldBeNull();
agent.LogicalCores.ShouldBeNull();
agent.ThreadsPerCore.ShouldBeNull();
agent.CpuFrequencyMhz.ShouldBeNull();
agent.CpuFlags.ShouldBeNull();
agent.CpuCacheBytes.ShouldBeNull();
agent.ContainerCpuLimit.ShouldBeNull();
// Memory
agent.MemTotalBytes.ShouldBeNull();
agent.MemAvailableBytes.ShouldBeNull();
agent.SwapTotalBytes.ShouldBeNull();
agent.SwapFreeBytes.ShouldBeNull();
agent.ContainerMemoryLimitBytes.ShouldBeNull();
// Disk
agent.DiskInventoryJson.ShouldBeNull();
// GPU
agent.GpuInventoryJson.ShouldBeNull();
// Host
agent.OsName.ShouldBeNull();
agent.KernelVersion.ShouldBeNull();
agent.Arch.ShouldBeNull();
agent.UptimeMachineSeconds.ShouldBeNull();
// Container
agent.ContainerId.ShouldBeNull();
agent.ContainerImage.ShouldBeNull();
// Network & Temperature
agent.NetworkInterfacesJson.ShouldBeNull();
agent.TemperatureJson.ShouldBeNull();
}
private static Agent CreateAgent()
{
var agent = new Agent(Guid.NewGuid(), "TestAgent", "test-host", "1.0.0");
agent.Register("hash", "pref");
return agent;
}
}