- Job quotidien 4h : supprime les AgentLogEntry plus vieux que la rétention (Logs:RetentionDays, défaut 7 j), par lots de 5000 + pause 100 ms (pas de verrou long ni de transaction log explosée) - Enregistré dans JobSetup + DI BackgroundJobs - Tests : 2 (purge des vieux uniquement, rien si rien de vieux)
114 lines
4.0 KiB
C#
114 lines
4.0 KiB
C#
using Heimdall.Domain.Entities;
|
|
using Heimdall.Domain.Enums;
|
|
using Heimdall.Infrastructure.Data;
|
|
using Heimdall.Infrastructure.Data.Interceptors;
|
|
using Heimdall.Infrastructure.Jobs;
|
|
using Microsoft.Data.Sqlite;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
using NSubstitute;
|
|
using Shouldly;
|
|
|
|
namespace Heimdall.Infrastructure.Tests;
|
|
|
|
public class AgentLogCleanupJobTests : IDisposable
|
|
{
|
|
private readonly SqliteConnection _connection;
|
|
private readonly TestDbContextFactory _dbFactory;
|
|
private readonly ILogger<AgentLogCleanupJob> _logger = Substitute.For<ILogger<AgentLogCleanupJob>>();
|
|
|
|
public AgentLogCleanupJobTests()
|
|
{
|
|
_connection = new SqliteConnection("DataSource=:memory:");
|
|
_connection.Open();
|
|
_dbFactory = CreateFactory(_connection);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_connection?.Close();
|
|
_connection?.Dispose();
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
|
|
private static TestDbContextFactory CreateFactory(SqliteConnection connection)
|
|
{
|
|
var opts = new DbContextOptionsBuilder<AppDbContext>()
|
|
.UseSqlite(connection)
|
|
.Options;
|
|
|
|
using var ctx = new AppDbContext(opts, new AuditInterceptor());
|
|
ctx.Database.EnsureCreated();
|
|
return new TestDbContextFactory(opts);
|
|
}
|
|
|
|
private async Task<Guid> SeedAgentAsync()
|
|
{
|
|
await using var db = await _dbFactory.CreateDbContextAsync();
|
|
var site = new Site("Site A");
|
|
db.Sites.Add(site);
|
|
await db.SaveChangesAsync();
|
|
var agent = new Agent(site.Id, "Agent 1", "host1", "1.0.0");
|
|
db.Agents.Add(agent);
|
|
await db.SaveChangesAsync();
|
|
return agent.Id;
|
|
}
|
|
|
|
private AgentLogCleanupJob CreateJob(int retentionDays = 7)
|
|
{
|
|
var config = new ConfigurationBuilder()
|
|
.AddInMemoryCollection(new Dictionary<string, string?>
|
|
{
|
|
["Logs:RetentionDays"] = retentionDays.ToString(System.Globalization.CultureInfo.InvariantCulture),
|
|
})
|
|
.Build();
|
|
return new AgentLogCleanupJob(_dbFactory, _logger, config);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ExecuteAsync_DeletesOnlyOldEntries()
|
|
{
|
|
var agentId = await SeedAgentAsync();
|
|
await using (var db = await _dbFactory.CreateDbContextAsync())
|
|
{
|
|
db.AgentLogEntries.Add(new AgentLogEntry(agentId, DateTime.UtcNow.AddDays(-30), AgentLogLevel.Warning, "old"));
|
|
db.AgentLogEntries.Add(new AgentLogEntry(agentId, DateTime.UtcNow.AddDays(-3), AgentLogLevel.Information, "recent"));
|
|
await db.SaveChangesAsync();
|
|
}
|
|
|
|
await CreateJob(retentionDays: 7).ExecuteAsync(CancellationToken.None);
|
|
|
|
await using var verify = await _dbFactory.CreateDbContextAsync();
|
|
var remaining = await verify.AgentLogEntries.ToListAsync();
|
|
remaining.Count.ShouldBe(1);
|
|
remaining[0].Message.ShouldBe("recent");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ExecuteAsync_NoOldEntries_NothingDeleted()
|
|
{
|
|
var agentId = await SeedAgentAsync();
|
|
await using (var db = await _dbFactory.CreateDbContextAsync())
|
|
{
|
|
db.AgentLogEntries.Add(new AgentLogEntry(agentId, DateTime.UtcNow.AddHours(-1), AgentLogLevel.Information, "fresh"));
|
|
await db.SaveChangesAsync();
|
|
}
|
|
|
|
await CreateJob(retentionDays: 7).ExecuteAsync(CancellationToken.None);
|
|
|
|
await using var verify = await _dbFactory.CreateDbContextAsync();
|
|
(await verify.AgentLogEntries.CountAsync()).ShouldBe(1);
|
|
}
|
|
|
|
private sealed class TestDbContextFactory(DbContextOptions<AppDbContext> opts)
|
|
: IDbContextFactory<AppDbContext>
|
|
{
|
|
private static readonly AuditInterceptor Audit = new();
|
|
|
|
public AppDbContext CreateDbContext() => new(opts, Audit);
|
|
public Task<AppDbContext> CreateDbContextAsync(CancellationToken ct = default)
|
|
=> Task.FromResult(new AppDbContext(opts, Audit));
|
|
}
|
|
}
|