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; /// /// Both artifact-deploy commands (site deploy-artifacts and deploy artifacts) 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. /// 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}"; [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("--url") { Recursive = true }; var format = new Option("--format") { Recursive = true }; var user = new Option("--username") { Recursive = true }; var password = new Option("--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(); } } }