Files
Heimdall/tests/Heimdall.Agent.Tests/LogBufferServiceTests.cs
T

108 lines
3.4 KiB
C#
Raw Normal View History

using Heimdall.Agent.Logging;
using Heimdall.Agent.Services;
using Heimdall.Domain.Enums;
using Microsoft.Extensions.Logging;
using Shouldly;
namespace Heimdall.Agent.Tests;
public class LogBufferServiceTests
{
[Theory]
[InlineData(LogLevel.Trace, "Trace")]
[InlineData(LogLevel.Debug, "Debug")]
[InlineData(LogLevel.Information, "Information")]
[InlineData(LogLevel.Warning, "Warning")]
[InlineData(LogLevel.Error, "Error")]
[InlineData(LogLevel.Critical, "Critical")]
public void ToCentralLevel_MapsMicrosoftNamesToAgentContract(LogLevel level, string expected)
{
var centralName = LogUploadService.ToCentralLevel(level);
centralName.ShouldBe(expected);
Enum.TryParse<AgentLogLevel>(centralName, true, out var parsed).ShouldBeTrue();
parsed.ShouldBe((AgentLogLevel)level);
}
private static BufferedLogEntry Entry(string message, LogLevel level = LogLevel.Information)
=> new(DateTime.UtcNow, level, message, null, "TestService");
[Fact]
public void TakeAll_ReturnsEntriesInOrder()
{
var buffer = new LogBufferService(10);
buffer.Enqueue(Entry("first"));
buffer.Enqueue(Entry("second"));
buffer.Enqueue(Entry("third"));
var entries = buffer.TakeAll();
entries.Count.ShouldBe(3);
entries[0].Message.ShouldBe("first");
entries[1].Message.ShouldBe("second");
entries[2].Message.ShouldBe("third");
buffer.Count.ShouldBe(0);
}
[Fact]
public void TakeAll_WhenEmpty_ReturnsEmptyList()
{
var buffer = new LogBufferService(10);
buffer.TakeAll().ShouldBeEmpty();
}
[Fact]
public void Enqueue_WhenFull_DropsOldestAndCounts()
{
var buffer = new LogBufferService(3);
buffer.Enqueue(Entry("a"));
buffer.Enqueue(Entry("b"));
buffer.Enqueue(Entry("c"));
// Overflow: the newest entry must survive (fresh Error > old Warnings).
buffer.Enqueue(Entry("d"));
buffer.Enqueue(Entry("e"));
var entries = buffer.TakeAll();
entries.Select(e => e.Message).ShouldBe(["c", "d", "e"]);
buffer.DroppedCount.ShouldBe(2);
}
[Fact]
public void Requeue_RestoresFailedUploadEntries()
{
var buffer = new LogBufferService(10);
buffer.Enqueue(Entry("a"));
var taken = buffer.TakeAll();
buffer.Requeue(taken);
buffer.Count.ShouldBe(1);
buffer.TakeAll()[0].Message.ShouldBe("a");
}
[Fact]
public void Sanitize_MasksSecrets()
{
LogSanitizer.Sanitize("Connecting with password=hunter2 and token=abc123")
.ShouldNotContain("hunter2");
LogSanitizer.Sanitize("Connecting with password=hunter2 and token=abc123")
.ShouldContain("***");
LogSanitizer.Sanitize("Authorization: Bearer eyJhbGciOiJIUzI1NiJ9")
.ShouldNotContain("eyJhbGciOiJIUzI1NiJ9");
LogSanitizer.Sanitize("{\"api_key\":\"super-secret-value\",\"user\":\"bob\"}")
.ShouldNotContain("super-secret-value");
LogSanitizer.Sanitize("snapshotUrl=https://cdn.example.com/snap/1.jpg")
.ShouldNotContain("cdn.example.com");
}
[Fact]
public void Sanitize_LeavesNormalMessagesIntact()
{
const string message = "Heartbeat OK — agent online";
LogSanitizer.Sanitize(message).ShouldBe(message);
LogSanitizer.Sanitize(null).ShouldBeEmpty();
}
}