|
| 1 | +using System.Net; |
| 2 | +using System.Reflection; |
| 3 | +using Microsoft.EntityFrameworkCore.Migrations; |
| 4 | +using PythonScriptRunner.Containers.Tests.Infrastructure; |
| 5 | +using PythonScriptRunner.EntityFrameworkCore; |
| 6 | + |
| 7 | +namespace PythonScriptRunner.Containers.Tests; |
| 8 | + |
| 9 | +/// <summary>Spec 009: the worker and dashboard images as they run in production.</summary> |
| 10 | +public sealed class HostImageTests(HostStack stack) |
| 11 | +{ |
| 12 | + private const long MaxWorkerImageBytes = 400L * 1024 * 1024; |
| 13 | + |
| 14 | + private static CancellationToken Ct => TestContext.Current.CancellationToken; |
| 15 | + |
| 16 | + [Fact] |
| 17 | + public async Task Spec009_AC07_BothImages_RunAsNonRootOnAReadOnlyFileSystem_AndAnswerTheHealthChecks() |
| 18 | + { |
| 19 | + var database = await stack.CreateDatabaseAsync("ac07"); |
| 20 | + await using var worker = stack.Host(stack.WorkerImage, database); |
| 21 | + await using var dashboard = stack.Host(stack.DashboardImage, database, HostStack.DashboardUsers()); |
| 22 | + |
| 23 | + await worker.StartAsync(Ct); |
| 24 | + await dashboard.StartAsync(Ct); |
| 25 | + |
| 26 | + foreach (var host in new[] { worker, dashboard }) |
| 27 | + { |
| 28 | + using var http = Client(host); |
| 29 | + (await http.GetAsync("/health/live", Ct)).StatusCode.ShouldBe(HttpStatusCode.OK); |
| 30 | + (await http.GetAsync("/health/ready", Ct)).StatusCode.ShouldBe(HttpStatusCode.OK); |
| 31 | + (await host.ExecAsync(["id", "-u"], Ct)).Stdout.Trim().ShouldBe("10001"); |
| 32 | + (await host.ExecAsync(["sh", "-c", "touch /app/escrita"], Ct)).ExitCode.ShouldNotBe(0, "the root file system is read-only"); |
| 33 | + |
| 34 | + // The psrunner wheel of the same commit (D-169) is what the host installs in each environment. |
| 35 | + var environment = (await host.ExecAsync(["sh", "-c", "tr '\\0' '\\n' < /proc/1/environ"], Ct)).Stdout; |
| 36 | + environment.ShouldContain("PythonScriptRunner__PsrunnerRequirement=/opt/psr/psrunner-"); |
| 37 | + } |
| 38 | + |
| 39 | + using var panel = Client(dashboard); |
| 40 | + (await panel.GetStringAsync("/psr/", Ct)).ShouldContain("id=\"app\"", customMessage: "the image embeds the built panel"); |
| 41 | + } |
| 42 | + |
| 43 | + [Fact] |
| 44 | + public async Task Spec009_AC08_TwoWorkersWithAutoMigrateStartingTogether_ApplyTheMigrationsOnce() |
| 45 | + { |
| 46 | + var database = await stack.CreateDatabaseAsync("ac08"); |
| 47 | + await using var first = stack.Host(stack.WorkerImage, database); |
| 48 | + await using var second = stack.Host(stack.WorkerImage, database); |
| 49 | + |
| 50 | + await Task.WhenAll(first.StartAsync(Ct), second.StartAsync(Ct)); |
| 51 | + |
| 52 | + var migrations = typeof(PsrDbContext).Assembly.GetTypes().Count(t => t.GetCustomAttribute<MigrationAttribute>() is not null); |
| 53 | + migrations.ShouldBeGreaterThan(0); |
| 54 | + var applied = await stack.ScalarAsync(database, $"SELECT count(*) FROM {PsrDbContext.Schema}.{PsrDbContext.MigrationsHistoryTable}"); |
| 55 | + applied.ShouldBe(migrations.ToString(System.Globalization.CultureInfo.InvariantCulture)); |
| 56 | + foreach (var worker in new[] { first, second }) |
| 57 | + { |
| 58 | + var (stdout, stderr) = await worker.GetLogsAsync(ct: Ct); |
| 59 | + (stdout + stderr).ShouldNotContain("Hosting failed to start"); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public async Task Spec009_WorkerImage_IsAtMost400MBWithoutPythonInterpreters() |
| 65 | + { |
| 66 | + var (code, output) = await HostImages.DockerAsync(["image", "inspect", "--format", "{{.Size}}", stack.WorkerImage], Ct); |
| 67 | + |
| 68 | + code.ShouldBe(0, output); |
| 69 | + long.Parse(output.Trim(), System.Globalization.CultureInfo.InvariantCulture).ShouldBeLessThanOrEqualTo(MaxWorkerImageBytes); |
| 70 | + } |
| 71 | + |
| 72 | + private static HttpClient Client(IContainer host) => |
| 73 | + new() { BaseAddress = new Uri($"http://{host.Hostname}:{host.GetMappedPublicPort(HostStack.HttpPort)}") }; |
| 74 | +} |
0 commit comments