145 lines
4.1 KiB
C#
145 lines
4.1 KiB
C#
// <copyright file="RuntimeAgentBaseTests.cs" company="StellaOps">
|
|
// Copyright (c) StellaOps. Licensed under the AGPL-3.0-or-later.
|
|
// </copyright>
|
|
|
|
using FluentAssertions;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Microsoft.Extensions.Time.Testing;
|
|
using Xunit;
|
|
|
|
namespace StellaOps.Signals.RuntimeAgent.Tests;
|
|
|
|
/// <summary>
|
|
/// Tests for <see cref="RuntimeAgentBase"/>.
|
|
/// </summary>
|
|
[Trait("Category", "Unit")]
|
|
public class RuntimeAgentBaseTests
|
|
{
|
|
private readonly FakeTimeProvider _timeProvider = new();
|
|
private readonly TestGuidGenerator _guidGenerator = new();
|
|
|
|
[Fact]
|
|
public void Constructor_InitializesProperties()
|
|
{
|
|
var agent = CreateAgent();
|
|
|
|
agent.AgentId.Should().Be("test-agent");
|
|
agent.Platform.Should().Be(RuntimePlatform.DotNet);
|
|
agent.State.Should().Be(AgentState.Stopped);
|
|
agent.Posture.Should().Be(RuntimePosture.None);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task StartAsync_TransitionsToRunning()
|
|
{
|
|
var agent = CreateAgent();
|
|
var options = RuntimeAgentOptions.Development;
|
|
|
|
await agent.StartAsync(options, CancellationToken.None);
|
|
|
|
agent.State.Should().Be(AgentState.Running);
|
|
agent.Posture.Should().Be(RuntimePosture.ActiveTracing);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task StartAsync_WhenAlreadyRunning_Throws()
|
|
{
|
|
var agent = CreateAgent();
|
|
await agent.StartAsync(RuntimeAgentOptions.Development, CancellationToken.None);
|
|
|
|
var act = () => agent.StartAsync(RuntimeAgentOptions.Development, CancellationToken.None);
|
|
|
|
await act.Should().ThrowAsync<InvalidOperationException>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task StopAsync_TransitionsToStopped()
|
|
{
|
|
var agent = CreateAgent();
|
|
await agent.StartAsync(RuntimeAgentOptions.Development, CancellationToken.None);
|
|
|
|
await agent.StopAsync(CancellationToken.None);
|
|
|
|
agent.State.Should().Be(AgentState.Stopped);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task PauseAsync_TransitionsToPaused()
|
|
{
|
|
var agent = CreateAgent();
|
|
await agent.StartAsync(RuntimeAgentOptions.Development, CancellationToken.None);
|
|
|
|
await agent.PauseAsync(CancellationToken.None);
|
|
|
|
agent.State.Should().Be(AgentState.Paused);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ResumeAsync_TransitionsBackToRunning()
|
|
{
|
|
var agent = CreateAgent();
|
|
await agent.StartAsync(RuntimeAgentOptions.Development, CancellationToken.None);
|
|
await agent.PauseAsync(CancellationToken.None);
|
|
|
|
await agent.ResumeAsync(CancellationToken.None);
|
|
|
|
agent.State.Should().Be(AgentState.Running);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SetPostureAsync_ChangesPosture()
|
|
{
|
|
var agent = CreateAgent();
|
|
await agent.StartAsync(RuntimeAgentOptions.Development, CancellationToken.None);
|
|
|
|
await agent.SetPostureAsync(RuntimePosture.Sampled, CancellationToken.None);
|
|
|
|
agent.Posture.Should().Be(RuntimePosture.Sampled);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetStatistics_ReturnsStatistics()
|
|
{
|
|
var agent = CreateAgent();
|
|
await agent.StartAsync(RuntimeAgentOptions.Development, CancellationToken.None);
|
|
|
|
var stats = agent.GetStatistics();
|
|
|
|
stats.AgentId.Should().Be("test-agent");
|
|
stats.State.Should().Be(AgentState.Running);
|
|
stats.TotalEventsCollected.Should().Be(0);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DisposeAsync_StopsAgent()
|
|
{
|
|
var agent = CreateAgent();
|
|
await agent.StartAsync(RuntimeAgentOptions.Development, CancellationToken.None);
|
|
|
|
await agent.DisposeAsync();
|
|
|
|
agent.State.Should().Be(AgentState.Stopped);
|
|
}
|
|
|
|
private DotNetEventPipeAgent CreateAgent()
|
|
{
|
|
return new DotNetEventPipeAgent(
|
|
"test-agent",
|
|
_timeProvider,
|
|
_guidGenerator,
|
|
NullLogger<DotNetEventPipeAgent>.Instance);
|
|
}
|
|
|
|
private sealed class TestGuidGenerator : IGuidGenerator
|
|
{
|
|
private int _counter;
|
|
|
|
public Guid NewGuid()
|
|
{
|
|
var bytes = new byte[16];
|
|
BitConverter.GetBytes(++_counter).CopyTo(bytes, 0);
|
|
return new Guid(bytes);
|
|
}
|
|
}
|
|
}
|