123 lines
3.5 KiB
C#
123 lines
3.5 KiB
C#
using Heimdall.Agent.Services;
|
|||
|
|
|
||
|
|
namespace Heimdall.Agent.Tests.Services;
|
||
|
|
|
||
|
|
public class ContainerInfoProviderDetailedTests
|
||
|
|
{
|
||
|
|
[Fact]
|
||
|
|
public void Collect_ShouldReturnContainerInfo()
|
||
|
|
{
|
||
|
|
var provider = new ContainerInfoProvider();
|
||
|
|
var result = provider.Collect();
|
||
|
|
|
||
|
|
result.ShouldNotBeNull();
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void Collect_ShouldNotThrow()
|
||
|
|
{
|
||
|
|
var provider = new ContainerInfoProvider();
|
||
|
|
var act = () => provider.Collect();
|
||
|
|
act.ShouldNotThrow();
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void Collect_IsContainer_ShouldBeConsistentWithDockerEnv()
|
||
|
|
{
|
||
|
|
var provider = new ContainerInfoProvider();
|
||
|
|
var result = provider.Collect();
|
||
|
|
|
||
|
|
var actuallyInContainer = File.Exists("/.dockerenv") || File.Exists("/run/.containerenv");
|
||
|
|
result.IsContainer.ShouldBe(actuallyInContainer);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void Collect_ContainerId_ShouldBeNullOrHex_WhenContainer()
|
||
|
|
{
|
||
|
|
var provider = new ContainerInfoProvider();
|
||
|
|
var result = provider.Collect();
|
||
|
|
|
||
|
|
if (result.IsContainer && result.ContainerId is not null)
|
||
|
|
{
|
||
|
|
result.ContainerId.Length.ShouldBeGreaterThanOrEqualTo(12);
|
||
|
|
result.ContainerId.All(c => "0123456789abcdef".Contains(c)).ShouldBeTrue();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void Collect_MemoryLimitBytes_ShouldBeNullOrPositive()
|
||
|
|
{
|
||
|
|
var provider = new ContainerInfoProvider();
|
||
|
|
var result = provider.Collect();
|
||
|
|
|
||
|
|
if (result.MemoryLimitBytes.HasValue)
|
||
|
|
{
|
||
|
|
result.MemoryLimitBytes.Value.ShouldBeGreaterThan(0);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void Collect_CpuLimit_ShouldBeNullOrPositive()
|
||
|
|
{
|
||
|
|
var provider = new ContainerInfoProvider();
|
||
|
|
var result = provider.Collect();
|
||
|
|
|
||
|
|
if (result.CpuLimit.HasValue)
|
||
|
|
{
|
||
|
|
result.CpuLimit.Value.ShouldBeGreaterThan(0);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void Collect_CalledTwice_ShouldReturnConsistentResults()
|
||
|
|
{
|
||
|
|
var provider = new ContainerInfoProvider();
|
||
|
|
var first = provider.Collect();
|
||
|
|
var second = provider.Collect();
|
||
|
|
|
||
|
|
first.IsContainer.ShouldBe(second.IsContainer);
|
||
|
|
first.ContainerId.ShouldBe(second.ContainerId);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ── ContainerInfo record tests ──────────────────────────────────
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void ContainerInfo_DefaultValues_ShouldBeCorrect()
|
||
|
|
{
|
||
|
|
var info = new ContainerInfo();
|
||
|
|
|
||
|
|
info.IsContainer.ShouldBeFalse();
|
||
|
|
info.ContainerId.ShouldBeNull();
|
||
|
|
info.ContainerImage.ShouldBeNull();
|
||
|
|
info.MemoryLimitBytes.ShouldBeNull();
|
||
|
|
info.CpuLimit.ShouldBeNull();
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void ContainerInfo_WithValues_ShouldStoreCorrectly()
|
||
|
|
{
|
||
|
|
var info = new ContainerInfo
|
||
|
|
{
|
||
|
|
IsContainer = true,
|
||
|
|
ContainerId = "abc123def456",
|
||
|
|
ContainerImage = "heimdall/agent:latest",
|
||
|
|
MemoryLimitBytes = 4_294_967_296,
|
||
|
|
CpuLimit = 200
|
||
|
|
};
|
||
|
|
|
||
|
|
info.IsContainer.ShouldBeTrue();
|
||
|
|
info.ContainerId.ShouldBe("abc123def456");
|
||
|
|
info.ContainerImage.ShouldBe("heimdall/agent:latest");
|
||
|
|
info.MemoryLimitBytes.ShouldBe(4_294_967_296);
|
||
|
|
info.CpuLimit.ShouldBe(200);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void ContainerInfo_RecordEquality_ShouldWork()
|
||
|
|
{
|
||
|
|
var info1 = new ContainerInfo { IsContainer = true, ContainerId = "abc" };
|
||
|
|
var info2 = new ContainerInfo { IsContainer = true, ContainerId = "abc" };
|
||
|
|
|
||
|
|
info1.ShouldBe(info2);
|
||
|
|
}
|
||
|
|
}
|