Skip to content

Commit 4e868cf

Browse files
authored
Merge pull request #16 from ScriptRunnerKit/fase-8/logs-dos-conteineres
test: testes de contêiner guardam os logs quando falham
2 parents 6245c9e + 70d3539 commit 4e868cf

9 files changed

Lines changed: 195 additions & 5 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,15 @@ jobs:
193193
PSR_TEST_WORKER_IMAGE: psr-worker:ci
194194
PSR_TEST_DASHBOARD_IMAGE: psr-dashboard:ci
195195

196+
# A failed test keeps the logs of its containers (hosts, PostgreSQL, RabbitMQ, compose services).
197+
- name: Upload container logs
198+
if: failure()
199+
uses: actions/upload-artifact@v7
200+
with:
201+
name: container-logs
202+
path: tests/PythonScriptRunner.Containers.Tests/bin/Debug/net10.0/TestResults/container-logs/
203+
if-no-files-found: ignore
204+
196205
# Spec 009, AC-1/AC-2: the Aspire AppHost brings everything up and the author runs CS-1 and CS-2.
197206
apphost:
198207
name: Aspire AppHost

‎CONTRIBUTING.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ A pull request is merged only when every job of `.github/workflows/ci.yml` is gr
6565
them in an incompatible way needs a new schema or protocol version.
6666
- **Changelog:** user-visible changes get a line in [CHANGELOG.md](CHANGELOG.md), under the version being prepared.
6767
- **Commits:** [Conventional Commits](https://www.conventionalcommits.org/) (`feat:`, `fix:`, `docs:`, `test:`,
68-
`build:`, `ci:`, `chore:`).
68+
`build:`, `ci:`, `chore:`). Commit messages and pull requests describe what changed and why, so that they read on their
69+
own; link the issue when there is one.
6970

7071
## Releases
7172

