test(core): add metrics provider and parser tests
This commit is contained in:
1 parent
1ed14c0a19
commit
77ab104be3
4 files changed
+155
No files matched your search
@@ -0,0 +1,31 @@
|
||||
using Vantage.Core.Metrics;
|
||||
using Vantage.Core.Tests.Fixtures;
|
||||
using Xunit;
|
||||
|
||||
namespace Vantage.Core.Tests.Metrics;
|
||||
|
||||
public class MetricsBundleParserTests
|
||||
{
|
||||
[Fact]
|
||||
public void Parse_BuildsHostMetricsFromFullBundle()
|
||||
{
|
||||
var metrics = MetricsBundleParser.Parse(MetricsFixtures.FullBundle());
|
||||
|
||||
Assert.Equal(80.0, metrics.CpuUsagePercent, 1);
|
||||
Assert.Equal(8000000L * 1024, metrics.MemoryTotalBytes);
|
||||
Assert.Equal(6000000L * 1024, metrics.MemoryUsedBytes);
|
||||
Assert.Equal(75.0, metrics.MemoryUsedPercent, 1);
|
||||
Assert.Equal(100000000000L, metrics.DiskTotalBytes);
|
||||
Assert.Equal(40000000000L, metrics.DiskUsedBytes);
|
||||
Assert.Equal(40.0, metrics.DiskUsedPercent, 1);
|
||||
Assert.Equal(0.50, metrics.Load1, 3);
|
||||
Assert.Equal(123456.78, metrics.Uptime.TotalSeconds, 2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Parse_ThrowsWhenSectionMissing()
|
||||
{
|
||||
var truncated = "@@vantage:loadavg\n" + MetricsFixtures.Loadavg + "\n";
|
||||
Assert.Throws<FormatException>(() => MetricsBundleParser.Parse(truncated));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using Vantage.Core.Metrics;
|
||||
using Xunit;
|
||||
|
||||
namespace Vantage.Core.Tests.Metrics;
|
||||
|
||||
public class MetricsHistoryTests
|
||||
{
|
||||
[Fact]
|
||||
public void Snapshot_ReturnsValuesInInsertionOrder()
|
||||
{
|
||||
var history = new MetricsHistory(5);
|
||||
history.Add(1);
|
||||
history.Add(2);
|
||||
history.Add(3);
|
||||
|
||||
Assert.Equal(new double[] { 1, 2, 3 }, history.Snapshot());
|
||||
Assert.Equal(3, history.Latest);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Add_WrapsAroundAndKeepsMostRecentWindow()
|
||||
{
|
||||
var history = new MetricsHistory(3);
|
||||
for (var i = 1; i <= 5; i++)
|
||||
history.Add(i);
|
||||
|
||||
Assert.Equal(3, history.Count);
|
||||
Assert.Equal(new double[] { 3, 4, 5 }, history.Snapshot());
|
||||
Assert.Equal(5, history.Latest);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_RejectsNonPositiveCapacity()
|
||||
{
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => new MetricsHistory(0));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using Vantage.Core.Metrics;
|
||||
using Vantage.Core.Models;
|
||||
using Xunit;
|
||||
|
||||
namespace Vantage.Core.Tests.Metrics;
|
||||
|
||||
public class MockMetricsProviderTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task GetMetricsAsync_ProducesValuesInPlausibleRanges()
|
||||
{
|
||||
var provider = new MockMetricsProvider(() => DateTimeOffset.UnixEpoch.AddSeconds(500));
|
||||
var host = new HostConfig { Id = "host-a", Name = "a" };
|
||||
|
||||
var metrics = await provider.GetMetricsAsync(host, CancellationToken.None);
|
||||
|
||||
Assert.InRange(metrics.CpuUsagePercent, 0, 100);
|
||||
Assert.InRange(metrics.MemoryUsedPercent, 0, 100);
|
||||
Assert.True(metrics.MemoryTotalBytes > 0);
|
||||
Assert.True(metrics.MemoryUsedBytes <= metrics.MemoryTotalBytes);
|
||||
Assert.True(metrics.Uptime > TimeSpan.Zero);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetMetricsAsync_VariesOverTimeButKeepsTotalsStable()
|
||||
{
|
||||
var now = DateTimeOffset.UnixEpoch;
|
||||
var provider = new MockMetricsProvider(() => now);
|
||||
var host = new HostConfig { Id = "host-b" };
|
||||
|
||||
now = DateTimeOffset.UnixEpoch.AddSeconds(1000);
|
||||
var first = await provider.GetMetricsAsync(host, CancellationToken.None);
|
||||
now = DateTimeOffset.UnixEpoch.AddSeconds(1012);
|
||||
var second = await provider.GetMetricsAsync(host, CancellationToken.None);
|
||||
|
||||
Assert.NotEqual(first.CpuUsagePercent, second.CpuUsagePercent);
|
||||
Assert.Equal(first.MemoryTotalBytes, second.MemoryTotalBytes);
|
||||
Assert.Equal(first.DiskTotalBytes, second.DiskTotalBytes);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetMetricsAsync_IsDeterministicPerHostIdentity()
|
||||
{
|
||||
var at = DateTimeOffset.UnixEpoch.AddSeconds(2000);
|
||||
var providerOne = new MockMetricsProvider(() => at);
|
||||
var providerTwo = new MockMetricsProvider(() => at);
|
||||
|
||||
var one = await providerOne.GetMetricsAsync(new HostConfig { Id = "same" }, CancellationToken.None);
|
||||
var two = await providerTwo.GetMetricsAsync(new HostConfig { Id = "same" }, CancellationToken.None);
|
||||
|
||||
Assert.Equal(one.MemoryTotalBytes, two.MemoryTotalBytes);
|
||||
Assert.Equal(one.DiskTotalBytes, two.DiskTotalBytes);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using Vantage.Core.Metrics;
|
||||
using Vantage.Core.Models;
|
||||
using Vantage.Core.Tests.Fakes;
|
||||
using Vantage.Core.Tests.Fixtures;
|
||||
using Xunit;
|
||||
|
||||
namespace Vantage.Core.Tests.Metrics;
|
||||
|
||||
public class SshMetricsProviderTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task GetMetricsAsync_ParsesBundleFromRunner()
|
||||
{
|
||||
var runner = new FakeSshCommandRunner(MetricsFixtures.FullBundle());
|
||||
var provider = new SshMetricsProvider(runner);
|
||||
|
||||
var metrics = await provider.GetMetricsAsync(new HostConfig { DiskMount = "/srv" }, CancellationToken.None);
|
||||
|
||||
Assert.Equal(80.0, metrics.CpuUsagePercent, 1);
|
||||
Assert.Equal(75.0, metrics.MemoryUsedPercent, 1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetMetricsAsync_UsesConfiguredMountInCommand()
|
||||
{
|
||||
var runner = new FakeSshCommandRunner(MetricsFixtures.FullBundle());
|
||||
var provider = new SshMetricsProvider(runner);
|
||||
|
||||
await provider.GetMetricsAsync(new HostConfig { DiskMount = "/srv/data" }, CancellationToken.None);
|
||||
|
||||
Assert.Contains("df -B1 -P '/srv/data'", runner.Commands.Single());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user