Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/DeployArtifactsExitCodeTests.cs
T
Joseph Doherty 513fa1aa16 docs(tests): fix CommentChecker findings across tests/ — 13,392 → 0 (repo-wide scan clean)
Comment/XML-doc changes only:
- add summaries to ~10k test methods, fixtures, fakes and helpers
  (+ <param> on theories, <returns> on async Task methods,
  <inheritdoc/> on interface implementations the checker can resolve)
- strip ~2.6k project-tracking references (WP*, P*-*, row-NN, Task N,
  #NN, Component-0NN) from comments, keeping the explanatory text;
  reword false positives in comments only
- real docs on fakes implementing BCL/library interfaces
  (IDisposable, ILogger, IOptionsMonitor, IAsyncLifetime)
- reword the last 5 src/ false positives (CWE-1236, failures-1,
  year-0001, M365)

Only non-comment edits: four one-line nested classes split across lines
so members can carry docs. Build 0 errors / 0 warnings.
2026-10-07 15:05:49 -04:00

72 lines
3.8 KiB
C#

using System.CommandLine;
using System.Net;
using System.Net.Sockets;
using System.Text;
using ZB.MOM.WW.ScadaBridge.CLI.Commands;
namespace ZB.MOM.WW.ScadaBridge.CLI.Tests;
/// <summary>
/// Both artifact-deploy commands (<c>site deploy-artifacts</c> and <c>deploy artifacts</c>) exit
/// 1 when any site in the summary failed. A per-site failure is a successful HTTP call, so they
/// used to exit 0 and a script could not tell a failed site from a clean deploy.
/// </summary>
public class DeployArtifactsExitCodeTests
{
private const string AllSucceeded =
"{\"deploymentId\":\"d1\",\"siteResults\":[{\"siteId\":\"site-a\",\"siteName\":\"A\",\"success\":true}]," +
"\"successCount\":1,\"failureCount\":0}";
private const string OneFailed =
"{\"deploymentId\":\"d1\",\"siteResults\":[{\"siteId\":\"site-a\",\"siteName\":\"A\",\"success\":true}," +
"{\"siteId\":\"site-b\",\"siteName\":\"B\",\"success\":false,\"errorMessage\":\"Secret 'k' ... missing\"}]," +
"\"successCount\":1,\"failureCount\":1}";
/// <summary>Verifies that the artifact deploy command exits with a non-zero code exactly when a site failed or the response is unrecognized.</summary>
/// <param name="group">The deploy command group under test (<c>site</c> or <c>deploy</c>).</param>
/// <param name="response">The JSON response the management endpoint returns.</param>
/// <param name="expected">The expected process exit code.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
[Theory]
[InlineData("site", AllSucceeded, 0)]
[InlineData("site", OneFailed, 1)]
[InlineData("site", "{\"unexpected\":true}", 1)]
[InlineData("deploy", AllSucceeded, 0)]
[InlineData("deploy", OneFailed, 1)]
[InlineData("deploy", "{\"unexpected\":true}", 1)]
public async Task ArtifactDeployExitCodeFollowsFailureCount(string group, string response, int expected)
{
var listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start();
try
{
var port = ((IPEndPoint)listener.LocalEndpoint).Port;
var server = Task.Run(async () =>
{
using var client = await listener.AcceptTcpClientAsync();
await using var stream = client.GetStream();
using var reader = new StreamReader(stream, Encoding.ASCII, leaveOpen: true);
while (await reader.ReadLineAsync() is { Length: > 0 }) { }
var body = Encoding.UTF8.GetBytes(response);
var headers = Encoding.ASCII.GetBytes($"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {body.Length}\r\nConnection: close\r\n\r\n");
await stream.WriteAsync(headers);
await stream.WriteAsync(body);
});
var root = new RootCommand();
var url = new Option<string>("--url") { Recursive = true };
var format = new Option<string>("--format") { Recursive = true };
var user = new Option<string>("--username") { Recursive = true };
var password = new Option<string>("--password") { Recursive = true };
root.Add(url); root.Add(format); root.Add(user); root.Add(password);
root.Add(SiteCommands.Build(url, format, user, password));
root.Add(DeployCommands.Build(url, format, user, password));
string[] verb = group == "site" ? ["site", "deploy-artifacts"] : ["deploy", "artifacts"];
var exit = await root.Parse([.. verb, "--url", $"http://127.0.0.1:{port}",
"--username", "test", "--password", "test", "--format", "json"]).InvokeAsync();
await server.WaitAsync(TimeSpan.FromSeconds(5));
Assert.Equal(expected, exit);
}
finally { listener.Stop(); }
}
}