Alla #25 mancava la prova promessa: esportare un milione di righe dentro il contenitore con il tetto di memoria di Unraid senza che il bot cada. Sul PC non c'è Docker, quindi la prova va sul runner, e lì possono girare anche le schermate con un bot vero, che finora si facevano solo a mano. - prove-a-mano.yml, solo da Actions: lavoro «carico» con l'immagine costruita come in verifica, su una rete interna con un PostgreSQL usa e getta; prova-carico.sh riempie l'archivio in SQL (1,2 milioni di decisioni e di puntate), prova-carico.js entra dall'interfaccia, esporta 1.000.000 righe e ne annulla una seconda; si ferma su caduta, OOM, picco residente oltre 768 MB o file .tmp rimasti, e stampa il peso dell'immagine. - Lavoro «schermate»: bot con --simulatore dentro il lavoro (mai nell'immagine) e Mimante.Schermate con il Chromium di Debian, schermate come artefatto. - PipelineTests: timeout, password solo per nome, rete interna, --memory=1g e simulatore fuori dall'immagine anche per il flusso nuovo. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
200 lines
9.4 KiB
C#
200 lines
9.4 KiB
C#
using System.Text.RegularExpressions;
|
|
using Xunit;
|
|
|
|
namespace Mimante.Tests.Packaging;
|
|
|
|
/// <summary>
|
|
/// The CI of the repository (.gitea/workflows and .gitea/scripts), read as text: the rules that must survive later
|
|
/// edits. The runner talks to the Docker daemon of Unraid, where the real PostgreSQL of Mimante runs, so nothing may
|
|
/// publish a port and the tried container lives on an internal network; the image is published only from main or a
|
|
/// release tag and :latest only from a release; credentials are never printed; external tools are pinned.
|
|
/// </summary>
|
|
public sealed partial class PipelineTests
|
|
{
|
|
private static readonly string Root = FindRoot();
|
|
|
|
[Fact]
|
|
public void EveryPushOfEveryBranchIsVerifiedAndTagsAreLeftToTheRelease()
|
|
{
|
|
var workflow = Workflow("verifica.yml");
|
|
Assert.Contains("on:\n push:\n branches: [\"**\"]\n workflow_dispatch:", workflow);
|
|
var code = Code(workflow);
|
|
Assert.DoesNotContain("pull_request", code);
|
|
Assert.DoesNotContain("tags:", code);
|
|
Assert.Contains("cancel-in-progress: ${{ gitea.ref != 'refs/heads/main' }}", code);
|
|
Assert.Contains("tags: [\"v*\"]", Workflow("rilascio.yml"));
|
|
}
|
|
|
|
[Fact]
|
|
public void JobsFollowOneAnotherAndEachHasATimeout()
|
|
{
|
|
var verifica = Workflow("verifica.yml");
|
|
Assert.Contains("needs: controlli", Job(verifica, "prove"));
|
|
Assert.Contains("needs: prove", Job(verifica, "immagine"));
|
|
Assert.Contains("needs: [prove, immagine]", Job(Workflow("rilascio.yml"), "release"));
|
|
foreach (var name in new[] { "verifica.yml", "rilascio.yml", "pulizia.yml", "prove-a-mano.yml" })
|
|
{
|
|
var text = Workflow(name);
|
|
foreach (Match job in JobHeader().Matches(text))
|
|
Assert.True(Job(text, job.Groups["name"].Value).Contains("timeout-minutes:"), $"{name}: {job.Groups["name"].Value} has no timeout");
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void NothingPublishesAPortOnTheDaemonOfUnraid()
|
|
{
|
|
foreach (var file in Directory.GetFiles(Path.Combine(Root, ".gitea"), "*.*", SearchOption.AllDirectories)
|
|
.Where(f => f.EndsWith(".yml") || f.EndsWith(".sh")))
|
|
{
|
|
var code = Code(File.ReadAllText(file).Replace("\r\n", "\n"));
|
|
Assert.False(Regex.IsMatch(code, @"^\s*ports:", RegexOptions.Multiline), $"{file}: ports");
|
|
Assert.False(Regex.IsMatch(code, @"\s(-p|--publish)(\s|=)\S*\d"), $"{file}: docker run -p");
|
|
Assert.DoesNotContain("--network host", code);
|
|
}
|
|
var trial = Script("prova-immagine.sh");
|
|
Assert.Contains("docker network create --internal", trial);
|
|
Assert.DoesNotContain("--network bridge", trial);
|
|
}
|
|
|
|
[Fact]
|
|
public void TestsRunWithPostgresAndNoneMaySkip()
|
|
{
|
|
foreach (var name in new[] { "verifica.yml", "rilascio.yml" })
|
|
{
|
|
var prove = Job(Workflow(name), "prove");
|
|
Assert.Contains("image: postgres:18", prove);
|
|
Assert.Contains("TEST_DB_HOST: postgres", prove);
|
|
Assert.Contains("dotnet build Mimante.slnx -c Release -nologo -warnaserror", prove);
|
|
Assert.Contains("dotnet test tests/Mimante.Tests -c Release --no-build", prove);
|
|
Assert.Contains("bash .gitea/scripts/riepilogo-prove.sh out/prove 0", prove);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void LatestComesOnlyFromAReleaseAndMainOnlyFromMain()
|
|
{
|
|
var verifica = Code(Workflow("verifica.yml"));
|
|
Assert.DoesNotContain(":latest", verifica.Replace(":latest resta", ""));
|
|
Assert.Contains("[ \"$RIFERIMENTO\" != refs/heads/main ]", verifica);
|
|
Assert.Contains("NOMI+=(\"$IMAGE:main\")", verifica);
|
|
|
|
var rilascio = Code(Workflow("rilascio.yml"));
|
|
Assert.Contains("*-*) echo \"Prova del rilascio: :latest non si sposta.\" ;;", rilascio);
|
|
Assert.Contains("[ -z \"$TOKEN\" ]", rilascio);
|
|
}
|
|
|
|
[Fact]
|
|
public void CredentialsAreNeverPrintedNorPassedOnTheCommandLine()
|
|
{
|
|
foreach (var name in new[] { "verifica.yml", "rilascio.yml", "pulizia.yml", "prove-a-mano.yml" })
|
|
{
|
|
var code = Code(Workflow(name));
|
|
Assert.DoesNotContain("docker login", code);
|
|
Assert.False(Regex.IsMatch(code, @"echo[^\n]*\$\{?(TOKEN|REGISTRY_TOKEN|AUTH)\b"), $"{name} prints a credential");
|
|
Assert.False(Regex.IsMatch(code, @"set -x|bash -x"), $"{name} traces commands");
|
|
// A secret goes into a step only through env:, never inline in a script line.
|
|
foreach (var line in code.Split('\n').Where(l => l.Contains("secrets.")))
|
|
Assert.Matches(@"^\s+[A-Z_]+: \$\{\{ secrets\.[A-Z_]+ \}\}$", line);
|
|
}
|
|
Assert.Contains("-H @<(printf 'Authorization: token %s\\n' \"$REGISTRY_TOKEN\")", Script("pulizia-docker.sh"));
|
|
|
|
foreach (var trial in new[] { Script("prova-immagine.sh"), Script("prova-carico.sh") })
|
|
{
|
|
// Passwords reach the containers by name from the environment, never as a value on the command line.
|
|
Assert.Contains("-e DB_PASSWORD -e UI_PASSWORD", trial);
|
|
Assert.Contains("-e POSTGRES_PASSWORD postgres:18", trial);
|
|
Assert.DoesNotMatch(@"-e (DB|UI|POSTGRES)_PASSWORD=", trial);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void TheManualTrialsRunOnlyByHandAndTheLoadTrialKeepsTheMemoryCapOfUnraid()
|
|
{
|
|
var workflow = Workflow("prove-a-mano.yml");
|
|
Assert.Contains("on:\n workflow_dispatch:\n\npermissions:", workflow);
|
|
Assert.Contains("bash .gitea/scripts/prova-carico.sh \"$PROVA\" \"$ESECUZIONE\"", Job(workflow, "carico"));
|
|
Assert.Contains("bash .gitea/scripts/pulizia-docker.sh esecuzione \"$ESECUZIONE\"", Job(workflow, "carico"));
|
|
Assert.DoesNotContain("--push", Code(workflow));
|
|
// The simulator runs inside the job, never inside the image.
|
|
Assert.Contains("--simulatore", Job(workflow, "schermate"));
|
|
Assert.DoesNotContain("--simulatore", Code(Job(workflow, "carico")));
|
|
|
|
var load = Code(Script("prova-carico.sh"));
|
|
Assert.Contains("docker network create --internal", load);
|
|
Assert.Contains("--memory=1g", load);
|
|
Assert.Contains("--label \"mimante.ci.prova=$esecuzione\"", Script("prova-carico.sh"));
|
|
Assert.Contains("nome=\"mimante-prova-$esecuzione\"", load);
|
|
}
|
|
|
|
[Fact]
|
|
public void TheMarkOfTheTrialIsNeverPutInTheImage()
|
|
{
|
|
Assert.DoesNotContain("mimante.ci.prova", Code(Script("costruisci-immagine.sh")));
|
|
Assert.Contains("--label \"mimante.ci.prova=$esecuzione\"", Script("prova-immagine.sh"));
|
|
Assert.Contains("--provenance=false --sbom=false", Script("costruisci-immagine.sh"));
|
|
}
|
|
|
|
[Fact]
|
|
public void TheCleanupNeverPrunesTheWholeDaemon()
|
|
{
|
|
var code = Code(Script("pulizia-docker.sh"));
|
|
foreach (var forbidden in new[] { "system prune", "container prune", "volume prune", "network prune", "image prune", "builder prune" })
|
|
Assert.DoesNotContain(forbidden, code);
|
|
Assert.Contains("^mimante-ci:[0-9]+$", code);
|
|
Assert.Contains("^mimante-ci-[0-9]+$", code);
|
|
Assert.Contains("^sha-[0-9a-f]{7,40}$", code);
|
|
}
|
|
|
|
[Fact]
|
|
public void ExternalToolsArePinnedAndChecked()
|
|
{
|
|
var tool = Script("strumento.sh");
|
|
Assert.Matches(@"versione=""\d+\.\d+\.\d+""", tool);
|
|
Assert.Equal(2, Regex.Matches(tool, @"sha256=""[0-9a-f]{64}""").Count);
|
|
Assert.Contains("sha256sum -c", tool);
|
|
Assert.DoesNotContain("latest", Code(tool));
|
|
foreach (var name in new[] { "verifica.yml", "rilascio.yml" })
|
|
Assert.DoesNotContain("aquasecurity/trivy-action", Workflow(name));
|
|
}
|
|
|
|
[Fact]
|
|
public void ScriptsAreLfAndBashOnly()
|
|
{
|
|
var attributes = File.ReadAllText(Path.Combine(Root, ".gitattributes"));
|
|
Assert.Matches(@"(?m)^\*\.sh\s+text eol=lf", attributes);
|
|
foreach (var file in Directory.GetFiles(Path.Combine(Root, ".gitea", "scripts"), "*.sh"))
|
|
Assert.StartsWith("#!/usr/bin/env bash", File.ReadAllText(file));
|
|
}
|
|
|
|
// ---- helpers ----
|
|
|
|
private static string Workflow(string name) => Read(".gitea", "workflows", name);
|
|
|
|
private static string Script(string name) => Read(".gitea", "scripts", name);
|
|
|
|
private static string Read(params string[] parts) => File.ReadAllText(Path.Combine([Root, .. parts])).Replace("\r\n", "\n");
|
|
|
|
/// <summary>The text without comment lines, so that rules are checked on what runs, not on what is explained.</summary>
|
|
private static string Code(string text) =>
|
|
string.Join('\n', text.Split('\n').Where(l => !l.TrimStart().StartsWith('#')));
|
|
|
|
/// <summary>The block of one job: from « name:» to the next job header.</summary>
|
|
private static string Job(string workflow, string name)
|
|
{
|
|
var start = workflow.IndexOf($"\n {name}:\n", StringComparison.Ordinal);
|
|
Assert.True(start >= 0, $"job {name} not found");
|
|
var next = JobHeader().Match(workflow, start + name.Length + 4);
|
|
return next.Success ? workflow[start..next.Index] : workflow[start..];
|
|
}
|
|
|
|
[GeneratedRegex(@"\n (?<name>[a-z][a-z0-9_-]*):\n (runs-on|needs):")]
|
|
private static partial Regex JobHeader();
|
|
|
|
private static string FindRoot()
|
|
{
|
|
for (var d = new DirectoryInfo(AppContext.BaseDirectory); d != null; d = d.Parent)
|
|
if (File.Exists(Path.Combine(d.FullName, "Mimante.slnx"))) return d.FullName;
|
|
throw new InvalidOperationException("Mimante.slnx not found above " + AppContext.BaseDirectory);
|
|
}
|
|
}
|