91 lines
2.7 KiB
C#
91 lines
2.7 KiB
C#
using Heimdall.Agent.Logging;
|
|||
|
|
using Microsoft.Extensions.Logging;
|
||
|
|
using Shouldly;
|
||
|
|
|
||
|
|
namespace Heimdall.Agent.Tests;
|
||
|
|
|
||
|
|
public class LogBufferServiceTests
|
||
|
|
{
|
||
|
|
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();
|
||
|
|
}
|
||
|
|
}
|