I flussi di prima non erano mai partiti (stavano in una sottocartella) e avrebbero pubblicato la porta 5432 sul Docker di Unraid, dove gira il PostgreSQL vero. Ora, come in Febe: a ogni push controlli (sintassi, versione, CHANGELOG, elenco chiuso dei pacchetti, niente risorse esterne nell'interfaccia, JSON con commenti come li legge il programma) e gitleaks su tutta la storia; tutte le prove con un postgres:18 di servizio senza porte, nessuna saltata; immagine costruita, provata su una rete Docker interna con un PostgreSQL usa e getta (salute, file di /config a 99:100, arresto entro 15 s) e scansionata con Trivy, pubblicata come :main/:sha- solo da main. Il rilascio sul tag pubblica :X.Y.Z e :latest (un -rc.N solo la sua etichetta) e allega il modello Unraid; la pulizia notturna tocca solo i resti della CI di Mimante e le :sha- vecchie. PipelineTests tiene ferme queste regole; le sue prove leggono .gitea e per questo non girano dentro la costruzione dell'immagine. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
179 lines
8.3 KiB
C#
179 lines
8.3 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" })
|
|
{
|
|
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" })
|
|
{
|
|
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"));
|
|
|
|
var trial = Script("prova-immagine.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 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);
|
|
}
|
|
}
|