Pulizia: una prova scritta male non diventa mai la pulizia vera
Lo script riconosceva solo --prova esatto e ignorava ogni altro argomento: a mano «completa --dry-run», «-n» o «prova» facevano partire la pulizia completa vera sul demone condiviso, con codice 0. Ora ogni argomento si controlla prima di chiamare Docker: un'opzione diversa da --prova o un argomento in più fermano tutto con codice 2 e l'uso. Allo stesso modo il passo di pulizia.yml accettava come prova solo il testo «true» e trattava ogni altro valore come pulizia vera; un avvio con l'API passa il valore così com'è («True», «1»). Ora «true» è la prova, «false» o niente la pulizia vera, qualunque altro valore un errore senza toccare nulla (il valore nel messaggio è ripulito, così non può aggiungere un comando del flusso). Le prove fanno girare il blocco del passo vero con uno script finto per nove valori, e lo script con dieci argomenti sbagliati: codice 2 e nessuna chiamata a Docker. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5988c503f7
commit
46d780c826
5 files changed
+147
-17
No files matched your search
@@ -18,6 +18,8 @@ public sealed partial class PipelineTests
|
||||
{
|
||||
private const string RegistryApi = "http://gitea.invalid/api/v1";
|
||||
|
||||
private const string WeeklyStep = "Pulizia completa del demone Docker e del registro";
|
||||
|
||||
// Low entropy on purpose: gitleaks reads every commit, and a fake value shaped like a token would look like one.
|
||||
private const string RegistryToken = "gettone-finto-gettone-finto";
|
||||
|
||||
@@ -97,15 +99,98 @@ public sealed partial class PipelineTests
|
||||
StringAssert.Contains(input, "type: boolean", "Gitea 28 shows a checkbox and puts \"true\" or \"false\" in the event");
|
||||
StringAssert.Contains(input, "default: false", "by hand the cleanup is real unless the box is ticked");
|
||||
|
||||
var step = Step(Job(weekly, "docker"), "Pulizia completa del demone Docker e del registro");
|
||||
var step = Step(Job(weekly, "docker"), WeeklyStep);
|
||||
StringAssert.Contains(step, "PROVA: ${{ gitea.event.inputs.prova }}");
|
||||
StringAssert.Contains(step, "REGISTRY_TOKEN: ${{ secrets.REGISTRY_TOKEN }}");
|
||||
StringAssert.Contains(step, "GITEA_API: ${{ gitea.server_url }}/api/v1");
|
||||
StringAssert.Contains(step, "PROPRIETARIO: ${{ gitea.repository_owner }}");
|
||||
StringAssert.Contains(step, "if [ \"${PROVA:-}\" = true ]; then\n bash .gitea/scripts/pulizia-docker.sh completa --prova\n else\n bash .gitea/scripts/pulizia-docker.sh completa\n");
|
||||
Assert.DoesNotContain("settimanale", Code(weekly), "the weekly cleanup is the complete one");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[Timeout(120_000, CooperativeCancellation = true)]
|
||||
public async Task TheWeeklyStepTriesOnlyForTrueCleansForFalseOrNothingAndStopsOnAnyOtherValue()
|
||||
{
|
||||
// The run block of the step, as the runner executes it, with a cleanup script that only records how it is
|
||||
// called. The box of the form gives "true" or "false" and Monday no value at all; a dispatch through the API
|
||||
// passes any text as it is, and anything else must not become the real cleanup.
|
||||
var bash = RequireBash();
|
||||
var block = RunBlock(Step(Job(Workflow("pulizia.yml"), "docker"), WeeklyStep));
|
||||
using var folder = new TempFolder("febe-passo-pulizia");
|
||||
Directory.CreateDirectory(folder.Combine(".gitea", "scripts"));
|
||||
await File.WriteAllTextAsync(folder.Combine(".gitea", "scripts", "pulizia-docker.sh"), "printf '%s\\n' \"$*\" >> chiamate\n", TestContext.CancellationToken);
|
||||
await File.WriteAllTextAsync(folder.Combine("passo.sh"), block, TestContext.CancellationToken);
|
||||
(string? Value, string? Called)[] cases =
|
||||
[
|
||||
("true", "completa --prova"),
|
||||
("false", "completa"),
|
||||
(string.Empty, "completa"),
|
||||
(null, "completa"),
|
||||
("True", null),
|
||||
("1", null),
|
||||
("yes", null),
|
||||
("true ", null),
|
||||
("x\n::add-mask::y", null),
|
||||
];
|
||||
foreach (var (value, called) in cases)
|
||||
{
|
||||
File.Delete(folder.Combine("chiamate"));
|
||||
var step = await RunProcessAsync(bash, ["--noprofile", "--norc", "-eo", "pipefail", "passo.sh"], start =>
|
||||
{
|
||||
start.WorkingDirectory = folder.Path;
|
||||
start.Environment.Remove("PROVA");
|
||||
if (value is not null)
|
||||
{
|
||||
start.Environment["PROVA"] = value;
|
||||
}
|
||||
});
|
||||
var calls = await ReadLinesAsync(folder.Combine("chiamate"));
|
||||
var report = $"PROVA=«{value}»: codice {step.Code}\n{step.Output}\n{step.Errors}";
|
||||
if (called is not null)
|
||||
{
|
||||
Assert.AreEqual(0, step.Code, report);
|
||||
CollectionAssert.AreEqual(new[] { called }, calls, report);
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.AreEqual(2, step.Code, report);
|
||||
Assert.IsEmpty(calls, report);
|
||||
StringAssert.StartsWith(step.Output, "::error::", report);
|
||||
Assert.HasCount(1, Lines(step.Output), "a value cannot add a workflow command of its own");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[Timeout(120_000, CooperativeCancellation = true)]
|
||||
public async Task AWrongOptionOrAWordTooManyStopsTheCleanupBeforeItTouchesDocker()
|
||||
{
|
||||
// By hand on the server a dry run spelt another way must not become the real cleanup.
|
||||
var bash = RequireBash();
|
||||
string[][] wrong =
|
||||
[
|
||||
["completa", "--dry-run"],
|
||||
["completa", "--Prova"],
|
||||
["completa", "-n"],
|
||||
["completa", "prova"],
|
||||
["--prova"],
|
||||
[],
|
||||
["penzolanti", "--forza"],
|
||||
["settimanale", "-f"],
|
||||
["esecuzione", "777", "778"],
|
||||
["residui", "90", "10"],
|
||||
];
|
||||
foreach (var arguments in wrong)
|
||||
{
|
||||
var run = await RunCleanupAsync(bash, SharedDaemon.World, touchesDocker: false, arguments);
|
||||
var report = $"«{string.Join(' ', arguments)}»\n{run.Report}";
|
||||
Assert.AreEqual(2, run.Code, report);
|
||||
Assert.IsEmpty(run.Calls, report);
|
||||
StringAssert.Contains(run.Errors, "Uso: ", report);
|
||||
Assert.IsEmpty(run.Output, report);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[Timeout(300_000, CooperativeCancellation = true)]
|
||||
public async Task OnADaemonSharedWithARealFebeTheCleanupOfTheCiTakesOnlyWhatTheCiMade()
|
||||
@@ -576,7 +661,14 @@ public sealed partial class PipelineTests
|
||||
/// API is on a domain that cannot exist (.invalid): even if a real docker or curl came first on the PATH they could
|
||||
/// reach nothing, and the test fails because the fakes recorded no call or curl complained on stderr.
|
||||
/// </summary>
|
||||
private async Task<CleanupRun> RunCleanupAsync(string bash, World world, params string[] arguments)
|
||||
private Task<CleanupRun> RunCleanupAsync(string bash, World world, params string[] arguments) =>
|
||||
RunCleanupAsync(bash, world, touchesDocker: true, arguments);
|
||||
|
||||
/// <summary>
|
||||
/// As <see cref="RunCleanupAsync(string, World, string[])"/>; with <paramref name="touchesDocker"/> false the run
|
||||
/// may call no docker at all (wrong arguments stop before it).
|
||||
/// </summary>
|
||||
private async Task<CleanupRun> RunCleanupAsync(string bash, World world, bool touchesDocker, string[] arguments)
|
||||
{
|
||||
using var folder = new TempFolder("febe-pulizia");
|
||||
var bin = folder.Combine("bin");
|
||||
@@ -641,7 +733,11 @@ public sealed partial class PipelineTests
|
||||
RegistryCalls = await ReadLinesAsync(Path.Combine(registry, "calls")),
|
||||
Deleted = await ReadLinesAsync(Path.Combine(registry, "deleted")),
|
||||
};
|
||||
Assert.IsNotEmpty(run.Calls, $"the fake docker client was not the one called\n{run.Report}");
|
||||
if (touchesDocker)
|
||||
{
|
||||
Assert.IsNotEmpty(run.Calls, $"the fake docker client was not the one called\n{run.Report}");
|
||||
}
|
||||
|
||||
Assert.DoesNotContain("unexpected call", run.Errors, run.Report);
|
||||
Assert.DoesNotContain("fake curl:", run.Errors, run.Report);
|
||||
Assert.DoesNotContain("curl: (", run.Errors, $"a real curl was called\n{run.Report}");
|
||||
@@ -713,6 +809,18 @@ public sealed partial class PipelineTests
|
||||
return match.Success ? match.Groups["count"].Value : null;
|
||||
}
|
||||
|
||||
/// <summary>The «run: |» block of a step without its indentation, as the runner writes it to a script.</summary>
|
||||
private static string RunBlock(string step)
|
||||
{
|
||||
const string start = "run: |\n";
|
||||
var at = step.IndexOf(start, StringComparison.Ordinal);
|
||||
Assert.IsGreaterThanOrEqualTo(0, at, $"the step has no «{start.Trim()}» block");
|
||||
var lines = step[(at + start.Length)..].Split('\n');
|
||||
var indent = new string(' ', lines.First(l => l.Trim().Length > 0).TakeWhile(c => c == ' ').Count());
|
||||
var body = lines.TakeWhile(l => l.Trim().Length == 0 || l.StartsWith(indent, StringComparison.Ordinal));
|
||||
return string.Concat(body.Select(l => (l.Length >= indent.Length ? l[indent.Length..] : string.Empty) + "\n"));
|
||||
}
|
||||
|
||||
/// <summary>The body of a shell function of a script, from «name() {» to the closing brace at the start of a line.</summary>
|
||||
private static string Function(string script, string name)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user