‎README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,10 @@ the memory limit through cgroup v2 and the compose from `aspire publish`; the co
569569
dotnet test --project tests/PythonScriptRunner.Containers.Tests # needs Docker
570570
```
571571

572+
When a container test fails, it saves the logs of its containers to `TestResults/container-logs/<test>/` next to the test
573+
assembly, and the end of the worker and dashboard logs goes to the test output. The CI uploads that folder as the
574+
`container-logs` artifact.
575+
572576
The dashboard performance test uses about 200,000 runs; `PSR_PERF_RUNS_PER_DAY=50000` runs the full average volume (90
573577
days). The dashboard's OpenAPI document is compared with
574578
`tests/PythonScriptRunner.Dashboard.Tests/Snapshots/openapi-v1.verified.json`. After you review an intended change,

‎tests/PythonScriptRunner.Containers.Tests/CgroupTests.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace PythonScriptRunner.Containers.Tests;
88
/// Spec 001, AC-30 (D-180): with a writable cgroup v2 (a privileged container) the memory limit of an attempt is the
99
/// memory really used, enforced by the kernel, and the numeric libraries keep their threads.
1010
/// </summary>
11-
public sealed class CgroupTests(HostStack stack)
11+
public sealed class CgroupTests(HostStack stack) : ContainerTest
1212
{
1313
private static readonly Dictionary<string, string> Stages = new()
1414
{

‎tests/PythonScriptRunner.Containers.Tests/ComposeTests.cs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace PythonScriptRunner.Containers.Tests;
99

1010
/// <summary>Spec 009, AC-3: the docker-compose generated by <c>aspire publish</c> runs CS-1 with the host images.</summary>
11-
public sealed class ComposeTests(HostStack stack)
11+
public sealed class ComposeTests(HostStack stack) : ContainerTest
1212
{
1313
private const string Version = "compose-test";
1414

@@ -57,6 +57,13 @@ await File.WriteAllTextAsync(Path.Combine(output, ".env"), $"""
5757
}
5858
finally
5959
{
60+
// `down` removes the services: their logs are kept for the report of a failed test.
61+
foreach (var service in new[] { "worker", "dashboard", "postgres", "rabbitmq" })
62+
{
63+
var (_, log) = await RunAsync("docker", output, "compose", "-p", project, "logs", "--no-color", "--no-log-prefix", service);
64+
ContainerLogs.Add($"compose-{service}", log, host: service is "worker" or "dashboard");
65+
}
66+
6067
await RunAsync("docker", output, "compose", "-p", project, "down", "-v");
6168
}
6269
}

‎tests/PythonScriptRunner.Containers.Tests/HostImageTests.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace PythonScriptRunner.Containers.Tests;
88

99
/// <summary>Spec 009: the worker and dashboard images as they run in production.</summary>
10-
public sealed class HostImageTests(HostStack stack)
10+
public sealed class HostImageTests(HostStack stack) : ContainerTest
1111
{
1212
private const long MaxWorkerImageBytes = 400L * 1024 * 1024;
1313

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
using System.Collections.Concurrent;
2+
using System.Text;
3+
using DotNet.Testcontainers.Configurations;
4+
5+
namespace PythonScriptRunner.Containers.Tests.Infrastructure;
6+
7+
/// <summary>
8+
/// The output of the containers, captured while they run: a test removes its containers when it ends, and their logs
9+
/// with them. When a test fails, <see cref="ReportAsync"/> saves every log to <c>TestResults/container-logs/&lt;test&gt;/</c>
10+
/// next to the test assembly (the CI uploads that folder) and writes the end of the host logs to the test output.
11+
/// </summary>
12+
public static class ContainerLogs
13+
{
14+
private const int TailLines = 80;
15+
private const string SharedKey = "";
16+
17+
private static readonly ConcurrentDictionary<string, ConcurrentQueue<Entry>> Logs = new();
18+
19+
/// <summary>An output consumer that keeps what the container writes, for the current test.</summary>
20+
/// <param name="name">File name of the log (made unique within the test).</param>
21+
/// <param name="host">Whether the end of the log also goes to the test output (the worker and the dashboard).</param>
22+
public static IOutputConsumer Capture(string name, bool host = true) => Capture(CurrentTest(), name, host);
23+
24+
/// <summary>An output consumer for a container shared by every test (PostgreSQL, RabbitMQ).</summary>
25+
public static IOutputConsumer CaptureShared(string name) => Capture(SharedKey, name, host: false);
26+
27+
/// <summary>Adds a log read some other way (<c>docker compose logs</c>) to the current test.</summary>
28+
public static void Add(string name, string text, bool host = true) => Register(CurrentTest(), name, () => text, host);
29+
30+
/// <summary>Called when a test class is disposed: writes the logs if the test failed and forgets them.</summary>
31+
public static async Task ReportAsync()
32+
{
33+
if (TestContext.Current.Test is not { } test)
34+
{
35+
return;
36+
}
37+
38+
Logs.TryRemove(test.UniqueID, out var own);
39+
if (TestContext.Current.TestState?.Result != TestResult.Failed)
40+
{
41+
return;
42+
}
43+
44+
var name = TestContext.Current.TestMethod?.MethodName ?? test.UniqueID;
45+
var folder = Path.Combine(AppContext.BaseDirectory, "TestResults", "container-logs", name);
46+
Directory.CreateDirectory(folder);
47+
var output = TestContext.Current.TestOutputHelper;
48+
var shared = Logs.TryGetValue(SharedKey, out var common) ? common : [];
49+
foreach (var entry in (own ?? []).Concat(shared))
50+
{
51+
var text = entry.Read();
52+
await File.WriteAllTextAsync(Path.Combine(folder, $"{entry.Name}.log"), text);
53+
if (entry.Host && output is not null)
54+
{
55+
var lines = text.Split('\n');
56+
output.WriteLine($"----- {entry.Name}: last {Math.Min(TailLines, lines.Length)} of {lines.Length} lines -----");
57+
output.WriteLine(string.Join('\n', lines.TakeLast(TailLines)));
58+
}
59+
}
60+
61+
output?.WriteLine($"Container logs: {folder}");
62+
}
63+
64+
private static IOutputConsumer Capture(string key, string name, bool host)
65+
{
66+
var stdout = new LogStream();
67+
var stderr = new LogStream();
68+
Register(key, name, () => stderr.Length == 0 ? stdout.Text() : $"{stdout.Text()}\n----- stderr -----\n{stderr.Text()}", host);
69+
return Consume.RedirectStdoutAndStderrToStream(stdout, stderr);
70+
}
71+
72+
private static void Register(string key, string name, Func<string> read, bool host)
73+
{
74+
var entries = Logs.GetOrAdd(key, _ => new ConcurrentQueue<Entry>());
75+
lock (entries)
76+
{
77+
var unique = name;
78+
for (var i = 2; entries.Any(e => e.Name == unique); i++)
79+
{
80+
unique = $"{name}-{i}";
81+
}
82+
83+
entries.Enqueue(new Entry(unique, read, host));
84+
}
85+
}
86+
87+
private static string CurrentTest() =>
88+
TestContext.Current.Test?.UniqueID ?? throw new InvalidOperationException("Container logs are captured inside a test.");
89+
90+
private sealed record Entry(string Name, Func<string> Read, bool Host);
91+
92+
/// <summary>A write-only buffer that Testcontainers fills from a background task and that is read after the container is gone.</summary>
93+
private sealed class LogStream : Stream
94+
{
95+
private readonly MemoryStream _buffer = new();
96+
97+
public override bool CanRead => false;
98+
99+
public override bool CanSeek => false;
100+
101+
public override bool CanWrite => true;
102+
103+
public override long Length
104+
{
105+
get
106+
{
107+
lock (_buffer)
108+
{
109+
return _buffer.Length;
110+
}
111+
}
112+
}
113+
114+
public override long Position
115+
{
116+
get => throw new NotSupportedException();
117+
set => throw new NotSupportedException();
118+
}
119+
120+
public string Text()
121+
{
122+
lock (_buffer)
123+
{
124+
return Encoding.UTF8.GetString(_buffer.GetBuffer(), 0, (int)_buffer.Length);
125+
}
126+
}
127+
128+
public override void Write(byte[] buffer, int offset, int count)
129+
{
130+
lock (_buffer)
131+
{
132+
_buffer.Write(buffer, offset, count);
133+
}
134+
}
135+
136+
public override void Flush()
137+
{
138+
}
139+
140+
public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException();
141+
142+
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
143+
144+
public override void SetLength(long value) => throw new NotSupportedException();
145+
146+
// Testcontainers disposes the consumer with the container; the captured text stays readable.
147+
protected override void Dispose(bool disposing)
148+
{
149+
}
150+
}
151+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace PythonScriptRunner.Containers.Tests.Infrastructure;
2+
3+
/// <summary>Base of the container tests: when a test fails, the logs of its containers are kept (<see cref="ContainerLogs"/>).</summary>
4+
public abstract class ContainerTest : IAsyncLifetime
5+
{
6+
public ValueTask InitializeAsync() => ValueTask.CompletedTask;
7+
8+
public async ValueTask DisposeAsync()
9+
{
10+
await ContainerLogs.ReportAsync();
11+
GC.SuppressFinalize(this);
12+
}
13+
}

‎tests/PythonScriptRunner.Containers.Tests/Infrastructure/HostStack.cs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ public async ValueTask InitializeAsync()
3737
_postgres = new PostgreSqlBuilder("postgres:16-alpine")
3838
.WithNetwork(_network).WithNetworkAliases("pg")
3939
.WithUsername("psr").WithPassword("psr").WithDatabase("psr")
40+
.WithOutputConsumer(ContainerLogs.CaptureShared("postgres"))
4041
.Build();
4142
_rabbit = new RabbitMqBuilder("rabbitmq:3.13-alpine")
4243
.WithNetwork(_network).WithNetworkAliases("rabbit")
4344
.WithUsername("psr").WithPassword("psr")
45+
.WithOutputConsumer(ContainerLogs.CaptureShared("rabbitmq"))
4446
.Build();
4547
var images = Task.WhenAll(HostImages.ResolveAsync(HostImages.Worker, Ct), HostImages.ResolveAsync(HostImages.Dashboard, Ct));
4648
await Task.WhenAll(_postgres.StartAsync(Ct), _rabbit.StartAsync(Ct), images);
@@ -66,12 +68,15 @@ public async Task<string> ScalarAsync(string database, string sql)
6668
/// <summary>
6769
/// A host container (not started) with migrations on, waiting until <c>/health/ready</c> answers 200;
6870
/// <paramref name="privileged"/> runs it privileged and as root, so it owns a writable <c>/sys/fs/cgroup</c>: a
69-
/// delegable cgroup v2 (D-180), like a systemd service with <c>Delegate=yes</c>.
71+
/// delegable cgroup v2 (D-180), like a systemd service with <c>Delegate=yes</c>. Its output is kept for the report of a
72+
/// failed test (<see cref="ContainerLogs"/>).
7073
/// </summary>
7174
public IContainer Host(string image, string database, IReadOnlyDictionary<string, string>? environment = null, bool privileged = false)
7275
{
76+
var logName = image == WorkerImage ? "worker" : image == DashboardImage ? "dashboard" : "host";
7377
var builder = new ContainerBuilder(image)
7478
.WithNetwork(_network)
79+
.WithOutputConsumer(ContainerLogs.Capture(logName))
7580
.WithPrivileged(privileged)
7681
.WithEnvironment("ConnectionStrings__psr", $"Host=pg;Database={database};Username=psr;Password=psr")
7782
.WithEnvironment("ConnectionStrings__rabbitmq", "amqp://psr:psr@rabbit:5672/")

0 commit comments

Comments
 (0)