21e9bad12f
- Every script gets built-ins: Set-ExecuteRGroupMembership (member when the value is true, above 0, a non-empty collection or yes-like text; pipe results in), Add-/Remove-ExecuteRGroupMembership, and executer_group in Bash. They need no credentials: the agent takes their lines out of the output and sends the answers with the job's final state. - The server adds or removes the device in that script group, creating it (top level, kind Script) the first time a device joins. Each run decides again. - Only script groups change: a curated group by that name is ignored, so a device can never add itself to one. Last answer per group wins; 25 a job. - Console: Script kind in the group editor, tree and lists (reported devices, no hand-listing), editor completions and snippets for the built-ins. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
166 lines
6.2 KiB
C#
166 lines
6.2 KiB
C#
using Agent.PowerShell;
|
|
using Shared.Contracts.Messages;
|
|
using Shared.Contracts.Models;
|
|
using Xunit;
|
|
|
|
namespace Agent.Tests;
|
|
|
|
public class ScriptGroupReportTests
|
|
{
|
|
private const string Script = """
|
|
Set-ExecuteRGroupMembership 'Bool True' $true
|
|
Set-ExecuteRGroupMembership 'Bool False' $false
|
|
Set-ExecuteRGroupMembership 'Count Three' 3
|
|
Set-ExecuteRGroupMembership 'Count Zero' 0
|
|
Set-ExecuteRGroupMembership 'Text No' 'no'
|
|
Set-ExecuteRGroupMembership 'Text Yes' 'present'
|
|
Set-ExecuteRGroupMembership 'Empty Array' @()
|
|
Set-ExecuteRGroupMembership 'Null' $null
|
|
1, 2 | Set-ExecuteRGroupMembership 'Piped Two'
|
|
@() | Set-ExecuteRGroupMembership 'Piped None'
|
|
Get-ChildItem -Path $env:windir -Filter 'no-such-file-*.xyz' | Set-ExecuteRGroupMembership 'Piped Nothing Found'
|
|
Add-ExecuteRGroupMembership 'Added'
|
|
Remove-ExecuteRGroupMembership 'Removed'
|
|
Add-ExecuteRGroupMembership 'Last Wins'
|
|
Remove-ExecuteRGroupMembership 'last wins'
|
|
Write-Output 'done'
|
|
""";
|
|
|
|
private static readonly Dictionary<string, bool> Expected = new()
|
|
{
|
|
["Bool True"] = true,
|
|
["Bool False"] = false,
|
|
["Count Three"] = true,
|
|
["Count Zero"] = false,
|
|
["Text No"] = false,
|
|
["Text Yes"] = true,
|
|
["Empty Array"] = false,
|
|
["Null"] = false,
|
|
["Piped Two"] = true,
|
|
["Piped None"] = false,
|
|
["Piped Nothing Found"] = false,
|
|
["Added"] = true,
|
|
["Removed"] = false,
|
|
["last wins"] = false,
|
|
};
|
|
|
|
private static void AssertReports(ScriptGroupReportCollector collector, List<NormalizedOutputRecord> published)
|
|
{
|
|
var reports = collector.Reports!.ToDictionary(r => r.Group, r => r.Member);
|
|
Assert.Equal(Expected.OrderBy(e => e.Key), reports.OrderBy(e => e.Key));
|
|
Assert.DoesNotContain(published, r => r.Text.Contains(ScriptGroups.Marker));
|
|
Assert.Contains(published, r => r.Text == "Script group “Added”: this device is a member.");
|
|
Assert.Contains(published, r => r.Text.Contains("done"));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task PowerShell7BuiltInsDecideMembership()
|
|
{
|
|
var collector = new ScriptGroupReportCollector();
|
|
var published = new List<NormalizedOutputRecord>();
|
|
var result = await new PowerShellExecutionService().ExecuteAsync(
|
|
Script,
|
|
parameters: null,
|
|
prelude: ScriptGroupBuiltIns.Compose(ScriptEngine.PowerShell, null),
|
|
onRecord: r =>
|
|
{
|
|
lock (published)
|
|
{
|
|
published.Add(collector.Take(r));
|
|
}
|
|
},
|
|
onProgress: _ => { },
|
|
cancellationToken: TestContext.Current.CancellationToken);
|
|
|
|
Assert.False(result.HadErrors, string.Join("\n", published.Select(p => p.Text)));
|
|
AssertReports(collector, published);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task WindowsPowerShellBuiltInsDecideMembership()
|
|
{
|
|
var collector = new ScriptGroupReportCollector();
|
|
var published = new List<NormalizedOutputRecord>();
|
|
var result = await ExternalScriptRunner.RunAsync(
|
|
ScriptEngine.WindowsPowerShell,
|
|
Script,
|
|
prelude: ScriptGroupBuiltIns.Compose(ScriptEngine.WindowsPowerShell, null),
|
|
parameters: new Dictionary<string, object?>(),
|
|
secrets: null,
|
|
"Secrets",
|
|
r =>
|
|
{
|
|
lock (published)
|
|
{
|
|
published.Add(collector.Take(r));
|
|
}
|
|
},
|
|
TestContext.Current.CancellationToken);
|
|
|
|
Assert.True(result.ExitCode == 0, string.Join("\n", published.Select(p => p.Text)));
|
|
AssertReports(collector, published);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task BashBuiltInDecidesMembership()
|
|
{
|
|
Assert.SkipUnless(ExternalScriptRunner.IsAvailable(ScriptEngine.Bash), "bash is not installed.");
|
|
var collector = new ScriptGroupReportCollector();
|
|
var published = new List<NormalizedOutputRecord>();
|
|
var result = await ExternalScriptRunner.RunAsync(
|
|
ScriptEngine.Bash,
|
|
"""
|
|
executer_group "Yes"
|
|
executer_group "Count" "$(printf 'a\nb\n' | wc -l)"
|
|
executer_group "Zero" 0
|
|
executer_group "Word" "False"
|
|
executer_group 'Quote "and" \slash' true
|
|
echo done
|
|
""",
|
|
prelude: ScriptGroupBuiltIns.Compose(ScriptEngine.Bash, null),
|
|
parameters: new Dictionary<string, object?>(),
|
|
secrets: null,
|
|
"Secrets",
|
|
r =>
|
|
{
|
|
lock (published)
|
|
{
|
|
published.Add(collector.Take(r));
|
|
}
|
|
},
|
|
TestContext.Current.CancellationToken);
|
|
|
|
Assert.True(result.ExitCode == 0, string.Join("\n", published.Select(p => p.Text)));
|
|
var reports = collector.Reports!.ToDictionary(r => r.Group, r => r.Member);
|
|
Assert.True(reports["Yes"]);
|
|
Assert.True(reports["Count"]);
|
|
Assert.False(reports["Zero"]);
|
|
Assert.False(reports["Word"]);
|
|
// A backslash makes the name unusable: reported as a warning, not kept.
|
|
Assert.DoesNotContain(reports.Keys, k => k.Contains("Quote"));
|
|
Assert.Contains(published, r => r.Stream == OutputStreamKind.Warning && r.Text.Contains("Quote \"and\""));
|
|
}
|
|
|
|
[Fact]
|
|
public void LinesThatOnlyLookLikeReportsAreLeftAlone()
|
|
{
|
|
var collector = new ScriptGroupReportCollector();
|
|
var line = new NormalizedOutputRecord(OutputStreamKind.Output, ScriptGroups.Marker + "{not json");
|
|
|
|
Assert.Same(line, collector.Take(line));
|
|
Assert.Null(collector.Reports);
|
|
}
|
|
|
|
[Fact]
|
|
public void AJobReportsOnAtMostTheLimit()
|
|
{
|
|
var collector = new ScriptGroupReportCollector();
|
|
for (var i = 0; i < ScriptGroups.MaxReportsPerJob + 3; i++)
|
|
{
|
|
collector.Take(new NormalizedOutputRecord(OutputStreamKind.Information, $"{ScriptGroups.Marker}{{\"group\":\"G{i}\",\"member\":true}}"));
|
|
}
|
|
|
|
Assert.Equal(ScriptGroups.MaxReportsPerJob, collector.Reports!.Count);
|
|
}
|
|
}
|