Files
Heimdall/tests/Heimdall.Agent.Tests/LogBufferServiceTests.cs
T
Arnaud Dartois 0136bf33ec feat(logs): remontée des logs agents vers le central — buffer, API, UI (Fixes #50 #51 #52)
- Agent : LogBufferService (ring buffer borné, drop-oldest), AgentLoggingProvider
  (capture ILogger non-bloquante), LogSanitizer (masque password/token/secret/bearer),
  LogUploadService (batch POST /api/agents/{id}/logs, retry, drop si 400)
- Central : entité AgentLogEntry + migration AddAgentLogEntries (index AgentId+TimestampUtc),
  SubmitAgentLogsCommand (batch <= 200, entrées invalides ignorées),
  GetAgentLogsQuery (pagination, filtres niveau/période/recherche),
  endpoints POST + GET /api/agents/{id}/logs
- UI : onglet Logs dans AgentDetail (filtres, pagination, auto-refresh toggle)
- Logs agent enrichis : heartbeat succès (Debug), résolution IP (Debug)
- Tests : LogBufferServiceTests (6), AgentDetailLogsTests (4), handlers AgentLogs (8)
2026-08-11 18:51:52 +02:00

92 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();
}
}