213 lines
7.5 KiB
C#
213 lines
7.5 KiB
C#
using Heimdall.Application.Agents;
|
|||
|
|
using Heimdall.Application.Agents.Commands.Heartbeat;
|
||
|
|
using Heimdall.Application.DTOs;
|
||
|
|
using Heimdall.Domain.Entities;
|
||
|
|
using Heimdall.Domain.Enums;
|
||
|
|
|
||
|
|
namespace Heimdall.Application.Tests.Agents;
|
||
|
|
|
||
|
|
public class AgentTelemetryMappingTests
|
||
|
|
{
|
||
|
|
[Fact]
|
||
|
|
public void ApplyTo_ShouldSetLiveMetrics()
|
||
|
|
{
|
||
|
|
var agent = CreateAgent();
|
||
|
|
var cmd = new HeartbeatCommand(agent.Id, null, null, null, null, null,
|
||
|
|
CpuUsagePercent: 42.5,
|
||
|
|
MemoryUsageBytes: 4_294_967_296,
|
||
|
|
DiskUsagePercent: 73.1,
|
||
|
|
UptimeSeconds: 86400);
|
||
|
|
|
||
|
|
cmd.ApplyTo(agent);
|
||
|
|
|
||
|
|
agent.CpuUsagePercent.ShouldBe(42.5);
|
||
|
|
agent.MemoryUsageBytes.ShouldBe(4_294_967_296);
|
||
|
|
agent.DiskUsagePercent.ShouldBe(73.1);
|
||
|
|
agent.UptimeSeconds.ShouldBe(86400);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void ApplyTo_ShouldSetCpuInventory_WhenPresent()
|
||
|
|
{
|
||
|
|
var agent = CreateAgent();
|
||
|
|
var cmd = new HeartbeatCommand(agent.Id, null, null, null, null, null,
|
||
|
|
CpuVendor: "GenuineIntel",
|
||
|
|
CpuModel: "Intel Core i7-12700K",
|
||
|
|
CpuArchitecture: "x86_64",
|
||
|
|
PhysicalCores: 12,
|
||
|
|
LogicalCores: 20,
|
||
|
|
ThreadsPerCore: 2,
|
||
|
|
CpuFrequencyMhz: 3600.0,
|
||
|
|
CpuFlags: "avx,avx2,fma",
|
||
|
|
CpuCacheBytes: 25_165_824,
|
||
|
|
ContainerCpuLimit: 200);
|
||
|
|
|
||
|
|
cmd.ApplyTo(agent);
|
||
|
|
|
||
|
|
agent.CpuVendor.ShouldBe("GenuineIntel");
|
||
|
|
agent.CpuModel.ShouldBe("Intel Core 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");
|
||
|
|
agent.CpuCacheBytes.ShouldBe(25_165_824);
|
||
|
|
agent.ContainerCpuLimit.ShouldBe(200);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void ApplyTo_ShouldPreserveExistingCpuInventory_WhenNull()
|
||
|
|
{
|
||
|
|
var agent = CreateAgent();
|
||
|
|
agent.UpdateCpuInventory(new CpuInventoryData("AMD", "Ryzen", "x86_64", 8, 16, 2, 3400.0, "sse4", 16_777_216, null));
|
||
|
|
|
||
|
|
var cmd = new HeartbeatCommand(agent.Id, null, null, null, null, null,
|
||
|
|
CpuUsagePercent: 50.0);
|
||
|
|
|
||
|
|
cmd.ApplyTo(agent);
|
||
|
|
|
||
|
|
// CPU inventory should be preserved since no CPU inventory fields were sent
|
||
|
|
agent.CpuVendor.ShouldBe("AMD");
|
||
|
|
agent.CpuModel.ShouldBe("Ryzen");
|
||
|
|
agent.PhysicalCores.ShouldBe(8);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void ApplyTo_ShouldSetMemoryDiskInventory_WhenPresent()
|
||
|
|
{
|
||
|
|
var agent = CreateAgent();
|
||
|
|
var cmd = new HeartbeatCommand(agent.Id, null, null, null, null, null,
|
||
|
|
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: """[{"Mount":"/"}]""");
|
||
|
|
|
||
|
|
cmd.ApplyTo(agent);
|
||
|
|
|
||
|
|
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("""[{"Mount":"/"}]""");
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void ApplyTo_ShouldPreserveExistingMemoryDiskInventory_WhenNull()
|
||
|
|
{
|
||
|
|
var agent = CreateAgent();
|
||
|
|
agent.UpdateMemoryDiskInventory(new MemoryDiskInventoryData(8_000_000_000, 4_000_000_000, 2_000_000_000, 1_000_000_000, null, null));
|
||
|
|
|
||
|
|
var cmd = new HeartbeatCommand(agent.Id, null, null, null, null, null,
|
||
|
|
CpuUsagePercent: 50.0);
|
||
|
|
|
||
|
|
cmd.ApplyTo(agent);
|
||
|
|
|
||
|
|
agent.MemTotalBytes.ShouldBe(8_000_000_000);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void ApplyTo_ShouldSetGpuInventory_WhenPresent()
|
||
|
|
{
|
||
|
|
var agent = CreateAgent();
|
||
|
|
var cmd = new HeartbeatCommand(agent.Id, null, null, null, null, null,
|
||
|
|
GpuInventoryJson: """[{"Name":"RTX 4090"}]""");
|
||
|
|
|
||
|
|
cmd.ApplyTo(agent);
|
||
|
|
|
||
|
|
agent.GpuInventoryJson.ShouldBe("""[{"Name":"RTX 4090"}]""");
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void ApplyTo_ShouldSetHostInventory_WhenPresent()
|
||
|
|
{
|
||
|
|
var agent = CreateAgent();
|
||
|
|
var cmd = new HeartbeatCommand(agent.Id, null, null, null, null, null,
|
||
|
|
OsName: "Ubuntu 22.04 LTS",
|
||
|
|
KernelVersion: "5.15.0-76-generic",
|
||
|
|
Arch: "x86_64",
|
||
|
|
UptimeMachineSeconds: 86400,
|
||
|
|
ContainerId: "abc123",
|
||
|
|
ContainerImage: "heimdall/agent:latest",
|
||
|
|
NetworkInterfacesJson: """[{"Name":"eth0"}]""",
|
||
|
|
TemperatureJson: """[{"Zone":"coretemp"}]""");
|
||
|
|
|
||
|
|
cmd.ApplyTo(agent);
|
||
|
|
|
||
|
|
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("abc123");
|
||
|
|
agent.ContainerImage.ShouldBe("heimdall/agent:latest");
|
||
|
|
agent.NetworkInterfacesJson.ShouldBe("""[{"Name":"eth0"}]""");
|
||
|
|
agent.TemperatureJson.ShouldBe("""[{"Zone":"coretemp"}]""");
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void ApplyTo_ShouldPreserveExistingHostInventory_WhenNull()
|
||
|
|
{
|
||
|
|
var agent = CreateAgent();
|
||
|
|
agent.UpdateHostInventory(new HostInventoryData("Fedora", "6.5.0", "x86_64", 100000, "def456", "agent:v2", null, null));
|
||
|
|
|
||
|
|
var cmd = new HeartbeatCommand(agent.Id, null, null, null, null, null,
|
||
|
|
CpuUsagePercent: 50.0);
|
||
|
|
|
||
|
|
cmd.ApplyTo(agent);
|
||
|
|
|
||
|
|
agent.OsName.ShouldBe("Fedora");
|
||
|
|
agent.KernelVersion.ShouldBe("6.5.0");
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void ToTelemetryFields_ShouldReadAllFields()
|
||
|
|
{
|
||
|
|
var agent = CreateAgent();
|
||
|
|
agent.UpdateLiveMetrics(42.5, 4_294_967_296, 73.1, 86400);
|
||
|
|
agent.UpdateCpuInventory(new CpuInventoryData("Intel", "i7", "x86_64", 12, 20, 2, 3600.0, "avx", 25_165_824, 200));
|
||
|
|
agent.UpdateMemoryDiskInventory(new MemoryDiskInventoryData(17_179_869_184, 8_589_934_592, 2_147_483_648, 1_073_741_824, 4_294_967_296, """[{"Mount":"/"}]"""));
|
||
|
|
agent.UpdateGpuInventory("""[{"Name":"RTX 4090"}]""");
|
||
|
|
agent.UpdateHostInventory(new HostInventoryData("Ubuntu", "5.15.0", "x86_64", 86400, "abc", "img", "[]", "[]"));
|
||
|
|
|
||
|
|
var fields = agent.ToTelemetryFields();
|
||
|
|
|
||
|
|
fields.CpuUsagePercent.ShouldBe(42.5);
|
||
|
|
fields.MemoryUsageBytes.ShouldBe(4_294_967_296);
|
||
|
|
fields.DiskUsagePercent.ShouldBe(73.1);
|
||
|
|
fields.UptimeSeconds.ShouldBe(86400);
|
||
|
|
fields.CpuVendor.ShouldBe("Intel");
|
||
|
|
fields.MemTotalBytes.ShouldBe(17_179_869_184);
|
||
|
|
fields.GpuInventoryJson.ShouldBe("""[{"Name":"RTX 4090"}]""");
|
||
|
|
fields.OsName.ShouldBe("Ubuntu");
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void ApplyTo_RoundTrip_ShouldPreserveValues()
|
||
|
|
{
|
||
|
|
var agent = CreateAgent();
|
||
|
|
var cmd = new HeartbeatCommand(agent.Id, null, null, null, null, null,
|
||
|
|
CpuUsagePercent: 42.5,
|
||
|
|
MemoryUsageBytes: 4_294_967_296,
|
||
|
|
CpuVendor: "AMD",
|
||
|
|
MemTotalBytes: 16_000_000_000,
|
||
|
|
OsName: "Debian");
|
||
|
|
|
||
|
|
cmd.ApplyTo(agent);
|
||
|
|
|
||
|
|
var fields = agent.ToTelemetryFields();
|
||
|
|
|
||
|
|
fields.CpuUsagePercent.ShouldBe(42.5);
|
||
|
|
fields.MemoryUsageBytes.ShouldBe(4_294_967_296);
|
||
|
|
fields.CpuVendor.ShouldBe("AMD");
|
||
|
|
fields.MemTotalBytes.ShouldBe(16_000_000_000);
|
||
|
|
fields.OsName.ShouldBe("Debian");
|
||
|
|
}
|
||
|
|
|
||
|
|
private static Agent CreateAgent()
|
||
|
|
{
|
||
|
|
return new Agent(Guid.NewGuid(), "test-agent", "test-host", "1.0.0");
|
||
|
|
}
|
||
|
|
}
|