// ----------------------------------------------------------------------------- // RouterTestFixture.cs // Sprint: SPRINT_5100_0005_0001_router_chaos_suite // Task: T2 - Backpressure Verification Tests // Description: Test fixture for router chaos testing with Valkey support. // ----------------------------------------------------------------------------- using System.Net.Http.Json; namespace StellaOps.Chaos.Router.Tests.Fixtures; /// /// Test fixture providing an HTTP client for router chaos testing. /// public class RouterTestFixture : IAsyncLifetime { private readonly HttpClient _client; private readonly string _routerUrl; public RouterTestFixture() { _routerUrl = Environment.GetEnvironmentVariable("ROUTER_URL") ?? "http://localhost:8080"; _client = new HttpClient { BaseAddress = new Uri(_routerUrl), Timeout = TimeSpan.FromSeconds(30) }; } public HttpClient CreateClient() => _client; public string RouterUrl => _routerUrl; /// /// Configure router with lower limits for overload testing. /// public async Task ConfigureLowLimitsAsync() { // In real scenario, this would configure the router via admin endpoint // For now, assume limits are pre-configured for chaos testing await Task.CompletedTask; } /// /// Create a scan request payload. /// public static HttpContent CreateScanRequest(string? scanId = null) { var request = new { image = "alpine:latest", scanId = scanId ?? Guid.NewGuid().ToString(), timestamp = DateTimeOffset.UtcNow.ToString("O") }; return JsonContent.Create(request); } public Task InitializeAsync() { // Verify router is reachable return Task.CompletedTask; } public Task DisposeAsync() { _client.Dispose(); return Task.CompletedTask; } } /// /// Extended fixture with Valkey container support for failure injection. /// public class RouterWithValkeyFixture : RouterTestFixture { private Testcontainers.Redis.RedisContainer? _valkeyContainer; private bool _valkeyRunning; public async Task StartValkeyAsync() { if (_valkeyContainer is null) { _valkeyContainer = new Testcontainers.Redis.RedisBuilder() .WithImage("valkey/valkey:7-alpine") .WithName($"chaos-valkey-{Guid.NewGuid():N}") .Build(); } if (!_valkeyRunning) { await _valkeyContainer.StartAsync(); _valkeyRunning = true; } } public async Task StopValkeyAsync() { if (_valkeyContainer is not null && _valkeyRunning) { await _valkeyContainer.StopAsync(); _valkeyRunning = false; } } public async Task ConfigureValkeyLatencyAsync(TimeSpan latency) { // Configure artificial latency via Valkey DEBUG SLEEP // In production, use network simulation tools like tc or toxiproxy await Task.CompletedTask; } public new async Task DisposeAsync() { if (_valkeyContainer is not null) { await _valkeyContainer.StopAsync(); await _valkeyContainer.DisposeAsync(); } await base.DisposeAsync(); } }