84 lines
4.3 KiB
C#
84 lines
4.3 KiB
C#
using System.CommandLine;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using ZB.MOM.WW.ScadaBridge.CLI.Commands;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.CLI.Tests;
|
|
|
|
/// <summary>
|
|
/// <c>deploy area</c> (Gitea #40) sends <c>MgmtDeployArea</c> with the area id and
|
|
/// force flag, and derives its exit code from the summary's <c>failureCount</c>
|
|
/// exactly like <c>deploy site</c>: a per-instance failure is a successful HTTP
|
|
/// call, so without that a script could not detect it.
|
|
/// </summary>
|
|
public class DeployAreaExitCodeTests
|
|
{
|
|
/// <summary>Verifies the wire payload and the exit code derived from the summary.</summary>
|
|
/// <param name="response">The JSON response body served by the fake management endpoint.</param>
|
|
/// <param name="force">Whether <c>--force</c> is passed.</param>
|
|
/// <param name="expected">The expected process exit code.</param>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Theory]
|
|
[InlineData("{\"successCount\":2,\"failureCount\":0}", false, 0)]
|
|
[InlineData("{\"successCount\":1,\"failureCount\":1}", true, 1)]
|
|
[InlineData("{\"unexpected\":true}", false, 1)]
|
|
public async Task DeployArea_SendsAreaCommand_AndReturnsDomainExitCode(string response, bool force, int expected)
|
|
{
|
|
var listener = new TcpListener(IPAddress.Loopback, 0);
|
|
listener.Start();
|
|
try
|
|
{
|
|
var port = ((IPEndPoint)listener.LocalEndpoint).Port;
|
|
string? requestBody = null;
|
|
var server = Task.Run(async () =>
|
|
{
|
|
using var client = await listener.AcceptTcpClientAsync();
|
|
await using var stream = client.GetStream();
|
|
using var reader = new StreamReader(stream, Encoding.UTF8, leaveOpen: true);
|
|
var length = 0;
|
|
while (await reader.ReadLineAsync() is { Length: > 0 } line)
|
|
if (line.StartsWith("Content-Length:", StringComparison.OrdinalIgnoreCase))
|
|
length = int.Parse(line[15..].Trim());
|
|
var buffer = new char[length];
|
|
var read = 0;
|
|
while (read < length)
|
|
{
|
|
var count = await reader.ReadAsync(buffer.AsMemory(read));
|
|
if (count == 0) throw new EndOfStreamException();
|
|
read += count;
|
|
}
|
|
requestBody = new string(buffer);
|
|
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(DeployCommands.Build(url, format, user, password));
|
|
string[] args = ["deploy", "area", "--id", "12", "--url", $"http://127.0.0.1:{port}",
|
|
"--username", "test", "--password", "test", "--format", "json"];
|
|
if (force) args = [.. args, "--force"];
|
|
var exit = await root.Parse(args).InvokeAsync();
|
|
await server.WaitAsync(TimeSpan.FromSeconds(5));
|
|
|
|
Assert.Equal(expected, exit);
|
|
using var payload = JsonDocument.Parse(requestBody!);
|
|
Assert.Equal("MgmtDeployArea", payload.RootElement.GetProperty("command").GetString());
|
|
var command = payload.RootElement.GetProperty("payload");
|
|
Assert.Equal(12, GetPropertyIgnoreCase(command, "areaId").GetInt32());
|
|
Assert.Equal(force, GetPropertyIgnoreCase(command, "force").GetBoolean());
|
|
}
|
|
finally { listener.Stop(); }
|
|
}
|
|
|
|
private static JsonElement GetPropertyIgnoreCase(JsonElement element, string name) =>
|
|
element.EnumerateObject().First(p => string.Equals(p.Name, name, StringComparison.OrdinalIgnoreCase)).Value;
|
|
}
|