Files
ExecuteR/tests/Shared.Tests/ExitCodeClassifierTests.cs
Alphaeus Mote 408a960f42 feat(sequences): server-orchestrated script sequences that continue across restarts [skip ci]
A sequence is an ordered list of steps, each an existing script with its own
arguments, extra Functions, own S3 files, condition (previous outcome,
platform, full OS / Windows PE, groups), Restart On Completion (one
RestartSettings value over the script's own), Continue On Error and
Success/Warning/Failure exit-code lists (codes, ranges, hex, *; defaults
0 / 3010, 1641 / *; validated server side, no overlaps).

A start records the sequence run, one ordinary run per step with that
step's sealed snapshot, and each device's run with its first step's job.
Every step is a normal job sent by the JobDispatcher, so history, output,
run pages, dashboards and audit are unchanged. A SequenceWorker (on the
SCHEDULE_WORKER node, every 2 s) classifies a finished step, waits while its
restart is under way (Restarted, or a newer inventory boot time, which also
covers Windows PE), checks the next step's condition and queues it, with one
update conditioned on the device run's version and the next job in the same
transaction: duplicate or concurrent advances move a device once. The
batched job-state writer is untouched.

Sequences run from Run Now (POST /api/jobs with sequenceId), schedules and
event triggers (sequenceId on /api/triggers), and On Demand with the device
CLI, which returns at once for a sequence. Runs can be cancelled
(POST /api/sequence-runs/{id}/cancel); steps not yet sent are cancelled.
Jobs, runs and summaries carry a sequence step reference. Migrations for
SQLite, PostgreSQL and SQL Server.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 09:54:26 -05:00

95 lines
3.6 KiB
C#

using Shared.Contracts.Models;
using Xunit;
namespace Shared.Tests;
/// <summary>A sequence step's exit-code lists: parsing (codes, ranges, hex, negatives, *) and classification.</summary>
public class ExitCodeClassifierTests
{
[Theory]
[InlineData(0, StepOutcome.Success)]
[InlineData(3010, StepOutcome.Warning)]
[InlineData(1641, StepOutcome.Warning)]
[InlineData(1, StepOutcome.Failure)]
[InlineData(-1, StepOutcome.Failure)]
[InlineData(194, StepOutcome.Failure)]
public void TheDefaultsAreSuccessZeroWarningRestartCodesFailureTheRest(int code, StepOutcome expected)
=> Assert.Equal(expected, ExitCodeClassifier.Default.Classify(code));
[Fact]
public void ListsTakeCodesRangesHexAndNegatives()
{
Assert.True(ExitCodeList.TryParse("0, 3010;1641 2000-2099, 0x80070005, -5--1, 7 - 9", out var list, out var error), error);
Assert.All(new[] { 0, 3010, 1641, 2000, 2050, 2099, unchecked((int)0x80070005), -5, -3, -1, 7, 8, 9 }, c => Assert.True(list.Contains(c), $"{c}"));
Assert.All(new[] { 1, 1999, 2100, -6, 10 }, c => Assert.False(list.Contains(c), $"{c}"));
Assert.False(list.Wildcard);
}
[Theory]
[InlineData("abc")]
[InlineData("10-5")]
[InlineData("1,,x")]
[InlineData("99999999999")]
[InlineData("0x1FFFFFFFF")]
[InlineData("-0x10")]
public void BadEntriesAreRefusedWithTheEntry(string text)
{
Assert.False(ExitCodeList.TryParse(text, out _, out var error));
Assert.NotNull(error);
}
[Fact]
public void AnEmptyListIsEmptyAndAStarTakesEverythingElse()
{
var codes = new SequenceExitCodes(Success: "*", Warning: "", Failure: "1-10");
var classifier = ExitCodeClassifier.Create(codes);
Assert.Equal(StepOutcome.Success, classifier.Classify(0));
Assert.Equal(StepOutcome.Success, classifier.Classify(3010));
Assert.Equal(StepOutcome.Failure, classifier.Classify(5));
}
[Fact]
public void WithoutAStarAnUnlistedCodeIsAFailure()
{
var classifier = ExitCodeClassifier.Create(new SequenceExitCodes("0", "1", ""));
Assert.Equal(StepOutcome.Failure, classifier.Classify(2));
}
[Fact]
public void ListsMayNotOverlapAndOnlyOneMayTakeTheRest()
{
Assert.Contains("2050", new SequenceExitCodes("0, 2000-2099", "2050", "*").Validate());
Assert.Contains("both Success and Failure", new SequenceExitCodes("0", "3010", "*, 0").Validate()!);
Assert.NotNull(new SequenceExitCodes("*", "3010", "*").Validate());
Assert.Null(new SequenceExitCodes("0", "3010, 1641, 2000-2099", "*").Validate());
Assert.StartsWith("Warning:", new SequenceExitCodes("0", "x", "*").Validate());
}
[Fact]
public void MissingListsTakeTheirDefaults()
{
var normalized = new SequenceExitCodes(Warning: "5").Normalized();
Assert.Equal(("0", "5", "*"), (normalized.Success, normalized.Warning, normalized.Failure));
}
[Fact]
public void OnLinuxAndMacOs194CountsAs3010UnlessAListNamesIt()
{
Assert.Equal(StepOutcome.Warning, ExitCodeClassifier.Default.Classify(194, unix: true));
Assert.Equal(StepOutcome.Failure, ExitCodeClassifier.Default.Classify(194, unix: false));
Assert.Equal(StepOutcome.Success, ExitCodeClassifier.Create(new SequenceExitCodes("0, 194", "3010", "*")).Classify(194, unix: true));
}
[Fact]
public void ATooLongListIsRefused()
{
Assert.False(ExitCodeList.TryParse(string.Join(",", Enumerable.Range(0, ExitCodeList.MaxEntries + 1)), out _, out var error));
Assert.Contains("at most", error);
}
}