86 lines
2.3 KiB
C#
86 lines
2.3 KiB
C#
using Heimdall.Agent.Services;
|
|||
|
|
|
||
|
|
namespace Heimdall.Agent.Tests.Services;
|
||
|
|
|
||
|
|
public class CgroupReaderTests
|
||
|
|
{
|
||
|
|
[Fact]
|
||
|
|
public void GetCpuLimit_ShouldReturnNull_OrPositiveValue_OnLinux()
|
||
|
|
{
|
||
|
|
if (!OperatingSystem.IsLinux()) return;
|
||
|
|
|
||
|
|
var limit = CgroupReader.GetCpuLimit();
|
||
|
|
|
||
|
|
// May be null if not in a container (unlimited)
|
||
|
|
// Or positive if in a container with CPU limits
|
||
|
|
if (limit.HasValue)
|
||
|
|
{
|
||
|
|
limit.Value.ShouldBeGreaterThan(0);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void GetMemoryLimit_ShouldReturnNull_OrPositiveValue_OnLinux()
|
||
|
|
{
|
||
|
|
if (!OperatingSystem.IsLinux()) return;
|
||
|
|
|
||
|
|
var limit = CgroupReader.GetMemoryLimit();
|
||
|
|
|
||
|
|
// May be null if not in a container (unlimited)
|
||
|
|
// Or positive if in a container with memory limits
|
||
|
|
if (limit.HasValue)
|
||
|
|
{
|
||
|
|
limit.Value.ShouldBeGreaterThan(0);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void GetCpuLimit_ShouldReturnReasonableValue_WhenPresent()
|
||
|
|
{
|
||
|
|
if (!OperatingSystem.IsLinux()) return;
|
||
|
|
|
||
|
|
var limit = CgroupReader.GetCpuLimit();
|
||
|
|
if (limit.HasValue)
|
||
|
|
{
|
||
|
|
// CPU limit as percentage should be between 1% and 10000% (100 cores)
|
||
|
|
limit.Value.ShouldBeGreaterThanOrEqualTo(1);
|
||
|
|
limit.Value.ShouldBeLessThanOrEqualTo(10_000);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void GetMemoryLimit_ShouldReturnReasonableValue_WhenPresent()
|
||
|
|
{
|
||
|
|
if (!OperatingSystem.IsLinux()) return;
|
||
|
|
|
||
|
|
var limit = CgroupReader.GetMemoryLimit();
|
||
|
|
if (limit.HasValue)
|
||
|
|
{
|
||
|
|
// Memory limit should be at least 1MB and less than 1PB
|
||
|
|
limit.Value.ShouldBeGreaterThanOrEqualTo(1_048_576);
|
||
|
|
limit.Value.ShouldBeLessThanOrEqualTo(1_125_899_906_842_624);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void GetCpuLimit_CalledTwice_ShouldReturnConsistentResults()
|
||
|
|
{
|
||
|
|
if (!OperatingSystem.IsLinux()) return;
|
||
|
|
|
||
|
|
var first = CgroupReader.GetCpuLimit();
|
||
|
|
var second = CgroupReader.GetCpuLimit();
|
||
|
|
|
||
|
|
first.ShouldBe(second);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void GetMemoryLimit_CalledTwice_ShouldReturnConsistentResults()
|
||
|
|
{
|
||
|
|
if (!OperatingSystem.IsLinux()) return;
|
||
|
|
|
||
|
|
var first = CgroupReader.GetMemoryLimit();
|
||
|
|
var second = CgroupReader.GetMemoryLimit();
|
||
|
|
|
||
|
|
first.ShouldBe(second);
|
||
|
|
}
|
||
|
|
}
|