Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
AOC Guard CI / aoc-guard (push) Has been cancelled
AOC Guard CI / aoc-verify (push) Has been cancelled
Concelier Attestation Tests / attestation-tests (push) Has been cancelled
Export Center CI / export-ci (push) Has been cancelled
Notify Smoke Test / Notify Unit Tests (push) Has been cancelled
Notify Smoke Test / Notifier Service Tests (push) Has been cancelled
Notify Smoke Test / Notification Smoke Test (push) Has been cancelled
Policy Lint & Smoke / policy-lint (push) Has been cancelled
Scanner Analyzers / Discover Analyzers (push) Has been cancelled
Scanner Analyzers / Build Analyzers (push) Has been cancelled
Scanner Analyzers / Test Language Analyzers (push) Has been cancelled
Scanner Analyzers / Validate Test Fixtures (push) Has been cancelled
Scanner Analyzers / Verify Deterministic Output (push) Has been cancelled
Signals CI & Image / signals-ci (push) Has been cancelled
Signals Reachability Scoring & Events / reachability-smoke (push) Has been cancelled
Signals Reachability Scoring & Events / sign-and-upload (push) Has been cancelled
164 lines
6.2 KiB
C#
164 lines
6.2 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using StellaOps.Auth.Abstractions;
|
|
using StellaOps.Scheduler.Models;
|
|
using StellaOps.Scheduler.WebService.GraphJobs;
|
|
using StellaOps.Scheduler.WebService.GraphJobs.Events;
|
|
using StellaOps.Scheduler.WebService.Options;
|
|
using StackExchange.Redis;
|
|
|
|
namespace StellaOps.Scheduler.WebService.Tests;
|
|
|
|
public sealed class GraphJobEventPublisherTests
|
|
{
|
|
[Fact]
|
|
public async Task PublishAsync_LogsEvent_WhenDriverUnsupported()
|
|
{
|
|
var options = Microsoft.Extensions.Options.Options.Create(new SchedulerEventsOptions
|
|
{
|
|
GraphJobs =
|
|
{
|
|
Enabled = true,
|
|
Driver = "memory"
|
|
}
|
|
});
|
|
var loggerProvider = new ListLoggerProvider();
|
|
using var loggerFactory = LoggerFactory.Create(builder => builder.AddProvider(loggerProvider));
|
|
var publisher = new GraphJobEventPublisher(new OptionsMonitorStub<SchedulerEventsOptions>(options), new ThrowingRedisConnectionFactory(), loggerFactory.CreateLogger<GraphJobEventPublisher>());
|
|
|
|
var buildJob = new GraphBuildJob(
|
|
id: "gbj_test",
|
|
tenantId: "tenant-alpha",
|
|
sbomId: "sbom",
|
|
sbomVersionId: "sbom_v1",
|
|
sbomDigest: "sha256:" + new string('a', 64),
|
|
status: GraphJobStatus.Completed,
|
|
trigger: GraphBuildJobTrigger.SbomVersion,
|
|
createdAt: DateTimeOffset.UtcNow,
|
|
graphSnapshotId: "graph_snap",
|
|
attempts: 1,
|
|
cartographerJobId: "carto",
|
|
correlationId: "corr",
|
|
startedAt: DateTimeOffset.UtcNow.AddSeconds(-30),
|
|
completedAt: DateTimeOffset.UtcNow,
|
|
error: null,
|
|
metadata: Array.Empty<KeyValuePair<string, string>>());
|
|
|
|
var notification = new GraphJobCompletionNotification(
|
|
buildJob.TenantId,
|
|
GraphJobQueryType.Build,
|
|
GraphJobStatus.Completed,
|
|
DateTimeOffset.UtcNow,
|
|
GraphJobResponse.From(buildJob),
|
|
"oras://result",
|
|
"corr",
|
|
null);
|
|
|
|
await publisher.PublishAsync(notification, CancellationToken.None);
|
|
|
|
Assert.Contains(loggerProvider.Messages, message => message.Contains("unsupported driver", StringComparison.OrdinalIgnoreCase));
|
|
var eventPayload = loggerProvider.Messages.FirstOrDefault(message => message.Contains("\"kind\":\"scheduler.graph.job.completed\"", StringComparison.Ordinal));
|
|
Assert.NotNull(eventPayload);
|
|
Assert.Contains("\"tenant\":\"tenant-alpha\"", eventPayload);
|
|
Assert.Contains("\"resultUri\":\"oras://result\"", eventPayload);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task PublishAsync_Suppressed_WhenDisabled()
|
|
{
|
|
var options = Microsoft.Extensions.Options.Options.Create(new SchedulerEventsOptions());
|
|
var loggerProvider = new ListLoggerProvider();
|
|
using var loggerFactory = LoggerFactory.Create(builder => builder.AddProvider(loggerProvider));
|
|
var publisher = new GraphJobEventPublisher(new OptionsMonitorStub<SchedulerEventsOptions>(options), new ThrowingRedisConnectionFactory(), loggerFactory.CreateLogger<GraphJobEventPublisher>());
|
|
|
|
var overlayJob = new GraphOverlayJob(
|
|
id: "goj_test",
|
|
tenantId: "tenant-alpha",
|
|
graphSnapshotId: "graph_snap",
|
|
buildJobId: null,
|
|
overlayKind: GraphOverlayKind.Policy,
|
|
overlayKey: "policy@1",
|
|
subjects: Array.Empty<string>(),
|
|
status: GraphJobStatus.Completed,
|
|
trigger: GraphOverlayJobTrigger.Policy,
|
|
createdAt: DateTimeOffset.UtcNow,
|
|
attempts: 1,
|
|
correlationId: null,
|
|
startedAt: DateTimeOffset.UtcNow.AddSeconds(-10),
|
|
completedAt: DateTimeOffset.UtcNow,
|
|
error: null,
|
|
metadata: Array.Empty<KeyValuePair<string, string>>());
|
|
|
|
var notification = new GraphJobCompletionNotification(
|
|
overlayJob.TenantId,
|
|
GraphJobQueryType.Overlay,
|
|
GraphJobStatus.Completed,
|
|
DateTimeOffset.UtcNow,
|
|
GraphJobResponse.From(overlayJob),
|
|
null,
|
|
null,
|
|
null);
|
|
|
|
await publisher.PublishAsync(notification, CancellationToken.None);
|
|
|
|
Assert.DoesNotContain(loggerProvider.Messages, message => message.Contains(GraphJobEventKinds.GraphJobCompleted, StringComparison.Ordinal));
|
|
}
|
|
|
|
private sealed class OptionsMonitorStub<T> : IOptionsMonitor<T> where T : class
|
|
{
|
|
private readonly IOptions<T> _options;
|
|
|
|
public OptionsMonitorStub(IOptions<T> options)
|
|
{
|
|
_options = options;
|
|
}
|
|
|
|
public T CurrentValue => _options.Value;
|
|
|
|
public T Get(string? name) => _options.Value;
|
|
|
|
public IDisposable? OnChange(Action<T, string?> listener) => null;
|
|
}
|
|
|
|
private sealed class ThrowingRedisConnectionFactory : IRedisConnectionFactory
|
|
{
|
|
public Task<IConnectionMultiplexer> ConnectAsync(ConfigurationOptions options, CancellationToken cancellationToken)
|
|
=> throw new InvalidOperationException("Redis connection should not be established in this test.");
|
|
}
|
|
|
|
private sealed class ListLoggerProvider : ILoggerProvider
|
|
{
|
|
private readonly ListLogger _logger = new();
|
|
|
|
public IList<string> Messages => _logger.Messages;
|
|
|
|
public ILogger CreateLogger(string categoryName) => _logger;
|
|
|
|
public void Dispose()
|
|
{
|
|
}
|
|
|
|
private sealed class ListLogger : ILogger
|
|
{
|
|
public IList<string> Messages { get; } = new List<string>();
|
|
|
|
public IDisposable BeginScope<TState>(TState state) where TState : notnull => NullDisposable.Instance;
|
|
|
|
public bool IsEnabled(LogLevel logLevel) => true;
|
|
|
|
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
|
|
{
|
|
Messages.Add(formatter(state, exception));
|
|
}
|
|
|
|
private sealed class NullDisposable : IDisposable
|
|
{
|
|
public static readonly NullDisposable Instance = new();
|
|
public void Dispose()
|
|
{
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|