ConfigReceiverService.ApplySetting ne faisait que logger. Le setting heartbeat.interval.seconds est désormais appliqué à chaud : écrit dans AgentState.HeartbeatIntervalSeconds (snapshot thread-safe) et relu par AgentRegistrationService à chaque cycle — plus besoin de redémarrer l'agent pour changer l'intervalle. Validation 5-300s, valeurs invalides ignorées sans muter l'état. Tests : +2 (application live vérifiée, invalides ne mutent pas l'état).
163 lines
5.7 KiB
C#
163 lines
5.7 KiB
C#
using System.Reflection;
|
|
using Heimdall.Agent.Services;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using NSubstitute;
|
|
|
|
namespace Heimdall.Agent.Tests;
|
|
|
|
public class ConfigReceiverServiceTests
|
|
{
|
|
private static ConfigReceiverService CreateService(AgentState? state = null)
|
|
{
|
|
state ??= new AgentState { AgentId = Guid.Empty };
|
|
var options = Options.Create(new AgentOptions
|
|
{
|
|
CentralUrl = "http://localhost:5000",
|
|
SiteToken = "test"
|
|
});
|
|
var logger = Substitute.For<ILogger<ConfigReceiverService>>();
|
|
var httpFactory = Substitute.For<IHttpClientFactory>();
|
|
return new ConfigReceiverService(state, options, logger, httpFactory);
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_WithDependencies_DoesNotThrow()
|
|
{
|
|
var service = CreateService();
|
|
service.ShouldNotBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task StartStop_WhenNotRegistered_StopsGracefully()
|
|
{
|
|
var state = new AgentState { AgentId = Guid.Empty };
|
|
var service = CreateService(state);
|
|
|
|
await service.StartAsync(CancellationToken.None);
|
|
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(3));
|
|
await service.StopAsync(cts.Token);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task StartStop_WhenRegistered_StopsGracefully()
|
|
{
|
|
var state = new AgentState { AgentId = Guid.NewGuid(), ApiKey = "test-key" };
|
|
var service = CreateService(state);
|
|
|
|
await service.StartAsync(CancellationToken.None);
|
|
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
|
|
await service.StopAsync(cts.Token);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task StopAsync_WithoutStart_DoesNotThrow()
|
|
{
|
|
var service = CreateService();
|
|
await service.StopAsync(CancellationToken.None);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ExecuteAsync_CancelledWhileWaitingForAgentId_StopsGracefully()
|
|
{
|
|
var state = new AgentState { AgentId = Guid.Empty };
|
|
var service = CreateService(state);
|
|
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(100));
|
|
await service.StartAsync(cts.Token);
|
|
await service.StopAsync(CancellationToken.None);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ExecuteAsync_ConnectionRetriesThenCancels_StopsGracefully()
|
|
{
|
|
var state = new AgentState { AgentId = Guid.NewGuid(), ApiKey = "test-key" };
|
|
var service = CreateService(state);
|
|
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(2));
|
|
await service.StartAsync(cts.Token);
|
|
await service.StopAsync(CancellationToken.None);
|
|
}
|
|
|
|
[Fact]
|
|
public void ApplySetting_InvalidHeartbeatValue_DoesNotThrow()
|
|
{
|
|
var service = CreateService();
|
|
var method = typeof(ConfigReceiverService).GetMethod("ApplySetting",
|
|
BindingFlags.NonPublic | BindingFlags.Instance);
|
|
method.ShouldNotBeNull();
|
|
|
|
// Below minimum threshold (5)
|
|
Record.Exception(() => method.Invoke(service, ["heartbeat.interval.seconds", "3"]))
|
|
.ShouldBeNull();
|
|
// Non-numeric value
|
|
Record.Exception(() => method.Invoke(service, ["heartbeat.interval.seconds", "not-a-number"]))
|
|
.ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void ApplySetting_KnownAndUnknownKeys_DoesNotThrow()
|
|
{
|
|
var service = CreateService();
|
|
var method = typeof(ConfigReceiverService).GetMethod("ApplySetting",
|
|
BindingFlags.NonPublic | BindingFlags.Instance);
|
|
method.ShouldNotBeNull();
|
|
|
|
Record.Exception(() => method.Invoke(service, ["log.level", "Debug"]))
|
|
.ShouldBeNull();
|
|
Record.Exception(() => method.Invoke(service, ["unknown.key", "some-value"]))
|
|
.ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void ApplySetting_ValidHeartbeatInterval_DoesNotThrow()
|
|
{
|
|
var state = new AgentState { AgentId = Guid.NewGuid(), ApiKey = "test-key" };
|
|
var service = CreateService(state);
|
|
var method = typeof(ConfigReceiverService).GetMethod("ApplySetting",
|
|
BindingFlags.NonPublic | BindingFlags.Instance);
|
|
method.ShouldNotBeNull();
|
|
|
|
Record.Exception(() => method.Invoke(service, ["heartbeat.interval.seconds", "30"]))
|
|
.ShouldBeNull();
|
|
|
|
// The setting is applied live to the shared state
|
|
state.HeartbeatIntervalSeconds.ShouldBe(30);
|
|
}
|
|
|
|
[Fact]
|
|
public void ApplySetting_InvalidHeartbeatInterval_DoesNotMutateState()
|
|
{
|
|
var state = new AgentState { AgentId = Guid.NewGuid(), ApiKey = "test-key" };
|
|
var service = CreateService(state);
|
|
var method = typeof(ConfigReceiverService).GetMethod("ApplySetting",
|
|
BindingFlags.NonPublic | BindingFlags.Instance);
|
|
method.ShouldNotBeNull();
|
|
|
|
// Below minimum (5)
|
|
method.Invoke(service, ["heartbeat.interval.seconds", "3"]);
|
|
state.HeartbeatIntervalSeconds.ShouldBeNull();
|
|
|
|
// Above maximum (300)
|
|
method.Invoke(service, ["heartbeat.interval.seconds", "999"]);
|
|
state.HeartbeatIntervalSeconds.ShouldBeNull();
|
|
|
|
// Non-numeric
|
|
method.Invoke(service, ["heartbeat.interval.seconds", "abc"]);
|
|
state.HeartbeatIntervalSeconds.ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task StartStop_MultipleTimes_DoesNotThrow()
|
|
{
|
|
var state = new AgentState { AgentId = Guid.NewGuid(), ApiKey = "test-key" };
|
|
var service = CreateService(state);
|
|
|
|
// First start/stop cycle
|
|
await service.StartAsync(CancellationToken.None);
|
|
await service.StopAsync(CancellationToken.None);
|
|
|
|
// Second start/stop cycle
|
|
await service.StartAsync(CancellationToken.None);
|
|
await service.StopAsync(CancellationToken.None);
|
|
}
|
|
}
|