sqlinsight-pro/tests/SQLInsight.IntegrationTests/AgentSession.cs
Søren Eggert Lundsteen Olsen d574057114 Integration tests: fake Axapta 3.0 (bmssa) and AX 2012 R2 databases
Built from the schema files in the SQL Server 2022 container and read as the
least-privilege agent login: detection and discovery, 'ikke målbar' with the
exact GRANT and measuring after it (no customer table readable, no data
changed), the shipped grant/revoke scripts, AX 2012 history by RECID read
once, a locked AX table skipped within seconds, the SQL Scripts run as-is,
repository retention and read-back, and the web pages and API v1.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-02 13:54:23 +02:00

49 lines
1.8 KiB
C#

using SQLInsight.Collectors;
using SQLInsight.Connectivity;
using SQLInsight.Core.Instances;
namespace SQLInsight.IntegrationTests;
/// <summary>
/// A monitoring connection as the least-privilege agent login, opened exactly like the agent does.
/// </summary>
internal sealed class AgentSession : IAsyncDisposable
{
private readonly OpenedConnection _opened;
private AgentSession(OpenedConnection opened, InstanceInfo instance)
{
_opened = opened;
Instance = instance;
Capabilities = InstanceCapabilities.From(instance);
}
public InstanceInfo Instance { get; }
public InstanceCapabilities Capabilities { get; }
public System.Data.Common.DbConnection Connection => _opened.Connection;
public static async Task<AgentSession> OpenAsync(SqlServerFixture sql)
{
var opened = await new SqlConnectionFactory().OpenAsync(new ConnectionTarget
{
Server = sql.Server,
Authentication = AuthenticationMode.SqlLogin,
UserName = SqlServerFixture.AgentLogin,
Password = SqlServerFixture.AgentPassword,
Provider = ConnectionProvider.SqlClient,
TrustServerCertificate = true,
});
var instance = await InstanceProbe.DetectAsync(opened.Connection);
return new AgentSession(opened, instance);
}
public async Task<T> CollectAsync<T>(ICollector collector, T? previous = null, Core.Configuration.DatabaseSelection? selection = null)
where T : class
{
var context = new CollectorContext(_opened.Connection, Instance, Capabilities, DateTimeOffset.UtcNow, previous, CancellationToken.None, selection);
return (T)await collector.CollectAsync(context);
}
public ValueTask DisposeAsync() => _opened.DisposeAsync();
}