using System.CommandLine; using ZB.MOM.WW.ScadaBridge.CLI.Commands; namespace ZB.MOM.WW.ScadaBridge.CLI.Tests; /// /// CLI-1: site-relayed commands return their result record as HTTP 200 with the failure in a /// success flag. The CLI must exit 1 for success:false. /// [Collection("Console")] public class SuccessFlagExitCodeTests { private static RootCommand BuildRoot() { 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(CachedCallCommands.Build(url, format, user, password)); var dc = DataConnectionCommands.Build(url, format, user, password); ConnectionBrowseCommands.AddTo(dc, url, format, user, password); root.Add(dc); return root; } /// Verifies that the shared handler maps the success flag to the exit code. /// The response body. /// The expected exit code. [Theory] [InlineData("{\"correlationId\":\"c\",\"success\":true,\"errorMessage\":null}", 0)] [InlineData("{\"correlationId\":\"c\",\"success\":false,\"errorMessage\":\"not parked\"}", 1)] [InlineData("{\"Success\":false,\"Error\":\"one node failed\"}", 1)] [InlineData("{\"certs\":[]}", 0)] [InlineData("[1,2]", 0)] public void HandleSuccessFlagResponse_MapsFlagToExitCode(string json, int expected) { var originalOut = Console.Out; var originalErr = Console.Error; Console.SetOut(TextWriter.Null); Console.SetError(TextWriter.Null); try { Assert.Equal(expected, CommandHelpers.HandleSuccessFlagResponse(json, "json")); } finally { Console.SetOut(originalOut); Console.SetError(originalErr); } } /// Verifies that each relayed command exits 1 on success:false and 0 on success:true. /// Command line (without connection options). /// The response body. /// Expected exit code. /// A task that represents the asynchronous operation. [Theory] [InlineData("cached-call retry --site-id s --tracked-operation-id 11111111-2222-3333-4444-555555555555", "{\"correlationId\":\"c\",\"success\":false,\"errorMessage\":\"not parked\"}", 1)] [InlineData("cached-call retry --site-id s --tracked-operation-id 11111111-2222-3333-4444-555555555555", "{\"correlationId\":\"c\",\"success\":true}", 0)] [InlineData("cached-call discard --site-id s --tracked-operation-id 11111111-2222-3333-4444-555555555555", "{\"correlationId\":\"c\",\"success\":false,\"errorMessage\":\"gone\"}", 1)] [InlineData("data-connection certs remove --site s --thumbprint AB", "{\"success\":false,\"error\":\"node B unreachable\",\"certs\":null}", 1)] [InlineData("data-connection certs remove --site s --thumbprint AB", "{\"success\":true,\"error\":null,\"certs\":[]}", 0)] public async Task RelayedCommand_ExitCodeFollowsSuccessFlag(string arguments, string response, int expected) { var originalOut = Console.Out; var originalErr = Console.Error; Console.SetOut(TextWriter.Null); Console.SetError(TextWriter.Null); try { var (exit, _) = await FakeManagementServer.RunAsync("200 OK", response, async url => { var args = arguments.Split(' ').Concat(new[] { "--url", url, "--username", "t", "--password", "t", "--format", "json" }).ToArray(); return await BuildRoot().Parse(args).InvokeAsync(); }); Assert.Equal(expected, exit); } finally { Console.SetOut(originalOut); Console.SetError(originalErr); } } }