55 lines
4.6 KiB
C#
55 lines
4.6 KiB
C#
using System.Diagnostics;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using System.Text.Json;
|
|
using HealthApp.Provisioning;
|
|
using Xunit;
|
|
namespace HealthApp.DeploymentTests;
|
|
|
|
public sealed class ProvisioningTests
|
|
{
|
|
[Fact]
|
|
public void Preparation_generates_scoped_protected_material_and_refuses_overwrite()
|
|
{
|
|
var root = Path.Combine(Path.GetTempPath(), "healthapp-b-prepare-" + Guid.NewGuid()); var plan = Path.Combine(root, "plan"); Directory.CreateDirectory(root);
|
|
try
|
|
{
|
|
Installation.Prepare(plan, "localhost", 5443, root, null, null, null);
|
|
using var json = JsonDocument.Parse(File.ReadAllText(Path.Combine(plan, "installation.json"))); var password = json.RootElement.GetProperty("CertificatePassword").GetString();
|
|
using var cert = X509CertificateLoader.LoadPkcs12FromFile(Path.Combine(plan, "application.pfx"), password); Assert.True(cert.HasPrivateKey);
|
|
var settings = json.RootElement.GetProperty("Settings");
|
|
foreach (var removedPolicy in new[] { "Resources:MaxConcurrentRequests", "Resources:MaxConcurrentAssessments", "Resources:RequestDeadlineSeconds", "Resources:MaxPoolSize" })
|
|
Assert.False(settings.TryGetProperty(removedPolicy, out _));
|
|
Assert.Equal(3, json.RootElement.GetProperty("Accounts").GetArrayLength()); Assert.DoesNotContain("Service-Key", json.RootElement.GetRawText());
|
|
if (OperatingSystem.IsLinux()) Assert.Equal(UnixFileMode.UserRead | UnixFileMode.UserWrite, File.GetUnixFileMode(Path.Combine(plan, "installation.json")));
|
|
Assert.Throws<InvalidOperationException>(() => Installation.Prepare(plan, "localhost", 5443, root, null, null, null));
|
|
}
|
|
finally { Directory.Delete(root, true); }
|
|
}
|
|
[Theory]
|
|
[InlineData("debian", "12", false)]
|
|
[InlineData("debian", "12", true)]
|
|
[InlineData("fedora", "43", false)]
|
|
[InlineData("fedora", "43", true)]
|
|
[InlineData("manjaro", "rolling", false)]
|
|
[InlineData("manjaro", "rolling", true)]
|
|
public async Task Distribution_adapters_install_local_persistence_only_when_selected(string distribution, string version, bool local)
|
|
{
|
|
if (!OperatingSystem.IsLinux()) return;
|
|
var root = Path.Combine(Path.GetTempPath(), "healthapp-b-adapter-" + Guid.NewGuid()); Directory.CreateDirectory(root);
|
|
try
|
|
{
|
|
var executable = Path.Combine(root, "commands"); Directory.CreateDirectory(executable); var log = Path.Combine(root, "commands.log");
|
|
foreach (var name in new[] { "apt-get", "dnf", "pacman", "systemctl", "install", "runuser", "pg_isready" })
|
|
{ var p = Path.Combine(executable, name); await File.WriteAllTextAsync(p, "#!/bin/bash\nprintf '%s\\n' \"${0##*/} $*\" >> \"$HEALTHAPP_ADAPTER_LOG\"\n"); File.SetUnixFileMode(p, UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute); }
|
|
var dotnet = Path.Combine(executable, "dotnet"); await File.WriteAllTextAsync(dotnet, "#!/bin/bash\necho 'Microsoft.AspNetCore.App 10.0.12 [/test]'\n"); File.SetUnixFileMode(dotnet, UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute);
|
|
var os = Path.Combine(root, "os-release"); await File.WriteAllTextAsync(os, $"ID={distribution}\nVERSION_ID={version}\n");
|
|
var repository = FindRoot(); var script = Path.Combine(root, "run.sh"); await File.WriteAllTextAsync(script, $"source '{repository}/deploy/installer/bootstrap.sh'\ndotnet='{dotnet}'\nmode={(local ? "local" : "remote")}\nfail() {{ exit 9; }}\nensure_dependencies\n");
|
|
var start = new ProcessStartInfo("bash") { UseShellExecute = false, RedirectStandardError = true, RedirectStandardOutput = true }; start.ArgumentList.Add(script); start.Environment["PATH"] = executable + ":" + Environment.GetEnvironmentVariable("PATH"); start.Environment["HEALTHAPP_OS_RELEASE"] = os; start.Environment["HEALTHAPP_ADAPTER_LOG"] = log;
|
|
using var process = Process.Start(start)!; var output = process.StandardOutput.ReadToEndAsync(); var error = process.StandardError.ReadToEndAsync(); await process.WaitForExitAsync(); Assert.True(process.ExitCode == 0, await output + await error);
|
|
var commands = File.ReadAllText(log); Assert.Equal(local, commands.Contains("postgresql")); if (local) Assert.Contains("systemctl enable --now postgresql", commands); else Assert.DoesNotContain("pg_isready", commands);
|
|
}
|
|
finally { Directory.Delete(root, true); }
|
|
}
|
|
private static string FindRoot() { var d = new DirectoryInfo(AppContext.BaseDirectory); while (d is not null && !File.Exists(Path.Combine(d.FullName, "HealthApp.slnx"))) d = d.Parent; return d!.FullName; }
|
|
}
|